-
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 3 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 | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -479,10 +479,13 @@ end | |||||||||||||
_vmean(x::AbstractVector, vardim::Int) = mean(x) | ||||||||||||||
_vmean(x::AbstractMatrix, vardim::Int) = mean(x, dims=vardim) | ||||||||||||||
|
||||||||||||||
_abs2(x::Number) = abs2(x) | ||||||||||||||
_abs2(x) = x*x' | ||||||||||||||
|
||||||||||||||
# core functions | ||||||||||||||
|
||||||||||||||
unscaled_covzm(x::AbstractVector{<:Number}) = sum(abs2, x) | ||||||||||||||
unscaled_covzm(x::AbstractVector) = sum(t -> t*t', x) | ||||||||||||||
unscaled_covzm(x::AbstractVector{<:Number}) = sum(_abs2, x) | ||||||||||||||
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)) | ||||||||||||||
|
@@ -494,7 +497,25 @@ unscaled_covzm(x::AbstractMatrix, y::AbstractMatrix, vardim::Int) = | |||||||||||||
(vardim == 1 ? *(transpose(x), _conj(y)) : *(x, adjoint(y))) | ||||||||||||||
|
||||||||||||||
# covzm (with centered data) | ||||||||||||||
|
||||||||||||||
function covzm(itr::Any; corrected::Bool=true) | ||||||||||||||
y = iterate(itr) | ||||||||||||||
if y === nothing | ||||||||||||||
return Base.mapreduce_empty_iter(_abs2, Base.add_sum, itr, | ||||||||||||||
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.
Suggested change
But maybe it would be clearer to use the same approach for all methods (see suggestion for the two-argument case). |
||||||||||||||
Base.IteratorEltype(itr)) / 0 | ||||||||||||||
end | ||||||||||||||
count = 1 | ||||||||||||||
value, state = y | ||||||||||||||
f_value = _abs2(value) | ||||||||||||||
total = Base.reduce_first(Base.add_sum, f_value) | ||||||||||||||
y = iterate(itr, state) | ||||||||||||||
while y !== nothing | ||||||||||||||
value, state = y | ||||||||||||||
total += _abs2(value) | ||||||||||||||
count += 1 | ||||||||||||||
y = iterate(itr, state) | ||||||||||||||
end | ||||||||||||||
return total / (count - Int(corrected)) | ||||||||||||||
end | ||||||||||||||
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 +525,28 @@ function covzm(x::AbstractMatrix, vardim::Int=1; corrected::Bool=true) | |||||||||||||
A .= A .* b | ||||||||||||||
return A | ||||||||||||||
end | ||||||||||||||
function covzm(x::Any, y::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. Maybe this could just call 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. Have you tried this? 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. Just did.
Let me know what you think of that difference. It doesn't seem very important to me. The |
||||||||||||||
z = zip(x, y) | ||||||||||||||
z_itr = iterate(z) | ||||||||||||||
if z_itr === nothing | ||||||||||||||
# TODO: Understand how to improve this error. | ||||||||||||||
#return Base.mapreduce_empty_iter(t -> _conj(t[2])*t[1]', Base.add_sum, itr, | ||||||||||||||
# Base.IteratorEltype(x)) / 0 | ||||||||||||||
return NaN | ||||||||||||||
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. I don't think
Suggested change
|
||||||||||||||
end | ||||||||||||||
count = 1 | ||||||||||||||
value, state = z_itr | ||||||||||||||
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. Something like this would be more readable (if you adapt code elsewhere):
Suggested change
|
||||||||||||||
f_value = conj(value[2])*value[1]' | ||||||||||||||
total = Base.reduce_first(Base.add_sum, f_value) | ||||||||||||||
z_itr = iterate(z, state) | ||||||||||||||
while z_itr !== nothing | ||||||||||||||
value, state = z_itr | ||||||||||||||
total += conj(value[2])*value[1]' | ||||||||||||||
count += 1 | ||||||||||||||
z_itr = iterate(z, state) | ||||||||||||||
end | ||||||||||||||
return total ./ (count - Int(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.
Suggested change
|
||||||||||||||
end | ||||||||||||||
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) | ||||||||||||||
|
@@ -517,17 +560,75 @@ 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 | ||||||||||||||
## which can't be handled by broadcastz | ||||||||||||||
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.
Suggested change
(but this comment just be moved below) |
||||||||||||||
function covm(itr::Any, itrmean; corrected::Bool=true) | ||||||||||||||
y = iterate(itr) | ||||||||||||||
f = let itrmean = itrmean | ||||||||||||||
x -> _abs2(x-itrmean) | ||||||||||||||
end | ||||||||||||||
if y === nothing | ||||||||||||||
return Base.mapreduce_empty_iter(f, Base.add_sum, itr, | ||||||||||||||
Base.IteratorEltype(itr)) / 0 | ||||||||||||||
end | ||||||||||||||
count = 1 | ||||||||||||||
value, state = y | ||||||||||||||
f_value = f(value) | ||||||||||||||
total = Base.reduce_first(Base.add_sum, f_value) | ||||||||||||||
y = iterate(itr, state) | ||||||||||||||
while y !== nothing | ||||||||||||||
value, state = y | ||||||||||||||
total += f(value) | ||||||||||||||
count += 1 | ||||||||||||||
y = iterate(itr, state) | ||||||||||||||
end | ||||||||||||||
return total / (count - Int(corrected)) | ||||||||||||||
end | ||||||||||||||
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) | ||||||||||||||
function covm(x::Any, xmean, y::Any, ymean; corrected::Bool=true) | ||||||||||||||
z = zip(x, y) | ||||||||||||||
z_itr = iterate(z) | ||||||||||||||
if z_itr === nothing | ||||||||||||||
# TODO: Understand how to improve this error. | ||||||||||||||
#return Base.mapreduce_empty_iter(t -> _conj(t[2])*t[1]', Base.add_sum, itr, | ||||||||||||||
# Base.IteratorEltype(x)) / 0 | ||||||||||||||
return NaN | ||||||||||||||
end | ||||||||||||||
f = let xmean = xmean, ymean = ymean | ||||||||||||||
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. I'd just have 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. I would have to write |
||||||||||||||
t -> conj(t[2]-ymean)*(t[1]-xmean)' | ||||||||||||||
end | ||||||||||||||
count = 1 | ||||||||||||||
value, state = z_itr | ||||||||||||||
f_value = f(value) | ||||||||||||||
total = Base.reduce_first(Base.add_sum, f_value) | ||||||||||||||
z_itr = iterate(z, state) | ||||||||||||||
while z_itr !== nothing | ||||||||||||||
value, state = z_itr | ||||||||||||||
total += f(value) | ||||||||||||||
count += 1 | ||||||||||||||
z_itr = iterate(z, state) | ||||||||||||||
end | ||||||||||||||
return total ./ (count - Int(corrected)) | ||||||||||||||
end | ||||||||||||||
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 vector `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) | ||||||||||||||
covm(x, mean(x); corrected=corrected) | ||||||||||||||
end | ||||||||||||||
|
||||||||||||||
""" | ||||||||||||||
cov(x::AbstractVector; corrected::Bool=true) | ||||||||||||||
|
||||||||||||||
|
@@ -546,6 +647,18 @@ 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 `corrected` is `false`, computes ``\\frac{1}{n}\\sum_{i=1}^n | ||||||||||||||
(x_i-\\bar x) (y_i-\\bar y)^*``. | ||||||||||||||
""" | ||||||||||||||
cov(x::Any, y::Any; corrected::Bool=true) = | ||||||||||||||
covm(x, mean(x), y, mean(y); corrected=corrected) | ||||||||||||||
|
||||||||||||||
""" | ||||||||||||||
cov(x::AbstractVector, y::AbstractVector; corrected::Bool=true) | ||||||||||||||
|
||||||||||||||
|
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.
This method isn't needed if you dispatch in
_abs2
.