程式

如何計算R中的全域最小變異數投資組合?

  • November 4, 2016

我正在嘗試使用該globalMin.portfolio命令來計算 RStudio 中的全域最小變異數投資組合。我的程式碼如下(請注意,已經包含了幾個可能不是必需的庫,因為我不清楚執行這些功能需要哪些庫):

#Libraries
library(corpcor)
library(tseries)
library(fPortfolio)
library(quadprog)
library(SharpeR)
library(PortfolioAnalytics)
library(zoo)
library(plotly)
library(portfolio.r)
library(portfolio_noshorts.r)

rk.free=0.005

rm( list=ls() )
portfolio <- read.csv2("File Directory/Document.csv")
T <- nrow(portfolio)
portfolio <- portfolio[ seq(T,1,-1) , ]
stock1_r<- diff(log(portfolio$stock1))*100
stock2_r <- diff(log(portfolio$stock2))*100
stock3_r<- diff(log(portfolio$stock3))*100
stock4_r	<- diff(log(portfolio$stock4))*100
stock5_r    <- diff(log(portfolio$stock5))*100
stock6_r<- diff(log(portfolio$stock6))*100
stock7_r<- diff(log(portfolio$stock7))*100
stock8_r<- diff(log(portfolio$t))*100
stock9_r<- diff(log(portfolio$stock9))*100
stock10_r<- diff(log(portfolio$stock10))*100

portfolio_r <- data.frame(stock1_r, stock2_r, stock3_r, stock4_r, stock5_r, stock6_r, stock7_r, stock8_r, stock9_r, stock10_r)
er1=c(mean(stock1_r), mean(stock2_r), mean(stock3_r), mean(stock4_r), mean(stock5_r), mean(stock6_r), mean(stock7_r), mean(stock8_r), mean(stock9_r), mean(stock10_r))

#Covariance Matrix
covmat1<-cov(portfolio_r)

#Shrinkage estimate of covariance
cov.shrink(portfolio_r)

#Transform covariance to correlation matrix
cov2cor(covmat1)
ew1=rep(1,10)/10
er1
covmat1
ew1

#Compute global minimum variance portfolio
gmin.port = globalMin.portfolio(er1, covmat1)
attributes(gmin.port)
print(gmin.port)
summary(gmin.port, rk.free=r.free)
plot(gmin.port, col="blue")

但是,當我嘗試計算全域最小變異數投資組合時,我只是收到錯誤消息;"Error: could not find function "globalMin.portfolio". 即使計算了預期回報 ( er1) 和共變異數矩陣 ( covmat1) 並在程序執行時顯示輸出,也會發生這種情況。將不勝感激任何幫助。

最後 2 個呼叫必須從

library(portfolio.r) 

library(portfolio_noshorts.r)

source(portfolio.r)

source(portfolio_noshorts.r)

正確的文件必須在http://faculty.washington.edu/ezivot/econ424/上可用。

引用自:https://quant.stackexchange.com/questions/26317