Skip to content

Statistics extension for var and std #1290

@Wu-Chenyang

Description

@Wu-Chenyang

Currently, the Statistics extension only extends the mean function. The var and std of a StaticMatrix still returns a regular Matrix.

a = @SMatrix(rand(3,4))
mean(a, dims=2) # 3×1 SMatrix
var(a, dims=2) # 3×1 Matrix
std(a, dims=2) # 3×1 Matrix

I provide a preliminary extension for var and std as follows. Attaching them to the end of StaticArraysStatisticsExt.jl should work.

using Statistics
import Statistics: var, std

@inline var(a::StaticArray;corrected::Bool=true, mean=nothing, dims=:) = begin
    m = isnothing(mean) ? Statistics.mean(a; dims=dims) : mean
    denom = _mean_denom(a, dims) - (corrected ? 1 : 0)
    _reduce(+, (a.-m).^2, dims) / denom
end

@inline std(a::SArray; corrected::Bool=true, mean=nothing, dims=:) = sqrt.(var(a; corrected=corrected, mean=mean, dims=dims))

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