2020-02-26 14:14:06 +03:00
|
|
|
import math
|
2021-09-13 10:20:38 +03:00
|
|
|
import math.stats
|
2019-07-19 12:50:32 +03:00
|
|
|
|
|
|
|
fn test_freq() {
|
|
|
|
// Tests were also verified on Wolfram Alpha
|
2021-05-08 13:32:29 +03:00
|
|
|
data := [f64(10.0), f64(10.0), f64(5.9), f64(2.7)]
|
|
|
|
mut o := stats.freq(data, 10.0)
|
2019-07-19 12:50:32 +03:00
|
|
|
assert o == 2
|
2021-05-08 13:32:29 +03:00
|
|
|
o = stats.freq(data, 2.7)
|
2019-07-19 12:50:32 +03:00
|
|
|
assert o == 1
|
2021-05-08 13:32:29 +03:00
|
|
|
o = stats.freq(data, 15)
|
2019-07-19 12:50:32 +03:00
|
|
|
assert o == 0
|
|
|
|
}
|
|
|
|
|
2020-02-26 14:14:06 +03:00
|
|
|
fn tst_res(str1 string, str2 string) bool {
|
|
|
|
if (math.abs(str1.f64() - str2.f64())) < 1e-5 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-07-19 12:50:32 +03:00
|
|
|
fn test_mean() {
|
|
|
|
// Tests were also verified on Wolfram Alpha
|
2021-05-08 13:32:29 +03:00
|
|
|
mut data := [f64(10.0), f64(4.45), f64(5.9), f64(2.7)]
|
2019-07-19 12:50:32 +03:00
|
|
|
mut o := stats.mean(data)
|
|
|
|
// Some issue with precision comparison in f64 using == operator hence serializing to string
|
2020-02-26 14:14:06 +03:00
|
|
|
assert tst_res(o.str(), '5.762500')
|
2021-05-08 13:32:29 +03:00
|
|
|
data = [f64(-3.0), f64(67.31), f64(4.4), f64(1.89)]
|
2019-07-19 12:50:32 +03:00
|
|
|
o = stats.mean(data)
|
|
|
|
// Some issue with precision comparison in f64 using == operator hence serializing to string
|
2020-02-26 14:14:06 +03:00
|
|
|
assert tst_res(o.str(), '17.650000')
|
2021-05-08 13:32:29 +03:00
|
|
|
data = [f64(12.0), f64(7.88), f64(76.122), f64(54.83)]
|
2019-07-19 12:50:32 +03:00
|
|
|
o = stats.mean(data)
|
|
|
|
// Some issue with precision comparison in f64 using == operator hence serializing to string
|
2020-02-26 14:14:06 +03:00
|
|
|
assert tst_res(o.str(), '37.708000')
|
2019-07-19 12:50:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_geometric_mean() {
|
|
|
|
// Tests were also verified on Wolfram Alpha
|
2021-05-08 13:32:29 +03:00
|
|
|
mut data := [f64(10.0), f64(4.45), f64(5.9), f64(2.7)]
|
2019-07-19 12:50:32 +03:00
|
|
|
mut o := stats.geometric_mean(data)
|
|
|
|
// Some issue with precision comparison in f64 using == operator hence serializing to string
|
2021-05-08 13:32:29 +03:00
|
|
|
assert tst_res(o.str(), '5.15993')
|
|
|
|
data = [f64(-3.0), f64(67.31), f64(4.4), f64(1.89)]
|
2019-07-19 12:50:32 +03:00
|
|
|
o = stats.geometric_mean(data)
|
|
|
|
// Some issue with precision comparison in f64 using == operator hence serializing to string
|
2021-09-13 10:20:38 +03:00
|
|
|
ok := o.str() == 'nan' || o.str() == '-nan' || o.str() == '-1.#IND00' || o == f64(0)
|
|
|
|
|| o.str() == '-nan(ind)'
|
|
|
|
assert ok // Because in math it yields a complex number
|
2021-05-08 13:32:29 +03:00
|
|
|
data = [f64(12.0), f64(7.88), f64(76.122), f64(54.83)]
|
2019-07-19 12:50:32 +03:00
|
|
|
o = stats.geometric_mean(data)
|
|
|
|
// Some issue with precision comparison in f64 using == operator hence serializing to string
|
2021-05-08 13:32:29 +03:00
|
|
|
assert tst_res(o.str(), '25.064496')
|
2019-07-19 12:50:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_harmonic_mean() {
|
|
|
|
// Tests were also verified on Wolfram Alpha
|
2021-05-08 13:32:29 +03:00
|
|
|
mut data := [f64(10.0), f64(4.45), f64(5.9), f64(2.7)]
|
2019-07-19 12:50:32 +03:00
|
|
|
mut o := stats.harmonic_mean(data)
|
|
|
|
// Some issue with precision comparison in f64 using == operator hence serializing to string
|
2020-02-26 14:14:06 +03:00
|
|
|
assert tst_res(o.str(), '4.626519')
|
2021-05-08 13:32:29 +03:00
|
|
|
data = [f64(-3.0), f64(67.31), f64(4.4), f64(1.89)]
|
2019-07-19 12:50:32 +03:00
|
|
|
o = stats.harmonic_mean(data)
|
|
|
|
// Some issue with precision comparison in f64 using == operator hence serializing to string
|
2020-02-26 14:14:06 +03:00
|
|
|
assert tst_res(o.str(), '9.134577')
|
2021-05-08 13:32:29 +03:00
|
|
|
data = [f64(12.0), f64(7.88), f64(76.122), f64(54.83)]
|
2019-07-19 12:50:32 +03:00
|
|
|
o = stats.harmonic_mean(data)
|
|
|
|
// Some issue with precision comparison in f64 using == operator hence serializing to string
|
2020-02-26 14:14:06 +03:00
|
|
|
assert tst_res(o.str(), '16.555477')
|
2019-07-19 12:50:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_median() {
|
|
|
|
// Tests were also verified on Wolfram Alpha
|
|
|
|
// Assumes sorted array
|
|
|
|
|
|
|
|
// Even
|
2021-05-08 13:32:29 +03:00
|
|
|
mut data := [f64(2.7), f64(4.45), f64(5.9), f64(10.0)]
|
2019-07-19 12:50:32 +03:00
|
|
|
mut o := stats.median(data)
|
|
|
|
// Some issue with precision comparison in f64 using == operator hence serializing to string
|
2020-02-26 14:14:06 +03:00
|
|
|
assert tst_res(o.str(), '5.175000')
|
2021-05-08 13:32:29 +03:00
|
|
|
data = [f64(-3.0), f64(1.89), f64(4.4), f64(67.31)]
|
2019-07-19 12:50:32 +03:00
|
|
|
o = stats.median(data)
|
|
|
|
// Some issue with precision comparison in f64 using == operator hence serializing to string
|
2020-02-26 14:14:06 +03:00
|
|
|
assert tst_res(o.str(), '3.145000')
|
2021-05-08 13:32:29 +03:00
|
|
|
data = [f64(7.88), f64(12.0), f64(54.83), f64(76.122)]
|
2019-07-19 12:50:32 +03:00
|
|
|
o = stats.median(data)
|
|
|
|
// Some issue with precision comparison in f64 using == operator hence serializing to string
|
2020-02-26 14:14:06 +03:00
|
|
|
assert tst_res(o.str(), '33.415000')
|
2019-07-19 12:50:32 +03:00
|
|
|
|
|
|
|
// Odd
|
2021-05-08 13:32:29 +03:00
|
|
|
data = [f64(2.7), f64(4.45), f64(5.9), f64(10.0), f64(22)]
|
2019-07-19 12:50:32 +03:00
|
|
|
o = stats.median(data)
|
|
|
|
assert o == f64(5.9)
|
2021-05-08 13:32:29 +03:00
|
|
|
data = [f64(-3.0), f64(1.89), f64(4.4), f64(9), f64(67.31)]
|
2019-07-19 12:50:32 +03:00
|
|
|
o = stats.median(data)
|
|
|
|
assert o == f64(4.4)
|
2021-05-08 13:32:29 +03:00
|
|
|
data = [f64(7.88), f64(3.3), f64(12.0), f64(54.83), f64(76.122)]
|
2019-07-19 12:50:32 +03:00
|
|
|
o = stats.median(data)
|
|
|
|
assert o == f64(12.0)
|
|
|
|
}
|
|
|
|
|
2019-07-23 01:16:07 +03:00
|
|
|
fn test_mode() {
|
2019-07-19 12:50:32 +03:00
|
|
|
// Tests were also verified on Wolfram Alpha
|
2021-05-08 13:32:29 +03:00
|
|
|
mut data := [f64(2.7), f64(2.7), f64(4.45), f64(5.9), f64(10.0)]
|
2019-07-19 12:50:32 +03:00
|
|
|
mut o := stats.mode(data)
|
|
|
|
assert o == f64(2.7)
|
2021-05-08 13:32:29 +03:00
|
|
|
data = [f64(-3.0), f64(1.89), f64(1.89), f64(1.89), f64(9), f64(4.4), f64(4.4), f64(9),
|
2021-09-21 16:20:09 +03:00
|
|
|
f64(67.31)]
|
2019-07-19 12:50:32 +03:00
|
|
|
o = stats.mode(data)
|
|
|
|
assert o == f64(1.89)
|
|
|
|
// Testing greedy nature
|
2021-05-08 13:32:29 +03:00
|
|
|
data = [f64(2.0), f64(4.0), f64(2.0), f64(4.0)]
|
2019-07-19 12:50:32 +03:00
|
|
|
o = stats.mode(data)
|
|
|
|
assert o == f64(2.0)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_rms() {
|
|
|
|
// Tests were also verified on Wolfram Alpha
|
2021-05-08 13:32:29 +03:00
|
|
|
mut data := [f64(10.0), f64(4.45), f64(5.9), f64(2.7)]
|
2019-07-19 12:50:32 +03:00
|
|
|
mut o := stats.rms(data)
|
|
|
|
// Some issue with precision comparison in f64 using == operator hence serializing to string
|
2020-02-26 14:14:06 +03:00
|
|
|
assert tst_res(o.str(), '6.362046')
|
2021-05-08 13:32:29 +03:00
|
|
|
data = [f64(-3.0), f64(67.31), f64(4.4), f64(1.89)]
|
2019-07-19 12:50:32 +03:00
|
|
|
o = stats.rms(data)
|
|
|
|
// Some issue with precision comparison in f64 using == operator hence serializing to string
|
2020-02-26 14:14:06 +03:00
|
|
|
assert tst_res(o.str(), '33.773393')
|
2021-05-08 13:32:29 +03:00
|
|
|
data = [f64(12.0), f64(7.88), f64(76.122), f64(54.83)]
|
2019-07-19 12:50:32 +03:00
|
|
|
o = stats.rms(data)
|
|
|
|
// Some issue with precision comparison in f64 using == operator hence serializing to string
|
2020-02-26 14:14:06 +03:00
|
|
|
assert tst_res(o.str(), '47.452561')
|
2019-07-19 12:50:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_population_variance() {
|
|
|
|
// Tests were also verified on Wolfram Alpha
|
2021-05-08 13:32:29 +03:00
|
|
|
mut data := [f64(10.0), f64(4.45), f64(5.9), f64(2.7)]
|
2019-07-19 12:50:32 +03:00
|
|
|
mut o := stats.population_variance(data)
|
|
|
|
// Some issue with precision comparison in f64 using == operator hence serializing to string
|
2020-02-26 14:14:06 +03:00
|
|
|
assert tst_res(o.str(), '7.269219')
|
2021-05-08 13:32:29 +03:00
|
|
|
data = [f64(-3.0), f64(67.31), f64(4.4), f64(1.89)]
|
2019-07-19 12:50:32 +03:00
|
|
|
o = stats.population_variance(data)
|
|
|
|
// Some issue with precision comparison in f64 using == operator hence serializing to string
|
2020-02-26 14:14:06 +03:00
|
|
|
assert tst_res(o.str(), '829.119550')
|
2021-05-08 13:32:29 +03:00
|
|
|
data = [f64(12.0), f64(7.88), f64(76.122), f64(54.83)]
|
2019-07-19 12:50:32 +03:00
|
|
|
o = stats.population_variance(data)
|
|
|
|
// Some issue with precision comparison in f64 using == operator hence serializing to string
|
2020-02-26 14:14:06 +03:00
|
|
|
assert tst_res(o.str(), '829.852282')
|
2019-07-19 12:50:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_sample_variance() {
|
|
|
|
// Tests were also verified on Wolfram Alpha
|
2021-05-08 13:32:29 +03:00
|
|
|
mut data := [f64(10.0), f64(4.45), f64(5.9), f64(2.7)]
|
2019-07-19 12:50:32 +03:00
|
|
|
mut o := stats.sample_variance(data)
|
|
|
|
// Some issue with precision comparison in f64 using == operator hence serializing to string
|
2020-02-26 14:14:06 +03:00
|
|
|
assert tst_res(o.str(), '9.692292')
|
2021-05-08 13:32:29 +03:00
|
|
|
data = [f64(-3.0), f64(67.31), f64(4.4), f64(1.89)]
|
2019-07-19 12:50:32 +03:00
|
|
|
o = stats.sample_variance(data)
|
|
|
|
// Some issue with precision comparison in f64 using == operator hence serializing to string
|
2020-02-26 14:14:06 +03:00
|
|
|
assert tst_res(o.str(), '1105.492733')
|
2021-05-08 13:32:29 +03:00
|
|
|
data = [f64(12.0), f64(7.88), f64(76.122), f64(54.83)]
|
2019-07-19 12:50:32 +03:00
|
|
|
o = stats.sample_variance(data)
|
|
|
|
// Some issue with precision comparison in f64 using == operator hence serializing to string
|
2020-02-26 14:14:06 +03:00
|
|
|
assert tst_res(o.str(), '1106.469709')
|
2019-07-19 12:50:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_population_stddev() {
|
|
|
|
// Tests were also verified on Wolfram Alpha
|
2021-05-08 13:32:29 +03:00
|
|
|
mut data := [f64(10.0), f64(4.45), f64(5.9), f64(2.7)]
|
2019-07-19 12:50:32 +03:00
|
|
|
mut o := stats.population_stddev(data)
|
|
|
|
// Some issue with precision comparison in f64 using == operator hence serializing to string
|
2020-02-26 14:14:06 +03:00
|
|
|
assert tst_res(o.str(), '2.696149')
|
2021-05-08 13:32:29 +03:00
|
|
|
data = [f64(-3.0), f64(67.31), f64(4.4), f64(1.89)]
|
2019-07-19 12:50:32 +03:00
|
|
|
o = stats.population_stddev(data)
|
|
|
|
// Some issue with precision comparison in f64 using == operator hence serializing to string
|
2020-02-26 14:14:06 +03:00
|
|
|
assert tst_res(o.str(), '28.794436')
|
2021-05-08 13:32:29 +03:00
|
|
|
data = [f64(12.0), f64(7.88), f64(76.122), f64(54.83)]
|
2019-07-19 12:50:32 +03:00
|
|
|
o = stats.population_stddev(data)
|
|
|
|
// Some issue with precision comparison in f64 using == operator hence serializing to string
|
2020-02-26 14:14:06 +03:00
|
|
|
assert tst_res(o.str(), '28.807157')
|
2019-07-19 12:50:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_sample_stddev() {
|
|
|
|
// Tests were also verified on Wolfram Alpha
|
2021-05-08 13:32:29 +03:00
|
|
|
mut data := [f64(10.0), f64(4.45), f64(5.9), f64(2.7)]
|
2019-07-19 12:50:32 +03:00
|
|
|
mut o := stats.sample_stddev(data)
|
|
|
|
// Some issue with precision comparison in f64 using == operator hence serializing to string
|
2020-02-26 14:14:06 +03:00
|
|
|
assert tst_res(o.str(), '3.113245')
|
2021-05-08 13:32:29 +03:00
|
|
|
data = [f64(-3.0), f64(67.31), f64(4.4), f64(1.89)]
|
2019-07-19 12:50:32 +03:00
|
|
|
o = stats.sample_stddev(data)
|
|
|
|
// Some issue with precision comparison in f64 using == operator hence serializing to string
|
2020-02-26 14:14:06 +03:00
|
|
|
assert tst_res(o.str(), '33.248951')
|
2021-05-08 13:32:29 +03:00
|
|
|
data = [f64(12.0), f64(7.88), f64(76.122), f64(54.83)]
|
2019-07-19 12:50:32 +03:00
|
|
|
o = stats.sample_stddev(data)
|
|
|
|
// Some issue with precision comparison in f64 using == operator hence serializing to string
|
2020-02-26 14:14:06 +03:00
|
|
|
assert tst_res(o.str(), '33.263639')
|
2019-07-19 12:50:32 +03:00
|
|
|
}
|
|
|
|
|
2021-09-13 10:20:38 +03:00
|
|
|
fn test_absdev() {
|
2019-07-19 12:50:32 +03:00
|
|
|
// Tests were also verified on Wolfram Alpha
|
2021-05-08 13:32:29 +03:00
|
|
|
mut data := [f64(10.0), f64(4.45), f64(5.9), f64(2.7)]
|
2021-09-13 10:20:38 +03:00
|
|
|
mut o := stats.absdev(data)
|
2019-07-19 12:50:32 +03:00
|
|
|
// Some issue with precision comparison in f64 using == operator hence serializing to string
|
2020-02-26 14:14:06 +03:00
|
|
|
assert tst_res(o.str(), '2.187500')
|
2021-05-08 13:32:29 +03:00
|
|
|
data = [f64(-3.0), f64(67.31), f64(4.4), f64(1.89)]
|
2021-09-13 10:20:38 +03:00
|
|
|
o = stats.absdev(data)
|
2019-07-19 12:50:32 +03:00
|
|
|
// Some issue with precision comparison in f64 using == operator hence serializing to string
|
2020-02-26 14:14:06 +03:00
|
|
|
assert tst_res(o.str(), '24.830000')
|
2021-05-08 13:32:29 +03:00
|
|
|
data = [f64(12.0), f64(7.88), f64(76.122), f64(54.83)]
|
2021-09-13 10:20:38 +03:00
|
|
|
o = stats.absdev(data)
|
2019-07-19 12:50:32 +03:00
|
|
|
// Some issue with precision comparison in f64 using == operator hence serializing to string
|
2020-02-26 14:14:06 +03:00
|
|
|
assert tst_res(o.str(), '27.768000')
|
2019-07-19 12:50:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_min() {
|
|
|
|
// Tests were also verified on Wolfram Alpha
|
2021-05-08 13:32:29 +03:00
|
|
|
mut data := [f64(10.0), f64(4.45), f64(5.9), f64(2.7)]
|
2019-07-19 12:50:32 +03:00
|
|
|
mut o := stats.min(data)
|
|
|
|
assert o == f64(2.7)
|
2021-05-08 13:32:29 +03:00
|
|
|
data = [f64(-3.0), f64(67.31), f64(4.4), f64(1.89)]
|
2019-07-19 12:50:32 +03:00
|
|
|
o = stats.min(data)
|
|
|
|
assert o == f64(-3.0)
|
2021-05-08 13:32:29 +03:00
|
|
|
data = [f64(12.0), f64(7.88), f64(76.122), f64(54.83)]
|
2019-07-19 12:50:32 +03:00
|
|
|
o = stats.min(data)
|
|
|
|
assert o == f64(7.88)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_max() {
|
|
|
|
// Tests were also verified on Wolfram Alpha
|
2021-05-08 13:32:29 +03:00
|
|
|
mut data := [f64(10.0), f64(4.45), f64(5.9), f64(2.7)]
|
2019-07-19 12:50:32 +03:00
|
|
|
mut o := stats.max(data)
|
|
|
|
assert o == f64(10.0)
|
2021-05-08 13:32:29 +03:00
|
|
|
data = [f64(-3.0), f64(67.31), f64(4.4), f64(1.89)]
|
2019-07-19 12:50:32 +03:00
|
|
|
o = stats.max(data)
|
|
|
|
assert o == f64(67.31)
|
2021-05-08 13:32:29 +03:00
|
|
|
data = [f64(12.0), f64(7.88), f64(76.122), f64(54.83)]
|
2019-07-19 12:50:32 +03:00
|
|
|
o = stats.max(data)
|
|
|
|
assert o == f64(76.122)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_range() {
|
|
|
|
// Tests were also verified on Wolfram Alpha
|
2021-05-08 13:32:29 +03:00
|
|
|
mut data := [f64(10.0), f64(4.45), f64(5.9), f64(2.7)]
|
2019-07-19 12:50:32 +03:00
|
|
|
mut o := stats.range(data)
|
|
|
|
assert o == f64(7.3)
|
2021-05-08 13:32:29 +03:00
|
|
|
data = [f64(-3.0), f64(67.31), f64(4.4), f64(1.89)]
|
2019-07-19 12:50:32 +03:00
|
|
|
o = stats.range(data)
|
|
|
|
assert o == f64(70.31)
|
2021-05-08 13:32:29 +03:00
|
|
|
data = [f64(12.0), f64(7.88), f64(76.122), f64(54.83)]
|
2019-07-19 12:50:32 +03:00
|
|
|
o = stats.range(data)
|
|
|
|
assert o == f64(68.242)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_passing_empty() {
|
2020-04-26 14:49:31 +03:00
|
|
|
data := []f64{}
|
2021-05-08 13:32:29 +03:00
|
|
|
assert stats.freq(data, 0) == 0
|
2019-07-19 12:50:32 +03:00
|
|
|
assert stats.mean(data) == f64(0)
|
|
|
|
assert stats.geometric_mean(data) == f64(0)
|
|
|
|
assert stats.harmonic_mean(data) == f64(0)
|
|
|
|
assert stats.median(data) == f64(0)
|
|
|
|
assert stats.mode(data) == f64(0)
|
|
|
|
assert stats.rms(data) == f64(0)
|
|
|
|
assert stats.population_variance(data) == f64(0)
|
|
|
|
assert stats.sample_variance(data) == f64(0)
|
|
|
|
assert stats.population_stddev(data) == f64(0)
|
|
|
|
assert stats.sample_stddev(data) == f64(0)
|
2021-09-13 10:20:38 +03:00
|
|
|
assert stats.absdev(data) == f64(0)
|
2019-07-19 12:50:32 +03:00
|
|
|
assert stats.min(data) == f64(0)
|
|
|
|
assert stats.max(data) == f64(0)
|
|
|
|
assert stats.range(data) == f64(0)
|
2020-04-26 14:49:31 +03:00
|
|
|
}
|