Skip to content

AbstractWeights restriction to Real is too restrictive #745

Open
@JeffFessler

Description

@JeffFessler

The type of AbstractWeights is restricted to Real which precludes complex numbers, numbers with units from Unitful, etc. Using Number for the most general type would be more inclusive.

abstract type AbstractWeights{S<:Real, T<:Real, V<:AbstractVector{T}} <: AbstractVector{T} end

Of course many subtypes like ProbabilityWeights must be Real, but general weights do not, especially for how weights are used in the fit method for a Histogram. The follow MWE ideally would work, but fails due to the weights() method restrictions:

using StatsBase
#T = Float32 # this version works fine
T = ComplexF64 # this version errors at weights(w)
w = rand(T, 100) # weights 
data = randn(100)
fit(Histogram, data, weights(w))

An alternative to generalizing the AbstractWeights type would be to generalize the fit method so that the weights can simply be any Number type.

I would be eventually willing to help with a PR if there is openness to such a generalization, but it would helpful to know if there is a preference between generalizing Weights vs generalizing fit.

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