问题
For fportfolio package you need to put, as an input, a time series of returns and it internally calculate the expected return and the variance of the time series for then to be used in functions such as portfoliofrontier or tangencyportfolio. But in my case I have already the expected return matrix and the variance covariance matrix and I want to use the functions of fportfolio. How can I do this? thank in advance.
回答1:
I do not use fportfolio
; but if you only need the means and the variance-covariance matrix, you could simulate returns that have exactly the desired statistics.
Suppose you have an expected variance-covariance matrix S
and an expected returns vector m
. I make up some example data, for 5 assets. (I only create the matrix A
because it is an easy way to compute a valid variance-covariance matrix.)
na <- 5
nobs <- 6
A <- array(rnorm(nobs*na),
dim = c(nobs, na))*0.01
S <- cov(A)
m <- runif(na, min = 0, max = 0.0005)
So now we have S
and m
. We start by creating your input matrix; call it B
.
B <- array(rnorm( nrow(S)+1 * nrow(S)),
dim = c(nrow(S)+1, nrow(S)))
Since eventually, it should have exactly your statistics, we need to clean it: remove any residual correlations, set all volatilities to 1, and set means to 0.
B <- B %*% backsolve(chol(cov(B)),
diag(1, nrow(S)))
B <- t(B) - colMeans(B)
B <- B / apply(B, 1, sd)
B <- t(B)
Check. (Because of numerical precision means will not be exactly 0, and so on.)
round(apply(B, 2, sd), 10)
## [1] 1 1 1 1 1
round(apply(B, 2, mean), 10)
## [1] 0 0 0 0 0
round(cov(B), 10)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 0 0 0 0
## [2,] 0 1 0 0 0
## [3,] 0 0 1 0 0
## [4,] 0 0 0 1 0
## [5,] 0 0 0 0 1
And now make sure B
has the right properties.
B <- B %*% chol(S)
B <- t(B) + m
B <- t(B)
all.equal(cov(B), S)
## [1] TRUE
all.equal(colMeans(B), m)
## [1] TRUE
来源:https://stackoverflow.com/questions/58293991/how-to-use-fportfolio-package-in-r-for-non-time-series-input