PortfolioAnalytics使用定义的权重集进行回测

时间:2015-08-17 20:38:42

标签: r

我非常喜欢投资组合PortfolioAnalytics,并通过https://cran.r-project.org/web/packages/PortfolioAnalytics/index.html

中的一些示例

现在,我是否可以使用该套件来计算具有一组历史权重的某些风险指标,而不是优化投资组合?例如,下面的代码给出了不同的权重和风险贡献。假设我有一组固定的时间序列数据,其中包含cvs文件中资产的权重(下面我使用的是随机数据)。我应该怎么做才能在类似的图表中随时间提取权重/风险贡献?

例如:

library(PortfolioAnalytics)
data(edhec)

    R <- edhec[, 1:4]
funds <- colnames(R)

    init.portf <- portfolio.spec(funds, weight_seq=generatesequence(min=0,  max=1, by=0.002))
init.portf <- add.constraint(init.portf, "weight_sum")#, min_sum=0.99, max_sum=1.01)
init.portf <- add.constraint(init.portf, "box", min=0, max=0.65)


init.portf <- add.objective(init.portf, type="return", name="mean", multiplier=0)

# Add objective to minimize portfolio standard deviation
SDRB.portf <- add.objective(init.portf, type="risk", name="StdDev")

# Add StdDev risk budget objective for maximum percentage risk 
SDRB.portf <- add.objective(SDRB.portf, type="risk_budget", name="StdDev", max_prisk=0.4)

# Generate random portfolios
rp <- random_portfolios(init.portf, 5000)

# Run out of sample backtest with yearly rebalancing
SDRB.opt.bt <- optimize.portfolio.rebalancing(R, SDRB.portf,
                                              optimize_method="random", 
                                              rp=rp,
                                              trace=TRUE, 
                                              rebalance_on="years", 
                                              training_period=100, 
                                              trailing_periods=60)

# print method for optimize.portfolio.rebalancing objects
SDRB.opt.bt

# summary method for optimize.portfolio.rebalancing objects
tmp_summary <- summary(SDRB.opt.bt)
names(tmp_summary)

# print method for summary.optimize.portfolio.rebalancing objects
tmp_summary

# Extractor functions for summary.optimize.portfolio.rebalancing objects
extractWeights(tmp_summary)
extractObjectiveMeasures(tmp_summary)

# Extractor functions for optimize.portfolio.rebalancing objects
tmp_stats <- extractStats(SDRB.opt.bt)
head(tmp_stats[[1]])
tmp_weights <- extractWeights(SDRB.opt.bt)
tmp_obj <- extractObjectiveMeasures(SDRB.opt.bt)

# chart functions for optimize.portfolio.rebalancing
chart.Weights(SDRB.opt.bt)
chart.RiskBudget(SDRB.opt.bt, match.col="StdDev", risk.type="percent")

6个资产的随机权重(抱歉不知道将随机权重设置为1)

##make some random weights
Wa <-edhec[, 1:1]
w1<-runif(152,0,1)
w2<-runif(152,0,1)
w3<-runif(152,0,1)
w4<-runif(152,0,1)
w5<-runif(152,0,1)
w6<-runif(152,0,1)
weightsTest <- merge(Wa,w1,w2,w3,w4,w5,w6)
weightsTest <- weightsTest[, -(1), drop=FALSE]
##

那么我如何使用权重测试作为历史权重并计算风险贡献,如图表.RiskBudget在第一个例子中做了什么?

谢谢!

0 个答案:

没有答案