The matrix `D` is based on type `Int8` which overflows and wraps (without
error) very quickly. The `cov` function automatically converts to a `Float64`
but the manual calculation does not. To make the calculations match, try:
D = convert(Matrix{Int},D)
On Friday, March 4, 2016 at 10:42:02 AM UTC+2, paul analyst wrote:
>
> Why (D'*D)/k.-(mean(D,1)'*mean(D,1)) compute var = -0.0187777 if var must
> be 0.124326 ?**
>
>
> julia> using HDF5, JLD
>
> julia> D=load("D_test.jld","D_test");
>
> julia> k,l=size(D)
> (1000000,10)
>
> julia> cov(D[:,1],D[:,1],corrected=false)
> 0.12432634086422582
>
> julia> cov(D,corrected=false)
> 10x10 Array{Float64,2}:
> 0.124326 0.0239205 -0.0119771 -0.0201163 0.013
> 0.0239205 0.154538 -0.0156671 -0.0263335 -0.023
> -0.0119771 -0.0156671 0.0853087 -0.0158088 0.002
> -
>
> julia> (D'*D)/k.-(mean(D,1)'*mean(D,1))
> 10x10 Array{Float64,2}:
> -0.0187777 -0.0244635 -0.0119771 -0.0247243 -0.01791
> -0.0244635 -0.0320863 -0.0156671 -0.0324775 -0.02332
> -0.0119771 -0.0156671 -0.00761925 -0.0158088 -0.01140
>
> julia> (D[:,1]'*D[:,1])/k.-(mean(D[:,1],1)'*mean(D[:,1],1))
> 1-element Array{Float64,1}:
> 0.124326
>
> julia> mean(D,1)'
> 10x1 Array{Float64,2}:
> 0.136944
> 0.179135
> 0.08746
> 0.180755
> 0.130206
> 0.015541
> 0.036076
> 0.101094
> 0.155723
> 0.045505
>
> julia> mean(D,1)
> 1x10 Array{Float64,2}:
> 0.136944 0.179135 0.08746 0.180755 0.130206 0.015541 0
>
> julia> ee=mean(D[:,1],1)'*mean(D[:,1],1)
> 1-element Array{Float64,1}:
> 0.0187537
>
> julia> dd=(D[:,1]'*D[:,1])/k
> 1-element Array{Float64,1}:
> 0.14308
>
> julia> dd-ee
> 1-element Array{Float64,1}:
> 0.124326
>
>
> file:
>
> https://drive.google.com/open?id=0B9xW5VtANWhDOHlwVlZxZk1xanc
>
>
>