Zero Correlation

TanayLabUtilities.ZeroCorrelation.zero_cor_between_matrix_columns Function
zero_cor_between_matrix_columns(
    matrix::AbstractMatrix{<:Real};
    result::Maybe{AbstractMatrix{<:AbstractFloat}} = nothing,
    scratch_matrix::Maybe{AbstractMatrix{<:AbstractFloat}} = nothing,
    scratch_column::Maybe{AbstractVector{<:AbstractFloat}} = nothing,
)::AbstractMatrix{<:AbstractFloat}

Same as cor , except that undefined cases return 0.0 instead of NaN. In addition, unlike cor , this doesn't set entries to one when correlating matrices and a constant vector is correlated with itself. It properly sets all correlations with such vectors to zero, even with themselves. This does zero allocations if the optional parameters are provided.

m = [1 2 3 4; 1 3 2 5];
zero_cor_between_matrix_columns(m)

# output

4×4 Matrix{Float64}:
 0.0   0.0   0.0   0.0
 0.0   1.0  -1.0   1.0
 0.0  -1.0   1.0  -1.0
 0.0   1.0  -1.0   1.0

TanayLabUtilities.ZeroCorrelation.zero_cor_between_matrices_columns Function
zero_cor_between_matrices_columns(
    left_matrix::AbstractMatrix{<:Real},
    right_matrix::AbstractMatrix{<:Real};
    result::Maybe{AbstractMatrix{<:AbstractFloat}} = nothing,
    left_scratch_matrix::Maybe{AbstractMatrix{<:AbstractFloat}} = nothing,
    right_scratch_matrix::Maybe{AbstractMatrix{<:AbstractFloat}} = nothing,
    left_scratch_row::Maybe{AbstractVector{<:AbstractFloat}} = nothing,
    right_scratch_row::Maybe{AbstractVector{<:AbstractFloat}} = nothing,
)::AbstractMatrix{<:AbstractFloat}

Same as cor , except that undefined cases return 0.0 instead of NaN. This does zero allocations if the optional parameters are provided.

m = [1 2 3 4; 1 3 2 5];
n = [1 2 3; 1 3 2];
zero_cor_between_matrices_columns(m, n)

# output

4×3 Matrix{Float64}:
 0.0   0.0   0.0
 0.0   1.0  -1.0
 0.0  -1.0   1.0
 0.0   1.0  -1.0

TanayLabUtilities.ZeroCorrelation.zero_cor_between_vector_and_matrix_columns Function
zero_cor_between_vector_and_matrix_columns(
    left_vector::AbstractVector{<:Real},
    right_matrix::AbstractMatrix{<:Real};
    result::Maybe{AbstractVector{<:AbstractFloat}} = nothing,
    result_scratch::Maybe{AbstractVector{<:AbstractFloat}} = nothing,
    left_scratch::Maybe{AbstractVector{<:AbstractFloat}} = nothing,
    right_scratch::Maybe{AbstractMatrix{<:AbstractFloat}} = nothing,
)::AbstractVector{<:AbstractFloat}

Similar to cor , except that undefined cases return 0.0 instead of NaN, and returns a vector. This does zero allocations if the optional parameters are provided.

using StatsBase

println(cor.(Ref([1.0, 1.0]), eachcol([1.0 2.0; 2.0 1.0])))
println(cor.(Ref([1.0, 2.0]), eachcol([1.0 2.0; 2.0 1.0])))

# output

[NaN, NaN]
[1.0, -1.0]

println(zero_cor_between_vector_and_matrix_columns([1.0, 1.0], [1.0 2.0; 2.0 1.0]))
println(zero_cor_between_vector_and_matrix_columns([1.0, 2.0], [1.0 2.0; 2.0 1.0]))

# output

[0.0, 0.0]
[0.9999999999999998, -0.9999999999999998]

TanayLabUtilities.ZeroCorrelation.zero_cor_between_vectors Function
zero_cor_between_vectors(left_vector::AbstractVector, right_vector::AbstractVector)::AbstractFloat

Same as cor , except that undefined cases return 0.0 instead of NaN.

using StatsBase

println(cor([1.0, 1.0], [1.0, 2.0]))
println(cor([1.0, 2.0], [1.0, 2.0]))

# output

NaN
1.0

println(zero_cor_between_vectors([1.0, 1.0], [1.0, 2.0]))
println(zero_cor_between_vectors([1.0, 2.0], [1.0, 2.0]))

# output

0.0
1.0

Index