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

**URL:** <https://discourse.julialang.org/t/why-do-i-get-this-error-undefvarerror-cov-not-defined/50239>\
**Category:** Optimization (Mathematical)\
**Tags:** question, package\
**Created:** [November 16, 2020, 6:16pm UTC](https://discourse.julialang.org/t/why-do-i-get-this-error-undefvarerror-cov-not-defined/50239 "2020-11-16T18:16:54Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)\
**Post date:** [November 16, 2020, 6:16pm UTC](https://discourse.julialang.org/t/why-do-i-get-this-error-undefvarerror-cov-not-defined/50239/1 "2020-11-16T18:16:54Z")

</div>

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](https://jumper.readthedocs.io/en/latest/jumper.html)  
Did this error arise due to the lack of a package?

```julia
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

```

---

<div class="post-metadata">

**Author:** ![crinders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crinders/32/8434_2.png) [@crinders](https://discourse.julialang.org/u/crinders)\
**Post date:** [November 16, 2020, 6:28pm UTC](https://discourse.julialang.org/t/why-do-i-get-this-error-undefvarerror-cov-not-defined/50239/2 "2020-11-16T18:28:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)\
**Post date:** [November 16, 2020, 6:49pm UTC](https://discourse.julialang.org/t/why-do-i-get-this-error-undefvarerror-cov-not-defined/50239/3 "2020-11-16T18:49:23Z")

</div>

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:

```julia
eval_gamma (0)
eval_gamma (3)

```

Thank you!

---

<div class="post-metadata">

**Author:** ![JLDC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jldc/32/17627_2.png) [@JLDC](https://discourse.julialang.org/u/JLDC)\
**Post date:** [November 16, 2020, 7:29pm UTC](https://discourse.julialang.org/t/why-do-i-get-this-error-undefvarerror-cov-not-defined/50239/4 "2020-11-16T19:29:30Z")

</div>

What happens if you run

```julia
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)

---

<div class="post-metadata">

**Author:** ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)\
**Post date:** [November 16, 2020, 7:47pm UTC](https://discourse.julialang.org/t/why-do-i-get-this-error-undefvarerror-cov-not-defined/50239/5 "2020-11-16T19:47:53Z")

</div>

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

Thank you

---

<div class="post-metadata">

**Author:** ![JLDC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jldc/32/17627_2.png) [@JLDC](https://discourse.julialang.org/u/JLDC)\
**Post date:** [November 16, 2020, 8:28pm UTC](https://discourse.julialang.org/t/why-do-i-get-this-error-undefvarerror-cov-not-defined/50239/6 "2020-11-16T20:28:40Z")

</div>

What about

```julia
using Distributions
Distributions.cov(1:3)

```

is it still the same error?

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [November 16, 2020, 9:49pm UTC](https://discourse.julialang.org/t/why-do-i-get-this-error-undefvarerror-cov-not-defined/50239/7 "2020-11-16T21:49:42Z")

</div>

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

```nohighlight
using Statistics

```

---

<div class="post-metadata">

**Author:** ![JLDC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jldc/32/17627_2.png) [@JLDC](https://discourse.julialang.org/u/JLDC)\
**Post date:** [November 17, 2020, 7:33am UTC](https://discourse.julialang.org/t/why-do-i-get-this-error-undefvarerror-cov-not-defined/50239/8 "2020-11-17T07:33:39Z")

</div>

> [@odow](#):
>

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 `Distributions`and without having `Statistics` in my list of packages.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 17, 2020, 8:57am UTC](https://discourse.julialang.org/t/why-do-i-get-this-error-undefvarerror-cov-not-defined/50239/9 "2020-11-17T08:57:44Z")

</div>

> [@JLDC](#):
>
> as far as I understand includes `cov` required by Distributions?

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

```julia
julia> using Distributions

julia> parentmodule(cov)
Statistics

```

---

<div class="post-metadata">

**Author:** ![JLDC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jldc/32/17627_2.png) [@JLDC](https://discourse.julialang.org/u/JLDC)\
**Post date:** [November 17, 2020, 9:11am UTC](https://discourse.julialang.org/t/why-do-i-get-this-error-undefvarerror-cov-not-defined/50239/10 "2020-11-17T09:11:53Z")

</div>

@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

```julia
using Statistics
cov(1:3)

```

would work, when

```julia
using Distributions
cov(1:3)

```

would not.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 17, 2020, 9:17am UTC](https://discourse.julialang.org/t/why-do-i-get-this-error-undefvarerror-cov-not-defined/50239/11 "2020-11-17T09:17:53Z")

</div>

> [@JLDC](#):
>
> when
> 
> ```julia
> 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.

---

<div class="post-metadata">

**Author:** ![JLDC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jldc/32/17627_2.png) [@JLDC](https://discourse.julialang.org/u/JLDC)\
**Post date:** [November 17, 2020, 9:18am UTC](https://discourse.julialang.org/t/why-do-i-get-this-error-undefvarerror-cov-not-defined/50239/12 "2020-11-17T09:18:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)\
**Post date:** [November 17, 2020, 12:08pm UTC](https://discourse.julialang.org/t/why-do-i-get-this-error-undefvarerror-cov-not-defined/50239/13 "2020-11-17T12:08:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [November 17, 2020, 8:52pm UTC](https://discourse.julialang.org/t/why-do-i-get-this-error-undefvarerror-cov-not-defined/50239/14 "2020-11-17T20:52:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)\
**Post date:** [November 23, 2020, 5:42pm UTC](https://discourse.julialang.org/t/why-do-i-get-this-error-undefvarerror-cov-not-defined/50239/15 "2020-11-23T17:42:05Z")

</div>

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) `

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [November 23, 2020, 6:57pm UTC](https://discourse.julialang.org/t/why-do-i-get-this-error-undefvarerror-cov-not-defined/50239/16 "2020-11-23T18:57:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)\
**Post date:** [November 30, 2020, 6:42pm UTC](https://discourse.julialang.org/t/why-do-i-get-this-error-undefvarerror-cov-not-defined/50239/17 "2020-11-30T18:42:28Z")

</div>

Hi @odow, Thanks for the help  
Unfortunately the error continues, but now this error message has appeared: `MethodError: no method matching getindex (:: typeof (mean), :: Array {Float64,2}, :: Int64)`
