Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Statistics extension for var and std #1290

Open
Wu-Chenyang opened this issue Dec 14, 2024 · 0 comments
Open

Statistics extension for var and std #1290

Wu-Chenyang opened this issue Dec 14, 2024 · 0 comments

Comments

@Wu-Chenyang
Copy link

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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant