Why do I get this error "UndefVarError: cov not defined"

Could someone help me solve the error: UndefVarError: cov not defined of the test code taken from the site https://jumper.readthedocs.io/en/latest/jumper.html
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

What’s the trace? Can you give the complete error? Distributions defines cov.

Hi @crinders,

I used Distributions because I imagined that the package would compile the covariance which is defined as covar = cov (past_returns)
The complete error message: UndefVarError: cov not defined, appears when I try to compile:

eval_gamma (0)
eval_gamma (3)

Thank you!

What happens if you run

using Distributions
cov(1:3)

Do you get 1.0 or the same error? Which version of Distributions are you using? (Pkg.installed() to find out)

Hi, @JLDC
He continued with the same mistake. I’m using version 0.23.8

Thank you

What about

using Distributions
Distributions.cov(1:3)

is it still the same error?

cov is provided by Statistics. Instead of using Distributions, you need

using Statistics
6 Likes

For my personal understanding, as I am no Julia expert. Could you please explain why that matters? Isn’t StatsBase, which, as far as I understand includes cov required by Distributions? For instance, I can run cov and Distributions.cov by having loaded only Distributionsand without having Statistics in my list of packages.

It only makes sense to load Distributions if you actually need it, since it just reexports cov.

julia> using Distributions

julia> parentmodule(cov)
Statistics
2 Likes

@Tamas_Papp thanks for the reply!

I understand that, however, the person is following a tutorial, I assumed Distributions would be needed somewhere else in the tutorial. I think I haven’t express my question quite clearly, sorry. I am more confused as to why

using Statistics
cov(1:3)

would work, when

using Distributions
cov(1:3)

would not.

But it does work, at least with versions 0.22 and 0.23 of Distributions I tested with.

It does for me as well, but it doesn’t seem to work for the OP.

Hi @odow, thanks for the reply
What version of Julia are you using?

This will work on all 1.x versions of Julia.

1 Like

Hi @odow
I followed your instruction and it worked, thank you!!
However, a new error arose when compiling eval_gamma eval [0] and eval_gamma [3]
Error message: MethodError: no method matching getindex (:: typeof (eval_gamma), :: Int64)

Its eval_gamma(0), not eval_gamma[0].