我对R中的投资组合优化有疑问。我对R很新,并试图研究和寻找答案,但我不确定它是否正确。我希望有人可以在这里帮助我。
我已经使用计量经济模型从资产建模中获得了协方差矩阵(在这里,我使用DCC GARCH来模拟我的资产回报)。在进行预测之后,我将获得协方差矩阵。那么,现在,如何使用此协方差矩阵使用fPortfolio包进行投资组合优化?我发现的大多数示例仅使用资产回报来进行投资组合优化。但是,如果我们使用资产收益的预测均值和方差 - 协方差来创建最优资产配置模型呢?
我有以下可重现的代码。
library(zoo)
library(rugarch)
library(rmgarch)
data("EuStockMarkets")
EuStockLevel <- as.zoo(EuStockMarkets)[,c("DAX","CAC","FTSE")]
EuStockRet <- diff(log(EuStockLevel))
## GARCH-DCC
uspec = ugarchspec(mean.model = list(armaOrder = c(0,0)), variance.model = list(garchOrder = c(1,1), model = "sGARCH"), distribution.model = "norm")
spec1 = dccspec(uspec = multispec( replicate(3, uspec) ), dccOrder = c(1,1), distribution = "mvnorm")
fit1 = dccfit(spec1, data = EuStockRet, fit.control = list(eval.se=T))
#Forecasting
dcc.focast=dccforecast(fit1, n.ahead = 1, n.roll = 0)
print(dcc.focast)
covmat.focast = rcov(dcc.focast)
covmat = covmat.focast$`1975-02-03`[,,1] ##The Covariance matrix
DAX CAC FTSE
DAX 0.0002332114 0.0001624446 0.0001321865
CAC 0.0001624446 0.0001799988 0.0001139339
FTSE 0.0001321865 0.0001139339 0.0001372812
所以现在我想应用我为投资组合优化获得的协方差。
##Optimization (Use the forecasted variance covariance matrix!!!)
##You must convert your dataset into "timeSeries" object for R to be able to read it in fportfolio.
library(fPortfolio)
##To compute efficient portfolio
All.Data <- as.timeSeries(100* EuStockRet)
##Equal weight portfolio
ewPortfolio <- feasiblePortfolio(data = All.Data,spec = ewSpec,constraints = "LongOnly")
print(ewPortfolio)
##Minimum risk efficient portfolio
minriskSpec <- portfolioSpec()
targetReturn <- getTargetReturn(ewPortfolio@portfolio)["mean"]
setTargetReturn(minriskSpec) <- targetReturn
#Now, we optimize the portfolio for the specified target return :-
minriskPortfolio <- efficientPortfolio(data = All.Data,spec = minriskSpec,constraints = "LongOnly")
print(minriskPortfolio)
那么,我们实际上在哪里输入协方差矩阵?我做的正确吗?感谢是否有人可以在这里帮助我。
谢谢!
答案 0 :(得分:2)
您可以将EuroStockRet
对象作为timeseries
传递给fPortfolio
函数{{1>,而不是使用包zoo,rugarch,rmgarch中的函数来单独创建协方差矩阵。 (参见fPortfolio::covEstimator
),它接受一个?covEstimator
对象并以timeseries
期望的数据参数格式返回一个对象。类似的东西:
feasiblePortfolio
EuStockRet_with_cov <- covEstimator(x=EuStockRet);
ewPortfolio <- feasiblePortfolio(data = EuStockRet_with_cov, spec = ewSpec, constraints = "LongOnly");
还有其他各种方法可以计算协方差。它们详见第37页:fPortfolio Package
答案 1 :(得分:0)
您可以使用SetEstimator为fPortfolio软件包实现它。下面的示例:
import pandas as pd
from bs4 import BeautifulSoup
URL_filename = 'URL.csv'
URL_column_name = "Address"
data = pd.read_csv(input_filename,encoding='utf8')
weblink = (data[address_column_name]).tolist()
i=0
while i<len(weblink):
page = request.get(weblink[i])
Soup = BeautifulSoup(page.text,'html.parser')
print(Soup)
i+=1
其他参考:第293页here