|
22 | 22 | * ## Notes |
23 | 23 | * |
24 | 24 | * - Run this script from the directory in which fixtures should be written. |
| 25 | +* - Follows the test data generation patterns from Boost's ibeta_data.cpp |
| 26 | +* (see https://github.com/boostorg/math/blob/develop/tools/ibeta_data.cpp). |
| 27 | +* Reference values are computed via Boost library functions, which |
| 28 | +* internally promote double to long double (80-bit extended on x86), |
| 29 | +* providing slightly higher precision than double for reference data. |
| 30 | +* |
| 31 | +* ## Fixture Categories (matching Boost's test_ibeta.hpp) |
| 32 | +* |
| 33 | +* 1. output.json - Medium a,b in [1,40] (original fixture) |
| 34 | +* 2. small_ab.json - Small a,b in [0,10] (ibeta_small_data style) |
| 35 | +* 3. medium_ab.json - Medium a,b in [0,100] (ibeta_data style) |
| 36 | +* 4. large_range_ab.json - Large range a,b ~ 10^-5 to 10^6 (ibeta_large_data style) |
| 37 | +* 5. int_ab.json - Integer a,b in {1,5,9,...,37} (ibeta_int_data style) |
25 | 38 | * |
26 | 39 | */ |
27 | 40 | #include <stdlib.h> |
28 | 41 | #include <stdio.h> |
| 42 | +#include <cmath> |
29 | 43 | #include <boost/random/mersenne_twister.hpp> |
30 | 44 | #include <boost/random/uniform_int_distribution.hpp> |
31 | 45 | #include <boost/random/uniform_real_distribution.hpp> |
@@ -359,44 +373,330 @@ void generate( double *x, double *a, double *b, const unsigned int len, const ch |
359 | 373 | } |
360 | 374 |
|
361 | 375 | /** |
362 | | -* Main execution sequence. |
| 376 | +* Generates "small a,b" fixture (ibeta_small_data style). |
| 377 | +* |
| 378 | +* Following Boost's beta_data_generator_small: a,b = U[0,100]/10 giving |
| 379 | +* a,b in [0, 10], with random x in (0.0001, 1). |
| 380 | +* |
| 381 | +* @param name output filename |
363 | 382 | */ |
364 | | -int main( void ) { |
365 | | - unsigned int len; |
| 383 | +void generate_small_ab( const char *name ) { |
| 384 | + unsigned int len = 500; |
366 | 385 | double *x; |
367 | 386 | double *a; |
368 | 387 | double *b; |
| 388 | + unsigned int i; |
369 | 389 |
|
370 | | - // Define the array length: |
371 | | - len = 1000; |
| 390 | + uniform_real_distribution<> rand_x( 0.0001, 1.0 ); |
| 391 | + uniform_real_distribution<> rand_ab( 0.0, 100.0 ); |
372 | 392 |
|
373 | | - // Allocate arrays: |
374 | 393 | x = (double*) malloc( len * sizeof(double) ); |
375 | | - if ( x == NULL ) { |
| 394 | + a = (double*) malloc( len * sizeof(double) ); |
| 395 | + b = (double*) malloc( len * sizeof(double) ); |
| 396 | + if ( x == NULL || a == NULL || b == NULL ) { |
376 | 397 | printf( "Error allocating memory.\n" ); |
377 | 398 | exit( 1 ); |
378 | 399 | } |
| 400 | + |
| 401 | + for ( i = 0; i < len; i++ ) { |
| 402 | + a[ i ] = rand_ab( rng ) / 10.0; |
| 403 | + b[ i ] = rand_ab( rng ) / 10.0; |
| 404 | + x[ i ] = rand_x( rng ); |
| 405 | + |
| 406 | + // Ensure a,b > 0: |
| 407 | + if ( a[ i ] <= 0.0 ) { |
| 408 | + a[ i ] = 0.001; |
| 409 | + } |
| 410 | + if ( b[ i ] <= 0.0 ) { |
| 411 | + b[ i ] = 0.001; |
| 412 | + } |
| 413 | + } |
| 414 | + |
| 415 | + generate( x, a, b, len, name ); |
| 416 | + free( x ); |
| 417 | + free( a ); |
| 418 | + free( b ); |
| 419 | +} |
| 420 | + |
| 421 | +/** |
| 422 | +* Generates "medium a,b" fixture (ibeta_data style). |
| 423 | +* |
| 424 | +* Following Boost's beta_data_generator_medium: a,b = U[0,100], with |
| 425 | +* random x in (0.0001, 1). |
| 426 | +* |
| 427 | +* @param name output filename |
| 428 | +*/ |
| 429 | +void generate_medium_ab( const char *name ) { |
| 430 | + unsigned int len = 500; |
| 431 | + double *x; |
| 432 | + double *a; |
| 433 | + double *b; |
| 434 | + unsigned int i; |
| 435 | + |
| 436 | + uniform_real_distribution<> rand_x( 0.0001, 1.0 ); |
| 437 | + uniform_real_distribution<> rand_ab( 0.0, 100.0 ); |
| 438 | + |
| 439 | + x = (double*) malloc( len * sizeof(double) ); |
379 | 440 | a = (double*) malloc( len * sizeof(double) ); |
380 | | - if ( a == NULL ) { |
| 441 | + b = (double*) malloc( len * sizeof(double) ); |
| 442 | + if ( x == NULL || a == NULL || b == NULL ) { |
381 | 443 | printf( "Error allocating memory.\n" ); |
382 | 444 | exit( 1 ); |
383 | 445 | } |
| 446 | + |
| 447 | + for ( i = 0; i < len; i++ ) { |
| 448 | + a[ i ] = rand_ab( rng ); |
| 449 | + b[ i ] = rand_ab( rng ); |
| 450 | + x[ i ] = rand_x( rng ); |
| 451 | + |
| 452 | + // Ensure a,b > 0: |
| 453 | + if ( a[ i ] <= 0.0 ) { |
| 454 | + a[ i ] = 0.001; |
| 455 | + } |
| 456 | + if ( b[ i ] <= 0.0 ) { |
| 457 | + b[ i ] = 0.001; |
| 458 | + } |
| 459 | + } |
| 460 | + |
| 461 | + generate( x, a, b, len, name ); |
| 462 | + free( x ); |
| 463 | + free( a ); |
| 464 | + free( b ); |
| 465 | +} |
| 466 | + |
| 467 | +/** |
| 468 | +* Generates "large range a,b" fixture (ibeta_large_data style). |
| 469 | +* |
| 470 | +* Following Boost's beta_data_generator: a = U[1,5]*10^k1, |
| 471 | +* b = U[1,5]*10^k2, where k1,k2 are linearly spaced from -5 to 6 (11 |
| 472 | +* values each). For each (k1, k2) pair, 10 random x values in (0.0001, 1) |
| 473 | +* are generated. |
| 474 | +* |
| 475 | +* Total: 11 * 11 * 10 = 1210 points. |
| 476 | +* |
| 477 | +* @param name output filename |
| 478 | +*/ |
| 479 | +void generate_large_range_ab( const char *name ) { |
| 480 | + unsigned int nk = 11; |
| 481 | + unsigned int nx = 10; |
| 482 | + unsigned int len = nk * nk * nx; |
| 483 | + double *x; |
| 484 | + double *a; |
| 485 | + double *b; |
| 486 | + unsigned int idx; |
| 487 | + unsigned int ik1; |
| 488 | + unsigned int ik2; |
| 489 | + unsigned int ix; |
| 490 | + double k1; |
| 491 | + double k2; |
| 492 | + |
| 493 | + uniform_real_distribution<> rand_x( 0.0001, 1.0 ); |
| 494 | + uniform_real_distribution<> rand_scale( 1.0, 5.0 ); |
| 495 | + |
| 496 | + // Linearly spaced exponents from -5 to 6 (11 values): |
| 497 | + double exponents[ 11 ]; |
| 498 | + linspace_f64( exponents, nk, -5.0, 6.0 ); |
| 499 | + |
| 500 | + x = (double*) malloc( len * sizeof(double) ); |
| 501 | + a = (double*) malloc( len * sizeof(double) ); |
384 | 502 | b = (double*) malloc( len * sizeof(double) ); |
385 | | - if ( b == NULL ) { |
| 503 | + if ( x == NULL || a == NULL || b == NULL ) { |
386 | 504 | printf( "Error allocating memory.\n" ); |
387 | 505 | exit( 1 ); |
388 | 506 | } |
389 | 507 |
|
390 | | - // Generate fixture data: |
| 508 | + idx = 0; |
| 509 | + for ( ik1 = 0; ik1 < nk; ik1++ ) { |
| 510 | + k1 = exponents[ ik1 ]; |
| 511 | + for ( ik2 = 0; ik2 < nk; ik2++ ) { |
| 512 | + k2 = exponents[ ik2 ]; |
| 513 | + for ( ix = 0; ix < nx; ix++ ) { |
| 514 | + a[ idx ] = rand_scale( rng ) * pow( 10.0, k1 ); |
| 515 | + b[ idx ] = rand_scale( rng ) * pow( 10.0, k2 ); |
| 516 | + x[ idx ] = rand_x( rng ); |
| 517 | + idx++; |
| 518 | + } |
| 519 | + } |
| 520 | + } |
| 521 | + |
| 522 | + generate( x, a, b, len, name ); |
| 523 | + free( x ); |
| 524 | + free( a ); |
| 525 | + free( b ); |
| 526 | +} |
| 527 | + |
| 528 | +/** |
| 529 | +* Generates "integer a,b" fixture (ibeta_int_data style). |
| 530 | +* |
| 531 | +* Following Boost's beta_data_generator_int: a,b from {1,5,9,...,37} |
| 532 | +* (10 integer values, step 4), with 10 random x values in (0.0001, 1) for |
| 533 | +* each (a, b) pair. |
| 534 | +* |
| 535 | +* Total: 10 * 10 * 10 = 1000 points. |
| 536 | +* |
| 537 | +* @param name output filename |
| 538 | +*/ |
| 539 | +void generate_int_ab( const char *name ) { |
| 540 | + unsigned int na = 10; |
| 541 | + unsigned int nx = 10; |
| 542 | + unsigned int len = na * na * nx; |
| 543 | + double *x; |
| 544 | + double *a; |
| 545 | + double *b; |
| 546 | + unsigned int idx; |
| 547 | + unsigned int ia; |
| 548 | + unsigned int ib; |
| 549 | + unsigned int ix; |
| 550 | + |
| 551 | + int ab_vals[ 10 ] = { 1, 5, 9, 13, 17, 21, 25, 29, 33, 37 }; |
| 552 | + |
| 553 | + uniform_real_distribution<> rand_x( 0.0001, 1.0 ); |
| 554 | + |
| 555 | + x = (double*) malloc( len * sizeof(double) ); |
| 556 | + a = (double*) malloc( len * sizeof(double) ); |
| 557 | + b = (double*) malloc( len * sizeof(double) ); |
| 558 | + if ( x == NULL || a == NULL || b == NULL ) { |
| 559 | + printf( "Error allocating memory.\n" ); |
| 560 | + exit( 1 ); |
| 561 | + } |
| 562 | + |
| 563 | + idx = 0; |
| 564 | + for ( ia = 0; ia < na; ia++ ) { |
| 565 | + for ( ib = 0; ib < na; ib++ ) { |
| 566 | + for ( ix = 0; ix < nx; ix++ ) { |
| 567 | + a[ idx ] = (double) ab_vals[ ia ]; |
| 568 | + b[ idx ] = (double) ab_vals[ ib ]; |
| 569 | + x[ idx ] = rand_x( rng ); |
| 570 | + idx++; |
| 571 | + } |
| 572 | + } |
| 573 | + } |
| 574 | + |
| 575 | + generate( x, a, b, len, name ); |
| 576 | + free( x ); |
| 577 | + free( a ); |
| 578 | + free( b ); |
| 579 | +} |
| 580 | + |
| 581 | +/** |
| 582 | +* Generates "large asymmetric" fixture (ibeta_large_asym_data style). |
| 583 | +* |
| 584 | +* Following Boost's beta_data_generator_asym: a,b are geometrically |
| 585 | +* spaced from 0.1 to 50. For each (a,b) pair, x is placed near the |
| 586 | +* distribution mean a/(a+b) by multiplying with factors that approach |
| 587 | +* 1 from below and above. This stress-tests the crossover decision |
| 588 | +* boundary where the code chooses between the direct and reflected |
| 589 | +* incomplete beta formulas. |
| 590 | +* |
| 591 | +* Multipliers from Boost: {0.9, 0.99, 0.9999, 0.99999, 0.999999, |
| 592 | +* 0.99999999, 1.001, 1.1, 1.00001, 1.0000001, 1.000000001} |
| 593 | +* |
| 594 | +* Total: 10 * 10 * 11 = 1100 points (minus invalid x >= 1). |
| 595 | +* |
| 596 | +* @param name output filename |
| 597 | +*/ |
| 598 | +void generate_large_asym( const char *name ) { |
| 599 | + unsigned int na = 10; |
| 600 | + unsigned int nmul = 11; |
| 601 | + unsigned int max_len; |
| 602 | + unsigned int idx; |
| 603 | + unsigned int i; |
| 604 | + unsigned int j; |
| 605 | + unsigned int k; |
| 606 | + double *x; |
| 607 | + double *a; |
| 608 | + double *b; |
| 609 | + double a_val; |
| 610 | + double b_val; |
| 611 | + double x0; |
| 612 | + double xv; |
| 613 | + |
| 614 | + // Geometrically spaced a,b from 0.1 to 50 (10 values): |
| 615 | + double ab_vals[ 10 ]; |
| 616 | + for ( i = 0; i < na; i++ ) { |
| 617 | + ab_vals[ i ] = 0.1 * pow( 500.0, (double)i / (double)(na - 1) ); |
| 618 | + } |
| 619 | + |
| 620 | + // Multipliers from Boost's beta_data_generator_asym: |
| 621 | + double muls[ 11 ] = { |
| 622 | + 0.9, 0.99, 0.9999, 0.99999, 0.999999, |
| 623 | + 0.99999999, 1.001, 1.1, 1.00001, 1.0000001, |
| 624 | + 1.000000001 |
| 625 | + }; |
| 626 | + |
| 627 | + max_len = na * na * nmul; |
| 628 | + x = (double*) malloc( max_len * sizeof(double) ); |
| 629 | + a = (double*) malloc( max_len * sizeof(double) ); |
| 630 | + b = (double*) malloc( max_len * sizeof(double) ); |
| 631 | + if ( x == NULL || a == NULL || b == NULL ) { |
| 632 | + printf( "Error allocating memory.\n" ); |
| 633 | + exit( 1 ); |
| 634 | + } |
| 635 | + |
| 636 | + idx = 0; |
| 637 | + for ( i = 0; i < na; i++ ) { |
| 638 | + for ( j = 0; j < na; j++ ) { |
| 639 | + a_val = ab_vals[ i ]; |
| 640 | + b_val = ab_vals[ j ]; |
| 641 | + x0 = a_val / ( a_val + b_val ); |
| 642 | + for ( k = 0; k < nmul; k++ ) { |
| 643 | + xv = x0 * muls[ k ]; |
| 644 | + if ( xv >= 1.0 || xv <= 0.0 ) { |
| 645 | + continue; |
| 646 | + } |
| 647 | + a[ idx ] = a_val; |
| 648 | + b[ idx ] = b_val; |
| 649 | + x[ idx ] = xv; |
| 650 | + idx++; |
| 651 | + } |
| 652 | + } |
| 653 | + } |
| 654 | + |
| 655 | + generate( x, a, b, idx, name ); |
| 656 | + free( x ); |
| 657 | + free( a ); |
| 658 | + free( b ); |
| 659 | +} |
| 660 | + |
| 661 | +/** |
| 662 | +* Main execution sequence. |
| 663 | +*/ |
| 664 | +int main( void ) { |
| 665 | + unsigned int len; |
| 666 | + double *x; |
| 667 | + double *a; |
| 668 | + double *b; |
| 669 | + |
| 670 | + len = 1000; |
| 671 | + x = (double*) malloc( len * sizeof(double) ); |
| 672 | + a = (double*) malloc( len * sizeof(double) ); |
| 673 | + b = (double*) malloc( len * sizeof(double) ); |
| 674 | + if ( x == NULL || a == NULL || b == NULL ) { |
| 675 | + printf( "Error allocating memory.\n" ); |
| 676 | + exit( 1 ); |
| 677 | + } |
391 | 678 | rand_array_f64( x, len, 0.001, 0.999 ); |
392 | 679 | rand_array_f64( a, len, 1.0, 40.0 ); |
393 | 680 | rand_array_f64( b, len, 1.0, 40.0 ); |
394 | 681 | generate( x, a, b, len, "output.json" ); |
395 | 682 |
|
396 | | - // Free allocated memory: |
397 | 683 | free( x ); |
398 | 684 | free( a ); |
399 | 685 | free( b ); |
400 | 686 |
|
| 687 | + generate_small_ab( "small_ab.json" ); |
| 688 | + |
| 689 | + generate_medium_ab( "medium_ab.json" ); |
| 690 | + |
| 691 | + generate_large_range_ab( "large_range_ab.json" ); |
| 692 | + |
| 693 | + generate_int_ab( "int_ab.json" ); |
| 694 | + |
| 695 | + // --------------------------------------------------------------- |
| 696 | + // 6. Large asymmetric (ibeta_large_asym_data style): x near mean |
| 697 | + // 10 * 10 * 11 ~ 1100 points (minus invalid) |
| 698 | + // --------------------------------------------------------------- |
| 699 | + generate_large_asym( "large_asym.json" ); |
| 700 | + |
401 | 701 | return 0; |
402 | 702 | } |
0 commit comments