[R] Return Misalignment in Return.portfolio function in PerformanceAnalytics Package
Suphajak Ngamlak
Suphajak at phatrasecurities.com
Tue May 31 16:52:23 CEST 2016
Dear R users,
I am trying to calculate NAV of portfolio using Return.portfolio function in PerformanceAnalytics Package. I am having difficulties with how I should specify weight in the function.
I tried to replicate using fixed weights with rebalance_on = "months" by specifying weights explicitly. However, the returns I got were different
Below is the example of the code
# clear memory
rm(list=ls())
library(quantmod)
library(PerformanceAnalytics)
symbols = c(
"SPY", # US equities, SP500
"AGG" # US bonds, Barclay Agg
)
getSymbols(symbols, from="1970-01-01" , to="2014-09-15")
x.P <- do.call(merge, lapply(symbols, function(x) {
Cl(to.monthly(Ad(get(x)), drop.time = TRUE,
indexAt='endof'))
}))
colnames(x.P) = paste0(symbols, ".Adjusted")
x.R <- na.omit(Return.calculate(x.P))
# Create a weights vector
w = c(.6,.4) # Traditional 60/40 Equity/Bond portfolio weights
# Create monthly weight
w_mon = x.R[endpoints(x.R, on="months")]
w_mon$SPY.Adjusted = 0.6
w_mon$AGG.Adjusted = 0.4
# Rebalance back to 60/40 proportion
result.months1 = Return.portfolio(x.R, weights=w, rebalance_on = "months", verbose=TRUE)
result.months2 = Return.portfolio(x.R, weights=w_mon, verbose=TRUE)
test1 = data.frame(BOP = result.months1$BOP.Value, EOP = result.months1$EOP.Value, Ret = x.R)
test2 = data.frame(BOP = result.months2$BOP.Value, EOP = result.months2$EOP.Value, Ret = x.R)
# Show input and result
w
head(w_mon)
head(test1)
head(test2)
> w
[1] 0.6 0.4
> head(w_mon)
SPY.Adjusted AGG.Adjusted
2003-10-31 0.6 0.4
2003-11-28 0.6 0.4
2003-12-31 0.6 0.4
2004-01-30 0.6 0.4
2004-02-27 0.6 0.4
2004-03-31 0.6 0.4
>
> head(test1)
BOP.SPY.Adjusted BOP.AGG.Adjusted EOP.SPY.Adjusted EOP.AGG.Adjusted Ret.SPY.Adjusted Ret.AGG.Adjusted
2003-10-31 0.6000000 0.4000000 0.6321161 0.3962610 0.05352682 -0.009347612
2003-11-28 0.6170262 0.4113508 0.6237648 0.4127263 0.01092112 0.003343882
2003-12-31 0.6218947 0.4145965 0.6531841 0.4186563 0.05031296 0.009792217
2004-01-30 0.6431042 0.4287361 0.6558184 0.4306248 0.01976999 0.004405247
2004-02-27 0.6518659 0.4345773 0.6607121 0.4395380 0.01357061 0.011414925
2004-03-31 0.6601501 0.4401000 0.6514060 0.4431095 -0.01324559 0.006838188
> head(test2)
BOP.SPY.Adjusted BOP.AGG.Adjusted EOP.SPY.Adjusted EOP.AGG.Adjusted Ret.SPY.Adjusted Ret.AGG.Adjusted
2003-10-31 0.6000000 0.4000000 0.6065527 0.4013376 0.05352682 -0.009347612
2003-11-28 0.6047341 0.4031561 0.6351601 0.4071039 0.01092112 0.003343882
2003-12-31 0.6253584 0.4169056 0.6377217 0.4187422 0.05031296 0.009792217
2004-01-30 0.6338783 0.4225856 0.6424804 0.4274093 0.01976999 0.004405247
2004-02-27 0.6419339 0.4279559 0.6334311 0.4308824 0.01357061 0.011414925
2004-03-31 0.6385881 0.4257254 0.6265051 0.4137777 -0.01324559 0.006838188
We can see that even though test1 (from using rebalance_on) and test2 (from specifying weight) showed the same Ret.SPY.Adjusted and Ret.AGG.Adjusted.
The return that test 2 used in calculating EOP was from the next period. For example, for test2, EOP.SPY.Adjusted on 2003-10-31 (0.6065527) = BOP.SPY.Adjusted on 2003-10-31 (0.6000000) * Ret.SPY.Adjusted on 2003-11-28 (1+0.01092112)
Could you please suggest how should I set weight to get the same result as in test1?
[[alternative HTML version deleted]]
More information about the R-help
mailing list