Advertisement
Guest User

during the crisis

a guest
Jan 13th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 17.03 KB | None | 0 0
  1. # DURING THE CRISIS
  2.  
  3. ### EWP ###
  4.  
  5. #Weights
  6. weight10 <- rep(0.1, 10)
  7. weight50 <- rep(1/50, 50)
  8.  
  9. #Portfolios
  10. EWP_10DD <- Return.portfolio(duringthecrisis10D, weights = weight10)
  11. plot(EWP_10DD, col = "black",ylim=c(-0.15, 0.15), main = "Portfolio returns - 10 assets daily", xlab = "Date", ylab = "Returns")
  12. EWP_50DD <- Return.portfolio(duringthecrisis50D, weights = weight50)
  13. plot(EWP_50DD, col = "black",ylim=c(-0.15, 0.15), main = "Portfolio returns - 50 assets daily", xlab = "Date", ylab = "Returns")
  14. EWP_10MD<- Return.portfolio(duringthecrisis10M, weights = weight10)
  15. plot(EWP_10MD, col = "black",ylim=c(-0.21, 0.12), main = "Portfolio returns - 10 assets monthly", xlab = "Date", ylab = "Returns")
  16. EWP_50MD <- Return.portfolio(duringthecrisis50M, weights = weight50)
  17. plot(EWP_50MD, col = "black", ylim=c(-0.21, 0.12),main = "Portfolio returns - 50 assets monthly", xlab = "Date", ylab = "Returns")
  18.  
  19. #Monthly vs daily
  20. #Moyenne
  21. #Arrondir les chiffres
  22. mean_EWP_10DD <- round(mean(EWP_10DD),6)
  23. mean_EWP_50DD <- round(mean(EWP_50DD),6)
  24. mean_EWP_10MD <- round(mean(EWP_10MD),6)
  25. mean_EWP_50MD <- round(mean(EWP_50MD),6)
  26. #Graphique
  27. mean_portfolioEWPD <- c(mean_EWP_10DD, mean_EWP_50DD, mean_EWP_10MD,mean_EWP_50MD)
  28. 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"))
  29. text(x = meanEWPD,
  30.      y = mean_portfolioEWPD,
  31.      labels = mean_portfolioEWPD, pos="1",cex=1,font=2,col = "black")
  32. #Variance
  33. var_EWP_10DD <- round(var(EWP_10DD),6)
  34. var_EWP_50DD <- round(var(EWP_50DD),6)
  35. var_EWP_10MD <- round(var(EWP_10MD),6)
  36. var_EWP_50MD <- round(var(EWP_50MD),6)
  37. var_portfolioEWPD <- c(var_EWP_10DD, var_EWP_50DD, var_EWP_10MD,var_EWP_50MD)
  38. #Graphique
  39. 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"))
  40. text(x = varEWPD,
  41.      y = var_portfolioEWPD,
  42.      labels = var_portfolioEWPD, pos="1",cex=1,font=2,col = "black")
  43. # Corrélation
  44. cor(EWP_10DD, EWP_50DD)
  45. cor(EWP_10MD,EWP_50MD)
  46.  
  47. #______________________________________________________________________________________________________________________________________________________________________________________________________________#
  48.  
  49.  
  50. ### GLOBAL MINIMUM VARIANCE PORTFOLIO (GMVP) ###
  51.  
  52. #10 assets daily with short selling (SS)
  53. gmin.port10DSSD <- globalMin.portfolio(meanReturnsD10D, covmatD10D)
  54. gmin.port10DSSD
  55. w10DSSD <- gmin.port10DSSD$weights
  56. GMVP_10DSSD <- Return.portfolio(duringthecrisis10D, weights = w10DSSD)
  57. plot(GMVP_10DSSD, col = "black",ylim=c(-0.1, 0.1), main = "Portfolio returns - 10 assets daily", xlab = "Date", ylab = "Returns")
  58. #10 assets daily without short selling
  59. gmin.port10DD <- globalMin.portfolio(meanReturnsD10D, covmatD10D, shorts = FALSE)
  60. gmin.port10DD
  61. w10DD <- gmin.port10DD$weights
  62. GMVP_10DD <- Return.portfolio(duringthecrisis10D, weights = w10DD)
  63. plot(GMVP_10DD, col = "black",ylim=c(-0.1, 0.1), main = "Portfolio returns - 10 assets daily", xlab = "Date", ylab = "Returns")
  64.  
  65. #50 assets daily with short selling (SS)
  66. gmin.port50DSSD <- globalMin.portfolio(meanReturnsD50D, covmatD50D)
  67. gmin.port50DSSD
  68. w50DSSD <- gmin.port50DSSD$weights
  69. GMVP_50DSSD <- Return.portfolio(duringthecrisis50D, weights = w50DSSD)
  70. plot(GMVP_50DSSD, col = "black",ylim=c(-0.1, 0.1), main = "Portfolio returns - 50 assets daily", xlab = "Date", ylab = "Returns")
  71. #50 assets daily without short selling
  72. gmin.port50DD <- globalMin.portfolio(meanReturnsD50D, covmatD50D, shorts = FALSE)
  73. gmin.port50DD
  74. w50DD <- gmin.port50DD$weights
  75. GMVP_50DD <- Return.portfolio(duringthecrisis50D, weights = w50DD)
  76. plot(GMVP_50DD, col = "black",ylim=c(-0.1, 0.1), main = "Portfolio returns - 50 assets daily", xlab = "Date", ylab = "Returns")
  77.  
  78. #10 assets monthly with short selling (SS)
  79. gmin.port10MSSD <- globalMin.portfolio(meanReturnsD10M, covmatD10M)
  80. gmin.port10MSSD
  81. w10MSSD <- gmin.port10MSSD$weights
  82. GMVP_10MSSD <- Return.portfolio(duringthecrisis10M, weights = w10MSSD)
  83. plot(GMVP_10MSSD, col = "black",ylim=c(-0.12, 0.1), main = "Portfolio returns - 10 assets monthly", xlab = "Date", ylab = "Returns")
  84. #10 assets monthly without short selling
  85. gmin.port10MD <- globalMin.portfolio(meanReturnsD10M, covmatD10M, shorts = FALSE)
  86. gmin.port10MD
  87. w10MD <- gmin.port10MD$weights
  88. GMVP_10MD <- Return.portfolio(duringthecrisis10M, weights = w10MD)
  89. plot(GMVP_10MD, col = "black",ylim=c(-0.12, 0.1), main = "Portfolio returns - 10 assets monthly", xlab = "Date", ylab = "Returns")
  90.  
  91. #50 assets monthly with short selling (SS)
  92. gmin.port50MSSD <- globalMin.portfolio(meanReturnsD50M, covmatD50M)
  93. gmin.port50MSSD
  94. w50MSSD <- gmin.port50MSSD$weights
  95. GMVP_50MSSD <- Return.portfolio(duringthecrisis50M, weights = w50MSSD)
  96. plot(GMVP_50MSSD, col = "black",ylim=c(-0.12, 0.1), main = "Portfolio returns - 50 assets monthly", xlab = "Date", ylab = "Returns")
  97. #50 assets monthly without short selling
  98. gmin.port50MD <- globalMin.portfolio(meanReturnsD50M, covmatD50M)
  99. gmin.port50MD
  100. w50MD <- gmin.port50MD$weights
  101. GMVP_50MD <- Return.portfolio(duringthecrisis50M, weights = w50MD)
  102. plot(GMVP_50MD, col = "black",ylim=c(-0.12, 0.1), main = "Portfolio returns - 50 assets monthly", xlab = "Date", ylab = "Returns")
  103.  
  104. # Corrélation
  105. cor(GMVP_10DSSD, GMVP_50DSSD)
  106. cor(GMVP_10MSSD,GMVP_50MSSD)
  107. cor(GMVP_10DD, GMVP_50DD)
  108. cor(GMVP_10MD,GMVP_50MD)
  109.  
  110. #Monthly vs daily - Without short selling
  111. #Moyenne
  112. #Arrondir les chiffres
  113. mean_GMVP_10DD <- round(mean(GMVP_10DD),6)
  114. mean_GMVP_50DD <- round(mean(GMVP_50DD),6)
  115. mean_GMVP_10MD <- round(mean(GMVP_10MD),6)
  116. mean_GMVP_50MD <- round(mean(GMVP_50MD),6)
  117. #Graphique
  118. mean_portfolioGMVPD <- c(mean_GMVP_10DD, mean_GMVP_50DD, mean_GMVP_10MD, mean_GMVP_50MD)
  119. 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"))
  120. text(x = meanGMVPD,
  121.      y = mean_portfolioGMVPD,
  122.      labels = mean_portfolioGMVPD, pos="3",cex=1,font=2,col = "black")
  123.  
  124. #Variance
  125. #Arrondir les chiffres
  126. var_GMVP_10DD <- round(var(GMVP_10DD),6)
  127. var_GMVP_50DD <- round(var(GMVP_50DD),6)
  128. var_GMVP_10MD <- round(var(GMVP_10MD),6)
  129. var_GMVP_50MD <- round(var(GMVP_50MD),6)
  130. #Graphique
  131. var_portfolioGMVPD <- c(var_GMVP_10DD, var_GMVP_50DD, var_GMVP_10MD,var_GMVP_50MD)
  132. 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"))
  133. text(x = varGMVPD,
  134.      y = var_portfolioGMVPD,
  135.      labels = var_portfolioGMVPD, pos="3",cex=1,font=2,col = "black")
  136.  
  137. #Monthly vs daily - With short selling
  138. #Moyenne
  139. #Arrondir les chiffres
  140. mean_GMVP_10DSSD <- round(mean(GMVP_10DSSD),6)
  141. mean_GMVP_50DSSD <- round(mean(GMVP_50DSSD),6)
  142. mean_GMVP_10MSSD <- round(mean(GMVP_10MSSD),6)
  143. mean_GMVP_50MSSD <- round(mean(GMVP_50MSSD),6)
  144. #Graphique
  145. mean_portfolioGMVPSSD <- c(mean_GMVP_10DSSD, mean_GMVP_50DSSD, mean_GMVP_10MSSD, mean_GMVP_50MSSD)
  146. 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"))
  147. text(x = meanGMVPSSD,
  148.      y = mean_portfolioGMVPSSD,
  149.      labels = mean_portfolioGMVPSSD, pos="1",cex=1,font=2,col = "black")
  150.  
  151. #Variance
  152. #Arrondir les chiffres
  153. var_GMVP_10DSSD <- round(var(GMVP_10DSSD),6)
  154. var_GMVP_50DSSD <- round(var(GMVP_50DSSD),6)
  155. var_GMVP_10MSSD <- round(var(GMVP_10MSSD),6)
  156. var_GMVP_50MSSD <- round(var(GMVP_50MSSD),6)
  157. #Graphique
  158. var_portfolioGMVPSSD <- c(var_GMVP_10DSSD, var_GMVP_50DSSD, var_GMVP_10MSSD,var_GMVP_50MSSD)
  159. 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"))
  160. text(x = varGMVPSSD,
  161.      y = var_portfolioGMVPSSD,
  162.      labels = var_portfolioGMVPSSD, pos="3",cex=1,font=2,col = "black")
  163.  
  164. #______________________________________________________________________________________________________________________________________________________________________________________________________________#
  165.  
  166. ## MP (highest return portfolio) ##
  167.  
  168. # 10 assets daily
  169. # Create the portfolio specification
  170. port_spec10DD <- portfolio.spec(colnames(duringthecrisis10D))
  171. # Add a full investment constraint such that the weights sum to 1
  172. port_spec10DD <- add.constraint(portfolio = port_spec10DD, type = "full_investment")
  173. # Add a long only constraint such that the weight of an asset is Detween 0 and 1
  174. port_spec10DD <- add.constraint(portfolio = port_spec10DD, type = "long_only")
  175. # Add an objective to minimize portfolio standard deviation
  176. port_spec10DD <- add.objective(portfolio = port_spec10DD, type = "return", name = "mean")
  177. # Solve the optimization problem
  178. MP_10DD<- optimize.portfolio(duringthecrisis10D , portfolio = port_spec10DD, optimize_method = "ROI")
  179. #Extract the optimal weights
  180. extractWeights(MP_10DD)
  181. #Chart the optimal weights
  182. chart.Weights(MP_10DD)
  183. #Returns
  184. weightMP10DD <- extractWeights(MP_10DD)
  185. MP10DD <- Return.portfolio(duringthecrisis10D, weights = weightMP10DD)
  186.  
  187. # 10 assets monthly
  188. # Create the portfolio specification
  189. port_spec10MD <- portfolio.spec(colnames(duringthecrisis10M))
  190. # Add a full investment constraint such that the weights sum to 1
  191. port_spec10MD <- add.constraint(portfolio = port_spec10MD, type = "full_investment")
  192. # Add a long only constraint such that the weight of an asset is Detween 0 and 1
  193. port_spec10MD <- add.constraint(portfolio = port_spec10MD, type = "long_only")
  194. # Add an objective to minimize portfolio standard deviation
  195. port_spec10MD <- add.objective(portfolio = port_spec10MD, type = "return", name = "mean")
  196. # Solve the optimization problem
  197. MP_10MD<- optimize.portfolio(duringthecrisis10M , portfolio = port_spec10MD, optimize_method = "ROI")
  198. #Extract the optimal weights
  199. extractWeights(MP_10MD)
  200. #Chart the optimal weights
  201. chart.Weights(MP_10MD)
  202. #Returns
  203. weightMP10MD <- extractWeights(MP_10MD)
  204. MP10MD <- Return.portfolio(duringthecrisis10M, weights = weightMP10MD)
  205.  
  206. # 50 assets monthly
  207. # Create the portfolio specification
  208. port_spec50MD <- portfolio.spec(colnames(duringthecrisis50M))
  209. # Add a full investment constraint such that the weights sum to 1
  210. port_spec50MD <- add.constraint(portfolio = port_spec50MD, type = "full_investment")
  211. # Add a long only constraint such that the weight of an asset is Detween 0 and 1
  212. port_spec50MD <- add.constraint(portfolio = port_spec50MD, type = "long_only")
  213. # Add an objective to minimize portfolio standard deviation
  214. port_spec50MD <- add.objective(portfolio = port_spec50MD, type = "return", name = "mean")
  215. # Solve the optimization problem
  216. MP_50MD<- optimize.portfolio(duringthecrisis50M , portfolio = port_spec50MD, optimize_method = "ROI")
  217. #Extract the optimal weights
  218. extractWeights(MP_50MD)
  219. #Chart the optimal weights
  220. chart.Weights(MP_50MD)
  221. #Returns
  222. weightMP50MD <- extractWeights(MP_50MD)
  223. MP50MD <- Return.portfolio(duringthecrisis50M, weights = weightMP50MD)
  224.  
  225. # 50 assets daily
  226. # Create the portfolio specification
  227. port_spec50DD <- portfolio.spec(colnames(duringthecrisis50D))
  228. # Add a full investment constraint such that the weights sum to 1
  229. port_spec50DD <- add.constraint(portfolio = port_spec50DD, type = "full_investment")
  230. # Add a long only constraint such that the weight of an asset is Detween 0 and 1
  231. port_spec50DD <- add.constraint(portfolio = port_spec50DD, type = "long_only")
  232. # Add an objective to minimize portfolio standard deviation
  233. port_spec50DD <- add.objective(portfolio = port_spec50DD, type = "return", name = "mean")
  234. # Solve the optimization problem
  235. MP_50DD<- optimize.portfolio(duringthecrisis50D , portfolio = port_spec50MD, optimize_method = "ROI")
  236. #Extract the optimal weights
  237. extractWeights(MP_50DD)
  238. #Chart the optimal weights
  239. chart.Weights(MP_50DD)
  240. #Returns
  241. weightMP50DD <- extractWeights(MP_50DD)
  242. MP50DD <- Return.portfolio(duringthecrisis50D, weights = weightMP50DD)
  243.  
  244. #Graphiques avec les returns
  245. plot(MP10DD, col = "black", ylim=c(-0.2, 0.22), main = "MP portfolio returns - 10 assets daily", xlab = "Date", ylab = "Returns")
  246. plot(MP50DD, col = "black",ylim=c(-0.2, 0.22), main = "MP portfolio returns - 50 assets daily", xlab = "Date", ylab = "Returns")
  247. plot(MP10MD, col = "black",ylim=c(-0.42, 0.37), main = "MP portfolio returns - 10 assets monthly", xlab = "Date", ylab = "Returns")
  248. plot(MP50MD, col = "black",ylim=c(-0.25, 0.3), main = "MP portfolio returns - 50 assets monthly", xlab = "Date", ylab = "Returns")
  249.  
  250.  
  251. #Monthly vs daily - Without short selling
  252. #Moyenne
  253. #Arrondir les chiffres
  254. mean_MP_10DD <- round(mean(MP10DD),6)
  255. mean_MP_50DD <- round(mean(MP50DD),6)
  256. mean_MP_10MD <- round(mean(MP10MD),6)
  257. mean_MP_50MD <- round(mean(MP50MD),6)
  258. #Graphique
  259. mean_portfolioMPD <- c(mean_MP_10DD, mean_MP_50DD, mean_MP_10MD,mean_MP_50MD)
  260. 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"))
  261. text(x = meanMPD,
  262.      y = mean_portfolioMPD,
  263.      labels = mean_portfolioMPD, pos="1",cex=1,font=2,col = "black")
  264.  
  265. #Variance
  266. #Arrondir les chiffres
  267. var_MP_10DD <- round(var(MP10DD),6)
  268. var_MP_50DD <- round(var(MP50DD),6)
  269. var_MP_10MD <- round(var(MP10MD),6)
  270. var_MP_50MD <- round(var(MP50MD),6)
  271. #Graphique
  272. var_portfolioMPD <- c(var_MP_10DD, var_MP_50DD,var_MP_10MD,var_MP_50MD)
  273. 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"))
  274. text(x = varMPD,
  275.      y = var_portfolioMPD,
  276.      labels = var_portfolioMPD, pos="1",cex=1,font=2,col = "black")
  277.  
  278.  
  279. #______________________________________________________________________________________________________________________________________________________________________________________________________________#
  280.  
  281. ## ERC ##
  282.  
  283. #10 assets daily
  284. WeightERC_10DD <- optimalPortfolio(Sigma = cov(duringthecrisis10D), control = list(type = 'erc', constraint = 'lo'))
  285. WeightERC_10DD
  286. ERC_10DD <- Return.portfolio(duringthecrisis10D, weights = WeightERC_10DD)
  287.  
  288. #50 assets daily
  289. WeightERC_50DD <- optimalPortfolio(Sigma = cov(duringthecrisis50D), control = list(type = 'erc', constraint = 'lo'))
  290. WeightERC_50DD
  291. ERC_50DD <- Return.portfolio(duringthecrisis50D, weights = WeightERC_50DD)
  292.  
  293. #10 assets monthly
  294. WeightERC_10MD <- optimalPortfolio(Sigma = cov(duringthecrisis10M), control = list(type = 'erc', constraint = 'lo'))
  295. WeightERC_10MD
  296. ERC_10MD <- Return.portfolio(duringthecrisis10M, weights = WeightERC_10MD)
  297.  
  298. #50 assets monthly
  299. WeightERC_50MD <- optimalPortfolio(Sigma = cov(duringthecrisis50M), control = list(type = 'erc', constraint = 'lo'))
  300. WeightERC_50MD
  301. ERC_50MD <- Return.portfolio(duringthecrisis50M, weights = WeightERC_50MD)
  302.  
  303. #Graphiques avec les returns
  304. plot(ERC_10MD, col = "black",ylim=c(-0.15, 0.1), main = "ERC portfolio returns - 10 assets monthly", xlab = "Date", ylab = "Returns")
  305. plot(ERC_50MD, col = "black",ylim=c(-0.17, 0.1), main = "ERC portfolio returns - 50 assets monthly", xlab = "Date", ylab = "Returns")
  306. plot(ERC_10DD, col = "black", ylim=c(-0.1, 0.13),main = "ERC portfolio returns - 10 assets daily", xlab = "Date", ylab = "Returns")
  307. plot(ERC_50DD, col = "black",ylim=c(-0.1, 0.13), main = "ERC portfolio returns - 50 assets daily", xlab = "Date", ylab = "Returns")
  308.  
  309. #Monthly vs daily
  310. #Moyenne
  311. #Arrondir les chiffres
  312. mean_ERC_10DD <- round(mean(ERC_10DD),6)
  313. mean_ERC_50DD <- round(mean(ERC_50DD),6)
  314. mean_ERC_10MD <- round(mean(ERC_10MD),6)
  315. mean_ERC_50MD <- round(mean(ERC_50MD),6)
  316. #Graphique
  317. mean_portfolioERCD <- c(mean_ERC_10DD, mean_ERC_50DD, mean_ERC_10MD,mean_ERC_50MD)
  318. 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"))
  319. text(x = meanERCD,
  320.      y = mean_portfolioERCD,
  321.      labels = mean_portfolioERCD, pos="1",cex=1,font=2,col = "black")
  322.  
  323. #Variance
  324. #Arrondir les chiffres
  325. var_ERC_10DD <- round(var(ERC_10DD),6)
  326. var_ERC_50DD <- round(var(ERC_50DD),6)
  327. var_ERC_10MD <- round(var(ERC_10MD),6)
  328. var_ERC_50MD <- round(var(ERC_50MD),6)
  329. #Graphique
  330. var_portfolioERCD <- c(var_ERC_10DD, var_ERC_50DD, var_ERC_10MD,var_ERC_50MD)
  331. 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"))
  332. text(x = varERCD,
  333.      y = var_portfolioERCD,
  334.      labels = var_portfolioERCD, pos="1",cex=1,font=2,col = "black")
  335.  
  336. #______________________________________________________________________________________________________________________________________________________________________________________________________________#
  337.  
  338. #CORRELATIONS
  339.  
  340. corr10DD <- corrplot(cormatD10D,type="lower")
  341. corr50DD <- corrplot(cormatD50D,type="lower")
  342. corr10MD <- corrplot(cormatD10M,type="lower")
  343. corr50MD <- corrplot(cormatD50M,type="lower")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement