gonum.org/v1/gonum@v0.14.0/stat/moments_bench_test.go (about) 1 // Copyright ©2014 The Gonum Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // a set of benchmarks to evaluate the performance of the various 6 // moment statistics: Mean, Variance, StdDev, MeanVariance, MeanStdDev, 7 // Covariance, Correlation, Skew, ExKurtosis, Moment, MomentAbout, ... 8 // 9 // It tests both weighted and unweighted versions by using a slice of 10 // all ones. 11 12 package stat 13 14 import ( 15 "testing" 16 17 "golang.org/x/exp/rand" 18 ) 19 20 const ( 21 small = 10 22 medium = 1000 23 large = 100000 24 huge = 10000000 25 ) 26 27 // tests for unweighted versions 28 29 func RandomSlice(l int) []float64 { 30 s := make([]float64, l) 31 for i := range s { 32 s[i] = rand.Float64() 33 } 34 return s 35 } 36 37 func benchmarkMean(b *testing.B, s, wts []float64) { 38 b.ResetTimer() 39 for i := 0; i < b.N; i++ { 40 Mean(s, wts) 41 } 42 } 43 44 func BenchmarkMeanSmall(b *testing.B) { 45 s := RandomSlice(small) 46 benchmarkMean(b, s, nil) 47 } 48 49 func BenchmarkMeanMedium(b *testing.B) { 50 s := RandomSlice(medium) 51 benchmarkMean(b, s, nil) 52 } 53 54 func BenchmarkMeanLarge(b *testing.B) { 55 s := RandomSlice(large) 56 benchmarkMean(b, s, nil) 57 } 58 59 func BenchmarkMeanHuge(b *testing.B) { 60 s := RandomSlice(huge) 61 benchmarkMean(b, s, nil) 62 } 63 64 func BenchmarkMeanSmallWeighted(b *testing.B) { 65 s := RandomSlice(small) 66 wts := RandomSlice(small) 67 benchmarkMean(b, s, wts) 68 } 69 70 func BenchmarkMeanMediumWeighted(b *testing.B) { 71 s := RandomSlice(medium) 72 wts := RandomSlice(medium) 73 benchmarkMean(b, s, wts) 74 } 75 76 func BenchmarkMeanLargeWeighted(b *testing.B) { 77 s := RandomSlice(large) 78 wts := RandomSlice(large) 79 benchmarkMean(b, s, wts) 80 } 81 82 func BenchmarkMeanHugeWeighted(b *testing.B) { 83 s := RandomSlice(huge) 84 wts := RandomSlice(huge) 85 benchmarkMean(b, s, wts) 86 } 87 88 func benchmarkVariance(b *testing.B, s, wts []float64) { 89 b.ResetTimer() 90 for i := 0; i < b.N; i++ { 91 Variance(s, wts) 92 } 93 } 94 95 func BenchmarkVarianceSmall(b *testing.B) { 96 s := RandomSlice(small) 97 benchmarkVariance(b, s, nil) 98 } 99 100 func BenchmarkVarianceMedium(b *testing.B) { 101 s := RandomSlice(medium) 102 benchmarkVariance(b, s, nil) 103 } 104 105 func BenchmarkVarianceLarge(b *testing.B) { 106 s := RandomSlice(large) 107 benchmarkVariance(b, s, nil) 108 } 109 110 func BenchmarkVarianceHuge(b *testing.B) { 111 s := RandomSlice(huge) 112 benchmarkVariance(b, s, nil) 113 } 114 115 func BenchmarkVarianceSmallWeighted(b *testing.B) { 116 s := RandomSlice(small) 117 wts := RandomSlice(small) 118 benchmarkVariance(b, s, wts) 119 } 120 121 func BenchmarkVarianceMediumWeighted(b *testing.B) { 122 s := RandomSlice(medium) 123 wts := RandomSlice(medium) 124 benchmarkVariance(b, s, wts) 125 } 126 127 func BenchmarkVarianceLargeWeighted(b *testing.B) { 128 s := RandomSlice(large) 129 wts := RandomSlice(large) 130 benchmarkVariance(b, s, wts) 131 } 132 133 func BenchmarkVarianceHugeWeighted(b *testing.B) { 134 s := RandomSlice(huge) 135 wts := RandomSlice(huge) 136 benchmarkVariance(b, s, wts) 137 } 138 139 func benchmarkStdDev(b *testing.B, s, wts []float64) { 140 b.ResetTimer() 141 for i := 0; i < b.N; i++ { 142 StdDev(s, wts) 143 } 144 } 145 146 func BenchmarkStdDevSmall(b *testing.B) { 147 s := RandomSlice(small) 148 benchmarkStdDev(b, s, nil) 149 } 150 151 func BenchmarkStdDevMedium(b *testing.B) { 152 s := RandomSlice(medium) 153 benchmarkStdDev(b, s, nil) 154 } 155 156 func BenchmarkStdDevLarge(b *testing.B) { 157 s := RandomSlice(large) 158 benchmarkStdDev(b, s, nil) 159 } 160 161 func BenchmarkStdDevHuge(b *testing.B) { 162 s := RandomSlice(huge) 163 benchmarkStdDev(b, s, nil) 164 } 165 166 func BenchmarkStdDevSmallWeighted(b *testing.B) { 167 s := RandomSlice(small) 168 wts := RandomSlice(small) 169 benchmarkStdDev(b, s, wts) 170 } 171 172 func BenchmarkStdDevMediumWeighted(b *testing.B) { 173 s := RandomSlice(medium) 174 wts := RandomSlice(medium) 175 benchmarkStdDev(b, s, wts) 176 } 177 178 func BenchmarkStdDevLargeWeighted(b *testing.B) { 179 s := RandomSlice(large) 180 wts := RandomSlice(large) 181 benchmarkStdDev(b, s, wts) 182 } 183 184 func BenchmarkStdDevHugeWeighted(b *testing.B) { 185 s := RandomSlice(huge) 186 wts := RandomSlice(huge) 187 benchmarkStdDev(b, s, wts) 188 } 189 190 func benchmarkMeanVariance(b *testing.B, s, wts []float64) { 191 b.ResetTimer() 192 for i := 0; i < b.N; i++ { 193 MeanVariance(s, wts) 194 } 195 } 196 197 func BenchmarkMeanVarianceSmall(b *testing.B) { 198 s := RandomSlice(small) 199 benchmarkMeanVariance(b, s, nil) 200 } 201 202 func BenchmarkMeanVarianceMedium(b *testing.B) { 203 s := RandomSlice(medium) 204 benchmarkMeanVariance(b, s, nil) 205 } 206 207 func BenchmarkMeanVarianceLarge(b *testing.B) { 208 s := RandomSlice(large) 209 benchmarkMeanVariance(b, s, nil) 210 } 211 212 func BenchmarkMeanVarianceHuge(b *testing.B) { 213 s := RandomSlice(huge) 214 benchmarkMeanVariance(b, s, nil) 215 } 216 217 func BenchmarkMeanVarianceSmallWeighted(b *testing.B) { 218 s := RandomSlice(small) 219 wts := RandomSlice(small) 220 benchmarkMeanVariance(b, s, wts) 221 } 222 223 func BenchmarkMeanVarianceMediumWeighted(b *testing.B) { 224 s := RandomSlice(medium) 225 wts := RandomSlice(medium) 226 benchmarkMeanVariance(b, s, wts) 227 } 228 229 func BenchmarkMeanVarianceLargeWeighted(b *testing.B) { 230 s := RandomSlice(large) 231 wts := RandomSlice(large) 232 benchmarkMeanVariance(b, s, wts) 233 } 234 235 func BenchmarkMeanVarianceHugeWeighted(b *testing.B) { 236 s := RandomSlice(huge) 237 wts := RandomSlice(huge) 238 benchmarkMeanVariance(b, s, wts) 239 } 240 241 func benchmarkMeanStdDev(b *testing.B, s, wts []float64) { 242 b.ResetTimer() 243 for i := 0; i < b.N; i++ { 244 MeanStdDev(s, wts) 245 } 246 } 247 248 func BenchmarkMeanStdDevSmall(b *testing.B) { 249 s := RandomSlice(small) 250 benchmarkMeanStdDev(b, s, nil) 251 } 252 253 func BenchmarkMeanStdDevMedium(b *testing.B) { 254 s := RandomSlice(medium) 255 benchmarkMeanStdDev(b, s, nil) 256 } 257 258 func BenchmarkMeanStdDevLarge(b *testing.B) { 259 s := RandomSlice(large) 260 benchmarkMeanStdDev(b, s, nil) 261 } 262 263 func BenchmarkMeanStdDevHuge(b *testing.B) { 264 s := RandomSlice(huge) 265 benchmarkMeanStdDev(b, s, nil) 266 } 267 268 func BenchmarkMeanStdDevSmallWeighted(b *testing.B) { 269 s := RandomSlice(small) 270 wts := RandomSlice(small) 271 benchmarkMeanStdDev(b, s, wts) 272 } 273 274 func BenchmarkMeanStdDevMediumWeighted(b *testing.B) { 275 s := RandomSlice(medium) 276 wts := RandomSlice(medium) 277 benchmarkMeanStdDev(b, s, wts) 278 } 279 280 func BenchmarkMeanStdDevLargeWeighted(b *testing.B) { 281 s := RandomSlice(large) 282 wts := RandomSlice(large) 283 benchmarkMeanStdDev(b, s, wts) 284 } 285 286 func BenchmarkMeanStdDevHugeWeighted(b *testing.B) { 287 s := RandomSlice(huge) 288 wts := RandomSlice(huge) 289 benchmarkMeanStdDev(b, s, wts) 290 } 291 292 func benchmarkCovariance(b *testing.B, s1, s2, wts []float64) { 293 b.ResetTimer() 294 for i := 0; i < b.N; i++ { 295 Covariance(s1, s2, wts) 296 } 297 } 298 299 func BenchmarkCovarianceSmall(b *testing.B) { 300 s1 := RandomSlice(small) 301 s2 := RandomSlice(small) 302 benchmarkCovariance(b, s1, s2, nil) 303 } 304 305 func BenchmarkCovarianceMedium(b *testing.B) { 306 s1 := RandomSlice(medium) 307 s2 := RandomSlice(medium) 308 benchmarkCovariance(b, s1, s2, nil) 309 } 310 311 func BenchmarkCovarianceLarge(b *testing.B) { 312 s1 := RandomSlice(large) 313 s2 := RandomSlice(large) 314 benchmarkCovariance(b, s1, s2, nil) 315 } 316 317 func BenchmarkCovarianceHuge(b *testing.B) { 318 s1 := RandomSlice(huge) 319 s2 := RandomSlice(huge) 320 benchmarkCovariance(b, s1, s2, nil) 321 } 322 323 func BenchmarkCovarianceSmallWeighted(b *testing.B) { 324 s1 := RandomSlice(small) 325 s2 := RandomSlice(small) 326 wts := RandomSlice(small) 327 benchmarkCovariance(b, s1, s2, wts) 328 } 329 330 func BenchmarkCovarianceMediumWeighted(b *testing.B) { 331 s1 := RandomSlice(medium) 332 s2 := RandomSlice(medium) 333 wts := RandomSlice(medium) 334 benchmarkCovariance(b, s1, s2, wts) 335 } 336 337 func BenchmarkCovarianceLargeWeighted(b *testing.B) { 338 s1 := RandomSlice(large) 339 s2 := RandomSlice(large) 340 wts := RandomSlice(large) 341 benchmarkCovariance(b, s1, s2, wts) 342 } 343 344 func BenchmarkCovarianceHugeWeighted(b *testing.B) { 345 s1 := RandomSlice(huge) 346 s2 := RandomSlice(huge) 347 wts := RandomSlice(huge) 348 benchmarkCovariance(b, s1, s2, wts) 349 } 350 351 func benchmarkCorrelation(b *testing.B, s1, s2, wts []float64) { 352 b.ResetTimer() 353 for i := 0; i < b.N; i++ { 354 Correlation(s1, s2, wts) 355 } 356 } 357 358 func BenchmarkCorrelationSmall(b *testing.B) { 359 s1 := RandomSlice(small) 360 s2 := RandomSlice(small) 361 benchmarkCorrelation(b, s1, s2, nil) 362 } 363 364 func BenchmarkCorrelationMedium(b *testing.B) { 365 s1 := RandomSlice(medium) 366 s2 := RandomSlice(medium) 367 benchmarkCorrelation(b, s1, s2, nil) 368 } 369 370 func BenchmarkCorrelationLarge(b *testing.B) { 371 s1 := RandomSlice(large) 372 s2 := RandomSlice(large) 373 benchmarkCorrelation(b, s1, s2, nil) 374 } 375 376 func BenchmarkCorrelationHuge(b *testing.B) { 377 s1 := RandomSlice(huge) 378 s2 := RandomSlice(huge) 379 benchmarkCorrelation(b, s1, s2, nil) 380 } 381 382 func BenchmarkCorrelationSmallWeighted(b *testing.B) { 383 s1 := RandomSlice(small) 384 s2 := RandomSlice(small) 385 wts := RandomSlice(small) 386 benchmarkCorrelation(b, s1, s2, wts) 387 } 388 389 func BenchmarkCorrelationMediumWeighted(b *testing.B) { 390 s1 := RandomSlice(medium) 391 s2 := RandomSlice(medium) 392 wts := RandomSlice(medium) 393 benchmarkCorrelation(b, s1, s2, wts) 394 } 395 396 func BenchmarkCorrelationLargeWeighted(b *testing.B) { 397 s1 := RandomSlice(large) 398 s2 := RandomSlice(large) 399 wts := RandomSlice(large) 400 benchmarkCorrelation(b, s1, s2, wts) 401 } 402 403 func BenchmarkCorrelationHugeWeighted(b *testing.B) { 404 s1 := RandomSlice(huge) 405 s2 := RandomSlice(huge) 406 wts := RandomSlice(huge) 407 benchmarkCorrelation(b, s1, s2, wts) 408 } 409 410 func benchmarkSkew(b *testing.B, s, wts []float64) { 411 b.ResetTimer() 412 for i := 0; i < b.N; i++ { 413 Skew(s, wts) 414 } 415 } 416 417 func BenchmarkSkewSmall(b *testing.B) { 418 s := RandomSlice(small) 419 benchmarkSkew(b, s, nil) 420 } 421 422 func BenchmarkSkewMedium(b *testing.B) { 423 s := RandomSlice(medium) 424 benchmarkSkew(b, s, nil) 425 } 426 427 func BenchmarkSkewLarge(b *testing.B) { 428 s := RandomSlice(large) 429 benchmarkSkew(b, s, nil) 430 } 431 432 func BenchmarkSkewHuge(b *testing.B) { 433 s := RandomSlice(huge) 434 benchmarkSkew(b, s, nil) 435 } 436 437 func BenchmarkSkewSmallWeighted(b *testing.B) { 438 s := RandomSlice(small) 439 wts := RandomSlice(small) 440 benchmarkSkew(b, s, wts) 441 } 442 443 func BenchmarkSkewMediumWeighted(b *testing.B) { 444 s := RandomSlice(medium) 445 wts := RandomSlice(medium) 446 benchmarkSkew(b, s, wts) 447 } 448 449 func BenchmarkSkewLargeWeighted(b *testing.B) { 450 s := RandomSlice(large) 451 wts := RandomSlice(large) 452 benchmarkSkew(b, s, wts) 453 } 454 455 func BenchmarkSkewHugeWeighted(b *testing.B) { 456 s := RandomSlice(huge) 457 wts := RandomSlice(huge) 458 benchmarkSkew(b, s, wts) 459 } 460 461 func benchmarkExKurtosis(b *testing.B, s, wts []float64) { 462 b.ResetTimer() 463 for i := 0; i < b.N; i++ { 464 ExKurtosis(s, wts) 465 } 466 } 467 468 func BenchmarkExKurtosisSmall(b *testing.B) { 469 s := RandomSlice(small) 470 benchmarkExKurtosis(b, s, nil) 471 } 472 473 func BenchmarkExKurtosisMedium(b *testing.B) { 474 s := RandomSlice(medium) 475 benchmarkExKurtosis(b, s, nil) 476 } 477 478 func BenchmarkExKurtosisLarge(b *testing.B) { 479 s := RandomSlice(large) 480 benchmarkExKurtosis(b, s, nil) 481 } 482 483 func BenchmarkExKurtosisHuge(b *testing.B) { 484 s := RandomSlice(huge) 485 benchmarkExKurtosis(b, s, nil) 486 } 487 488 func BenchmarkExKurtosisSmallWeighted(b *testing.B) { 489 s := RandomSlice(small) 490 wts := RandomSlice(small) 491 benchmarkExKurtosis(b, s, wts) 492 } 493 494 func BenchmarkExKurtosisMediumWeighted(b *testing.B) { 495 s := RandomSlice(medium) 496 wts := RandomSlice(medium) 497 benchmarkExKurtosis(b, s, wts) 498 } 499 500 func BenchmarkExKurtosisLargeWeighted(b *testing.B) { 501 s := RandomSlice(large) 502 wts := RandomSlice(large) 503 benchmarkExKurtosis(b, s, wts) 504 } 505 506 func BenchmarkExKurtosisHugeWeighted(b *testing.B) { 507 s := RandomSlice(huge) 508 wts := RandomSlice(huge) 509 benchmarkExKurtosis(b, s, wts) 510 } 511 512 func benchmarkMoment(b *testing.B, n float64, s, wts []float64) { 513 b.ResetTimer() 514 for i := 0; i < b.N; i++ { 515 Moment(n, s, wts) 516 } 517 } 518 519 func BenchmarkMomentSmall(b *testing.B) { 520 s := RandomSlice(small) 521 benchmarkMoment(b, 5, s, nil) 522 } 523 524 func BenchmarkMomentMedium(b *testing.B) { 525 s := RandomSlice(medium) 526 benchmarkMoment(b, 5, s, nil) 527 } 528 529 func BenchmarkMomentLarge(b *testing.B) { 530 s := RandomSlice(large) 531 benchmarkMoment(b, 5, s, nil) 532 } 533 534 func BenchmarkMomentHuge(b *testing.B) { 535 s := RandomSlice(huge) 536 benchmarkMoment(b, 5, s, nil) 537 } 538 539 func BenchmarkMomentSmallWeighted(b *testing.B) { 540 s := RandomSlice(small) 541 wts := RandomSlice(small) 542 benchmarkMoment(b, 5, s, wts) 543 } 544 545 func BenchmarkMomentMediumWeighted(b *testing.B) { 546 s := RandomSlice(medium) 547 wts := RandomSlice(medium) 548 benchmarkMoment(b, 5, s, wts) 549 } 550 551 func BenchmarkMomentLargeWeighted(b *testing.B) { 552 s := RandomSlice(large) 553 wts := RandomSlice(large) 554 benchmarkMoment(b, 5, s, wts) 555 } 556 557 func BenchmarkMomentHugeWeighted(b *testing.B) { 558 s := RandomSlice(huge) 559 wts := RandomSlice(huge) 560 benchmarkMoment(b, 5, s, wts) 561 } 562 563 func benchmarkMomentAbout(b *testing.B, n float64, s []float64, mean float64, wts []float64) { 564 b.ResetTimer() 565 for i := 0; i < b.N; i++ { 566 MomentAbout(n, s, mean, wts) 567 } 568 } 569 570 func BenchmarkMomentAboutSmall(b *testing.B) { 571 s := RandomSlice(small) 572 benchmarkMomentAbout(b, 5, s, 0, nil) 573 } 574 575 func BenchmarkMomentAboutMedium(b *testing.B) { 576 s := RandomSlice(medium) 577 benchmarkMomentAbout(b, 5, s, 0, nil) 578 } 579 580 func BenchmarkMomentAboutLarge(b *testing.B) { 581 s := RandomSlice(large) 582 benchmarkMomentAbout(b, 5, s, 0, nil) 583 } 584 585 func BenchmarkMomentAboutHuge(b *testing.B) { 586 s := RandomSlice(huge) 587 benchmarkMomentAbout(b, 5, s, 0, nil) 588 } 589 590 func BenchmarkMomentAboutSmallWeighted(b *testing.B) { 591 s := RandomSlice(small) 592 wts := RandomSlice(small) 593 benchmarkMomentAbout(b, 5, s, 0, wts) 594 } 595 596 func BenchmarkMomentAboutMediumWeighted(b *testing.B) { 597 s := RandomSlice(medium) 598 wts := RandomSlice(medium) 599 benchmarkMomentAbout(b, 5, s, 0, wts) 600 } 601 602 func BenchmarkMomentAboutLargeWeighted(b *testing.B) { 603 s := RandomSlice(large) 604 wts := RandomSlice(large) 605 benchmarkMomentAbout(b, 5, s, 0, wts) 606 } 607 608 func BenchmarkMomentAboutHugeWeighted(b *testing.B) { 609 s := RandomSlice(huge) 610 wts := RandomSlice(huge) 611 benchmarkMomentAbout(b, 5, s, 0, wts) 612 }