Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # DURING THE CRISIS
- ### EWP ###
- #Weights
- weight10 <- rep(0.1, 10)
- weight50 <- rep(1/50, 50)
- #Portfolios
- EWP_10DD <- Return.portfolio(duringthecrisis10D, weights = weight10)
- plot(EWP_10DD, col = "black",ylim=c(-0.15, 0.15), main = "Portfolio returns - 10 assets daily", xlab = "Date", ylab = "Returns")
- EWP_50DD <- Return.portfolio(duringthecrisis50D, weights = weight50)
- plot(EWP_50DD, col = "black",ylim=c(-0.15, 0.15), main = "Portfolio returns - 50 assets daily", xlab = "Date", ylab = "Returns")
- EWP_10MD<- Return.portfolio(duringthecrisis10M, weights = weight10)
- plot(EWP_10MD, col = "black",ylim=c(-0.21, 0.12), main = "Portfolio returns - 10 assets monthly", xlab = "Date", ylab = "Returns")
- EWP_50MD <- Return.portfolio(duringthecrisis50M, weights = weight50)
- plot(EWP_50MD, col = "black", ylim=c(-0.21, 0.12),main = "Portfolio returns - 50 assets monthly", xlab = "Date", ylab = "Returns")
- #Monthly vs daily
- #Moyenne
- #Arrondir les chiffres
- mean_EWP_10DD <- round(mean(EWP_10DD),6)
- mean_EWP_50DD <- round(mean(EWP_50DD),6)
- mean_EWP_10MD <- round(mean(EWP_10MD),6)
- mean_EWP_50MD <- round(mean(EWP_50MD),6)
- #Graphique
- mean_portfolioEWPD <- c(mean_EWP_10DD, mean_EWP_50DD, mean_EWP_10MD,mean_EWP_50MD)
- meanEWPD <- barplot(mean_portfolioEWPD, col = "GREY", main = "Means of the portfolio returns - EWP", xlab = "Portfolio", ylab = "Mean", names.arg = c("10-D", "50-D", "10-M","50-M"))
- text(x = meanEWPD,
- y = mean_portfolioEWPD,
- labels = mean_portfolioEWPD, pos="1",cex=1,font=2,col = "black")
- #Variance
- var_EWP_10DD <- round(var(EWP_10DD),6)
- var_EWP_50DD <- round(var(EWP_50DD),6)
- var_EWP_10MD <- round(var(EWP_10MD),6)
- var_EWP_50MD <- round(var(EWP_50MD),6)
- var_portfolioEWPD <- c(var_EWP_10DD, var_EWP_50DD, var_EWP_10MD,var_EWP_50MD)
- #Graphique
- varEWPD <- barplot(var_portfolioEWPD, col = "GREY", main = "Variances of the portfolio return - EWP", xlab = "Portfolio", ylab = "Variance", names.arg = c("10-D", "50-D", "10-M","50-M"))
- text(x = varEWPD,
- y = var_portfolioEWPD,
- labels = var_portfolioEWPD, pos="1",cex=1,font=2,col = "black")
- # Corrélation
- cor(EWP_10DD, EWP_50DD)
- cor(EWP_10MD,EWP_50MD)
- #______________________________________________________________________________________________________________________________________________________________________________________________________________#
- ### GLOBAL MINIMUM VARIANCE PORTFOLIO (GMVP) ###
- #10 assets daily with short selling (SS)
- gmin.port10DSSD <- globalMin.portfolio(meanReturnsD10D, covmatD10D)
- gmin.port10DSSD
- w10DSSD <- gmin.port10DSSD$weights
- GMVP_10DSSD <- Return.portfolio(duringthecrisis10D, weights = w10DSSD)
- plot(GMVP_10DSSD, col = "black",ylim=c(-0.1, 0.1), main = "Portfolio returns - 10 assets daily", xlab = "Date", ylab = "Returns")
- #10 assets daily without short selling
- gmin.port10DD <- globalMin.portfolio(meanReturnsD10D, covmatD10D, shorts = FALSE)
- gmin.port10DD
- w10DD <- gmin.port10DD$weights
- GMVP_10DD <- Return.portfolio(duringthecrisis10D, weights = w10DD)
- plot(GMVP_10DD, col = "black",ylim=c(-0.1, 0.1), main = "Portfolio returns - 10 assets daily", xlab = "Date", ylab = "Returns")
- #50 assets daily with short selling (SS)
- gmin.port50DSSD <- globalMin.portfolio(meanReturnsD50D, covmatD50D)
- gmin.port50DSSD
- w50DSSD <- gmin.port50DSSD$weights
- GMVP_50DSSD <- Return.portfolio(duringthecrisis50D, weights = w50DSSD)
- plot(GMVP_50DSSD, col = "black",ylim=c(-0.1, 0.1), main = "Portfolio returns - 50 assets daily", xlab = "Date", ylab = "Returns")
- #50 assets daily without short selling
- gmin.port50DD <- globalMin.portfolio(meanReturnsD50D, covmatD50D, shorts = FALSE)
- gmin.port50DD
- w50DD <- gmin.port50DD$weights
- GMVP_50DD <- Return.portfolio(duringthecrisis50D, weights = w50DD)
- plot(GMVP_50DD, col = "black",ylim=c(-0.1, 0.1), main = "Portfolio returns - 50 assets daily", xlab = "Date", ylab = "Returns")
- #10 assets monthly with short selling (SS)
- gmin.port10MSSD <- globalMin.portfolio(meanReturnsD10M, covmatD10M)
- gmin.port10MSSD
- w10MSSD <- gmin.port10MSSD$weights
- GMVP_10MSSD <- Return.portfolio(duringthecrisis10M, weights = w10MSSD)
- plot(GMVP_10MSSD, col = "black",ylim=c(-0.12, 0.1), main = "Portfolio returns - 10 assets monthly", xlab = "Date", ylab = "Returns")
- #10 assets monthly without short selling
- gmin.port10MD <- globalMin.portfolio(meanReturnsD10M, covmatD10M, shorts = FALSE)
- gmin.port10MD
- w10MD <- gmin.port10MD$weights
- GMVP_10MD <- Return.portfolio(duringthecrisis10M, weights = w10MD)
- plot(GMVP_10MD, col = "black",ylim=c(-0.12, 0.1), main = "Portfolio returns - 10 assets monthly", xlab = "Date", ylab = "Returns")
- #50 assets monthly with short selling (SS)
- gmin.port50MSSD <- globalMin.portfolio(meanReturnsD50M, covmatD50M)
- gmin.port50MSSD
- w50MSSD <- gmin.port50MSSD$weights
- GMVP_50MSSD <- Return.portfolio(duringthecrisis50M, weights = w50MSSD)
- plot(GMVP_50MSSD, col = "black",ylim=c(-0.12, 0.1), main = "Portfolio returns - 50 assets monthly", xlab = "Date", ylab = "Returns")
- #50 assets monthly without short selling
- gmin.port50MD <- globalMin.portfolio(meanReturnsD50M, covmatD50M)
- gmin.port50MD
- w50MD <- gmin.port50MD$weights
- GMVP_50MD <- Return.portfolio(duringthecrisis50M, weights = w50MD)
- plot(GMVP_50MD, col = "black",ylim=c(-0.12, 0.1), main = "Portfolio returns - 50 assets monthly", xlab = "Date", ylab = "Returns")
- # Corrélation
- cor(GMVP_10DSSD, GMVP_50DSSD)
- cor(GMVP_10MSSD,GMVP_50MSSD)
- cor(GMVP_10DD, GMVP_50DD)
- cor(GMVP_10MD,GMVP_50MD)
- #Monthly vs daily - Without short selling
- #Moyenne
- #Arrondir les chiffres
- mean_GMVP_10DD <- round(mean(GMVP_10DD),6)
- mean_GMVP_50DD <- round(mean(GMVP_50DD),6)
- mean_GMVP_10MD <- round(mean(GMVP_10MD),6)
- mean_GMVP_50MD <- round(mean(GMVP_50MD),6)
- #Graphique
- mean_portfolioGMVPD <- c(mean_GMVP_10DD, mean_GMVP_50DD, mean_GMVP_10MD, mean_GMVP_50MD)
- meanGMVPD <- barplot(mean_portfolioGMVPD, col = "GREY",ylim=c(-0.0004, 0.003), main = "Means Returns - GMVP without short selling", xlab = "Portfolio", ylab = "Mean", names.arg = c("10-D", "50-D", "10-M","50-M"))
- text(x = meanGMVPD,
- y = mean_portfolioGMVPD,
- labels = mean_portfolioGMVPD, pos="3",cex=1,font=2,col = "black")
- #Variance
- #Arrondir les chiffres
- var_GMVP_10DD <- round(var(GMVP_10DD),6)
- var_GMVP_50DD <- round(var(GMVP_50DD),6)
- var_GMVP_10MD <- round(var(GMVP_10MD),6)
- var_GMVP_50MD <- round(var(GMVP_50MD),6)
- #Graphique
- var_portfolioGMVPD <- c(var_GMVP_10DD, var_GMVP_50DD, var_GMVP_10MD,var_GMVP_50MD)
- varGMVPD <- barplot(var_portfolioGMVPD, col = "GREY",ylim=c(0, 0.003), main = "Variances Returns - GMVP without short selling", xlab = "Portfolio", ylab = "Variance", names.arg = c("10-D", "50-D", "10-M","50-M"))
- text(x = varGMVPD,
- y = var_portfolioGMVPD,
- labels = var_portfolioGMVPD, pos="3",cex=1,font=2,col = "black")
- #Monthly vs daily - With short selling
- #Moyenne
- #Arrondir les chiffres
- mean_GMVP_10DSSD <- round(mean(GMVP_10DSSD),6)
- mean_GMVP_50DSSD <- round(mean(GMVP_50DSSD),6)
- mean_GMVP_10MSSD <- round(mean(GMVP_10MSSD),6)
- mean_GMVP_50MSSD <- round(mean(GMVP_50MSSD),6)
- #Graphique
- mean_portfolioGMVPSSD <- c(mean_GMVP_10DSSD, mean_GMVP_50DSSD, mean_GMVP_10MSSD, mean_GMVP_50MSSD)
- meanGMVPSSD <- barplot(mean_portfolioGMVPSSD, col = "GREY",ylim=c(-0.0005, 0.002), main = "Means Returns - GMVP with short selling", xlab = "Portfolio", ylab = "Mean", names.arg = c("10-D", "50-D", "10-M","50-M"))
- text(x = meanGMVPSSD,
- y = mean_portfolioGMVPSSD,
- labels = mean_portfolioGMVPSSD, pos="1",cex=1,font=2,col = "black")
- #Variance
- #Arrondir les chiffres
- var_GMVP_10DSSD <- round(var(GMVP_10DSSD),6)
- var_GMVP_50DSSD <- round(var(GMVP_50DSSD),6)
- var_GMVP_10MSSD <- round(var(GMVP_10MSSD),6)
- var_GMVP_50MSSD <- round(var(GMVP_50MSSD),6)
- #Graphique
- var_portfolioGMVPSSD <- c(var_GMVP_10DSSD, var_GMVP_50DSSD, var_GMVP_10MSSD,var_GMVP_50MSSD)
- varGMVPSSD <- barplot(var_portfolioGMVPSSD, col = "GREY",ylim=c(0, 0.002), main = "Variances Returns - GMVP with short selling", xlab = "Portfolio", ylab = "Variance", names.arg = c("10-D", "50-D", "10-M","50-M"))
- text(x = varGMVPSSD,
- y = var_portfolioGMVPSSD,
- labels = var_portfolioGMVPSSD, pos="3",cex=1,font=2,col = "black")
- #______________________________________________________________________________________________________________________________________________________________________________________________________________#
- ## MP (highest return portfolio) ##
- # 10 assets daily
- # Create the portfolio specification
- port_spec10DD <- portfolio.spec(colnames(duringthecrisis10D))
- # Add a full investment constraint such that the weights sum to 1
- port_spec10DD <- add.constraint(portfolio = port_spec10DD, type = "full_investment")
- # Add a long only constraint such that the weight of an asset is Detween 0 and 1
- port_spec10DD <- add.constraint(portfolio = port_spec10DD, type = "long_only")
- # Add an objective to minimize portfolio standard deviation
- port_spec10DD <- add.objective(portfolio = port_spec10DD, type = "return", name = "mean")
- # Solve the optimization problem
- MP_10DD<- optimize.portfolio(duringthecrisis10D , portfolio = port_spec10DD, optimize_method = "ROI")
- #Extract the optimal weights
- extractWeights(MP_10DD)
- #Chart the optimal weights
- chart.Weights(MP_10DD)
- #Returns
- weightMP10DD <- extractWeights(MP_10DD)
- MP10DD <- Return.portfolio(duringthecrisis10D, weights = weightMP10DD)
- # 10 assets monthly
- # Create the portfolio specification
- port_spec10MD <- portfolio.spec(colnames(duringthecrisis10M))
- # Add a full investment constraint such that the weights sum to 1
- port_spec10MD <- add.constraint(portfolio = port_spec10MD, type = "full_investment")
- # Add a long only constraint such that the weight of an asset is Detween 0 and 1
- port_spec10MD <- add.constraint(portfolio = port_spec10MD, type = "long_only")
- # Add an objective to minimize portfolio standard deviation
- port_spec10MD <- add.objective(portfolio = port_spec10MD, type = "return", name = "mean")
- # Solve the optimization problem
- MP_10MD<- optimize.portfolio(duringthecrisis10M , portfolio = port_spec10MD, optimize_method = "ROI")
- #Extract the optimal weights
- extractWeights(MP_10MD)
- #Chart the optimal weights
- chart.Weights(MP_10MD)
- #Returns
- weightMP10MD <- extractWeights(MP_10MD)
- MP10MD <- Return.portfolio(duringthecrisis10M, weights = weightMP10MD)
- # 50 assets monthly
- # Create the portfolio specification
- port_spec50MD <- portfolio.spec(colnames(duringthecrisis50M))
- # Add a full investment constraint such that the weights sum to 1
- port_spec50MD <- add.constraint(portfolio = port_spec50MD, type = "full_investment")
- # Add a long only constraint such that the weight of an asset is Detween 0 and 1
- port_spec50MD <- add.constraint(portfolio = port_spec50MD, type = "long_only")
- # Add an objective to minimize portfolio standard deviation
- port_spec50MD <- add.objective(portfolio = port_spec50MD, type = "return", name = "mean")
- # Solve the optimization problem
- MP_50MD<- optimize.portfolio(duringthecrisis50M , portfolio = port_spec50MD, optimize_method = "ROI")
- #Extract the optimal weights
- extractWeights(MP_50MD)
- #Chart the optimal weights
- chart.Weights(MP_50MD)
- #Returns
- weightMP50MD <- extractWeights(MP_50MD)
- MP50MD <- Return.portfolio(duringthecrisis50M, weights = weightMP50MD)
- # 50 assets daily
- # Create the portfolio specification
- port_spec50DD <- portfolio.spec(colnames(duringthecrisis50D))
- # Add a full investment constraint such that the weights sum to 1
- port_spec50DD <- add.constraint(portfolio = port_spec50DD, type = "full_investment")
- # Add a long only constraint such that the weight of an asset is Detween 0 and 1
- port_spec50DD <- add.constraint(portfolio = port_spec50DD, type = "long_only")
- # Add an objective to minimize portfolio standard deviation
- port_spec50DD <- add.objective(portfolio = port_spec50DD, type = "return", name = "mean")
- # Solve the optimization problem
- MP_50DD<- optimize.portfolio(duringthecrisis50D , portfolio = port_spec50MD, optimize_method = "ROI")
- #Extract the optimal weights
- extractWeights(MP_50DD)
- #Chart the optimal weights
- chart.Weights(MP_50DD)
- #Returns
- weightMP50DD <- extractWeights(MP_50DD)
- MP50DD <- Return.portfolio(duringthecrisis50D, weights = weightMP50DD)
- #Graphiques avec les returns
- plot(MP10DD, col = "black", ylim=c(-0.2, 0.22), main = "MP portfolio returns - 10 assets daily", xlab = "Date", ylab = "Returns")
- plot(MP50DD, col = "black",ylim=c(-0.2, 0.22), main = "MP portfolio returns - 50 assets daily", xlab = "Date", ylab = "Returns")
- plot(MP10MD, col = "black",ylim=c(-0.42, 0.37), main = "MP portfolio returns - 10 assets monthly", xlab = "Date", ylab = "Returns")
- plot(MP50MD, col = "black",ylim=c(-0.25, 0.3), main = "MP portfolio returns - 50 assets monthly", xlab = "Date", ylab = "Returns")
- #Monthly vs daily - Without short selling
- #Moyenne
- #Arrondir les chiffres
- mean_MP_10DD <- round(mean(MP10DD),6)
- mean_MP_50DD <- round(mean(MP50DD),6)
- mean_MP_10MD <- round(mean(MP10MD),6)
- mean_MP_50MD <- round(mean(MP50MD),6)
- #Graphique
- mean_portfolioMPD <- c(mean_MP_10DD, mean_MP_50DD, mean_MP_10MD,mean_MP_50MD)
- meanMPD <- barplot(mean_portfolioMPD, col = "GREY",ylim=c(-0.005, 0.015), main = "Means of the portfolios - MP", xlab = "Portfolio", ylab = "Mean", names.arg = c("10-D", "50-D", "10-M","50-M"))
- text(x = meanMPD,
- y = mean_portfolioMPD,
- labels = mean_portfolioMPD, pos="1",cex=1,font=2,col = "black")
- #Variance
- #Arrondir les chiffres
- var_MP_10DD <- round(var(MP10DD),6)
- var_MP_50DD <- round(var(MP50DD),6)
- var_MP_10MD <- round(var(MP10MD),6)
- var_MP_50MD <- round(var(MP50MD),6)
- #Graphique
- var_portfolioMPD <- c(var_MP_10DD, var_MP_50DD,var_MP_10MD,var_MP_50MD)
- varMPD <- barplot(var_portfolioMPD, col = "GREY", main = "Variances of the portfolios - MP", xlab = "Portfolio", ylab = "Variance", names.arg = c("10-D", "50-D", "10-M","50-M"))
- text(x = varMPD,
- y = var_portfolioMPD,
- labels = var_portfolioMPD, pos="1",cex=1,font=2,col = "black")
- #______________________________________________________________________________________________________________________________________________________________________________________________________________#
- ## ERC ##
- #10 assets daily
- WeightERC_10DD <- optimalPortfolio(Sigma = cov(duringthecrisis10D), control = list(type = 'erc', constraint = 'lo'))
- WeightERC_10DD
- ERC_10DD <- Return.portfolio(duringthecrisis10D, weights = WeightERC_10DD)
- #50 assets daily
- WeightERC_50DD <- optimalPortfolio(Sigma = cov(duringthecrisis50D), control = list(type = 'erc', constraint = 'lo'))
- WeightERC_50DD
- ERC_50DD <- Return.portfolio(duringthecrisis50D, weights = WeightERC_50DD)
- #10 assets monthly
- WeightERC_10MD <- optimalPortfolio(Sigma = cov(duringthecrisis10M), control = list(type = 'erc', constraint = 'lo'))
- WeightERC_10MD
- ERC_10MD <- Return.portfolio(duringthecrisis10M, weights = WeightERC_10MD)
- #50 assets monthly
- WeightERC_50MD <- optimalPortfolio(Sigma = cov(duringthecrisis50M), control = list(type = 'erc', constraint = 'lo'))
- WeightERC_50MD
- ERC_50MD <- Return.portfolio(duringthecrisis50M, weights = WeightERC_50MD)
- #Graphiques avec les returns
- plot(ERC_10MD, col = "black",ylim=c(-0.15, 0.1), main = "ERC portfolio returns - 10 assets monthly", xlab = "Date", ylab = "Returns")
- plot(ERC_50MD, col = "black",ylim=c(-0.17, 0.1), main = "ERC portfolio returns - 50 assets monthly", xlab = "Date", ylab = "Returns")
- plot(ERC_10DD, col = "black", ylim=c(-0.1, 0.13),main = "ERC portfolio returns - 10 assets daily", xlab = "Date", ylab = "Returns")
- plot(ERC_50DD, col = "black",ylim=c(-0.1, 0.13), main = "ERC portfolio returns - 50 assets daily", xlab = "Date", ylab = "Returns")
- #Monthly vs daily
- #Moyenne
- #Arrondir les chiffres
- mean_ERC_10DD <- round(mean(ERC_10DD),6)
- mean_ERC_50DD <- round(mean(ERC_50DD),6)
- mean_ERC_10MD <- round(mean(ERC_10MD),6)
- mean_ERC_50MD <- round(mean(ERC_50MD),6)
- #Graphique
- mean_portfolioERCD <- c(mean_ERC_10DD, mean_ERC_50DD, mean_ERC_10MD,mean_ERC_50MD)
- meanERCD <- barplot(mean_portfolioERCD, col = "GREY", main = "Means of the portfolios - ERC", xlab = "Portfolio", ylab = "Mean", names.arg = c("10-D", "50-D", "10-M","50-M"))
- text(x = meanERCD,
- y = mean_portfolioERCD,
- labels = mean_portfolioERCD, pos="1",cex=1,font=2,col = "black")
- #Variance
- #Arrondir les chiffres
- var_ERC_10DD <- round(var(ERC_10DD),6)
- var_ERC_50DD <- round(var(ERC_50DD),6)
- var_ERC_10MD <- round(var(ERC_10MD),6)
- var_ERC_50MD <- round(var(ERC_50MD),6)
- #Graphique
- var_portfolioERCD <- c(var_ERC_10DD, var_ERC_50DD, var_ERC_10MD,var_ERC_50MD)
- varERCD <- barplot(var_portfolioERCD, col = "GREY", main = "Means of the portfolios - ERC", xlab = "Portfolio", ylab = "Mean", names.arg = c("10-D", "50-D", "10-M","50-M"))
- text(x = varERCD,
- y = var_portfolioERCD,
- labels = var_portfolioERCD, pos="1",cex=1,font=2,col = "black")
- #______________________________________________________________________________________________________________________________________________________________________________________________________________#
- #CORRELATIONS
- corr10DD <- corrplot(cormatD10D,type="lower")
- corr50DD <- corrplot(cormatD50D,type="lower")
- corr10MD <- corrplot(cormatD10M,type="lower")
- corr50MD <- corrplot(cormatD50M,type="lower")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement