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

weighed mean/sum fail on vector of vectors #534

Closed
oschulz opened this issue Nov 6, 2019 · 1 comment
Closed

weighed mean/sum fail on vector of vectors #534

oschulz opened this issue Nov 6, 2019 · 1 comment

Comments

@oschulz
Copy link
Contributor

oschulz commented Nov 6, 2019

StatsBase currently fails to calculate weighted sums and means of vectors of vectors/arrays (unweighted sums/mean works fine).

Given

A = [[1.5, 2.5], [3.5, 4.5]]
w = Weights([0.5, 0.6])

The unweighted mean does what it should:

julia> mean(A)
2-element Array{Float64,1}:
 2.5
 3.5

However, the weighted mean fails:

julia> mean(A, w)
ERROR: DimensionMismatch("x and y are of different lengths!")

This happens because StatsBase defines mean(A, w) via sum(A, w) / sum(w), and sum(A, w) via dot(A, values(w)). However, dot operates recursively, and so tries to do dot(A[i], values(w)[i]) with each element of A (elements are vectors) and w (elements are scalars).

I believe this is a valid use case - @lmh91 and I encountered this while trying to calculate the weighted mean of 3D spatial positions (represented as a vector of static vectors).

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

No branches or pull requests

1 participant