### ----------------------------------------------------
### -- Statistics
### -- Licenses: Artistic-2.0
### -- Authors: Suman Khanal <suman81765@gmail.com>
### -- File: t/03-kendall.rakutest
### ----------------------------------------------------
use Test;
use Statistics;

# Testing kendall_cor_coef

my @x = [754,740,626,737,396,833,548,914,252,375,740,444,435,508,10,66,290,312,255,853,312,44,472,989,920,580,288,114,99,742,681,218,82,505,887,532,154,945,938,820,162,291,631,751,320,371,861,949,348,39,542,211,266,441,318,598,266,121,189,975,538,910,622,385,533,419,667,425,872,653,359,509,394,163,150,879,656,179,156,959,277,315,584,899,694,571,394,884,82,756,295,803,390,764,349,583,686,858,535,271];
my @y = [3024,2416,2046,4516,6977,4218,4439,2211,2223,749,4831,1136,6279,4874,2745,2214,5265,5522,655,442,6377,1310,5966,6071,5416,4655,508,1664,986,4246,6454,349,5534,4695,2022,6312,2273,6489,6088,5182,1466,4051,1881,3849,5712,5514,1869,2822,2658,1217,469,3716,2116,6474,4559,3798,2302,2167,4143,3413,2296,3489,2806,6088,662,5886,4932,5284,3643,5608,5443,2906,1101,5395,5448,4794,4337,5062,5194,6496,1633,1723,849,3410,1775,5264,3241,4298,4806,570,1922,3583,6193,1412,6211,1365,6552,1034,2832,5069];

my @data1_ints = [754,740,626,737,396,833,548,914,252,375];
my @data2_ints = [3024,2416,2046,4516,6977,4218,4439,2211,2223,749];

my @data1_ints_floats = [7.54,74,62.3,73.7,39,83.0,5.4,91.4,25.0,3.75];
my @data2_ints_floats = [30.24,24.16,20.46,45,6.7,4.8,4.9,22.0,2.23,7.49];




diag("Testing kendall_cor_coef...");
is kendall_cor_coef(@x, @y), 0.07074995524624558;
is kendall_cor_coef(@data1_ints, @data2_ints), 0.022222222222222223;
is kendall_cor_coef(@data1_ints_floats, @data2_ints_floats), 0.15555555555555556;
is kendall_cor_coef([1, 2, 3, 4, 5], [1, 2, 3, 4, 5]), 1.0;
is kendall_cor_coef([1, 2], [2, 3]), 1.0;
is-approx kendall_cor_coef([-1, -2, 5], [6, -2, -1]), 0.33333333333333, 1e-12;
is kendall_cor_coef([1, 2, 3, 4, 5], [5, 4, 3, 2, 1]), -1.0;
is kendall_cor_coef([1, 2, 3, 4, 5], [3, 4, 1, 5, 2]), 0.0;
is-approx kendall_cor_coef([1, 2, 2, 4, 5], [5, 4, 3, 2, 1]), -0.94868, 0.00001;
is kendall_cor_coef([1, 2, 3, 4, 5, 6, 7, 8], [1, 4, 9, 16, 25, 36, 49, 64]), 1.0;
is-approx kendall_cor_coef([15,18,10,20,25,14], [20,16,30,18,25,17]), -0.066666667;
is-approx kendall_cor_coef([6,6,4,6,8,6,8,4,4,6,6,8,8,8,8,8,8,4,4,4,4,8,8,8,8,4,4,4,8,6,8,4],
                     [160.0,160.0,108.0,258.0,360.0,225.0,360.0,146.7,140.8,167.6,167.6,
                     275.8,275.8,275.8,472.0,460.0,440.0,78.7,75.7,71.1,120.1,318.0,304.0,
                     350.0,400.0,79.0,120.3,95.1,351.0,145.0,301.0,121.0]), 0.8144262510988963, 0.00005;

is-approx kendall_cor_coef([1,1,3,4,3], [2,2,3,4,7]), 0.7071067811865476, 1e-12;
is-approx kendall_cor_coef([1,1,3,4,3,3],[2,2,3,4,7,7]), 0.5853694070049635, 1e-12;

done-testing;




# Currently the code below is not working, Find a way to make it work.

#`[

if $*KERNEL ~~ 'win32' {
    srand(1); # From Raku docs: Note that srand is called with a platform dependent value when a Raku program is started.
    # This is tested on my own Windows 10
    my @x = (1..1000).roll(1000);
    my @y = (200..7000).roll(1000);
    is kendall_cor_coef(@x, @y), 0.0022575558527578145;
}
elsif $*KERNEL ~~ 'linux' {
    srand(1); # From Raku docs: Note that srand is called with a platform dependent value when a Raku program is started.
    # This is tested on repl.it Linux platform
    my @x = (1..1000).roll(1000);
    my @y = (200..7000).roll(1000);
    is kendall_cor_coef(@x, @y), 0.028950499271910752;

}
else {
    skip "Not tested on Mac !", 1;
}
]




