### ----------------------------------------------------
### -- Statistics
### -- Licenses: Artistic-2.0
### -- Authors: Suman Khanal <suman81765@gmail.com>
### -- File: t/04-prob_distributions.rakutest
### ----------------------------------------------------
### -- Chunk 2 of 2
0.2, 1, 2,log_p => True), NaN;
is-approx raku_qweibull(0.2, 1, 2), 0.44628710262841953, 1e-12;
is raku_qweibull(0, 7), 0;
is-approx raku_qweibull(0.5, 1, 0.2), 0.13862943611198905, 1e-12;


diag("####    Testing raku_rweibull    ####");
element_wise_testing_with_precision -> $n {set_seed(111,222); raku_rweibull($n, 2, 2)}(10), 
                                    (1.393634435100773,1.5481072197202943,2.0971770705316657,0.8357424160495083,1.5441847679961411,0.4677604053455371,2.715328317118502,1.5935163877485303,2.626334393988074,1.1832568107733237),
                                    1e-12;

element_wise_testing_with_precision -> $n {set_seed(111,222); raku_rweibull($n, 2)}(5),
                                    (0.6968172175503865,0.7740536098601472,1.0485885352658328,0.41787120802475414,0.7720923839980706),
                                    1e-12;

####	Logistic distribution 	####

diag("####    Testing raku_dlogis    ####");
is-approx raku_dlogis(3), 0.045176659730912144, 1e-12;
is-approx raku_dlogis(3, 2, 3), 0.08106072033492359 , 1e-12;
is-approx raku_dlogis(4,2,3, log => True), -2.5940191290389203 , 1e-12;
is-approx raku_dlogis(14, 7), 0.0009102211801218265, 1e-12;
is-approx raku_dlogis(1, 3), 0.1049935854035065, 1e-12;


diag("####    Testing raku_plogis    ####");
is-approx raku_plogis(3), 0.9525741268224334, 1e-12;
is-approx raku_plogis(3, 2, 3), 0.5825702064623147 , 1e-12;
is-approx raku_plogis(4,2,3, log_p => True), -0.41437008685207205 , 1e-12;
is-approx raku_plogis(14, 7), 0.9990889488055994, 1e-12;
is-approx raku_plogis(1, 3), 0.11920292202211755, 1e-12;

diag("####    Testing raku_qlogis    ####");
is raku_qlogis(0), -Inf;
is-approx raku_qlogis(0.2, 2, 3), -2.1588830833596715 , 1e-12;
is raku_qlogis(1,2,3, log_p => True), NaN;
is raku_qlogis(14, 7), NaN;
is raku_qlogis(0.5, 3), 3;


diag("####    Testing raku_rlogis    ####");
element_wise_testing_with_precision -> $n {set_seed(111,222); raku_rlogis($n)}(5),
                                    (0.46988294460554764,0.19773530926871702,-0.6945353008974018,1.6565857139688709,0.20447545664046937),
                                    1e-12;

element_wise_testing_with_precision -> $n {set_seed(111,222); raku_rlogis($n,2)}(5),
                                    (2.4698829446055477,2.197735309268717,1.3054646991025982,3.656585713968871,2.2044754566404694),
                                    1e-12;


####    Studentized Range Distribution    ####

diag("####    Testing raku_ptukey    ####");
is raku_ptukey(2,3,1), NaN;
is-approx raku_ptukey(1,6,5), 0.02721150208597321, 1e-12;

diag("####    Testing raku_qtukey    ####");
is raku_qtukey(1,6,5), Inf;
is raku_qtukey(2,6,5), NaN;
is-approx raku_qtukey(0.95,6,5), 6.032902683224805, 1e-12;

####	Wilcoxon Rank Sum Distribution  	####

diag("####    Testing raku_dwilcox    ####");
is-approx raku_dwilcox(3,2,4), 0.13333333333333333, 1e-12;
is-approx raku_dwilcox(5,7,3, log => True), -3.1780538303479453, 1e-12;


diag("####    Testing raku_pwilcox    ####");
is-approx raku_pwilcox(3,2,4), 0.4, 1e-12;
is-approx raku_pwilcox(5,7,3, log_p => True), -2.0149030205422647, 1e-12;

