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