Skip to content

Commit 07a8ef4

Browse files
committed
Make cov()'s corrected argument a keyword argument and cleanup docstrings
For consistency with var and std. Also remove methods which are no longer needed now that deprecations have been removed. Add types to signatures in docstrings.
1 parent 970a742 commit 07a8ef4

File tree

3 files changed

+46
-52
lines changed

3 files changed

+46
-52
lines changed

base/deprecated.jl

+5
Original file line numberDiff line numberDiff line change
@@ -1324,5 +1324,10 @@ end
13241324

13251325
# END 0.6 deprecations
13261326

1327+
@deprecate cov(x::AbstractVector, corrected::Bool) cov(x, corrected=corrected)
1328+
@deprecate cov(x::AbstractMatrix, vardim::Int, corrected::Bool) cov(x, corrected=corrected)
1329+
@deprecate cov(X::AbstractVector, Y::AbstractVector, corrected::Bool) cov(X, Y, corrected=corrected)
1330+
@deprecate cov(X::AbstractVecOrMat, Y::AbstractVecOrMat, vardim::Int, corrected::Bool) cov(X, Y, vardim, corrected=corrected)
1331+
13271332
# BEGIN 1.0 deprecations
13281333
# END 1.0 deprecations

base/statistics.jl

+23-34
Original file line numberDiff line numberDiff line change
@@ -317,75 +317,64 @@ unscaled_covzm(x::AbstractMatrix, y::AbstractMatrix, vardim::Int) =
317317

318318
# covzm (with centered data)
319319

320-
covzm(x::AbstractVector, corrected::Bool=true) = unscaled_covzm(x) / (_length(x) - Int(corrected))
321-
covzm(x::AbstractMatrix, vardim::Int=1, corrected::Bool=true) =
320+
covzm(x::AbstractVector; corrected::Bool=true) = unscaled_covzm(x) / (_length(x) - Int(corrected))
321+
covzm(x::AbstractMatrix, vardim::Int=1; corrected::Bool=true) =
322322
scale!(unscaled_covzm(x, vardim), inv(size(x,vardim) - Int(corrected)))
323-
covzm(x::AbstractVector, y::AbstractVector, corrected::Bool=true) =
323+
covzm(x::AbstractVector, y::AbstractVector; corrected::Bool=true) =
324324
unscaled_covzm(x, y) / (_length(x) - Int(corrected))
325-
covzm(x::AbstractVecOrMat, y::AbstractVecOrMat, vardim::Int=1, corrected::Bool=true) =
325+
covzm(x::AbstractVecOrMat, y::AbstractVecOrMat, vardim::Int=1; corrected::Bool=true) =
326326
scale!(unscaled_covzm(x, y, vardim), inv(_getnobs(x, y, vardim) - Int(corrected)))
327327

328328
# covm (with provided mean)
329329

330-
covm(x::AbstractVector, xmean, corrected::Bool=true) =
331-
covzm(x .- xmean, corrected)
332-
covm(x::AbstractMatrix, xmean, vardim::Int=1, corrected::Bool=true) =
333-
covzm(x .- xmean, vardim, corrected)
334-
covm(x::AbstractVector, xmean, y::AbstractVector, ymean, corrected::Bool=true) =
335-
covzm(x .- xmean, y .- ymean, corrected)
336-
covm(x::AbstractVecOrMat, xmean, y::AbstractVecOrMat, ymean, vardim::Int=1, corrected::Bool=true) =
337-
covzm(x .- xmean, y .- ymean, vardim, corrected)
330+
covm(x::AbstractVector, xmean; corrected::Bool=true) =
331+
covzm(x .- xmean; corrected=corrected)
332+
covm(x::AbstractMatrix, xmean, vardim::Int=1; corrected::Bool=true) =
333+
covzm(x .- xmean, vardim; corrected=corrected)
334+
covm(x::AbstractVector, xmean, y::AbstractVector, ymean; corrected::Bool=true) =
335+
covzm(x .- xmean, y .- ymean; corrected=corrected)
336+
covm(x::AbstractVecOrMat, xmean, y::AbstractVecOrMat, ymean, vardim::Int=1; corrected::Bool=true) =
337+
covzm(x .- xmean, y .- ymean, vardim; corrected=corrected)
338338

