`cov` not behaving properly for vector of vectors?

Open
#114 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
julia
Domain
analytics, data

Research direction

Inspect src/Statistics.jl around unscaled_covzm and the referenced lines near 525 and 557, then reproduce the Julia examples for cov on vectors of vectors. Done means cov(A,B) produces the expected 5×5 covariance for vector-valued observations while preserving the existing behavior for scalar vectors.

Written by the indexing model from the issue text.

Description

I'm not sure if I'm understanding the behavior of cov correctly, but it seems like the current implementation for between two vectors only works on a vector of numbers. It currently gives the dot product between the vectors, which is only equivalent to what the documentation says when the vectors only contain scalars (since it's doing an inner product). At some point before bringing it over from StatsBase.jl, this probably worked according to this comment, but right now it doesn't give the behavior I expect. For example,

julia> N = 1000;

julia> A = [randn(5) for _ in 1:N];

julia> B = [randn(5) for _ in 1:N];

julia> using Statistics

julia> cov(A,B) # This is unexpected
0.15626480407767923

julia> sum((a-mean(A))*(b-mean(B))' for (a,b) in zip(A,B))/(N-1) # This is expected
5×5 Matrix{Float64}:
  9.35299e-16  -5.73004e-16  -4.02748e-16   9.26408e-16  -3.28066e-16
 -7.09476e-16   1.65189e-15   2.7001e-15   -9.89532e-16  -2.72944e-16
  2.08931e-16   3.56516e-16   7.28591e-16   9.61526e-16  -1.33894e-15
  3.51182e-16  -1.68034e-16  -1.23136e-16  -2.57385e-16   9.51302e-16
 -1.97373e-16   5.99676e-16   2.97838e-16  -1.7159e-16   -1.01532e-15

I'm not saying the above is the best possible implementation, just that it gives the answer I expect. I would propose (and be willing to put up a PR for!) a change where the current implementation of unscaled_covzm is specialized to AbstractVector{<:Number} and there's a different general implementation of it for AbstractVector, similar to what I put up above.

Dominant language
Julia
Stars
93
Forks
51
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from JuliaStats/Statistics.jl

All issues in JuliaStats/Statistics.jl

Similar issues

More Julia issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.