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