Reputation: 311
Using Julia 0.5. Given:
Supertech = [-.2 .1 .3 .5];
Slowpoke = [.05 .2 -.12 .09];
How in the world can I get a covariance. In Excel I just say
=covariance.p(Supertech,Slowpoke)
and it gives me the correct answer of -0.004875
For the life of me I can't figure out how to get this to work using StatsBase.cov()
I've tried putting this into a matrix like:
X = [Supertech; Slowpoke]'
which gives me a nice:
4×2 Array{Float64,2}:
-0.2 0.05
0.1 0.2
0.3 -0.12
0.5 0.09
but I can't get this simple thing to work. I keep coming up with dimension mismatches when I try to use the WeightedVector type.
Upvotes: 0
Views: 4749
Reputation: 31362
The syntax [-.2 .1 .3 .5]
doesn't create a vector, it creates a one-row matrix. The cov
function is actually defined in base Julia, but it requires vectors. So you simply need to use the syntax with commas to create vectors in the first place ([-.2, .1, .3, .5]
), or you can use the vec
function to reshape the matrix to a one-dimensional vector. It also uses the "corrected" covariance by default, whereas Excel is using the "uncorrected" covariance. You can use the third argument to specify that you don't want this correction.
julia> cov(vec(Supertech), vec(Slowpoke))
-0.0065
julia> cov(vec(Supertech), vec(Slowpoke), false)
-0.004875
Upvotes: 4