1

我正在对一系列股票进行投资组合优化,并试图提取重新平衡的投资组合的权重。

我遇到的问题:我没有得到重新平衡的投资组合的权重,而是得到 3 个日期。该项目的代码在下面。

library(ROI)
install.packages("DEoptim")
library(ggplot2)
install.packages("quantmod")
library(quantmod)
library(quantmod)
install.packages("PerfomanceAnalytics")
library(PerformanceAnalytics)
library(PortfolioAnalytics)
library(random)
install.packages("random")
library(random)
library(DEoptim)
install.packages("fPortfolio")
library(fPortfolio)
install.packages("foreach")
install.packages("doParallel")
library(PortfolioAnalytics)


#vector of stocks in my portfolio  of 
tickers <- c("FB", "AAPL", "AMZN", "GM", "GOOGL", "SQ", "NVDA","RYAM", "AMAT", "IMMR","SOI","PETS")
#bind porfolio prices 
portfolioPrices <- NULL
for(ticker in tickers) {
  portfolioPrices <- cbind(portfolioPrices,
                           getSymbols.yahoo(ticker, from='2003-01-03', periodicity = 'daily', auto.assign=FALSE)[,4])
}
#portfolio returns
portfolioReturns <- na.omit(ROC(portfolioPrices))
print(portfolioReturns)
portf <- portfolio.spec(colnames(portfolioReturns))
portf <- add.constraint(portf, type="weight_sum", min_sum=.99, max_sum=1,01)
portf <- add.constraint(portf, type="box", min=.02, max=.60) 
portf<-add.constraint(portf,type="transation_cost", ptc=.001)
portf <- add.objective(portf, type="return", name="mean")
portf <- add.objective(portf, type="risk", name="StdDev",target=.005)

rp<-random_portfolios(portf, 10000, "sample")
#optimize portfolio using the "DEoptim solver"
optPort <- optimize.portfolio(portfolioReturns, portf, optimize_method = "DEoptim", trace=TRUE)

#chart weights of optimized portfolio

chart.Weights(optPort)
summary(optPort)


chart.RiskReward(optPort, risk.col = "StDev", return.col = "mean", chart.assets = TRUE)


rp<-random_portfolios(portf, 10000, "sample")
#rebalance portfolo
opt_rebal <- optimize.portfolio.rebalancing(portfolioReturns,
                                            portf,
                                            optimize_method="ROI",
                                            rp=rp,
                                            rebalance_on="years",
                                            training_period=60,

                                            rolling_window=60)




extractWeights(optPort)
chart.Weights(optPort)
#extract weights of rebalanced portfolio
extractWeights(opt_rebal))

我怎样才能解决这个问题?

对你的帮助表示感谢。

谢谢你。

4

1 回答 1

0

首先,你的代码很乱!

因此,在给你解决方案的同时,我也清理了它。

以下是涵盖您问题所有方面的要点:

  1. 支持插件

由于您不仅使用DEoptim求解器ROI,还需要下载推荐的支持插件ROI

 install.packages(c("fGarch", 
                        "Rglpk", 
                        "ROI.plugin.glpk", 
                        "ROI.plugin.quadprog", 
                        "ROI.plugin.symphony",
                        "pso",
                        "GenSA",
                        "corpcor",
                        "testthat",
                        "nloptr", 
                        "MASS", 
                        "robustbase")
                      )

  1. 图书馆的使用

您应该以正确的顺序加载库一次,因为某些库可以相互屏蔽某些功能。这是推荐的顺序:

    library(ROI)
    library(ggplot2)
    library(quantmod)
    library(PerformanceAnalytics)
    library(random)
    library(DEoptim)
    library(fPortfolio)
    library(PortfolioAnalytics)
    library(dplyr) 
  1. 管道算子的使用

请注意,管道( )dplyr需要加载一个额外的库,即使您的代码更高效和可读!:%>%

