snanvariancepn.c (5868B)
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/snanvariancepn.h" 20 #include <stdint.h> 21 22 /** 23 * Computes the sum of single-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 snansumpw( const int64_t N, double *W, const float *X, const int64_t stride ) { 41 int64_t ix; 42 float *xp1; 43 float *xp2; 44 float sum; 45 int64_t M; 46 int64_t n; 47 int64_t i; 48 float s0; 49 float s1; 50 float s2; 51 float s3; 52 float s4; 53 float s5; 54 float s6; 55 float s7; 56 float 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.0f; 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 ] += (double)sum; 76 W[ 1 ] += (double)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.0f; 83 s1 = 0.0f; 84 s2 = 0.0f; 85 s3 = 0.0f; 86 s4 = 0.0f; 87 s5 = 0.0f; 88 s6 = 0.0f; 89 s7 = 0.0f; 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 ] += (double)sum; 156 W[ 1 ] += (double)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 = (float *)X + ( (n-N)*stride ); 164 xp2 = (float *)X; 165 } else { 166 xp1 = (float *)X; 167 xp2 = (float *)X + ( n*stride ); 168 } 169 snansumpw( n, W, xp1, stride ); 170 snansumpw( N-n, W, xp2, stride ); 171 } 172 173 /** 174 * Computes the variance of a single-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 float stdlib_strided_snanvariancepn( const int64_t N, const float correction, const float *X, const int64_t stride ) { 192 double W[] = { 0.0, 0.0 }; 193 int64_t ix; 194 int64_t i; 195 double nc; 196 double dM; 197 double n; 198 float M2; 199 float mu; 200 float M; 201 float d; 202 float v; 203 204 if ( N <= 0 ) { 205 return 0.0f / 0.0f; // NaN 206 } 207 if ( N == 1 || stride == 0 ) { 208 v = X[ 0 ]; 209 if ( v == v && (double)N-(double)correction > 0.0 ) { 210 return 0.0f; 211 } 212 return 0.0f / 0.0f; // NaN 213 } 214 // Compute an estimate for the mean... 215 snansumpw( N, W, X, stride ); 216 n = W[ 1 ]; 217 nc = n - (double)correction; 218 if ( nc <= 0.0 ) { 219 return 0.0f / 0.0f; // NaN 220 } 221 if ( stride < 0 ) { 222 ix = (1-N) * stride; 223 } else { 224 ix = 0; 225 } 226 mu = (float)( W[ 0 ] / n ); 227 228 // Compute the variance... 229 M2 = 0.0f; 230 M = 0.0f; 231 for ( i = 0; i < N; i++ ) { 232 v = X[ ix ]; 233 if ( v == v ) { 234 d = v - mu; 235 M2 += d * d; 236 M += d; 237 n += 1; 238 } 239 ix += stride; 240 } 241 dM = (double)M; 242 return (float)((double)M2/nc) - ( (float)(dM/n) * (float)(dM/nc) ); 243 }