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

rewrite _momentX methods in more functional style #900

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use an improved init value if possible - in many cases the result of sum(...) won't be of the same type as zero(m).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For instance, we could use

Suggested change
s = sum(x->abs2(x-m), v, init=zero(m))
init = (zero(eltype(v)) - zero(m))^2
s = sum(x->(x-m)^2, v; init=init)

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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, I think we need a different init here. Additionally, @inbounds is unsafe here as i might not be an actual index of wv.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
s = sum(i -> (@inbounds abs2(v[i] - m) * wv[i]), eachindex(v), init=zero(m))
init = (zero(eltype(v)) - zero(m))^2 * zero(eltype(wv))
s = sum(i -> (v[i] - m)^2 * wv[i], eachindex(v, wv); init=init)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@devmotion if v and wv do not have the same indices eachindex(v, wv) will throw and therefore it seems that @inbounds here should be safe. How about this:

Suggested change
s = sum(i -> (@inbounds abs2(v[i] - m) * wv[i]), eachindex(v), init=zero(m))
s = if isempty(v)
(zero(eltype(v)) - zero(m))^2*zero(eltype(wv))
elseif eachindex(v) == eachindex(wv)
sum(i -> (@inbounds (v[i] - m)^2 * wv[i]), eachindex(v, wv))
else
sum(i -> ((v_ - m)^2 * wv_) for (v_, wv_) in zip(v, wv))
end

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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
s = sum(x->(x-m)^3, v, init=zero(m))
init = (zero(eltype(v)) - zero(m))^3
s = sum(x->(x-m)^3, v; init=init)

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),
)
Comment on lines +179 to +183
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comments as above:

Suggested change
s = sum(
i -> (@inbounds (z = (v[i] - m); z * z * z * wv[i])),
eachindex(v),
init=zero(m),
)
init = (zero(eltype(v)) - zero(m))^3 * zero(eltype(wv))
s = sum(
i -> (v[i] - m)^3 * wv[i],
eachindex(v, wv);
init=init,
)

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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
s = sum(x-> (z = x-m; abs2(z*z)), v, init=zero(m))
init = (zero(eltype(v)) - zero(m))^4
s = sum(x->(x-m)^4, v; init=init)

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),
)
Comment on lines +193 to +197
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
s = sum(
i -> (@inbounds (z = (v[i] - m); abs2(z * z) * wv[i])),
eachindex(v),
init=zero(m),
)
init = (zero(eltype(v)) - zero(m))^4 * zero(eltype(wv))
s = sum(
i -> (v[i] - m)^4 * wv[i],
eachindex(v, wv);
init=init,
)

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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
s = sum(x -> (x - m)^k, v, init=zero(m))
init = (zero(eltype(v)) - zero(m))^k
s = sum(x -> (x - m)^k, v; init=init)

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),
)
Comment on lines +207 to +211
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
s = sum(
i -> (@inbounds (z = (v[i] - m); z^k * wv[i])),
eachindex(v),
init=zero(m),
)
init = (zero(eltype(v)) - zero(m))^k * zero(eltype(wv))
s = sum(
i -> (v[i] - m)^k * wv[i],
eachindex(v, wv);
init=init,
)

return s/sum(wv)
end


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

Expand Down
Loading