-
Notifications
You must be signed in to change notification settings - Fork 40
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
Make covariance and correlation work for any iterators #30
Changes from all commits
c13786d
dd9c04f
38a0706
174040c
131ae70
ff55dac
55fc30a
ddee4e2
056af67
1a15b52
2aa0187
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -388,6 +388,8 @@ function sqrt!(A::AbstractArray) | |
A | ||
end | ||
|
||
sqrt!(x::Number) = sqrt(x) | ||
|
||
stdm(A::AbstractArray, m; corrected::Bool=true) = | ||
sqrt.(varm(A, m; corrected=corrected)) | ||
|
||
|
@@ -479,13 +481,19 @@ end | |
_vmean(x::AbstractVector, vardim::Int) = mean(x) | ||
_vmean(x::AbstractMatrix, vardim::Int) = mean(x, dims=vardim) | ||
|
||
# core functions | ||
_abs2(x::Number) = abs2(x) | ||
_abs2(x) = x*x' | ||
|
||
_conjmul(x::Number, y::Number) = x * conj(y) | ||
_conjmul(x, y) = x * _conj(y)' | ||
|
||
unscaled_covzm(x::AbstractVector{<:Number}) = sum(abs2, x) | ||
unscaled_covzm(x::AbstractVector) = sum(t -> t*t', x) | ||
# core functions | ||
unscaled_covzm(itr) = sum(_abs2, itr) | ||
unscaled_covzm(x::AbstractVector) = sum(_abs2, x) | ||
unscaled_covzm(x::AbstractMatrix, vardim::Int) = (vardim == 1 ? _conj(x'x) : x * x') | ||
|
||
unscaled_covzm(x::AbstractVector, y::AbstractVector) = sum(conj(y[i])*x[i] for i in eachindex(y, x)) | ||
unscaled_covzm(x, y) = sum(t -> _conjmul(first(t), last(t)), zip(x, y)) | ||
unscaled_covzm(x::AbstractVector, y::AbstractVector) = sum(_conjmul(x[i], y[i]) for i in eachindex(y, x)) | ||
unscaled_covzm(x::AbstractVector, y::AbstractMatrix, vardim::Int) = | ||
(vardim == 1 ? *(transpose(x), _conj(y)) : *(transpose(x), transpose(_conj(y)))) | ||
unscaled_covzm(x::AbstractMatrix, y::AbstractVector, vardim::Int) = | ||
|
@@ -494,7 +502,7 @@ unscaled_covzm(x::AbstractMatrix, y::AbstractMatrix, vardim::Int) = | |
(vardim == 1 ? *(transpose(x), _conj(y)) : *(x, adjoint(y))) | ||
|
||
# covzm (with centered data) | ||
|
||
covzm(itr::Any; corrected::Bool=true) = covzm(collect(itr); corrected = corrected) | ||
covzm(x::AbstractVector; corrected::Bool=true) = unscaled_covzm(x) / (length(x) - Int(corrected)) | ||
function covzm(x::AbstractMatrix, vardim::Int=1; corrected::Bool=true) | ||
C = unscaled_covzm(x, vardim) | ||
|
@@ -504,6 +512,7 @@ function covzm(x::AbstractMatrix, vardim::Int=1; corrected::Bool=true) | |
A .= A .* b | ||
return A | ||
end | ||
covzm(x::Any, y::Any; corrected::Bool=true) = covzm(collect(x), collect(y); corrected = corrected) | ||
covzm(x::AbstractVector, y::AbstractVector; corrected::Bool=true) = | ||
unscaled_covzm(x, y) / (length(x) - Int(corrected)) | ||
function covzm(x::AbstractVecOrMat, y::AbstractVecOrMat, vardim::Int=1; corrected::Bool=true) | ||
|
@@ -518,20 +527,37 @@ end | |
# covm (with provided mean) | ||
## Use map(t -> t - xmean, x) instead of x .- xmean to allow for Vector{Vector} | ||
## which can't be handled by broadcast | ||
covm(itr::Any, itrmean; corrected::Bool=true) = | ||
covm(map(t -> t - itrmean, x); corrected = corrected) | ||
covm(x::AbstractVector, xmean; corrected::Bool=true) = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method is identical to the previous one so it's no longer needed. |
||
covzm(map(t -> t - xmean, x); corrected=corrected) | ||
covm(x::AbstractMatrix, xmean, vardim::Int=1; corrected::Bool=true) = | ||
covzm(x .- xmean, vardim; corrected=corrected) | ||
covm(x::Any, xmean, y::Any, ymean; corrected::Bool=true) = | ||
covzm(map(t -> t - xmean, x), map(t -> t - ymean, y); corrected=corrected) | ||
covm(x::AbstractVector, xmean, y::AbstractVector, ymean; corrected::Bool=true) = | ||
covzm(map(t -> t - xmean, x), map(t -> t - ymean, y); corrected=corrected) | ||
covm(x::AbstractVecOrMat, xmean, y::AbstractVecOrMat, ymean, vardim::Int=1; corrected::Bool=true) = | ||
covzm(x .- xmean, y .- ymean, vardim; corrected=corrected) | ||
|
||
# cov (API) | ||
""" | ||
cov(x::Any; corrected::Bool=true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better adapt the existing docstring (and method) to only mention iterators, since vectors are just a special case. Same for others. |
||
|
||
Compute the variance of the iterator `x`. If `corrected` is `true` (the default) then the sum | ||
is scaled with `n-1`, whereas the sum is scaled with `n` if `corrected` is `false` where `n` | ||
is the number of elements in the iterator, which is not necessarily known. | ||
""" | ||
function cov(x::Any, corrected::Bool=true) | ||
cx = collect(x) | ||
covm(cx, mean(cx); corrected=corrected) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is going to make another copy of |
||
end | ||
|
||
""" | ||
cov(x::AbstractVector; corrected::Bool=true) | ||
|
||
Compute the variance of the vector `x`. If `corrected` is `true` (the default) then the sum | ||
Compute the variance of the vector `x`. If `x` is a vector of vectors, returns the estimated | ||
variance-covariance matrix of elements in `x`. If `corrected` is `true` (the default) then the sum | ||
is scaled with `n-1`, whereas the sum is scaled with `n` if `corrected` is `false` where `n = length(x)`. | ||
""" | ||
cov(x::AbstractVector; corrected::Bool=true) = covm(x, mean(x); corrected=corrected) | ||
|
@@ -546,6 +572,22 @@ if `corrected` is `false` where `n = size(X, dims)`. | |
cov(X::AbstractMatrix; dims::Int=1, corrected::Bool=true) = | ||
covm(X, _vmean(X, dims), dims; corrected=corrected) | ||
|
||
""" | ||
cov(x::Any, y::Any; corrected::Bool=true) | ||
|
||
Compute the covariance between the iterators `x` and `y`. If `corrected` is `true` (the | ||
default), computes ``\\frac{1}{n-1}\\sum_{i=1}^n (x_i-\\bar x) (y_i-\\bar y)^*`` where | ||
``*`` denotes the complex conjugate and `n` is the number of elements in `x` which must equal | ||
the number of elements in `y`. If `x` and `y` are both vectors of vectors, computes the analagous | ||
estimator for the covariance matrix for `xi` and `yi. If `corrected` is `false`, computes | ||
``\\frac{1}{n}\\sum_{i=1}^n (x_i-\\bar x) (y_i-\\bar y)^*``. | ||
""" | ||
function cov(x::Any, y::Any; corrected::Bool=true) | ||
cx = collect(x) | ||
cy = collect(y) | ||
covm(cx, mean(cx), cy, mean(cy); corrected=corrected) | ||
end | ||
|
||
""" | ||
cov(x::AbstractVector, y::AbstractVector; corrected::Bool=true) | ||
|
||
|
@@ -628,10 +670,19 @@ function cov2cor!(C::AbstractMatrix, xsd::AbstractArray, ysd::AbstractArray) | |
end | ||
return C | ||
end | ||
|
||
function cov2cor!(xy::Number, xsd::Number, ysd::Number) | ||
xx = abs2(xsd) | ||
yy = abs2(ysd) | ||
clampcor(xy / max(xx, yy) / sqrt(min(xx, yy) / max(xx, yy))) | ||
end | ||
# corzm (non-exported, with centered data) | ||
|
||
corzm(x::AbstractVector{T}) where {T} = one(real(T)) | ||
corzm(itr) = corzm(collect(itr)) | ||
corzm(x::AbstractVector{T}) where {T<:Number} = one(real(T)) | ||
function corzm(x::AbstractVector{T}) where {T} | ||
c = unscaled_covzm(x) | ||
return cov2cor!(c, collect(sqrt(c[i,i]) for i in 1:min(size(c)...))) | ||
end | ||
function corzm(x::AbstractMatrix, vardim::Int=1) | ||
c = unscaled_covzm(x, vardim) | ||
return cov2cor!(c, collect(sqrt(c[i,i]) for i in 1:min(size(c)...))) | ||
|
@@ -644,9 +695,16 @@ corzm(x::AbstractMatrix, y::AbstractMatrix, vardim::Int=1) = | |
cov2cor!(unscaled_covzm(x, y, vardim), sqrt!(sum(abs2, x, dims=vardim)), sqrt!(sum(abs2, y, dims=vardim))) | ||
|
||
# corm | ||
|
||
corm(x::AbstractVector{T}, xmean) where {T} = one(real(T)) | ||
corm(itr::Any, itrmean) = corm(collect(itr), itrmean) | ||
corm(x::AbstractVector{<:Number}, xmean) = one(real(eltype(x))) | ||
function corm(x::AbstractVector, xmean) | ||
c = sum(t -> _abs2(t - xmean), x) | ||
return cov2cor!(c, collect(sqrt(c[i,i]) for i in 1:min(size(c)...))) | ||
end | ||
corm(x::AbstractMatrix, xmean, vardim::Int=1) = corzm(x .- xmean, vardim) | ||
corm(x::Any, mx, y::Any, my) = corm(collect(x), mx, collect(y), my) | ||
_one(x) = one(x) | ||
_one(x::AbstractArray) = fill(one(x[1]), size(x)) | ||
function corm(x::AbstractVector, mx, y::AbstractVector, my) | ||
require_one_based_indexing(x, y) | ||
n = length(x) | ||
|
@@ -655,31 +713,51 @@ function corm(x::AbstractVector, mx, y::AbstractVector, my) | |
|
||
@inbounds begin | ||
# Initialize the accumulators | ||
xx = zero(sqrt(abs2(one(x[1])))) | ||
yy = zero(sqrt(abs2(one(y[1])))) | ||
xy = zero(x[1] * y[1]') | ||
xx = zero(sqrt!(_abs2(_one(x[1])))) | ||
yy = zero(sqrt!(_abs2(_one(y[1])))) | ||
xy = zero(_conjmul(x[1], y[1])) | ||
|
||
@simd for i in eachindex(x, y) | ||
xi = x[i] - mx | ||
yi = y[i] - my | ||
xx += abs2(xi) | ||
yy += abs2(yi) | ||
xy += xi * yi' | ||
xx += _abs2(xi) | ||
yy += _abs2(yi) | ||
xy += _conjmul(xi, yi) | ||
end | ||
end | ||
return clampcor(xy / max(xx, yy) / sqrt(min(xx, yy) / max(xx, yy))) | ||
|
||
@show xx | ||
@show yy | ||
@show xy | ||
|
||
return cov2cor!(xy, xx, yy) | ||
end | ||
|
||
corm(x::AbstractVecOrMat, xmean, y::AbstractVecOrMat, ymean, vardim::Int=1) = | ||
corzm(x .- xmean, y .- ymean, vardim) | ||
|
||
# cor | ||
""" | ||
cor(x::AbstractVector) | ||
cor(itr::Any) | ||
|
||
Return the number one if `itr` iterates through scalars. Returns the correlation between | ||
elements of the iterator otherwise. | ||
""" | ||
cor(itr::Any) = cor(collect(itr)) | ||
|
||
""" | ||
cor(x::AbstractVector{<:Number}) | ||
|
||
Return the number one. | ||
""" | ||
cor(x::AbstractVector) = one(real(eltype(x))) | ||
cor(x::AbstractVector{<:Number}) = one(real(eltype(x))) | ||
|
||
""" | ||
cor(x::AbstractVector) | ||
|
||
Return the Pearson correlation matrix between elements in `x`. | ||
""" | ||
cor(x::AbstractVector) = corm(x, mean(x)) | ||
|
||
""" | ||
cor(X::AbstractMatrix; dims::Int=1) | ||
|
@@ -688,6 +766,18 @@ Compute the Pearson correlation matrix of the matrix `X` along the dimension `di | |
""" | ||
cor(X::AbstractMatrix; dims::Int=1) = corm(X, _vmean(X, dims), dims) | ||
|
||
""" | ||
cor(x::Any, y::Any) | ||
|
||
Compute the Pearson correlation between the iterators `x` and `y`. | ||
""" | ||
function cor(x::Any, y::Any) | ||
cx = collect(x) | ||
cy = collect(y) | ||
|
||
corm(cx, mean(cx), cy, mean(cy)) | ||
end | ||
|
||
""" | ||
cor(x::AbstractVector, y::AbstractVector) | ||
|
||
|
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.
Is this still needed?