Intermediate Portfolio Analysis in R
Ross Bennett
Instructor
# Run the optimization with periodic rebalancing opt_base <- optimize.portfolio.rebalancing(R = returns, optimize_method = "ROI", portfolio = base_port_spec, rebalance_on = "quarters", training_period = 60, rolling_window = 60)
# Calculate portfolio returns base_returns <- Return.portfolio(returns, extractWeights(opt_base)) colnames(base_returns) <- "base"
# Chart the optimal weights
chart.Weights(opt_base)
# Merge benchmark and portfolio returns
ret <- cbind(benchmark_returns, base_returns)
# Annualized performance
table.AnnualizedReturns(ret)
benchmark base
Annualized Return 0.0775 0.0772
Annualized Std Dev 0.1032 0.0436
Annualized Sharpe (Rf=0%) 0.7509 1.7714
# Make a copy of the portfolio specification box_port_spec <- base_port_spec
# Update the constraint box_port_spec <- add.constraint(portfolio = box_port_spec, type = "box", min = 0.05, max = 0.4, indexnum = 2)
# Backtest opt_box <- optimize.portfolio.rebalancing(R = returns, optimize_method = "ROI", portfolio = box_port_spec, rebalance_on = "quarters", training_period = 60, rolling_window = 60)
# Calculate portfolio returns box_returns <- Return.portfolio(returns, extractWeights(opt_box)) colnames(box_returns) <- "box"
# Chart the optimal weights
chart.Weights(opt_box)
# Merge box portfolio returns ret <- cbind(ret, box_returns)
# Annualized performance table.AnnualizedReturns(ret)
benchmark base box
Annualized Return 0.0775 0.0772 0.0760
Annualized Std Dev 0.1032 0.0436 0.0819
Annualized Sharpe (Rf=0%) 0.7509 1.7714 0.9282
Intermediate Portfolio Analysis in R