339339
# cov (API)
340340
"""
341-
cov(x[, corrected=true])
341+
cov(x::AbstractVector; corrected::Bool=true)
342342
343343
Compute the variance of the vector `x`. If `corrected` is `true` (the default) then the sum
344344
is scaled with `n-1`, whereas the sum is scaled with `n` if `corrected` is `false` where `n = length(x)`.
345345
"""
346-
cov(x::AbstractVector, corrected::Bool) = covm(x, Base.mean(x), corrected)
347-
# This ugly hack is necessary to make the method below considered more specific than the deprecated method. When the old keyword version has been completely deprecated, these two methods can be merged
348-
cov(x::AbstractVector) = covm(x, Base.mean(x), true)
346+
cov(x::AbstractVector; corrected::Bool=true) = covm(x, Base.mean(x); corrected=corrected)
349347

350348
"""
351-
cov(X[, vardim=1, corrected=true])
349+
cov(X::AbstractMatrix[, vardim::Int=1]; corrected::Bool=true)
352350
353351
Compute the covariance matrix of the matrix `X` along the dimension `vardim`. If `corrected`
354352
is `true` (the default) then the sum is scaled with `n-1`, whereas the sum is scaled with `n`
355353
if `corrected` is `false` where `n = size(X, vardim)`.
356354
"""
357-
cov(X::AbstractMatrix, vardim::Int, corrected::Bool=true) =
358-
covm(X, _vmean(X, vardim), vardim, corrected)
359-
# This ugly hack is necessary to make the method below considered more specific than the deprecated method. When the old keyword version has been completely deprecated, these two methods can be merged
360-
cov(X::AbstractMatrix) = cov(X, 1, true)
355+
cov(X::AbstractMatrix, vardim::Int=1; corrected::Bool=true) =
356+
covm(X, _vmean(X, vardim), vardim; corrected=corrected)
361357

362358
"""
363-
cov(x, y[, corrected=true])
359+
cov(x::AbstractVector, y::AbstractVector; corrected::Bool=true)
364360
365361
Compute the covariance between the vectors `x` and `y`. If `corrected` is `true` (the
366362
default), computes ``\\frac{1}{n-1}\\sum_{i=1}^n (x_i-\\bar x) (y_i-\\bar y)^*`` where
367363
``*`` denotes the complex conjugate and `n = length(x) = length(y)`. If `corrected` is
368364
`false`, computes ``\frac{1}{n}\sum_{i=1}^n (x_i-\\bar x) (y_i-\\bar y)^*``.
369365
"""
370-
cov(x::AbstractVector, y::AbstractVector, corrected::Bool) =
371-
covm(x, Base.mean(x), y, Base.mean(y), corrected)
372-
# This ugly hack is necessary to make the method below considered more specific than the deprecated method. When the old keyword version has been completely deprecated, these two methods can be merged
373-
cov(x::AbstractVector, y::AbstractVector) =
374-
covm(x, Base.mean(x), y, Base.mean(y), true)
366+
cov(x::AbstractVector, y::AbstractVector; corrected::Bool=true) =
367+
covm(x, Base.mean(x), y, Base.mean(y); corrected=corrected)
375368

376369
"""
377-
cov(X, Y[, vardim=1, corrected=true])
370+
cov(X::AbstractVecOrMat, Y::AbstractVecOrMat[, vardim::Int=1]; corrected::Bool=true)
378371
379372
Compute the covariance between the vectors or matrices `X` and `Y` along the dimension
380373
`vardim`. If `corrected` is `true` (the default) then the sum is scaled with `n-1`, whereas
381374
the sum is scaled with `n` if `corrected` is `false` where `n = size(X, vardim) = size(Y, vardim)`.
382375
"""
383-
cov(X::AbstractVecOrMat, Y::AbstractVecOrMat, vardim::Int, corrected::Bool=true) =
384-
covm(X, _vmean(X, vardim), Y, _vmean(Y, vardim), vardim, corrected)
385-
# This ugly hack is necessary to make the method below considered more specific than the deprecated method. When the old keyword version has been completely deprecated, these methods can be merged
386-
cov(x::AbstractVector, Y::AbstractMatrix) = cov(x, Y, 1, true)
387-
cov(X::AbstractMatrix, y::AbstractVector) = cov(X, y, 1, true)
388-
cov(X::AbstractMatrix, Y::AbstractMatrix) = cov(X, Y, 1, true)
376+
cov(X::AbstractVecOrMat, Y::AbstractVecOrMat, vardim::Int=1; corrected::Bool=true) =
377+
covm(X, _vmean(X, vardim), Y, _vmean(Y, vardim), vardim; corrected=corrected)
389378

