Skip to content

Commit 50ddf1a

Browse files
committed
Always require StatsBase and improve statistics methods
1 parent 1978380 commit 50ddf1a

File tree

5 files changed

+44
-56
lines changed

5 files changed

+44
-56
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ size(flatview(A_nested)) == (2, 3, 6)
6868

6969
There is a full duality between the nested and the flat view of the data. `A_flat` may be resized freely without breaking the inner consistency of `A_nested`: Changes in the shape of one will result in changes in the shape of the other.
7070

71+
### Statistics functions
72+
73+
`AbstractVectorOfSimilarArrays` supports the functions `sum`, `mean` and `var`, `AbstractVectorOfSimilarVectors` additionally support `cov` and `cor`.
74+
75+
Methods for these function are defined both without and with weights (via `StatsBase.AbstractWeights`). Because of this, `ArraysOfArrays` currently requires `StatsBase`. It's possible that this requirement can be dropped in the future, though (see JuliaLang/julia#29974).
7176

7277
## VectorOfArrays
7378

REQUIRE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
julia 0.7
22
Requires
3+
StatsBase
34
UnsafeArrays

src/ArraysOfArrays.jl

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,13 @@ module ArraysOfArrays
66

77
using Statistics
88

9-
using Requires
109
using UnsafeArrays
1110

11+
import StatsBase
12+
1213
include("util.jl")
1314
include("functions.jl")
1415
include("array_of_similar_arrays.jl")
1516
include("vector_of_arrays.jl")
1617

17-
function __init__()
18-
@require StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91" include("statsbase_support.jl")
19-
end
20-
2118
end # module

src/array_of_similar_arrays.jl

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -312,38 +312,63 @@ Base.convert(R::Type{VectorOfSimilarVectors}, A::AbstractVector{<:AbstractVector
312312

313313

314314
"""
315-
sum(X::VectorOfSimilarVectors)
315+
sum(X::AbstractVectorOfSimilarArrays)
316+
sum(X::AbstractVectorOfSimilarArrays, w::StatsBase.AbstractWeights)
316317
317318
Compute the sum of the elements vectors of `X`. Equivalent to `sum` of
318-
`flatview(X)` along dimension 2.
319+
`flatview(X)` along the last dimension.
319320
"""
320-
Main.sum(X::VectorOfSimilarVectors{<:Real}) = sum(flatview(X); dims = 2)
321+
Base.sum(X::AbstractVectorOfSimilarArrays{T,M}) where {T,M} = sum(flatview(X); dims = M + 1)
322+
Base.sum(X::AbstractVectorOfSimilarArrays{T,M}, w::StatsBase.AbstractWeights) where {T,M} = sum(flatview(X), w, M + 1)
321323

322324

323325
"""
324-
mean(X::VectorOfSimilarVectors)
326+
mean(X::AbstractVectorOfSimilarArrays)
327+
mean(X::AbstractVectorOfSimilarArrays, w::StatsBase.AbstractWeights)
325328
326329
Compute the mean of the elements vectors of `X`. Equivalent to `mean` of
327-
`flatview(X)` along dimension 2.
330+
`flatview(X)` along the last dimension.
328331
"""
329-
Statistics.mean(X::VectorOfSimilarVectors) = mean(flatview(X); dims = 2)
332+
Statistics.mean(X::AbstractVectorOfSimilarArrays{T,M}) where {T,M} =
333+
mean(flatview(X); dims = M + 1)
334+
Statistics.mean(X::AbstractVectorOfSimilarArrays{T,M}, w::StatsBase.AbstractWeights) where {T,M} =
335+
mean(flatview(X), w, M + 1)
330336

331337

332338
"""
333-
cov(X::VectorOfSimilarVectors; corrected::Bool = true)
334-
cov(X::VectorOfSimilarVectors, w::AbstractVector; corrected::Bool = true)
339+
var(X::AbstractVectorOfSimilarArrays; corrected::Bool = true)
340+
var(X::AbstractVectorOfSimilarArrays, w::StatsBase.AbstractWeights; corrected::Bool = true)
341+
342+
Compute the sample variance of the elements vectors of `X`. Equivalent to
343+
`var` of `flatview(X)` along the last dimension.
344+
"""
345+
Statistics.var(X::AbstractVectorOfSimilarArrays{T,M}; corrected::Bool = true) where {T,M} =
346+
var(flatview(X); dims = M + 1, corrected = corrected)
347+
Statistics.var(X::AbstractVectorOfSimilarArrays{T,M}, w::StatsBase.AbstractWeights; corrected::Bool = true) where {T,M} =
348+
var(flatview(X), w, M + 1; corrected = corrected)
349+
350+
351+
"""
352+
cov(X::AbstractVectorOfSimilarVectors; corrected::Bool = true)
353+
cov(X::AbstractVectorOfSimilarVectors, w::StatsBase.AbstractWeights; corrected::Bool = true)
335354
336355
Compute the covariance matrix between the elements of the elements of `X`
337356
along `X`. Equivalent to `cov` of `flatview(X)` along dimension 2.
338357
"""
339-
Statistics.cov(X::VectorOfSimilarVectors; corrected::Bool = true) =
358+
Statistics.cov(X::AbstractVectorOfSimilarVectors; corrected::Bool = true) =
340359
cov(flatview(X); dims = 2, corrected = corrected)
360+
Statistics.cov(X::AbstractVectorOfSimilarVectors, w::StatsBase.AbstractWeights; corrected::Bool = true) =
361+
cov(flatview(X), w, 2; corrected = corrected)
341362

342363

343364
"""
344-
cor(X::VectorOfSimilarVectors)
365+
cor(X::AbstractVectorOfSimilarVectors)
366+
cor(X::AbstractVectorOfSimilarVectors, w::StatsBase.AbstractWeights)
345367
346368
Compute the Pearson correlation matrix between the elements of the elements of
347369
`X` along `X`. Equivalent to `cor` of `flatview(X)` along dimension 2.
348370
"""
349-
Statistics.cor(X::VectorOfSimilarVectors) = cor(flatview(X); dims = 2)
371+
Statistics.cor(X::AbstractVectorOfSimilarVectors) =
372+
cor(flatview(X); dims = 2)
373+
Statistics.cor(X::AbstractVectorOfSimilarVectors, w::StatsBase.AbstractWeights) =
374+
cor(flatview(X), w, 2)

src/statsbase_support.jl

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)