我致力于投资组合优化,在其中我尝试最小化投资组合密度与目标收益之间的差异度量。为此,我编写了一个正在运行的代码,但有两个主要问题。
1)我想积分更大的范围,但是返回的是非限定值。这主要是由于密度估算函数返回的值的极限范围然后为0。我该如何解决这个问题,因为当我在Kdensity中设置更高的支持时,我得到了一个错误。
2)可能我的发散度度量(Kullback Leibler)为负,这在实践中是不可能的,但我并不知道负值来自何处。有人可以帮我吗?
在下面显示我已经尝试过的内容。平均目标收益率和方差是最佳夏普比率投资组合之一。
S1P <- cp2dp(cp = c(0.11, 0.1625,-0.4, 6.50), family ="ST")
S2P <- cp2dp(cp = c(0.05, 0.035,-0.02, 4), family ="ST")
S3P <- cp2dp(cp = c(0.045, 0.2 , -0.5, 10), family = "ST")
S4P <- cp2dp(cp = c(0.08 , 0.07 , 0 , 5) , family = "ST")
S1 <- as.matrix(rst(n = 250 , xi = S1P[1] , omega = S1P[2], alpha = S1P[3] , nu = S1P[4]), ncol = 1 )
S2 <- as.matrix(rst(n = 250, xi = S2P[1] , omega = S2P[2], alpha = S2P[3] , nu = S2P[4]), ncol = 1 )
S3 <- as.matrix(rst(n = 250 , xi = S3P[1], omega = S3P[2], alpha = S3P[3], nu = S3P[4]) , ncol = 1 )
S4 <- as.matrix(rst(n = 250 , xi = S4P[1], omega = S4P[2] , alpha = S4P[3] , nu =S4P[4] ), ncol = 1 )
namesRET <- c("S1","S2","S3","S4")
RET <- cbind(S1,S2,S3,S4)
d <- as.Date(1:nrow(RET))
RET<- xts(RET, order.by=d)
colnames(RET) <- namesRET
fsharpe <- as.numeric(cp2dp(cp=c(MeanTargetRET,SigmaTargetRET,0.07,0.3),family ="ST"))
ObmSharpe1RET <- function(w){
Portfolio <- RET%*%t(w)
fTarget <- function(x) dst(x,xi = fsharpe[1], omega = fsharpe[2], alpha =fsharpe[3] , nu = fsharpe[4])
fPortf <- kdensity(Portfolio)
KL <- function(x) fPortf(x)*log(fPortf(x)/fTarget(x))
return(integrate(KL , -0.25, 0.25 )$value)
}
rmindRET<- fmincon(x1, ObmSharpe1RET, heq = heq2 , lb = wmin1 , ub = wmax1)
rmindRET$par
rmindRET$value
我从没想过有时会有负值