390379
##### correlation #####
391380

test/statistics.jl

+18-18
Original file line numberDiff line numberDiff line change
@@ -186,50 +186,50 @@ for vd in [1, 2], zm in [true, false], cr in [true, false]
186186
y1 = vec(Y[1,:])
187187
end
188188

189-
c = zm ? Base.covm(x1, 0, cr) :
190-
cov(x1, cr)
189+
c = zm ? Base.covm(x1, 0, corrected=cr) :
190+
cov(x1, corrected=cr)
191191
@test isa(c, Float64)
192192
@test c Cxx[1,1]
193-
@inferred cov(x1, cr)
193+
@inferred cov(x1, corrected=cr)
194194

195195
@test cov(X) == Base.covm(X, mean(X, 1))
196-
C = zm ? Base.covm(X, 0, vd, cr) :
197-
cov(X, vd, cr)
196+
C = zm ? Base.covm(X, 0, vd, corrected=cr) :
197+
cov(X, vd, corrected=cr)
198198
@test size(C) == (k, k)
199199
@test C Cxx
200-
@inferred cov(X, vd, cr)
200+
@inferred cov(X, vd, corrected=cr)
201201

202202
@test cov(x1, y1) == Base.covm(x1, mean(x1), y1, mean(y1))
203-
c = zm ? Base.covm(x1, 0, y1, 0, cr) :
204-
cov(x1, y1, cr)
203+
c = zm ? Base.covm(x1, 0, y1, 0, corrected=cr) :
204+
cov(x1, y1, corrected=cr)
205205
@test isa(c, Float64)
206206
@test c Cxy[1,1]
207-
@inferred cov(x1, y1, cr)
207+
@inferred cov(x1, y1, corrected=cr)
208208

209209
if vd == 1
210210
@test cov(x1, Y) == Base.covm(x1, mean(x1), Y, mean(Y, 1))
211211
end
212-
C = zm ? Base.covm(x1, 0, Y, 0, vd, cr) :
213-
cov(x1, Y, vd, cr)
212+
C = zm ? Base.covm(x1, 0, Y, 0, vd, corrected=cr) :
213+
cov(x1, Y, vd, corrected=cr)
214214
@test size(C) == (1, k)
215215
@test vec(C) Cxy[1,:]
216-
@inferred cov(x1, Y, vd, cr)
216+
@inferred cov(x1, Y, vd, corrected=cr)
217217

218218
if vd == 1
219219
@test cov(X, y1) == Base.covm(X, mean(X, 1), y1, mean(y1))
220220
end
221-
C = zm ? Base.covm(X, 0, y1, 0, vd, cr) :
222-
cov(X, y1, vd, cr)
221+
C = zm ? Base.covm(X, 0, y1, 0, vd, corrected=cr) :
222+
cov(X, y1, vd, corrected=cr)
223223
@test size(C) == (k, 1)
224224
@test vec(C) Cxy[:,1]
225-
@inferred cov(X, y1, vd, cr)
225+
@inferred cov(X, y1, vd, corrected=cr)
226226

227227
@test cov(X, Y) == Base.covm(X, mean(X, 1), Y, mean(Y, 1))
228-
C = zm ? Base.covm(X, 0, Y, 0, vd, cr) :
229-
cov(X, Y, vd, cr)
228+
C = zm ? Base.covm(X, 0, Y, 0, vd, corrected=cr) :
229+
cov(X, Y, vd, corrected=cr)
230230
@test size(C) == (k, k)
231231
@test C Cxy
232-
@inferred cov(X, Y, vd, cr)
232+
@inferred cov(X, Y, vd, corrected=cr)
233233
end
234234

235235
# test correlation

0 commit comments

Comments
 (0)