Skip to content

var(x, w::AbstractWeights) converts to Float64 when using weights #968

@abhro

Description

@abhro

If a sample vector x is declared to have Rationals, the use of var() with weights promotes the final answer to a float.

MWE (Minimal working example):

julia> using StatsBase

julia> x = (1:5) // 9
5-element Vector{Rational{Int64}}:
 1//9
 2//9
 1//3
 4//9
 5//9

julia> mean(x)
1//3

julia> var(x; corrected=false)
2//81

julia> w = UnitWeights{eltype(x)}(length(x))
5-element UnitWeights{Rational{Int64}}:
 1
 1
 1
 1
 1

julia> var(x, w)
0.024691358024691357

julia> var(x; corrected=false) |> float      # for verification
0.024691358024691357

The expected behavior is to keep the same eltype of x:

julia> var(x, w)
2//81

It seems the culprit is the _momentN functions in moments.jl, where s = 0.0 is set as the initial value. See, for example,

s = 0.0

I'll probably be submitting a PR to address this in the coming days.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions