From 4c27fcfcf7dcde3595378835d66b62424844f915 Mon Sep 17 00:00:00 2001 From: Jonas Krimmer <jonas.krimmer@kit.edu> Date: Fri, 28 Mar 2025 16:13:45 +0100 Subject: [PATCH] feat: cumulative mean & var --- src/estimate_statistics.jl | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/estimate_statistics.jl b/src/estimate_statistics.jl index 9c9528a..15d08f7 100644 --- a/src/estimate_statistics.jl +++ b/src/estimate_statistics.jl @@ -233,9 +233,41 @@ function scv(u::AbstractArray{T,N}; dims=:) where {T,N} end function cumscv(u::AbstractArray{T,N}; dims=N) where {T,N} + sz = ntuple(i -> i == dims ? size(u, dims) : 1, N) + I = abs2.(u) I² = I .^ 2 - cumsum(I²; dims) .* (1:size(I, dims))./ cumsum(I; dims) .^ 2 .- 1 + + (cumsum(I²; dims) .* reshape(1:size(u, dims), sz) ./ cumsum(I; dims) .^ 2 .- 1) +end + +""" + cummean(u::AbstractArray{T,N}; dims=N) where {T,N} + +Compute the cumulative mean of an array `u` along the specified dimension `dims`. Returns an array of the same size as `u`, containing the cumulative mean along the specified dimension. + +""" +function cummean(u::AbstractArray{T,N}; dims=N) where {T,N} + sz = ntuple(i -> i == dims ? size(u, dims) : 1, N) + cumsum(u; dims) ./ reshape(1:size(u, dims), sz) +end + + +""" + cumvar(u::AbstractArray{T,N}; dims=N, corrected::Bool=true) where {T,N} + +Compute the optionally corrected cumulative variance of an array `u` along the specified dimension `dims`. Returns an array of the same size as `u`, containing the cumulative variance along the specified dimension. +""" +function cumvar(u::AbstractArray{T,N}; dims=N, corrected::Bool=true) where {T,N} + sz = ntuple(i -> i == dims ? size(u, dims) : 1, N) + n = reshape(1:size(u, dims), sz) + + σ² = cummean(u.^2) .- cummean(u).^2 + if corrected + @. σ² *= n / (n - 1) + end + + return σ² end -- GitLab