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

Mean with weight view fails #826

Open
adkabo opened this issue Aug 20, 2022 · 2 comments
Open

Mean with weight view fails #826

adkabo opened this issue Aug 20, 2022 · 2 comments
Labels

Comments

@adkabo
Copy link

adkabo commented Aug 20, 2022

using StatsBase, Statistics
julia> mean(rand(100), @view ProbabilityWeights(rand(100))[:])
ERROR: MethodError: objects of type Vector{Float64} are not callable
Use square brackets [] for indexing an Array.
Stacktrace:
 [1] _mean(f::Vector{Float64}, A::SubArray{Float64, 1, ProbabilityWeights{Float64, Float64, Vector{Float64}}, Tuple{UnitRange{Int64}}, false}, dims::Colon)
   @ Statistics /nix/store/15kh8pp59zbnhxcxh2l66xr1hzly00y9-julia-bin-1.7.1/share/julia/stdlib/v1.7/Statistics/src/Statistics.jl:176
 [2] mean(f::Vector{Float64}, A::SubArray{Float64, 1, ProbabilityWeights{Float64, Float64, Vector{Float64}}, Tuple{UnitRange{Int64}}, false}; dims::Function)
   @ Statistics /nix/store/15kh8pp59zbnhxcxh2l66xr1hzly00y9-julia-bin-1.7.1/share/julia/stdlib/v1.7/Statistics/src/Statistics.jl:104
 [3] mean(f::Vector{Float64}, A::SubArray{Float64, 1, ProbabilityWeights{Float64, Float64, Vector{Float64}}, Tuple{UnitRange{Int64}}, false})
   @ Statistics /nix/store/15kh8pp59zbnhxcxh2l66xr1hzly00y9-julia-bin-1.7.1/share/julia/stdlib/v1.7/Statistics/src/Statistics.jl:104
 [4] top-level scope
   @ REPL[7]:1

julia> versioninfo()
Julia Version 1.7.1
Commit ac5cc99908 (2021-12-22 19:35 UTC)
@ararslan
Copy link
Member

This is happening because StatsBase provides weighting by defining methods like mean(x, weights::AbstractWeights) but using @view makes weights a SubArray, so method that's called is mean(f, input) which is interpreting the vector as a function to call. The issue here is actually with StatsBase, which should probably be defining view/Base.maybeview on AbstractWeights to return something that is also <:AbstractWeights.

@ararslan ararslan transferred this issue from JuliaStats/Statistics.jl Aug 21, 2022
@nalimilan
Copy link
Member

See #723.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants