Skip to content

Commit 8b5fbde

Browse files
committed
reduce allocations in stat_var - WIP
1 parent a794de0 commit 8b5fbde

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/stat/non_hp_stat.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,19 +297,21 @@ function stat_wmean(f, x::AbstractVector{T}, w::AbstractArray{S,1}) where {T} wh
297297
end
298298
stat_wmean(x::AbstractVector{T}, w::AbstractArray{S,1}) where {T} where {S} = stat_wmean(identity, x, w)
299299

300-
300+
_abs2_var_barrier(x,y,f::F) where F = abs2(f(x)-y)
301+
_meanval_var_barrier(n, sval)::Union{Missing, Float64} = n == 0 ? missing : sval / n
301302
function stat_var(f, x::AbstractArray{T,1}, dof=true)::Union{Float64,Missing} where {T<:Union{Missing,INTEGERS,FLOATS}}
302303
all(ismissing, x) && return missing
303304
# any(ISNAN, x) && return convert(eltype(x), NaN)
304305
# meanval = stat_mean(f, x)
305306
# n = mapreduce(!ismissing∘f, +, x)
306307
sval = stat_sum(y -> f(y) * 1.0, x)
307308
n = mapreduce(!ismissing f, +, x)
308-
meanval = n == 0 ? missing : sval / n
309+
meanval = _meanval_var_barrier(n, sval)
309310

310311
ss = 0.0
311312
for i in 1:length(x)
312-
ss = _stat_add_sum(ss, abs2(f(x[i]) - meanval))
313+
# ss = _stat_add_sum(ss, abs2(f(x[i]) - meanval))
314+
ss = _stat_add_sum(ss, _abs2_var_barrier(x[i], meanval, f))
313315
end
314316

315317
if n == 0

0 commit comments

Comments
 (0)