Skip to content

Accept reinterpreted arrays for weighted covariance calculation #882

Open
@tscode

Description

@tscode

I like to keep observed variables in structs and use reinterpret to efficiently convert a vector of variables to a matrix. However, this clashes with cov when weights are given. Consider this example (I am on julia 1.9.0):

using StatsBase

struct Observation
  a :: Float64
  b :: Float64
end

obs = [Observation(1., 2.), Observation(2., 4.)]
weights = [0.5, 0.5]

data = reinterpret(reshape, Float64, obs) # get a 'reinterpreted' Float64 matrix
cov(data, dims = 2, corrected = false) # works as expected
cov(data, Weights(weights), 2) # fails

data = unsafe_wrap(Array, pointer(data), size(data))
cov(data, Weights(weights), 2) # works as expected

The issue is that reinterpret(reshape, Float64, obs) does not yield a subtype of DenseMatrix, even though it is a dense matrix.
I am not sure if this issue should be resolved by StatsBase or can be tackled upstream.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions