StatsBase.jl icon indicating copy to clipboard operation
StatsBase.jl copied to clipboard

Missing method for cor(x::Vector, y::Vector, w::Weights)

Open cossio opened this issue 4 years ago • 4 comments

Currently there is no method available for:

StatsBase.cor(x::AbstractVector, y::AbstractVector, w::AbstractWeights)

Is there a reason this method is missing?

cossio avatar Sep 13 '21 09:09 cossio

A possible implementation (nevermind the types):

function wcor(x::AbstractVector, y::AbstractVector, w::AbstractVector)
    @assert length(x) == length(y) == length(w)
    w_ = w ./ sum(w)
    x_ = x .- sum(w_ .* x)
    y_ = y .- sum(w_ .* y)
    return sum(w_ .* x_ .* y_) ./ sqrt(sum(w_ .* x_.^2) * sum(w_ .* y_.^2))
end

cossio avatar Sep 13 '21 10:09 cossio

+1 for adding the method, and also methods for cov, scattermat and mean_and_cov. Though the implementation will have to be more efficient than that.

nalimilan avatar Sep 13 '21 12:09 nalimilan

What do you suggest to make it more efficient @nalimilan ?

cossio avatar Sep 13 '21 13:09 cossio

Have a look at how it's done in cov.jl (and in Statistics's covzm_unscaled). The method taking a matrix can probably be adapted without too much difficulty.

nalimilan avatar Sep 13 '21 19:09 nalimilan