Missing methods for moments of complex random variable

Open
#994 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
julia
Domain
data

Research direction

Start by reproducing the shown failures and reading StatsBase/src/moments.jl, especially the existing Real-only methods for moment, var, and mean_and_var. Determine the intended definitions for complex and weighted first and second moments, including the variance convention. Done means the requested complex-valued calls work consistently with the existing unweighted behavior and have coverage for the demonstrated cases.

Written by the indexing model from the issue text.

Description

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)
Dominant language
Julia
Stars
615
Forks
198
Avg merge
1d 2h
Merged PRs (30d)
5

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from JuliaStats/StatsBase.jl

All issues in JuliaStats/StatsBase.jl

Similar issues

More Julia issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.