dnanvariancepn.c (5741B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2020 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 #include "stdlib/stats/base/dnanvariancepn.h" 20 #include <stdint.h> 21 22 /** 23 * Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using pairwise summation. 24 * 25 * ## Method 26 * 27 * - This implementation uses pairwise summation, which accrues rounding error `O(log2 N)` instead of `O(N)`. The recursion depth is also `O(log2 N)`. 28 * 29 * ## References 30 * 31 * - Higham, Nicholas J. 1993. "The Accuracy of Floating Point Summation." _SIAM Journal on Scientific Computing_ 14 (4): 783–99. doi:[10.1137/0914050](https://doi.org/10.1137/0914050). 32 * 33 * @private 34 * @param N number of indexed elements 35 * @param W two-element output array 36 * @param X input array 37 * @param stride stride length 38 * @return output value 39 */ 40 static void dnansumpw( const int64_t N, double *W, const double *X, const int64_t stride ) { 41 double *xp1; 42 double *xp2; 43 double sum; 44 int64_t ix; 45 int64_t M; 46 int64_t n; 47 int64_t i; 48 double s0; 49 double s1; 50 double s2; 51 double s3; 52 double s4; 53 double s5; 54 double s6; 55 double s7; 56 double v; 57 58 if ( stride < 0 ) { 59 ix = (1-N) * stride; 60 } else { 61 ix = 0; 62 } 63 if ( N < 8 ) { 64 // Use simple summation... 65 sum = 0.0; 66 n = 0; 67 for ( i = 0; i < N; i++ ) { 68 v = X[ ix ]; 69 if ( v == v ) { 70 sum += X[ ix ]; 71 n += 1; 72 } 73 ix += stride; 74 } 75 W[ 0 ] += sum; 76 W[ 1 ] += n; 77 return; 78 } 79 // Blocksize for pairwise summation: 128 (NOTE: decreasing the blocksize decreases rounding error as more pairs are summed, but also decreases performance. Because the inner loop is unrolled eight times, the blocksize is effectively `16`.) 80 if ( N <= 128 ) { 81 // Sum a block with 8 accumulators (by loop unrolling, we lower the effective blocksize to 16)... 82 s0 = 0.0; 83 s1 = 0.0; 84 s2 = 0.0; 85 s3 = 0.0; 86 s4 = 0.0; 87 s5 = 0.0; 88 s6 = 0.0; 89 s7 = 0.0; 90 n = 0; 91 92 M = N % 8; 93 for ( i = 0; i < N-M; i += 8 ) { 94 v = X[ ix ]; 95 if ( v == v ) { 96 s0 += v; 97 n += 1; 98 } 99 ix += stride; 100 v = X[ ix ]; 101 if ( v == v ) { 102 s1 += v; 103 n += 1; 104 } 105 ix += stride; 106 v = X[ ix ]; 107 if ( v == v ) { 108 s2 += v; 109 n += 1; 110 } 111 ix += stride; 112 v = X[ ix ]; 113 if ( v == v ) { 114 s3 += v; 115 n += 1; 116 } 117 ix += stride; 118 v = X[ ix ]; 119 if ( v == v ) { 120 s4 += v; 121 n += 1; 122 } 123 ix += stride; 124 v = X[ ix ]; 125 if ( v == v ) { 126 s5 += v; 127 n += 1; 128 } 129 ix += stride; 130 v = X[ ix ]; 131 if ( v == v ) { 132 s6 += v; 133 n += 1; 134 } 135 ix += stride; 136 v = X[ ix ]; 137 if ( v == v ) { 138 s7 += v; 139 n += 1; 140 } 141 ix += stride; 142 } 143 // Pairwise sum the accumulators: 144 sum = ((s0+s1) + (s2+s3)) + ((s4+s5) + (s6+s7)); 145 146 // Clean-up loop... 147 for (; i < N; i++ ) { 148 v = X[ ix ]; 149 if ( v == v ) { 150 sum += X[ ix ]; 151 n += 1; 152 } 153 ix += stride; 154 } 155 W[ 0 ] += sum; 156 W[ 1 ] += n; 157 return; 158 } 159 // Recurse by dividing by two, but avoiding non-multiples of unroll factor... 160 n = N / 2; 161 n -= n % 8; 162 if ( stride < 0 ) { 163 xp1 = (double *)X + ( (n-N)*stride ); 164 xp2 = (double *)X; 165 } else { 166 xp1 = (double *)X; 167 xp2 = (double *)X + ( n*stride ); 168 } 169 dnansumpw( n, W, xp1, stride ); 170 dnansumpw( N-n, W, xp2, stride ); 171 } 172 173 /** 174 * Computes the variance of a double-precision floating-point strided array ignoring `NaN` values and using a two-pass algorithm. 175 * 176 * ## Method 177 * 178 * - This implementation uses a two-pass approach, as suggested by Neely (1966). 179 * 180 * ## References 181 * 182 * - Neely, Peter M. 1966. "Comparison of Several Algorithms for Computation of Means, Standard Deviations and Correlation Coefficients." _Communications of the ACM_ 9 (7). Association for Computing Machinery: 496–99. doi:[10.1145/365719.365958](https://doi.org/10.1145/365719.365958). 183 * - Schubert, Erich, and Michael Gertz. 2018. "Numerically Stable Parallel Computation of (Co-)Variance." In _Proceedings of the 30th International Conference on Scientific and Statistical Database Management_. New York, NY, USA: Association for Computing Machinery. doi:[10.1145/3221269.3223036](https://doi.org/10.1145/3221269.3223036). 184 * 185 * @param N number of indexed elements 186 * @param correction degrees of freedom adjustment 187 * @param X input array 188 * @param stride stride length 189 * @return output value 190 */ 191 double stdlib_strided_dnanvariancepn( const int64_t N, const double correction, const double *X, const int64_t stride ) { 192 double W[] = { 0.0, 0.0 }; 193 int64_t ix; 194 int64_t i; 195 double mu; 196 double M2; 197 double nc; 198 double M; 199 double n; 200 double d; 201 double v; 202 203 if ( N <= 0 ) { 204 return 0.0 / 0.0; // NaN 205 } 206 if ( N == 1 || stride == 0 ) { 207 v = X[ 0 ]; 208 if ( v == v && (double)N-correction > 0.0 ) { 209 return 0.0; 210 } 211 return 0.0 / 0.0; // NaN 212 } 213 // Compute an estimate for the mean... 214 dnansumpw( N, W, X, stride ); 215 n = W[ 1 ]; 216 nc = n - correction; 217 if ( nc <= 0.0 ) { 218 return 0.0 / 0.0; // NaN 219 } 220 if ( stride < 0 ) { 221 ix = (1-N) * stride; 222 } else { 223 ix = 0; 224 } 225 mu = W[ 0 ] / n; 226 227 // Compute the variance... 228 M2 = 0.0; 229 M = 0.0; 230 for ( i = 0; i < N; i++ ) { 231 v = X[ ix ]; 232 if ( v == v ) { 233 d = v - mu; 234 M2 += d * d; 235 M += d; 236 } 237 ix += stride; 238 } 239 return (M2/nc) - ((M/n)*(M/nc)); 240 }