# What is optimize.portfolio.rebalancing in R-package PortfolioAnalytics calculating?

I recently started using the R-package PortfolioAnalytics for performing some portfolio optimization. And I'm trying to get a grasp on what exactly the function optimize.portfolio.rebalancing is calculating. In particular, I'm wondering why the weights calculated (from the second period onwards) are different than the ones that I get when directly calling optimize.portfolio.

I was under the impression that optimize.portfolio.rebalancing was basically a "wrapper around optimize.portfolio" (from the manual), so I thought that the function simply calls optimize.portfolio repeatedly for periods of length rolling_window. But then, shouldn't the results be identical to what a manual call of optimize.portfolio with the exact same periods yields? Or is optimize.portfolio.rebalancing performing some additional calculations? What am I missing?

Here's a minimal example illustrating my question:

data("edhec")
returns <- edhec[, 1:4]
funds <- colnames(returns)

portfolio <- portfolio.spec(assets = funds)
portfolio <- add.constraint(portfolio = portfolio, type = "full_investment")
portfolio <- add.constraint(portfolio = portfolio, type = "long_only")
portfolio <- add.objective(portfolio = portfolio, type = "risk", name = "ES")

portfolio.rebalanced <- optimize.portfolio.rebalancing(R = returns,
portfolio = portfolio,
optimize_method = "ROI",
rebalance_on = "quarters",
training_period = 12,
rolling_window = 12)

# check, if optimize.portfolio gives the same results: first period
portfolio.optimized <- optimize.portfolio(R = returns["::1997-12-31",],
portfolio = portfolio,
optimize_method = "ROI")
portfolio.optimized
portfolio.rebalanced$opt_rebalancing$1997-12-31
# YES!

# but: last period
portfolio.optimized <- optimize.portfolio(R = returns["2006-08-31::2009-08-31",],
portfolio = portfolio,
optimize_method = "ROI")
portfolio.optimized
portfolio.rebalanced$opt_rebalancing$2009-08-31
# NO! What's wrong?
• I'm thinking that if it's rolling window and not expanding window, it might drop the later observations and take into account only the 12 latest ones...I will simulate something and check it :) Commented Aug 10, 2016 at 15:51
• What other libraries do you use? I'm getting errors if I run the same code Error: "package:ROI" %in% search() || requireNamespace("ROI", quietly = TRUE) is not TRUE Commented Aug 10, 2016 at 15:59
• Forgot to mention the required libraries: quantmod, PerformanceAnalytics, PortfolioAnalytics and ROI are loaded. Also, ROI.plugin.glpk and ROI.plugin.quadprog. Not sure, which of these are really necessary. Commented Aug 10, 2016 at 16:22
• As to it only taking into account the latest 12 observations... yes, that's what I'm expecting. And this is what I'm trying to do manually by calculating "2006-08-31::2009-08-31" (3 years, so 12 quarters, right?). It works for the first period, but not from the second period onwards (check the periods in portfolio.rebalanced$opt_rebalancing$...), so I'm thinking the following periods are adjusted in some way. Commented Aug 10, 2016 at 16:42
• I think its data feed, because if you put rebalance on 'quarters', it takes as end points only end of March, June,... and you are checking it against end of August...so try to change that to 2009-08-31..also you should have only 12 months training, so not from 2006, but 2008? Would dig deeper, but I still cant make it work:D , it says: Error: paste0("package:", plugin) %in% search() || requireNamespace(plugin, .... is not TRUE it is something wrong with the ROI... Commented Aug 10, 2016 at 19:44

It rebalances on endpoints(R), you passed it quarters, so every quarter as determined by endpoints(returns,'quarters') it will rebalance using the last 12 observations.

In your case there you called optimize portfolio on a dataset of 37 observations

nrow(edhec["2006-08-31::2009-08-31"])
[1] 37

This should explain the discrepancy between the two calls as one has a dataset of 12 vs 37.

While in your first test you called optimize portfolio on a dataset of 12 observations which in line with the optimize.portfolio.rebalancing call

nrow(edhec["::1997-12-31",])
[1] 12