cat("Weights for Strategy 1:\n")
for (i in 1:nrow(strategy1_selection)) {
cat(strategy1_selection$Asset[i], ":", round(weights_strategy1[i], 2), "%\n")
}
cat("\n")
Print weights for Strategy 2
cat("Weights for Strategy 2:\n")
for (i in 1:nrow(strategy2_selection)) {
cat(strategy2_selection$Asset[i], ":", round(weights_strategy2[i], 2), "%\n")
}
Global Minimum Variance Portfolio - without short selling
For GMVP, the weights are calculated based on minimizing portfolio variance
The weights are inversely proportional to the asset's risk (standard deviation)
cat("\nGlobal Minimum Variance Portfolio Weights (without short selling):\n")
for (i in 1:nrow(strategy1_selection)) {
cat(strategy1_selection$Asset[i], ":", round(gmvp_weights[i], 2), "%\n")
}
cat("\nGlobal Minimum Variance Portfolio Weights (without short selling) for Strategy 2:\n")
for (i in 1:nrow(strategy2_selection)) {
cat(strategy2_selection$Asset[i], ":", round(gmvp_weights_strategy2[i], 2), "%\n")
}
Tangency Portfolio - with short selling
For the Tangency Portfolio, weights are calculated based on maximizing the Sharpe ratio
This involves short selling to achieve the optimal risk-return trade-off
cat("\nTangency Portfolio Weights (with short selling) for Strategy 1:\n")
for (i in 1:nrow(strategy1_selection)) {
cat(strategy1_selection$Asset[i], ":", round(strategy1_tangency_weights[i], 2), "%\n")
}
cat("\nTangency Portfolio Weights (with short selling) for Strategy 2:\n")
for (i in 1:nrow(strategy2_selection)) {
cat(strategy2_selection$Asset[i], ":", round(strategy2_tangency_weights[i], 2), "%\n")
}
Portfolio Return and Risk Measures
Portfolio Return is the weighted sum of individual asset returns
cat("\nValue at Risk (Monte Carlo Simulation Method):\n")
cat("Strategy 1 Portfolio VaR: ", round(portfolio_var_montecarlo_strategy1, 6), " Euro\n")
cat("Strategy 2 Portfolio VaR: ", round(portfolio_var_montecarlo_strategy2, 6), " Euro\n")
Before this question the above coding was done...........According to the above give me code for Your supervisor is interested to identify certain factors that may be helpful to explain your Portfolio return. Henceforth, you need to identify certain factors that might influence your chosen Portfolio's and comment on the explainability of the chosen factors.
Note: Kindly mention the data source from where you have fetched data for the factors.
Hint: You have to regress your Portfolio Return on your chosen factors
Insert the code chunk below............. i have historical data of VIX and DXY_US …these are the names of file …VIX has column Date and AdjClose and DXY_US has column date and price …All this is historical monthly data between April 30, 2008, to January 29, 2021…
To analyze the factors that might influence your chosen portfolio's return using R, you can conduct a linear regression analysis where the portfolio returns are the dependent variable, and the chosen factors are the independent variables. Since you've provided historical data for VIX (a measure of market volatility index) and DXY_US (U.S. Dollar Index), these can serve as the independent variables in your regression analysis to see how they explain the portfolio returns.
Let's assume the following steps to load, prepare, and analyze the data:
Load the historical data for VIX and DXY_US.
Merge these datasets based on the date, aligning them with your portfolio return data.
Perform a linear regression analysis with your portfolio returns as the dependent variable and the VIX and DXY_US as independent variables.
Interpret the results to understand the relationship between these factors and your portfolio returns.
This example will illustrate these steps based on the provided scenario:
# Load necessary libraries
library(lubridate)
library(ggplot2)
# Assume you have already calculated monthly portfolio returns. Let's create a placeholder data frame for that
# This is just an example. You should replace it with your actual monthly portfolio returns
portfolio_returns <- data.frame(Date = seq(as.Date("2008-04-30"), as.Date("2021-01-29"), by="month"),
Portfolio_Returns = runif(n = 154, min = -0.05, max = 0.07)) # Random returns
# Load historical VIX and DXY_US data - Replace the read.csv() paths with the actual paths where your files are located
VIX_data <- read.csv("path_to_VIX.csv", stringsAsFactors = FALSE)
DXY_US_data <- read.csv("path_to_DXY_US.csv", stringsAsFactors = FALSE)
# Convert dates to Date class and ensure consistency in column names
VIX_data$Date <- as.Date(VIX_data$Date)
DXY_US_data$Date <- as.Date(DXY_US_data$date)
names(DXY_US_data)[2] <- 'DXY_US_Price'
# Merge the VIX, DXY_US data, and Portfolio Returns based on Date
merged_data <- merge(portfolio_returns, VIX_data, by = "Date")
merged_data <- merge(merged_data, DXY_US_data, by = "Date")
# Check head to ensure proper merge
head(merged_data)
# Perform linear regression: Portfolio Returns ~ VIX + DXY_US Price
model <- lm(Portfolio_Returns ~ AdjClose + DXY_US_Price, data = merged_data)
summary(model)
# Plotting to visualize relationships (optional):
# Relationship between VIX and Portfolio Returns
ggplot(merged_data, aes(x=AdjClose, y=Portfolio_Returns)) + geom_point() + geom_smooth(method="lm", color="blue") + labs(title="VIX vs. Portfolio Returns", x="VIX", y="Portfolio Return")
# Relationship between DXY_US Price and Portfolio Returns
ggplot(merged_data, aes(x=DXY_US_Price, y=Portfolio_Returns)) + geom_point() + geom_smooth(method="lm", color="red") + labs(title="DXY_US Price vs. Portfolio Returns", x="DXY_US Price", y="Portfolio Return")
Replace "path_to_VIX.csv" and "path_to_DXY_US.csv" with the actual file paths of your VIX and DXY_US datasets. This code does not only fit the linear regression model but also provides visualizations to help understand the relationship between your portfolio returns and the factors VIX and DXY. This will help in identifying the explanatory power of market volatility and the U.S. dollar index on your portfolio's performance.
Note: Data preparation is key. Ensure your portfolio returns data is aligned and has the same frequency (e.g., monthly) as your independent variables for accurate analysis.