R 中级投资组合分析
Ross Bennett
Instructor
# 运行带定期再平衡的优化 opt_base <- optimize.portfolio.rebalancing(R = returns, optimize_method = "ROI", portfolio = base_port_spec, rebalance_on = "quarters", training_period = 60, rolling_window = 60)# 计算组合收益 base_returns <- Return.portfolio(returns, extractWeights(opt_base)) colnames(base_returns) <- "base"
# 绘制最优权重
chart.Weights(opt_base)

# 合并基准与组合收益
ret <- cbind(benchmark_returns, base_returns)
# 年化绩效
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

# 复制组合规范 box_port_spec <- base_port_spec# 更新约束 box_port_spec <- add.constraint(portfolio = box_port_spec, type = "box", min = 0.05, max = 0.4, indexnum = 2)# 回测 opt_box <- optimize.portfolio.rebalancing(R = returns, optimize_method = "ROI", portfolio = box_port_spec, rebalance_on = "quarters", training_period = 60, rolling_window = 60)# 计算组合收益 box_returns <- Return.portfolio(returns, extractWeights(opt_box)) colnames(box_returns) <- "box"
# 绘制最优权重
chart.Weights(opt_box)

# 合并盒约束组合的收益 ret <- cbind(ret, box_returns)# 年化绩效 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
R 中级投资组合分析