-
Notifications
You must be signed in to change notification settings - Fork 152
Open
Description
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
Labels
No labels