@@ -7,7 +7,7 @@ Standard library module for basic statistics functionality.
77"""
88module Statistics
99
10- using LinearAlgebra, SparseArrays
10+ using LinearAlgebra
1111
1212using Base: has_offset_axes, require_one_based_indexing
1313
@@ -1095,94 +1095,9 @@ quantile(itr, p; sorted::Bool=false, alpha::Real=1.0, beta::Real=alpha) =
10951095quantile (v:: AbstractVector , p; sorted:: Bool = false , alpha:: Real = 1.0 , beta:: Real = alpha) =
10961096 quantile! (sorted ? v : Base. copymutable (v), p; sorted= sorted, alpha= alpha, beta= beta)
10971097
1098-
1099- # #### SparseArrays optimizations #####
1100-
1101- function cov (X:: SparseMatrixCSC ; dims:: Int = 1 , corrected:: Bool = true )
1102- vardim = dims
1103- a, b = size (X)
1104- n, p = vardim == 1 ? (a, b) : (b, a)
1105-
1106- # The covariance can be decomposed into two terms
1107- # 1/(n - 1) ∑ (x_i - x̄)*(x_i - x̄)' = 1/(n - 1) (∑ x_i*x_i' - n*x̄*x̄')
1108- # which can be evaluated via a sparse matrix-matrix product
1109-
1110- # Compute ∑ x_i*x_i' = X'X using sparse matrix-matrix product
1111- out = Matrix (unscaled_covzm (X, vardim))
1112-
1113- # Compute x̄
1114- x̄ᵀ = mean (X, dims= vardim)
1115-
1116- # Subtract n*x̄*x̄' from X'X
1117- @inbounds for j in 1 : p, i in 1 : p
1118- out[i,j] -= x̄ᵀ[i] * x̄ᵀ[j]' * n
1119- end
1120-
1121- # scale with the sample size n or the corrected sample size n - 1
1122- return rmul! (out, inv (n - corrected))
1123- end
1124-
1125- # This is the function that does the reduction underlying var/std
1126- function centralize_sumabs2! (R:: AbstractArray{S} , A:: SparseMatrixCSC{Tv,Ti} , means:: AbstractArray ) where {S,Tv,Ti}
1127- require_one_based_indexing (R, A, means)
1128- lsiz = Base. check_reducedims (R,A)
1129- for i in 1 : max (ndims (R), ndims (means))
1130- if axes (means, i) != axes (R, i)
1131- throw (DimensionMismatch (" dimension $i of `mean` should have indices $(axes (R, i)) , but got $(axes (means, i)) " ))
1132- end
1133- end
1134- isempty (R) || fill! (R, zero (S))
1135- isempty (A) && return R
1136-
1137- rowval = rowvals (A)
1138- nzval = nonzeros (A)
1139- m = size (A, 1 )
1140- n = size (A, 2 )
1141-
1142- if size (R, 1 ) == size (R, 2 ) == 1
1143- # Reduction along both columns and rows
1144- R[1 , 1 ] = centralize_sumabs2 (A, means[1 ])
1145- elseif size (R, 1 ) == 1
1146- # Reduction along rows
1147- @inbounds for col = 1 : n
1148- mu = means[col]
1149- r = convert (S, (m - length (nzrange (A, col)))* abs2 (mu))
1150- @simd for j = nzrange (A, col)
1151- r += abs2 (nzval[j] - mu)
1152- end
1153- R[1 , col] = r
1154- end
1155- elseif size (R, 2 ) == 1
1156- # Reduction along columns
1157- rownz = fill (convert (Ti, n), m)
1158- @inbounds for col = 1 : n
1159- @simd for j = nzrange (A, col)
1160- row = rowval[j]
1161- R[row, 1 ] += abs2 (nzval[j] - means[row])
1162- rownz[row] -= 1
1163- end
1164- end
1165- for i = 1 : m
1166- R[i, 1 ] += rownz[i]* abs2 (means[i])
1167- end
1168- else
1169- # Reduction along a dimension > 2
1170- @inbounds for col = 1 : n
1171- lastrow = 0
1172- @simd for j = nzrange (A, col)
1173- row = rowval[j]
1174- for i = lastrow+ 1 : row- 1
1175- R[i, col] = abs2 (means[i, col])
1176- end
1177- R[row, col] = abs2 (nzval[j] - means[row, col])
1178- lastrow = row
1179- end
1180- for i = lastrow+ 1 : m
1181- R[i, col] = abs2 (means[i, col])
1182- end
1183- end
1184- end
1185- return R
1098+ # If package extensions are not supported in this Julia version
1099+ if ! isdefined (Base, :get_extension )
1100+ include (" ../ext/SparseArraysExt.jl" )
11861101end
11871102
11881103end # module
0 commit comments