# All about totalvar

**URL:** <https://discourse.julialang.org/t/all-about-totalvar/67350>\
**Category:** General Usage\
**Tags:** question, statistics\
**Created:** [August 30, 2021, 1:18pm UTC](https://discourse.julialang.org/t/all-about-totalvar/67350 "2021-08-30T13:18:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Naba7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/naba7/32/10203_2.png) [@Naba7](https://discourse.julialang.org/u/Naba7)\
**Post date:** [August 30, 2021, 1:18pm UTC](https://discourse.julialang.org/t/all-about-totalvar/67350/1 "2021-08-30T13:18:02Z")

</div>

The official docs of `totalvar(X)` says if X is a matrix, then the `totalvar(X) == sum(Diagonal(X))` but clearly in the [tests](https://github.com/JuliaStats/StatsBase.jl/blob/master/test/scalarstats.jl)it is showing a different value. You can check line 146.

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [August 30, 2021, 1:30pm UTC](https://discourse.julialang.org/t/all-about-totalvar/67350/2 "2021-08-30T13:30:57Z")

</div>

The documentation says that it is the sum of the diagonal of the _covariance_ matrix. Hence

```julia
julia> using StatsBase
julia> X = [1 2 5
            4 1 6
            4 0 4];
julia> cov(X)
3×3 Matrix{Float64}:
  3.0 -1.5 0.0
 -1.5 1.0 0.5
  0.0 0.5 1.0

```

So, the test looks correct to me.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [August 30, 2021, 1:31pm UTC](https://discourse.julialang.org/t/all-about-totalvar/67350/3 "2021-08-30T13:31:49Z")

</div>

Totalvar is " is equivalent to the sum of the diagonal elements of the covariance matrix of `X` ", not of matrix `X` itself:

```julia
totalvar(X) == sum(Diagonal(cov(X))) # true

```

_ **NB:** you should add_`using StatsBase`
