Skip to content

Commit

Permalink
rewrite the _momentX methods in more functional style
Browse files Browse the repository at this point in the history
  • Loading branch information
kalmarek committed Oct 23, 2023
1 parent 77e63df commit 8cbd195
Showing 1 changed file with 28 additions and 58 deletions.
86 changes: 28 additions & 58 deletions src/moments.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,87 +161,57 @@ end

##### General central moment
function _moment2(v::AbstractArray{<:Real}, m::Real; corrected=false)
n = length(v)
s = 0.0
for i = 1:n
@inbounds z = v[i] - m
s += z * z
end
varcorrection(n, corrected) * s
s = sum(x->abs2(x-m), v, init=zero(m))
return varcorrection(length(v), corrected) * s
end

function _moment2(v::AbstractArray{<:Real}, wv::AbstractWeights, m::Real; corrected=false)
n = length(v)
s = 0.0
for i = 1:n
@inbounds z = v[i] - m
@inbounds s += (z * z) * wv[i]
end

varcorrection(wv, corrected) * s
s = sum(i -> (@inbounds abs2(v[i] - m) * wv[i]), eachindex(v), init=zero(m))
return varcorrection(wv, corrected) * s
end

function _moment3(v::AbstractArray{<:Real}, m::Real)
n = length(v)
s = 0.0
for i = 1:n
@inbounds z = v[i] - m
s += z * z * z
end
s / n
s = sum(x->(x-m)^3, v, init=zero(m))
return s/length(v)
end

function _moment3(v::AbstractArray{<:Real}, wv::AbstractWeights, m::Real)
n = length(v)
s = 0.0
for i = 1:n
@inbounds z = v[i] - m
@inbounds s += (z * z * z) * wv[i]
end
s / sum(wv)
s = sum(
i -> (@inbounds (z = (v[i] - m); z * z * z * wv[i])),
eachindex(v),
init=zero(m),
)
return s/sum(wv)
end

function _moment4(v::AbstractArray{<:Real}, m::Real)
n = length(v)
s = 0.0
for i = 1:n
@inbounds z = v[i] - m
s += abs2(z * z)
end
s / n
s = sum(x-> (z = x-m; abs2(z*z)), v, init=zero(m))
return s/length(v)
end

function _moment4(v::AbstractArray{<:Real}, wv::AbstractWeights, m::Real)
n = length(v)
s = 0.0
for i = 1:n
@inbounds z = v[i] - m
@inbounds s += abs2(z * z) * wv[i]
end
s / sum(wv)
s = sum(
i -> (@inbounds (z = (v[i] - m); abs2(z * z) * wv[i])),
eachindex(v),
init=zero(m),
)
return s/sum(wv)
end

function _momentk(v::AbstractArray{<:Real}, k::Int, m::Real)
n = length(v)
s = 0.0
for i = 1:n
@inbounds z = v[i] - m
s += (z ^ k)
end
s / n
s = sum(x -> (x - m)^k, v, init=zero(m))
return s/length(v)
end

function _momentk(v::AbstractArray{<:Real}, k::Int, wv::AbstractWeights, m::Real)
n = length(v)
s = 0.0
for i = 1:n
@inbounds z = v[i] - m
@inbounds s += (z ^ k) * wv[i]
end
s / sum(wv)
s = sum(
i -> (@inbounds (z = (v[i] - m); z^k * wv[i])),
eachindex(v),
init=zero(m),
)
return s/sum(wv)
end


"""
moment(v, k, [wv::AbstractWeights], m=mean(v))
Expand Down

0 comments on commit 8cbd195

Please sign in to comment.