Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize with rebalancing weights not adding up to 1 (minimum variance). #30

Open
SeveralAbility opened this issue Apr 9, 2021 · 0 comments

Comments

@SeveralAbility
Copy link

SeveralAbility commented Apr 9, 2021

I have an issue that I can't seem to solve where when running my data through optimize.portfolio.rebalancing with the objective being to minimise variance, I get the warning message

In Return.portfolio.geometric(R = R, weights = weights, wealth.index = wealth.index,  :
  The weights for one or more periods do not sum up to 1: assuming a return of 0 for the residual weights

When I run the same data but with the objective to maximise return I do not get this error. I've tried to give a snippet my code that doesn't below, I've also attached the csv file I used to input the values.

library(PortfolioAnalytics)
library(PerformanceAnalytics)
library(ROI)
library(ROI.plugin.glpk)
library(ROI.plugin.quadprog)
library(openxlsx)
library(zoo)
library(xts)
library(ggplot2)
Raw.Data <- read.csv('~/Desktop/Portfolio Optimisation/Data/Combined Data.csv')
attach(Raw.Data)
dates <- seq(as.Date("2005-06-01"), length=188, by="months")
TimeSeries <- xts(x=Raw.Data[1:188,2:8], order.by=dates)
Returns.Extra.Row <- Return.calculate(TimeSeries, method = "discrete")
Returns <- Returns.Extra.Row[-c(1),]
fund.names <-colnames(Returns)
pspec <- portfolio.spec(assets=fund.names)
pspec <- add.constraint(portfolio=pspec, type="full_investment")
pspec <- add.constraint(portfolio=pspec,type="box",min=0,max=1)
Weight0.05 <- seq(from = 0.05, to = 0.05, length.out = 7)
Weight1.00 <- seq(from = 1.00, to = 1.00, length.out = 7)
min0.05_max1.00 <- pspec
min0.05_max1.00 <- add.constraint(portfolio=min0.05_max1.00, 
                                  type="box", 
                                  min=Weight0.05, 
                                  max=Weight1.00)

# Maximising return works as expected
MaximiseReturn_min0.05_max1.00 <- add.objective(portfolio=min0.05_max1.00, type="return", name="mean")
OptimiseMaximiseReturn_min0.05_max1.00 <- optimize.portfolio.rebalancing(R=Returns, portfolio=MaximiseReturn_min0.05_max1.00,optimize_method="ROI",rebalance_on="years",training_period=12)
SummaryOptimiseMaximiseReturn_min0.05_max1.00 <- summary (OptimiseMaximiseReturn_min0.05_max1.00)
chart.Weights(OptimiseMaximiseReturn_min0.05_max1.00, colorset = rainbow(7), cex.legend = 0.6)
plot.zoo(SummaryOptimiseMaximiseReturn_min0.05_max1.00$portfolio_returns, ylab = "Returns")
plot.zoo(cumsum(SummaryOptimiseMaximiseReturn_min0.05_max1.00$portfolio_returns), ylab = "Cumulative Returns")
plot.zoo(exp(cumsum(SummaryOptimiseMaximiseReturn_min0.05_max1.00$portfolio_returns)), ylab = "Compounded Cumulative Returns")

# Minimising variance not working as expected
MinimiseVariance_min0.05_max1.00 <- add.objective(portfolio=min0.05_max1.00, type="risk", name="StdDev")
OptimiseMinimiseVariance_min0.05_max1.00 <- optimize.portfolio.rebalancing(R=Returns, portfolio=MinimiseVariance_min0.05_max1.00,optimize_method="ROI",rebalance_on="years",training_period=12)
SummaryOptimiseMinimiseVariance_min0.05_max1.00 <- summary (OptimiseMinimiseVariance_min0.05_max1.00)
chart.Weights(OptimiseMinimiseVariance_min0.05_max1.00, colorset = rainbow(7), cex.legend = 0.6)
plot.zoo(SummaryOptimiseMinimiseVariance_min0.05_max1.00$portfolio_returns, ylab = "Returns")
plot.zoo(cumsum(SummaryOptimiseMinimiseVariance_min0.05_max1.00$portfolio_returns), ylab = "Cumulative Returns")
plot.zoo(exp(cumsum(SummaryOptimiseMinimiseVariance_min0.05_max1.00$portfolio_returns)), ylab = "Compounded Cumulative Returns")
# Running the code below shows that for some dates the weights are given as NA
SummaryOptimiseMinimiseVariance_min0.05_max1.00$weights

Combined Data.csv

Session info

R version 4.0.4 (2021-02-15)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.7

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] ggplot2_3.3.3              openxlsx_4.2.3             ROI.plugin.quadprog_1.0-0 
 [4] ROI.plugin.glpk_1.0-0      ROI_1.0-0                  PortfolioAnalytics_1.1.0  
 [7] PerformanceAnalytics_2.0.4 foreach_1.5.1              xts_0.12.1                
[10] zoo_1.8-9                 

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.6       magrittr_2.0.1   munsell_0.5.0    colorspace_2.0-0 lattice_0.20-41 
 [6] R6_2.5.0         rlang_0.4.10     quadprog_1.5-8   fansi_0.4.2      tools_4.0.4     
[11] grid_4.0.4       gtable_0.3.0     utf8_1.2.1       registry_0.5-1   withr_2.4.1     
[16] ellipsis_0.3.1   iterators_1.0.13 tibble_3.1.0     lifecycle_1.0.0  crayon_1.4.1    
[21] zip_2.1.1        vctrs_0.3.6      Rglpk_0.6-4      codetools_0.2-18 glue_1.4.2      
[26] slam_0.1-48      stringi_1.5.3    pillar_1.5.1     compiler_4.0.4   scales_1.1.1    
[31] pkgconfig_2.0.3 

Any help would be appreciated, and since this is my first github post hopefully I've formatted everything correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant