Skip to content

Commit e66b336

Browse files
committed
Implement sum, mean, var and cov for VectorOfSimilarVectors
1 parent 8987b91 commit e66b336

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

REQUIRE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
julia 0.7
2+
Requires
23
UnsafeArrays

src/ArraysOfArrays.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@ __precompile__(true)
44

55
module ArraysOfArrays
66

7+
using Statistics
8+
9+
using Requires
710
using UnsafeArrays
811

912
include("util.jl")
1013
include("functions.jl")
1114
include("array_of_similar_arrays.jl")
1215
include("vector_of_arrays.jl")
1316

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

src/array_of_similar_arrays.jl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,3 +295,41 @@ Base.convert(R::Type{VectorOfSimilarVectors}, A::AbstractVector{<:AbstractVector
295295

296296

297297
@inline Base.IndexStyle(A::VectorOfSimilarVectors) = IndexLinear()
298+
299+
300+
"""
301+
sum(X::VectorOfSimilarVectors)
302+
303+
Compute the sum of the elements vectors of `X`. Equivalent to `sum` of
304+
`flatview(X)` along dimension 2.
305+
"""
306+
Main.sum(X::VectorOfSimilarVectors{<:Real}) = sum(flatview(X); dims = 2)
307+
308+
309+
"""
310+
mean(X::VectorOfSimilarVectors)
311+
312+
Compute the mean of the elements vectors of `X`. Equivalent to `mean` of
313+
`flatview(X)` along dimension 2.
314+
"""
315+
Statistics.mean(X::VectorOfSimilarVectors) = mean(flatview(X); dims = 2)
316+
317+
318+
"""
319+
cov(X::VectorOfSimilarVectors; corrected::Bool = true)
320+
cov(X::VectorOfSimilarVectors, w::AbstractVector; corrected::Bool = true)
321+
322+
Compute the covariance matrix between the elements of the elements of `X`
323+
along `X`. Equivalent to `cov` of `flatview(X)` along dimension 2.
324+
"""
325+
Statistics.cov(X::VectorOfSimilarVectors; corrected::Bool = true) =
326+
cov(flatview(X); dims = 2, corrected = corrected)
327+
328+
329+
"""
330+
cor(X::VectorOfSimilarVectors)
331+
332+
Compute the Pearson correlation matrix between the elements of the elements of
333+
`X` along `X`. Equivalent to `cor` of `flatview(X)` along dimension 2.
334+
"""
335+
Statistics.cor(X::VectorOfSimilarVectors) = cor(flatview(X); dims = 2)

src/statsbase_support.jl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# This file is a part of ArraysOfArrays.jl, licensed under the MIT License (MIT).
2+
3+
4+
"""
5+
sum(X::VectorOfSimilarVectors, w::StatsBase.AbstractWeights)
6+
7+
Compute the sum of the elements vectors of `X` with weights `w`. Equivalent
8+
to `sum` of `flatview(X)` along dimension 2.
9+
"""
10+
Main.sum(X::VectorOfSimilarVectors, w::StatsBase.AbstractWeights) = sum(flatview(X), w, 2)
11+
12+
13+
"""
14+
mean(X::VectorOfSimilarVectors, w::StatsBase.AbstractWeights)
15+
16+
Compute the mean of the elements vectors of `X` with weights `w`. Equivalent
17+
to `mean` of `flatview(X)` along dimension 2.
18+
"""
19+
Statistics.mean(X::VectorOfSimilarVectors, w::StatsBase.AbstractWeights) = mean(flatview(X), w, 2)
20+
21+
22+
"""
23+
cov(X::VectorOfSimilarVectors, w::StatsBase.AbstractWeights; corrected::Bool = true)
24+
25+
Compute the covariance matrix between the elements of the elements of `X`
26+
along `X` with weights `w`. Equivalent to `cov` of `flatview(X)` along
27+
dimension 2.
28+
"""
29+
Statistics.cov(X::VectorOfSimilarVectors, w::StatsBase.AbstractWeights; corrected::Bool = true) =
30+
cov(flatview(X), w, 2; corrected = corrected)
31+
32+
33+
"""
34+
cor(X::VectorOfSimilarVectors, w::StatsBase.AbstractWeights)
35+
36+
Compute the Pearson correlation matrix between the elements of the elements of
37+
`X` along `X` with weights `w`. Equivalent to `cor` of `flatview(X)` along
38+
dimension 2.
39+
"""
40+
Statistics.cor(X::VectorOfSimilarVectors, w::StatsBase.AbstractWeights) = cor(flatview(X), w, 2)

0 commit comments

Comments
 (0)