Could someone help me solve the error: UndefVarError: cov not defined
of the test code taken from the site JuMPeR - Robust Optimization with JuMP — JuMPeR -- Julia for Mathematical Programming 0.0 documentation
Did this error arise due to the lack of a package?
using JuMP, JuMPeR, GLPKMathProgInterface, Distributions
N = 2000
NUM_ASSET = 3000
# Setup the robust optimization model
function solve_portfolio(past_returns, Gamma, pref_cuts)
# Create covariance matrix and mean vector
covar = cov(past_returns)
means = mean(past_returns, 1)
A = round(chol(covar),2)
m = RobustModel(solver=GLPKSolverLP(OutputFlag=0))
# Variables
@variable(m, obj) # Put objective as constraint using dummy variable
@variable(m, x[1:NUM_ASSET] >= 0)
# Uncertainties
@uncertain(m, r[1:NUM_ASSET] ) # The returns
@uncertain(m, -1 <= z[1:NUM_ASSET] <= 1 ) # The "standard normals"
@uncertain(m, 0 <= y[1:NUM_ASSET] <= 1 ) # |z|/box
@objective(m, Max, obj)
# Portfolio constraint
@constraint(m, sum([ x[i] for i=1:NUM_ASSET ]) == 1)
# The objective constraint - uncertain
@constraint(m, sum([ r[i]*x[i] for i=1:NUM_ASSET ]) - obj >= 0)
# Build uncertainty set
# First, link returns to the standard normals
for asset_ind = 1:NUM_ASSET
@constraint(m, r[asset_ind] ==
sum([ A[asset_ind, j] * z[j] for j=1:NUM_ASSET ]) + means[asset_ind] )
end
# Then link absolute values to standard normals
for asset_ind = 1:NUM_ASSET
@constraint(m, y[asset_ind] >= -z[asset_ind] / box)
@constraint(m, y[asset_ind] >= z[asset_ind] / box)
end
# Finally, limit how much the standard normals can vary from means
@constraint(m, sum([ y[j] for j=1:NUM_ASSET ]) <= Gamma)
solveRobust(m, prefer_cuts=pref_cuts)
return getValue(x)
end
function eval_gamma(Gamma)
x = solve_portfolio(past_returns, 1, true)
future_z = future_returns * x[:]
sort!(future_z)
println("Selected solution summary stats for Gamma $Gamma")
println("10%: ", future_z[int(NUM_FUTURE*0.1)])
println("20%: ", future_z[int(NUM_FUTURE*0.2)])
println("30%: ", future_z[int(NUM_FUTURE*0.3)])
println("Mean: ", mean(future_z))
println("Maximum: ", future_z[end])
end
past_returns = generate_data(1000)
future_returns = generate_data(1000)
eval_gamma(0) # UndefVarError: cov not defined
eval_gamma(3) # UndefVarError: cov not defined