diag("####    Testing raku_qwilcox    ####");
is raku_qwilcox(3,2,4), NaN;
is raku_qwilcox(0.1, 10, 5), 14;
is raku_qwilcox(0, 10, 5), 0;
is raku_qwilcox(1, 10, 5), 50;

diag("####    Testing raku_rwilcox    ####");
is -> $n {set_seed(111,222); raku_rwilcox($n, 1, 2)}(2), (2, 2);
is -> $n {set_seed(111,222); raku_rwilcox($n, 3, 2)}(5), (3,4,3,3,6);

####	Wilcoxon Signed Rank Distribution 	####

diag("####    Testing raku_dsignrank    ####");
is raku_dsignrank(3,2), 0.25;
is-approx raku_dsignrank(5,7, log => True), -3.753417975251507, 1e-12;

diag("####    Testing raku_psignrank    ####");
is raku_psignrank(3,2), 1;
is-approx raku_psignrank(5,7, log_p => True), -2.549445170925571, 1e-12; 

diag("####    Testing raku_qsignrank    ####");
is raku_qsignrank(1,2), 3;
is raku_qsignrank(0,7, log_p => True), 28;
is raku_qsignrank(1, 7), 28;
is raku_qsignrank(1, 7, log_p => True), NaN;    


diag("####    Testing raku_rsignrank    ####");
is -> $n {set_seed(111,222); raku_rsignrank($n, 40)}(3), (375, 326, 250);
is -> $n {set_seed(111,222); raku_rsignrank($n, 2)}(5), (3, 2, 3, 2, 2);


####	Gamma and Related functions 	####

diag("####    Testing beta and lbeta    ####");
is-approx beta(2,3), 0.08333333333333333, 1e-12;
is-approx beta(30,2), 0.001075268817204301, 1e-12;

is-approx lbeta(2,3), -2.4849066497880004, 1e-12;
is-approx lbeta(30,2), -6.835184586147302, 1e-12;

diag("####    Testing gamma and lgamma    ####");
is gamma(3), 2; 
is-approx gamma(2.2), 1.1018024908797128, 1e-12; 
is gamma(5), 24; 
is gamma(100), 9.332621544394404e+155; 
is-approx gamma(4.9), 20.667385961857857, 1e-12; 

is lgamma(0), Inf;
is lgamma(1), 0;
is-approx lgamma(4.2), 2.04855563696059, 1e-12;
is lgamma(-4), Inf;
is-approx lgamma(100), 359.1342053695754, 1e-12;
is-approx lgamma(42), 114.0342117814617, 1e-12;

diag("####    Testing psi/di/tri-gamma    ####");
is-approx psigamma(5.Num), 1.5061176684318007, 1e-12;
is-approx psigamma(5, 2), -0.04878973224511449, 1e-12;

is-approx digamma(5), 1.5061176684318007, 1e-12;
is-approx digamma(400), 5.990214026274974, 1e-12;
is-approx digamma(4.2), 1.3113388912865995, 1e-12;

is-approx trigamma(5), 0.22132295573711527, 1e-12;
is-approx trigamma(400), 0.002503127604163412, 1e-12;
is-approx trigamma(4.2), 0.2686649407314008, 1e-12;


diag("####    Testing choose and lchoose    ####");
is choose(3,1), 3;
is choose(0,0), 1;
is choose(3,5), 0;
is choose(30,4), 27405;
is lchoose(3,4), -Inf;
is-approx lchoose(30,4), 10.218480757480219, 1e-12;

diag("####    Testing factorial and lfactorial    ####");
is factorial(9), 362880;
is factorial(0), 1;
is-approx factorial(0.2), 0.9181687423997607, 0.01;
is-approx factorial(10.2), 5819090.083978569, 0.01;
is-approx lfactorial(4), 3.1780538303479458, 1e-12;
is lfactorial(-4), Inf;
is lfactorial(0), 0;
is-approx lfactorial(0.2), -0.0853740900033158, 1e-12;


done-testing;


