Correlation matrices should probably not use `oneunit`

Open
#198 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
68/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
julia
Domain
data

Research direction

Start in src/Statistics.jl at line 652 and inspect how correlation matrices and cor2cov! use oneunit. Reproduce the reported dimensional-data case, then verify that correlation matrices use multiplicative identities on the diagonal and remain dimensionless without breaking the in-place behavior.

Written by the indexing model from the issue text.

Description

Correlation matrices divide each column by the unit, so they are by definition dimensionless. However, the oneunit function is used here.
https://github.com/JuliaStats/Statistics.jl/blob/ad4a0cd12d0abd0a8aed8469a2e71c63ace1773d/src/Statistics.jl#L652

I can't think of an example where this would work. This won't work with Unitful.jl, because the covariance matrix can only have a single unit; if you try to use cor2cov! the type will change, because it has to be unitless.

X = randn(10,3) .*u"m"

julia> cov(X)
3×3 Matrix{Quantity{Float64, 𝐋^2, Unitful.FreeUnits{(m^2,), 𝐋^2, nothing}}}:
  1.09164 m^2    0.289683 m^2  -0.139717 m^2
  0.289683 m^2   0.481522 m^2  -0.150258 m^2
 -0.139717 m^2  -0.150258 m^2   1.2722 m^2

julia> cor(X)
ERROR: DimensionError: m^2 and 0.3995548704289101 are not dimensionally compatible.

I can't see this working for Dates either. The oneunit function should be switched to the one function, because the diagonals of correlation matrices are multiplicative identities. If the return type of one(S[i]) cannot be converted to the element type of the covariance matrix S, there's no way to produce the correlation matrix through in-place modification of S anyway. It's best to just construct a new matrix using the element type of one(S[i]).

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.