How to use portfolio analytics optimizer with specific covariance matrix?

26 Views Asked by At

I would like to use the PortfolioAnalytics package to optimize a problem using my own covariance matrix and ignore the historical returns but I am unable to do so whether I provide the covmat via objective or constraints (the code below is copied from the link)

# Load the package
library(PortfolioAnalytics)

# Load the data
data(indexes)

# Subset the data
index_returns <- indexes[, c(1:4)]

# Print the head of the data
head(index_returns)

# Create the portfolio specification
port_spec <- portfolio.spec(colnames(index_returns))

# Add a full investment constraint such that the weights sum to 1
port_spec <- add.constraint(portfolio =port_spec, type = "full_investment")

# Add a long only constraint such that the weight of an asset is between 0 and 1
port_spec <- add.constraint(portfolio = port_spec, type = "long_only")

# Add an objective to minimize portfolio standard deviation
port_spec <- add.objective(portfolio = port_spec, type = "risk", name = "StdDev")

# Solve the optimization problem
opt <- optimize.portfolio(index_returns, portfolio = port_spec, optimize_method = "ROI")

# Extract the optimal weights
extractWeights(opt)

When I provide my own cov matrix I somehow get the exact same weights:

n <- 4  # Define the size of each dimension (n by n by n)
# Create 4X4 matrix filled with zeros
covmat<- array(0, dim = c(n, n))
port_spec <- add.objective(portfolio=port_spec, type='risk', cov=covmat, name='StdDev')
0

There are 0 best solutions below