Skip to content

Missing methods for moments of complex random variable #994

Open

Description

@FerreolS

Hi,
It seems that some methods are missing to compute the firsts 2 (weighted) moments of complex random variables.
Is any conceptual issues that prevents me to propose a PR implementing these?

julia> N = 1_000_000;

julia> x =  randn(N);

julia> xc = randn(ComplexF64,N);

julia> w = rand(N);

julia> mean(x)
-0.0006234813354142627

julia> mean(xc)
0.0002818700630158139 + 0.0004029044695052171im

julia> var(x)
0.9994220449190467

julia> var(xc)
1.000575477440655

julia> moment(xc,1)
ERROR: MethodError: no method matching moment(::Vector{ComplexF64}, ::Int64)
The function `moment` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  moment(::AbstractArray{<:Real}, ::Int64)
   @ StatsBase ~/.julia/packages/StatsBase/lRQEN/src/moments.jl:314
  moment(::AbstractArray{<:Real}, ::Int64, ::Real)
   @ StatsBase ~/.julia/packages/StatsBase/lRQEN/src/moments.jl:314
  moment(::AbstractArray{<:Real}, ::Int64, ::AbstractWeights)
   @ StatsBase ~/.julia/packages/StatsBase/lRQEN/src/moments.jl:321
  ...

Stacktrace:
 [1] top-level scope
   @ REPL[59]:1

julia> moment(x,1)
-1.2931877790833823e-17

julia> wsum(xc,w)
180.43932062446382 + 205.28102880933926im

julia> mean(xc,weights(w))
0.00036099221296754424 + 0.00041069126515038817im

julia> var(xc,weights(w))
ERROR: MethodError: no method matching var(::Vector{ComplexF64}, ::Weights{Float64, Float64, Vector{Float64}})
The function `var` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  var(::CovarianceEstimator, ::AbstractVector; kwargs...)
   @ StatsBase ~/.julia/packages/StatsBase/lRQEN/src/cov.jl:296
  var(::AbstractArray{<:Real}, ::AbstractWeights, ::Int64; mean, corrected)
   @ StatsBase ~/.julia/packages/StatsBase/lRQEN/src/moments.jl:63
  var(::AbstractArray; corrected, mean, dims)
   @ Statistics ~/.julia/packages/Statistics/gbcbG/src/Statistics.jl:380
  ...

Stacktrace:
 [1] top-level scope
   @ REPL[63]:1

julia> mean_and_var(x, weights(w))
(0.00035486592111588897, 0.9988627381401368)

julia> mean_and_var(xc, weights(w))
ERROR: MethodError: no method matching mean_and_var(::Vector{ComplexF64}, ::Weights{Float64, Float64, Vector{Float64}})
The function `mean_and_var` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  mean_and_var(::Any; corrected)
   @ StatsBase ~/.julia/packages/StatsBase/lRQEN/src/moments.jl:117
  mean_and_var(::AbstractArray{<:Real}, ::AbstractWeights; corrected)
   @ StatsBase ~/.julia/packages/StatsBase/lRQEN/src/moments.jl:139
  mean_and_var(::AbstractArray{<:Real}, ::AbstractWeights, ::Int64; corrected)
   @ StatsBase ~/.julia/packages/StatsBase/lRQEN/src/moments.jl:163
  ...

Stacktrace:
 [1] top-level scope
   @ REPL[65]:1

julia> mean_and_var(xc)
(0.0002818700630158139 + 0.0004029044695052171im, 1.000575477440655)

Activity

  1. FerreolS commented on Feb 23, 2026

    @FerreolS
    Author

    More generally the signature is x::AbstractArray{<:Real} in many methods is too restrictive as it also prevent the use of units

     julia> x =  randn(N) .* 1u"m";
    
    julia> moment(x,1)
    ERROR: MethodError: no method matching moment(::Vector{Quantity{Float64, 𝐋, Unitful.FreeUnits{(m,), 𝐋, nothing}}}, ::Int64)
    The function `moment` exists, but no method is defined for this combination of argument types.
    
    Closest candidates are:
      moment(::AbstractArray{<:Real}, ::Int64)
       @ StatsBase ~/.julia/packages/StatsBase/lRQEN/src/moments.jl:314
      moment(::AbstractArray{<:Real}, ::Int64, ::Real)
       @ StatsBase ~/.julia/packages/StatsBase/lRQEN/src/moments.jl:314
      moment(::AbstractArray{<:Real}, ::Int64, ::AbstractWeights)
       @ StatsBase ~/.julia/packages/StatsBase/lRQEN/src/moments.jl:321
      ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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