Skip to content

Commit b7fe44c

Browse files
authored
Accept any iterable in scalar stats (#472)
1 parent 9535edc commit b7fe44c

File tree

3 files changed

+180
-100
lines changed

3 files changed

+180
-100
lines changed

src/moments.jl

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## var
44
"""
5-
varm(x, w::AbstractWeights, m, [dim]; corrected=false)
5+
varm(x::AbstractArray, w::AbstractWeights, m, [dim]; corrected=false)
66
77
Compute the variance of a real-valued array `x` with a known mean `m`, optionally
88
over a dimension `dim`. Observations in `x` are weighted using weight vector `w`.
@@ -22,7 +22,7 @@ varm(v::RealArray, w::AbstractWeights, m::Real; corrected::DepBool=nothing) =
2222
_moment2(v, w, m; corrected=depcheck(:varm, corrected))
2323

2424
"""
25-
var(x, w::AbstractWeights, [dim]; mean=nothing, corrected=false)
25+
var(x::AbstractArray, w::AbstractWeights, [dim]; mean=nothing, corrected=false)
2626
2727
Compute the variance of a real-valued array `x`, optionally over a dimension `dim`.
2828
Observations in `x` are weighted using weight vector `w`.
@@ -98,7 +98,7 @@ end
9898

9999
## std
100100
"""
101-
stdm(v, w::AbstractWeights, m, [dim]; corrected=false)
101+
stdm(x::AbstractArray, w::AbstractWeights, m, [dim]; corrected=false)
102102
103103
Compute the standard deviation of a real-valued array `x` with a known mean `m`,
104104
optionally over a dimension `dim`. Observations in `x` are weighted using weight vector `w`.
@@ -118,7 +118,7 @@ stdm(v::RealArray, w::AbstractWeights, m::Real; corrected::DepBool=nothing) =
118118
sqrt(varm(v, w, m, corrected=depcheck(:stdm, corrected)))
119119

120120
"""
121-
std(v, w::AbstractWeights, [dim]; mean=nothing, corrected=false)
121+
std(x::AbstractArray, w::AbstractWeights, [dim]; mean=nothing, corrected=false)
122122
123123
Compute the standard deviation of a real-valued array `x`,
124124
optionally over a dimension `dim`. Observations in `x` are weighted using weight vector `w`.
@@ -153,66 +153,68 @@ std(v::RealArray, w::AbstractWeights, dim::Int; mean=nothing,
153153
"""
154154
mean_and_var(x, [w::AbstractWeights], [dim]; corrected=false) -> (mean, var)
155155
156-
Return the mean and variance of a real-valued array `x`, optionally over a dimension
157-
`dim`, as a tuple. Observations in `x` can be weighted using weight vector `w`.
156+
Return the mean and standard deviation of collection `x`. If `x` is an `AbstractArray`,
157+
`dim` can be specified as a tuple to compute statistics over these dimensions.
158+
A weighting vector `w` can be specified to weight the estimates.
158159
Finally, bias correction is be applied to the variance calculation if `corrected=true`.
159160
See [`var`](@ref) documentation for more details.
160161
"""
161-
function mean_and_var(A::RealArray; corrected::Bool=true)
162-
m = mean(A)
163-
v = varm(A, m; corrected=corrected)
162+
function mean_and_var(x; corrected::Bool=true)
163+
m = mean(x)
164+
v = varm(x, m; corrected=corrected)
164165
m, v
165166
end
166167

167168
"""
168169
mean_and_std(x, [w::AbstractWeights], [dim]; corrected=false) -> (mean, std)
169170
170-
Return the mean and standard deviation of a real-valued array `x`, optionally
171-
over a dimension `dim`, as a tuple. A weighting vector `w` can be specified
172-
to weight the estimates. Finally, bias correction is applied to the
171+
Return the mean and standard deviation of collection `x`. If `x` is an `AbstractArray`,
172+
`dim` can be specified as a tuple to compute statistics over these dimensions.
173+
A weighting vector `w` can be specified to weight the estimates.
174+
Finally, bias correction is applied to the
173175
standard deviation calculation if `corrected=true`.
174176
See [`std`](@ref) documentation for more details.
175177
"""
176-
function mean_and_std(A::RealArray; corrected::Bool=true)
177-
m = mean(A)
178-
s = stdm(A, m; corrected=corrected)
178+
function mean_and_std(x; corrected::Bool=true)
179+
m = mean(x)
180+
s = stdm(x, m; corrected=corrected)
179181
m, s
180182
end
181183

182-
function mean_and_var(A::RealArray, w::AbstractWeights; corrected::DepBool=nothing)
183-
m = mean(A, w)
184-
v = varm(A, w, m; corrected=depcheck(:mean_and_var, corrected))
184+
function mean_and_var(x::RealArray, w::AbstractWeights; corrected::DepBool=nothing)
185+
m = mean(x, w)
186+
v = varm(x, w, m; corrected=depcheck(:mean_and_var, corrected))
185187
m, v
186188
end
187-
function mean_and_std(A::RealArray, w::AbstractWeights; corrected::DepBool=nothing)
188-
m = mean(A, w)
189-
s = stdm(A, w, m; corrected=depcheck(:mean_and_std, corrected))
189+
function mean_and_std(x::RealArray, w::AbstractWeights; corrected::DepBool=nothing)
190+
m = mean(x, w)
191+
s = stdm(x, w, m; corrected=depcheck(:mean_and_std, corrected))
190192
m, s
191193
end
192194

193195

194-
function mean_and_var(A::RealArray, dim::Int; corrected::Bool=true)
195-
m = mean(A, dims = dim)
196-
v = varm(A, m, dims = dim, corrected=corrected)
196+
function mean_and_var(x::RealArray, dim::Int; corrected::Bool=true)
197+
m = mean(x, dims = dim)
198+
v = varm(x, m, dims = dim, corrected=corrected)
197199
m, v
198200
end
199-
function mean_and_std(A::RealArray, dim::Int; corrected::Bool=true)
200-
m = mean(A, dims = dim)
201-
s = stdm(A, m, dim; corrected=corrected)
201+
function mean_and_std(x::RealArray, dim::Int; corrected::Bool=true)
202+
m = mean(x, dims = dim)
203+
s = stdm(x, m, dim; corrected=corrected)
202204
m, s
203205
end
204206

205207

206-
function mean_and_var(A::RealArray, w::AbstractWeights, dims::Int;
208+
function mean_and_var(x::RealArray, w::AbstractWeights, dims::Int;
207209
corrected::DepBool=nothing)
208-
m = mean(A, w, dims=dims)
209-
v = varm(A, w, m, dims; corrected=depcheck(:mean_and_var, corrected))
210+
m = mean(x, w, dims=dims)
211+
v = varm(x, w, m, dims; corrected=depcheck(:mean_and_var, corrected))
210212
m, v
211213
end
212-
function mean_and_std(A::RealArray, w::AbstractWeights, dims::Int;
214+
function mean_and_std(x::RealArray, w::AbstractWeights, dims::Int;
213215
corrected::DepBool=nothing)
214-
m = mean(A, w, dims=dims)
215-
s = stdm(A, w, m, dims; corrected=depcheck(:mean_and_std, corrected))
216+
m = mean(x, w, dims=dims)
217+
s = stdm(x, w, m, dims; corrected=depcheck(:mean_and_std, corrected))
216218
m, s
217219
end
218220

0 commit comments

Comments
 (0)