#vector of stocks in my portfolio  of 
tickers <- c("FB", "AAPL", "AMZN", "GM", "GOOGL", "SQ", "NVDA","RYAM", "AMAT", "IMMR","SOI","PETS")

#bind porfolio prices 
portfolioPrices <- NULL
for(ticker in tickers) {
  portfolioPrices <- cbind(portfolioPrices,
                           getSymbols.yahoo(ticker, from='2003-01-03', periodicity = 'daily', auto.assign=FALSE)[,4])
}
#portfolio returns
portfolioReturns <- na.omit(ROC(portfolioPrices))
print(portfolioReturns)

portf <- portfolio.spec(colnames(portfolioReturns)) %>% 
  add.constraint(type="weight_sum", min_sum=1, max_sum=1) %>% 
  add.constraint(type="box", min=.02, max=.60) %>% 
  add.constraint(type="transation_cost", ptc=.001) %>% 
  add.objective(type="return", name="mean") %>%
  add.objective(type="risk", name="StdDev",target=.005)
  1. 消除冗余

不知道为什么你不能使用之前输入的随机投资组合rp作为optPort的输入opt_rebal

rp<-random_portfolios(portf, 10000, "sample")

#optimize portfolio using the "DEoptim solver"
optPort <- optimize.portfolio(portfolioReturns, portf, optimize_method = "DEoptim", trace=TRUE,
                              rp=rp)

#chart weights of optimized portfolio
chart.Weights(optPort)
summary(optPort)

# chart.RiskReward(optPort, risk.col = "StDev", return.col = "mean", chart.assets = TRUE)

#not sure why you cannot use the previous random portfolio!!
rp<-random_portfolios(portf, 10000, "sample")
  1. 了解风险回报图的使用

这个函数调用中有一个错误,我认为这是由于你的双重目标,portf因为它可能会阻止你获得有效的边界。不确定;这不是必需的,而是您探索的任务:-)

# chart.RiskReward(optPort, risk.col = "StDev", return.col = "mean", chart.assets = TRUE)
  1. 了解投资回报率

ROI不同于其他后端,因此需要单独的投资组合规范portfolio.spec和投资组合优化optimize.portfoliooptimize.portfolio.rebalancing.

这是实现它的一种方法(注意没有add.objective内部的投资组合规范):

portf2 <- portfolio.spec(colnames(portfolioReturns)) %>% 
  add.constraint(type="weight_sum", min_sum=1, max_sum=1) %>%
  add.constraint(type="box", min=.02, max=.60) %>% 
  add.constraint(type="transation_cost", ptc=.001) 

#this optimises based on Sharpe Ratio
optPort2 <- optimize.portfolio(portfolioReturns, portf2, optimize_method = "ROI", trace=TRUE,
                               maxSR=TRUE)

#rebalance portfolo
opt_rebal <- optimize.portfolio.rebalancing(portfolioReturns,
                                            portf2,
                                            optimize_method="ROI",
                                            rp=rp,
                                            rebalance_on="years",
                                            training_period=60,
                                            rolling_window=60)

extractWeights(optPort)
chart.Weights(optPort)

#extract weights of rebalanced portfolio
extractWeights(opt_rebal)

输出:

> extractWeights(opt_rebal)
           FB.Close AAPL.Close AMZN.Close GM.Close GOOGL.Close SQ.Close NVDA.Close RYAM.Close AMAT.Close
2017-12-29      0.6        0.2       0.02     0.02        0.02     0.02       0.02       0.02       0.02
2018-12-31      0.6        0.2       0.02     0.02        0.02     0.02       0.02       0.02       0.02
2019-09-20      0.6        0.2       0.02     0.02        0.02     0.02       0.02       0.02       0.02
           IMMR.Close SOI.Close PETS.Close
2017-12-29       0.02      0.02       0.02
2018-12-31       0.02      0.02       0.02
2019-09-20       0.02      0.02       0.02

您可以阅读有关文档optimize.portfolio以了解它可以解决哪些有限类型的凸优化问题。

于 2019-09-21T15:39:15.613 回答