-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Import weighted stats and moments from StatsBase to Statistics #31395
Closed
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6ca9eb3
Import weighted stats and moments from StatsBase to Statistics
nalimilan d5e33de
Remove moment and combined stats, make other functions accept any ite…
nalimilan 9a41048
Implement weighted sum
nalimilan 022d029
Move optimized weighted sum methods to LinearAlgebra
nalimilan 24f530a
More tests
nalimilan d7ae38b
Move varcorrection to Statistics.jl
nalimilan 4618723
Docs
nalimilan d627393
Performance fix
nalimilan 7f364f4
Cleanup
nalimilan f51d8db
Fix bug
nalimilan 1f7b3d9
Fix TODO
nalimilan d5e0135
Another cleanup, enable more tests
nalimilan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# Optimized method for weighted sum with BlasReal | ||
# dot cannot be used for other types as it uses + rather than add_sum for accumulation, | ||
# and therefore does not return the correct type | ||
Base._sum(A::AbstractArray{T}, dims::Colon, w::AbstractArray{T}) where {T<:BlasReal} = | ||
dot(vec(A), vec(w)) | ||
|
||
# Optimized methods for weighted sum over dimensions with BlasReal | ||
# (generic method is defined in base/reducedim.jl) | ||
# | ||
# _wsum! is specialized for following cases: | ||
# (a) A is a dense matrix with eltype <: BlasReal: we call gemv! | ||
# The internal function that implements this is _wsum2_blas! | ||
# | ||
# (b) A is a contiguous array with eltype <: BlasReal: | ||
# dim == 1: treat A like a matrix of size (d1, d2 x ... x dN) | ||
# dim == N: treat A like a matrix of size (d1 x ... x d(N-1), dN) | ||
# otherwise: decompose A into multiple pages, and apply _wsum2_blas! | ||
# for each | ||
# The internal function that implements this is _wsumN! | ||
# | ||
# (c) A is a general dense array with eltype <: BlasReal: | ||
# dim <= 2: delegate to (a) and (b) | ||
# otherwise, decompose A into multiple pages | ||
# The internal function that implements this is _wsumN! | ||
|
||
function _wsum2_blas!(R::StridedVector{T}, A::StridedMatrix{T}, w::StridedVector{T}, | ||
dim::Int, init::Bool) where T<:BlasReal | ||
beta = ifelse(init, zero(T), one(T)) | ||
trans = dim == 1 ? 'T' : 'N' | ||
BLAS.gemv!(trans, one(T), A, w, beta, R) | ||
return R | ||
end | ||
|
||
function _wsumN!(R::StridedArray{T}, A::StridedArray{T,N}, w::StridedVector{T}, | ||
dim::Int, init::Bool) where {T<:BlasReal,N} | ||
if dim == 1 | ||
m = size(A, 1) | ||
n = div(length(A), m) | ||
_wsum2_blas!(view(R,:), reshape(A, (m, n)), w, 1, init) | ||
elseif dim == N | ||
n = size(A, N) | ||
m = div(length(A), n) | ||
_wsum2_blas!(view(R,:), reshape(A, (m, n)), w, 2, init) | ||
else # 1 < dim < N | ||
m = 1 | ||
for i = 1:dim-1 | ||
m *= size(A, i) | ||
end | ||
n = size(A, dim) | ||
k = 1 | ||
for i = dim+1:N | ||
k *= size(A, i) | ||
end | ||
Av = reshape(A, (m, n, k)) | ||
Rv = reshape(R, (m, k)) | ||
for i = 1:k | ||
_wsum2_blas!(view(Rv,:,i), view(Av,:,:,i), w, 2, init) | ||
end | ||
end | ||
return R | ||
end | ||
|
||
function _wsumN!(R::StridedArray{T}, A::DenseArray{T,N}, w::StridedVector{T}, | ||
dim::Int, init::Bool) where {T<:BlasReal,N} | ||
@assert N >= 3 | ||
if dim <= 2 | ||
m = size(A, 1) | ||
n = size(A, 2) | ||
npages = 1 | ||
for i = 3:N | ||
npages *= size(A, i) | ||
end | ||
rlen = ifelse(dim == 1, n, m) | ||
Rv = reshape(R, (rlen, npages)) | ||
for i = 1:npages | ||
_wsum2_blas!(view(Rv,:,i), view(A,:,:,i), w, dim, init) | ||
end | ||
else | ||
Base._wsum_general!(R, A, w, dim, init) | ||
end | ||
return R | ||
end | ||
|
||
Base._wsum!(R::StridedArray{T}, A::DenseMatrix{T}, w::StridedVector{T}, | ||
dim::Int, init::Bool) where {T<:BlasReal} = | ||
_wsum2_blas!(view(R,:), A, w, dim, init) | ||
|
||
Base._wsum!(R::StridedArray{T}, A::DenseArray{T}, w::StridedVector{T}, | ||
dim::Int, init::Bool) where {T<:BlasReal} = | ||
_wsumN!(R, A, w, dim, init) | ||
|
||
Base._wsum!(R::StridedVector{T}, A::DenseArray{T}, w::StridedVector{T}, | ||
dim::Int, init::Bool) where {T<:BlasReal} = | ||
Base._wsum1!(R, A, w, init) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code is quite ugly but I'm not sure what's the best solution. For unweighted sum, reducing over
dim > N
is a no-op, so that's easy, but for the weighted sum it amounts to multiplying values by their corresponding weight. Maybe this should just be an error?