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

Preserve eltype where possible for moments #688

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 27 additions & 21 deletions src/moments.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function var(v::RealArray, w::AbstractWeights; mean=nothing,
corrected::DepBool=nothing)
corrected = depcheck(:var, corrected)

if mean == nothing
if mean === nothing
varm(v, w, Statistics.mean(v, w); corrected=corrected)
else
varm(v, w, mean; corrected=corrected)
Expand Down Expand Up @@ -85,14 +85,16 @@ end
function varm(A::RealArray, w::AbstractWeights, M::RealArray, dim::Int;
corrected::DepBool=nothing)
corrected = depcheck(:varm, corrected)
varm!(similar(A, Float64, Base.reduced_indices(axes(A), dim)), A, w, M,
T = promote_type(eltype(A), eltype(w), eltype(M))
palday marked this conversation as resolved.
Show resolved Hide resolved
varm!(similar(A, T, Base.reduced_indices(axes(A), dim)), A, w, M,
dim; corrected=corrected)
end

function var(A::RealArray, w::AbstractWeights, dim::Int; mean=nothing,
corrected::DepBool=nothing)
corrected = depcheck(:var, corrected)
var!(similar(A, Float64, Base.reduced_indices(axes(A), dim)), A, w, dim;
T = promote_type(eltype(A), eltype(w))
var!(similar(A, T, Base.reduced_indices(axes(A), dim)), A, w, dim;
mean=mean, corrected=corrected)
end

Expand Down Expand Up @@ -223,7 +225,7 @@ end
##### General central moment
function _moment2(v::RealArray, m::Real; corrected=false)
n = length(v)
s = 0.0
s = zero(eltype(v)) - zero(m)
palday marked this conversation as resolved.
Show resolved Hide resolved
for i = 1:n
@inbounds z = v[i] - m
s += z * z
Expand All @@ -233,7 +235,7 @@ end

function _moment2(v::RealArray, wv::AbstractWeights, m::Real; corrected=false)
n = length(v)
s = 0.0
s = zero(eltype(wv)) * (zero(eltype(v)) - zero(m))^2
palday marked this conversation as resolved.
Show resolved Hide resolved
for i = 1:n
@inbounds z = v[i] - m
@inbounds s += (z * z) * wv[i]
Expand All @@ -244,7 +246,7 @@ end

function _moment3(v::RealArray, m::Real)
n = length(v)
s = 0.0
s = zero(eltype(v)) - zero(m)
palday marked this conversation as resolved.
Show resolved Hide resolved
for i = 1:n
@inbounds z = v[i] - m
s += z * z * z
Expand All @@ -254,7 +256,7 @@ end

function _moment3(v::RealArray, wv::AbstractWeights, m::Real)
n = length(v)
s = 0.0
s = zero(eltype(wv)) * (zero(eltype(v)) - zero(m))^2
palday marked this conversation as resolved.
Show resolved Hide resolved
for i = 1:n
@inbounds z = v[i] - m
@inbounds s += (z * z * z) * wv[i]
Expand All @@ -264,7 +266,7 @@ end

function _moment4(v::RealArray, m::Real)
n = length(v)
s = 0.0
s = zero(eltype(v)) - zero(m)
palday marked this conversation as resolved.
Show resolved Hide resolved
for i = 1:n
@inbounds z = v[i] - m
s += abs2(z * z)
Expand All @@ -274,7 +276,7 @@ end

function _moment4(v::RealArray, wv::AbstractWeights, m::Real)
n = length(v)
s = 0.0
s = zero(eltype(wv)) * (zero(eltype(v)) - zero(m))^2
palday marked this conversation as resolved.
Show resolved Hide resolved
for i = 1:n
@inbounds z = v[i] - m
@inbounds s += abs2(z * z) * wv[i]
Expand All @@ -284,7 +286,7 @@ end

function _momentk(v::RealArray, k::Int, m::Real)
n = length(v)
s = 0.0
s = zero(eltype(v)) - zero(m)
for i = 1:n
@inbounds z = v[i] - m
s += (z ^ k)
Expand All @@ -294,7 +296,7 @@ end

function _momentk(v::RealArray, k::Int, wv::AbstractWeights, m::Real)
n = length(v)
s = 0.0
s = zero(eltype(wv)) * (zero(eltype(v)) - zero(m))^2
palday marked this conversation as resolved.
Show resolved Hide resolved
for i = 1:n
@inbounds z = v[i] - m
@inbounds s += (z ^ k) * wv[i]
Expand Down Expand Up @@ -341,8 +343,9 @@ specifying a weighting vector `wv` and a center `m`.
"""
function skewness(v::RealArray, m::Real)
n = length(v)
cm2 = 0.0 # empirical 2nd centered moment (variance)
cm3 = 0.0 # empirical 3rd centered moment
T = promote_type(eltype(v), typeof(m))
palday marked this conversation as resolved.
Show resolved Hide resolved
cm2 = zero(T) # empirical 2nd centered moment (variance)
cm3 = zero(T) # empirical 3rd centered moment
for i = 1:n
@inbounds z = v[i] - m
z2 = z * z
Expand All @@ -358,8 +361,9 @@ end
function skewness(v::RealArray, wv::AbstractWeights, m::Real)
n = length(v)
length(wv) == n || throw(DimensionMismatch("Inconsistent array lengths."))
cm2 = 0.0 # empirical 2nd centered moment (variance)
cm3 = 0.0 # empirical 3rd centered moment
T = promote_type(eltype(v), eltype(wv), typeof(m))
cm2 = zero(T) # empirical 2nd centered moment (variance)
cm3 = zero(T) # empirical 3rd centered moment

@inbounds for i = 1:n
x_i = v[i]
Expand Down Expand Up @@ -388,8 +392,9 @@ specifying a weighting vector `wv` and a center `m`.
"""
function kurtosis(v::RealArray, m::Real)
n = length(v)
cm2 = 0.0 # empirical 2nd centered moment (variance)
cm4 = 0.0 # empirical 4th centered moment
T = promote_type(eltype(v), typeof(m))
cm2 = zero(T) # empirical 2nd centered moment (variance)
cm4 = zero(T) # empirical 4th centered moment
for i = 1:n
@inbounds z = v[i] - m
z2 = z * z
Expand All @@ -398,14 +403,15 @@ function kurtosis(v::RealArray, m::Real)
end
cm4 /= n
cm2 /= n
return (cm4 / (cm2 * cm2)) - 3.0
return (cm4 / (cm2 * cm2)) - 3
end

function kurtosis(v::RealArray, wv::AbstractWeights, m::Real)
n = length(v)
length(wv) == n || throw(DimensionMismatch("Inconsistent array lengths."))
cm2 = 0.0 # empirical 2nd centered moment (variance)
cm4 = 0.0 # empirical 4th centered moment
T = promote_type(eltype(v), eltype(wv), typeof(m))
cm2 = zero(T) # empirical 2nd centered moment (variance)
cm4 = zero(T) # empirical 4th centered moment

@inbounds for i = 1 : n
x_i = v[i]
Expand All @@ -419,7 +425,7 @@ function kurtosis(v::RealArray, wv::AbstractWeights, m::Real)
sw = sum(wv)
cm4 /= sw
cm2 /= sw
return (cm4 / (cm2 * cm2)) - 3.0
return (cm4 / (cm2 * cm2)) - 3
end

kurtosis(v::RealArray) = kurtosis(v, mean(v))
Expand Down
16 changes: 16 additions & 0 deletions test/moments.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using StatsBase
using Statistics
using Test

@testset "StatsBase.Moments" begin
Expand Down Expand Up @@ -278,4 +279,19 @@ end
@test moment(x, 5, w) ≈ sum((x2 .- 4).^5) / 5
end

@testset "Preservation of eltypes in moments" begin
xs = Float16[1, 2, 3, 4, 5];
ws = AnalyticWeights(Float16[1, 1, 1, 1, 1]);
@test typeof(std(xs)) === Float16
@test typeof(var(xs)) === Float16
@test typeof(mean(xs, ws)) === Float16
@test typeof(std(xs, ws)) === Float16
@test typeof(var(xs, ws)) === Float16
@test typeof(skewness(xs, ws)) === Float16
@test typeof(kurtosis(xs, ws)) === Float16
for i in 1:5
@test typeof(moment(xs, i, ws)) === Float16
end
end

end # @testset "StatsBase.Moments"