|
| 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 | +'use strict'; |
| 20 | + |
| 21 | +// MODULES // |
| 22 | + |
| 23 | +var tape = require( 'tape' ); |
| 24 | +var sqrt = require( '@stdlib/math/base/special/sqrt' ); |
| 25 | +var isnan = require( '@stdlib/math/base/assert/is-nan' ); |
| 26 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 27 | +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); |
| 28 | +var stdevwd = require( './../lib' ); |
| 29 | + |
| 30 | + |
| 31 | +// TESTS // |
| 32 | + |
| 33 | +tape( 'main export is a function', function test( t ) { |
| 34 | + t.ok( true, __filename ); |
| 35 | + t.strictEqual( typeof stdevwd, 'function', 'main export is a function' ); |
| 36 | + t.end(); |
| 37 | +}); |
| 38 | + |
| 39 | +tape( 'the function has an arity of 4', function test( t ) { |
| 40 | + t.strictEqual( stdevwd.length, 4, 'has expected arity' ); |
| 41 | + t.end(); |
| 42 | +}); |
| 43 | + |
| 44 | +tape( 'the function calculates the population standard deviation of a strided array', function test( t ) { |
| 45 | + var x; |
| 46 | + var v; |
| 47 | + |
| 48 | + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; |
| 49 | + v = stdevwd( x.length, 0, x, 1 ); |
| 50 | + t.strictEqual( v, sqrt( 53.5/x.length ), 'returns expected value' ); |
| 51 | + |
| 52 | + x = [ -4.0, -4.0 ]; |
| 53 | + v = stdevwd( x.length, 0, x, 1 ); |
| 54 | + t.strictEqual( v, 0.0, 'returns expected value' ); |
| 55 | + |
| 56 | + x = [ NaN, 4.0 ]; |
| 57 | + v = stdevwd( x.length, 0, x, 1 ); |
| 58 | + t.strictEqual( isnan( v ), true, 'returns expected value' ); |
| 59 | + |
| 60 | + t.end(); |
| 61 | +}); |
| 62 | + |
| 63 | +tape( 'the function calculates the population standard deviation of a strided array (accessors)', function test( t ) { |
| 64 | + var x; |
| 65 | + var v; |
| 66 | + |
| 67 | + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; |
| 68 | + v = stdevwd( x.length, 0, toAccessorArray( x ), 1 ); |
| 69 | + t.strictEqual( v, sqrt( 53.5/x.length ), 'returns expected value' ); |
| 70 | + |
| 71 | + x = [ -4.0, -4.0 ]; |
| 72 | + v = stdevwd( x.length, 0, toAccessorArray( x ), 1 ); |
| 73 | + t.strictEqual( v, 0.0, 'returns expected value' ); |
| 74 | + |
| 75 | + x = [ NaN, 4.0 ]; |
| 76 | + v = stdevwd( x.length, 0, toAccessorArray( x ), 1 ); |
| 77 | + t.strictEqual( isnan( v ), true, 'returns expected value' ); |
| 78 | + |
| 79 | + t.end(); |
| 80 | +}); |
| 81 | + |
| 82 | +tape( 'the function calculates the sample standard deviation of a strided array', function test( t ) { |
| 83 | + var x; |
| 84 | + var v; |
| 85 | + |
| 86 | + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; |
| 87 | + v = stdevwd( x.length, 1, x, 1 ); |
| 88 | + t.strictEqual( v, sqrt( 53.5/(x.length-1) ), 'returns expected value' ); |
| 89 | + |
| 90 | + x = [ -4.0, -4.0 ]; |
| 91 | + v = stdevwd( x.length, 1, x, 1 ); |
| 92 | + t.strictEqual( v, 0.0, 'returns expected value' ); |
| 93 | + |
| 94 | + x = [ NaN, 4.0 ]; |
| 95 | + v = stdevwd( x.length, 1, x, 1 ); |
| 96 | + t.strictEqual( isnan( v ), true, 'returns expected value' ); |
| 97 | + |
| 98 | + t.end(); |
| 99 | +}); |
| 100 | + |
| 101 | +tape( 'the function calculates the sample standard deviation of a strided array (accessors)', function test( t ) { |
| 102 | + var x; |
| 103 | + var v; |
| 104 | + |
| 105 | + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; |
| 106 | + v = stdevwd( x.length, 1, toAccessorArray( x ), 1 ); |
| 107 | + t.strictEqual( v, sqrt( 53.5/(x.length-1) ), 'returns expected value' ); |
| 108 | + |
| 109 | + x = [ -4.0, -4.0 ]; |
| 110 | + v = stdevwd( x.length, 1, toAccessorArray( x ), 1 ); |
| 111 | + t.strictEqual( v, 0.0, 'returns expected value' ); |
| 112 | + |
| 113 | + x = [ NaN, 4.0 ]; |
| 114 | + v = stdevwd( x.length, 1, toAccessorArray( x ), 1 ); |
| 115 | + t.strictEqual( isnan( v ), true, 'returns expected value' ); |
| 116 | + |
| 117 | + t.end(); |
| 118 | +}); |
| 119 | + |
| 120 | +tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `NaN`', function test( t ) { |
| 121 | + var x; |
| 122 | + var v; |
| 123 | + |
| 124 | + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; |
| 125 | + |
| 126 | + v = stdevwd( 0, 1, x, 1 ); |
| 127 | + t.strictEqual( isnan( v ), true, 'returns expected value' ); |
| 128 | + |
| 129 | + v = stdevwd( -1, 1, x, 1 ); |
| 130 | + t.strictEqual( isnan( v ), true, 'returns expected value' ); |
| 131 | + |
| 132 | + t.end(); |
| 133 | +}); |
| 134 | + |
| 135 | +tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `NaN` (accessors)', function test( t ) { |
| 136 | + var x; |
| 137 | + var v; |
| 138 | + |
| 139 | + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; |
| 140 | + |
| 141 | + v = stdevwd( 0, 1, toAccessorArray( x ), 1 ); |
| 142 | + t.strictEqual( isnan( v ), true, 'returns expected value' ); |
| 143 | + |
| 144 | + v = stdevwd( -1, 1, toAccessorArray( x ), 1 ); |
| 145 | + t.strictEqual( isnan( v ), true, 'returns expected value' ); |
| 146 | + |
| 147 | + t.end(); |
| 148 | +}); |
| 149 | + |
| 150 | +tape( 'if provided an `N` parameter equal to `1`, the function returns a population standard deviation of `0`', function test( t ) { |
| 151 | + var x; |
| 152 | + var v; |
| 153 | + |
| 154 | + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; |
| 155 | + |
| 156 | + v = stdevwd( 1, 0, x, 1 ); |
| 157 | + t.strictEqual( v, 0.0, 'returns expected value' ); |
| 158 | + |
| 159 | + t.end(); |
| 160 | +}); |
| 161 | + |
| 162 | +tape( 'if provided an `N` parameter equal to `1`, the function returns a population standard deviation of `0` (accessors)', function test( t ) { |
| 163 | + var x; |
| 164 | + var v; |
| 165 | + |
| 166 | + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; |
| 167 | + |
| 168 | + v = stdevwd( 1, 0, toAccessorArray( x ), 1 ); |
| 169 | + t.strictEqual( v, 0.0, 'returns expected value' ); |
| 170 | + |
| 171 | + t.end(); |
| 172 | +}); |
| 173 | + |
| 174 | +tape( 'if provided a `correction` parameter yielding `N-correction` less than or equal to `0`, the function returns `NaN`', function test( t ) { |
| 175 | + var x; |
| 176 | + var v; |
| 177 | + |
| 178 | + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; |
| 179 | + |
| 180 | + v = stdevwd( x.length, x.length, x, 1 ); |
| 181 | + t.strictEqual( isnan( v ), true, 'returns expected value' ); |
| 182 | + |
| 183 | + v = stdevwd( x.length, x.length+1, x, 1 ); |
| 184 | + t.strictEqual( isnan( v ), true, 'returns expected value' ); |
| 185 | + |
| 186 | + t.end(); |
| 187 | +}); |
| 188 | + |
| 189 | +tape( 'if provided a `correction` parameter yielding `N-correction` less than or equal to `0`, the function returns `NaN` (accessors)', function test( t ) { |
| 190 | + var x; |
| 191 | + var v; |
| 192 | + |
| 193 | + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; |
| 194 | + |
| 195 | + v = stdevwd( x.length, x.length, toAccessorArray( x ), 1 ); |
| 196 | + t.strictEqual( isnan( v ), true, 'returns expected value' ); |
| 197 | + |
| 198 | + v = stdevwd( x.length, x.length+1, toAccessorArray( x ), 1 ); |
| 199 | + t.strictEqual( isnan( v ), true, 'returns expected value' ); |
| 200 | + |
| 201 | + t.end(); |
| 202 | +}); |
| 203 | + |
| 204 | +tape( 'the function supports a `stride` parameter', function test( t ) { |
| 205 | + var x; |
| 206 | + var v; |
| 207 | + |
| 208 | + x = [ |
| 209 | + 1.0, // 0 |
| 210 | + 2.0, |
| 211 | + 2.0, // 1 |
| 212 | + -7.0, |
| 213 | + -2.0, // 2 |
| 214 | + 3.0, |
| 215 | + 4.0, // 3 |
| 216 | + 2.0 |
| 217 | + ]; |
| 218 | + |
| 219 | + v = stdevwd( 4, 1, x, 2 ); |
| 220 | + |
| 221 | + t.strictEqual( v, 2.5, 'returns expected value' ); |
| 222 | + t.end(); |
| 223 | +}); |
| 224 | + |
| 225 | +tape( 'the function supports a `stride` parameter (accessors)', function test( t ) { |
| 226 | + var x; |
| 227 | + var v; |
| 228 | + |
| 229 | + x = [ |
| 230 | + 1.0, // 0 |
| 231 | + 2.0, |
| 232 | + 2.0, // 1 |
| 233 | + -7.0, |
| 234 | + -2.0, // 2 |
| 235 | + 3.0, |
| 236 | + 4.0, // 3 |
| 237 | + 2.0 |
| 238 | + ]; |
| 239 | + |
| 240 | + v = stdevwd( 4, 1, toAccessorArray( x ), 2 ); |
| 241 | + |
| 242 | + t.strictEqual( v, 2.5, 'returns expected value' ); |
| 243 | + t.end(); |
| 244 | +}); |
| 245 | + |
| 246 | +tape( 'the function supports a negative `stride` parameter', function test( t ) { |
| 247 | + var x; |
| 248 | + var v; |
| 249 | + |
| 250 | + x = [ |
| 251 | + 1.0, // 3 |
| 252 | + 2.0, |
| 253 | + 2.0, // 2 |
| 254 | + -7.0, |
| 255 | + -2.0, // 1 |
| 256 | + 3.0, |
| 257 | + 4.0, // 0 |
| 258 | + 2.0 |
| 259 | + ]; |
| 260 | + |
| 261 | + v = stdevwd( 4, 1, x, -2 ); |
| 262 | + |
| 263 | + t.strictEqual( v, 2.5, 'returns expected value' ); |
| 264 | + t.end(); |
| 265 | +}); |
| 266 | + |
| 267 | +tape( 'the function supports a negative `stride` parameter (accessors)', function test( t ) { |
| 268 | + var x; |
| 269 | + var v; |
| 270 | + |
| 271 | + x = [ |
| 272 | + 1.0, // 3 |
| 273 | + 2.0, |
| 274 | + 2.0, // 2 |
| 275 | + -7.0, |
| 276 | + -2.0, // 1 |
| 277 | + 3.0, |
| 278 | + 4.0, // 0 |
| 279 | + 2.0 |
| 280 | + ]; |
| 281 | + |
| 282 | + v = stdevwd( 4, 1, toAccessorArray( x ), -2 ); |
| 283 | + |
| 284 | + t.strictEqual( v, 2.5, 'returns expected value' ); |
| 285 | + t.end(); |
| 286 | +}); |
| 287 | + |
| 288 | +tape( 'if provided a `stride` parameter equal to `0`, the function returns `0`', function test( t ) { |
| 289 | + var x; |
| 290 | + var v; |
| 291 | + |
| 292 | + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; |
| 293 | + |
| 294 | + v = stdevwd( x.length, 1, x, 0 ); |
| 295 | + t.strictEqual( v, 0.0, 'returns expected value' ); |
| 296 | + |
| 297 | + t.end(); |
| 298 | +}); |
| 299 | + |
| 300 | +tape( 'if provided a `stride` parameter equal to `0`, the function returns `0` (accessors)', function test( t ) { |
| 301 | + var x; |
| 302 | + var v; |
| 303 | + |
| 304 | + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; |
| 305 | + |
| 306 | + v = stdevwd( x.length, 1, toAccessorArray( x ), 0 ); |
| 307 | + t.strictEqual( v, 0.0, 'returns expected value' ); |
| 308 | + |
| 309 | + t.end(); |
| 310 | +}); |
| 311 | + |
| 312 | +tape( 'the function supports view offsets', function test( t ) { |
| 313 | + var x0; |
| 314 | + var x1; |
| 315 | + var v; |
| 316 | + |
| 317 | + x0 = new Float64Array([ |
| 318 | + 2.0, |
| 319 | + 1.0, // 0 |
| 320 | + 2.0, |
| 321 | + -2.0, // 1 |
| 322 | + -2.0, |
| 323 | + 2.0, // 2 |
| 324 | + 3.0, |
| 325 | + 4.0, // 3 |
| 326 | + 6.0 |
| 327 | + ]); |
| 328 | + |
| 329 | + x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 330 | + |
| 331 | + v = stdevwd( 4, 1, x1, 2 ); |
| 332 | + t.strictEqual( v, 2.5, 'returns expected value' ); |
| 333 | + |
| 334 | + t.end(); |
| 335 | +}); |
| 336 | + |
| 337 | +tape( 'the function supports view offsets (accessors)', function test( t ) { |
| 338 | + var x0; |
| 339 | + var x1; |
| 340 | + var v; |
| 341 | + |
| 342 | + x0 = new Float64Array([ |
| 343 | + 2.0, |
| 344 | + 1.0, // 0 |
| 345 | + 2.0, |
| 346 | + -2.0, // 1 |
| 347 | + -2.0, |
| 348 | + 2.0, // 2 |
| 349 | + 3.0, |
| 350 | + 4.0, // 3 |
| 351 | + 6.0 |
| 352 | + ]); |
| 353 | + |
| 354 | + x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 355 | + |
| 356 | + v = stdevwd( 4, 1, toAccessorArray( x1 ), 2 ); |
| 357 | + t.strictEqual( v, 2.5, 'returns expected value' ); |
| 358 | + |
| 359 | + t.end(); |
| 360 | +}); |
0 commit comments