;; some tests for statistics package (import test statistics srfi-1) ;; -- to help the tests (define (to-4-dp f) (/ (round (* f 10000)) 10000)) (define (to-5-dp f) (/ (round (* f 100000)) 100000)) (define (=4 n1 n2) (= (to-4-dp n1) (to-4-dp n2))) (define (=5 n1 n2) (= (to-5-dp n1) (to-5-dp n2))) (test-group "utilities" (test "cumsum" '(1 3 6 10 15) (cumsum '(1 2 3 4 5))) (test "average-rank" 3 (average-rank 3 '(1 2 3 4 5))) (test "average-rank" 7/2 (average-rank 3 '(1 2 3 3 4 5))) (test-assert "beta-incomplete" (=5 0.28367 (beta-incomplete 0.2 0.1 0.3))) (test "bin-and-count" (vector 5 5) (bin-and-count '(1 2 3 4 5 6 7 8 9 10) 2)) (test "bin-and-count" (vector 2 2 2 2 2) (bin-and-count '(1 2 3 4 5 6 7 8 9 10) 5)) (test "bin-and-count" (vector 5 1) (bin-and-count '(1 2 2 2 3 9) 2)) (test "combinations" 120 (combinations 10 3) ) (test "factorial" 3628800 (factorial 10)) (test-assert "fisher-z-transform" (=5 (fisher-z-transform 0.1) 0.10034)) (test-assert "fisher-z-transform" (=5 (fisher-z-transform 0.5) 0.54931)) (test "gamma-incomplete" (list 0.44217 0.0) (let-values (((a b) (gamma-incomplete 2.0 1.5))) (list (to-5-dp a) b))) (test-assert "gamma-ln" (=5 (gamma-ln 0.5) 0.57236)) (test "permutations" 720 (permutations 10 3)) ) ;(test (random-weighted-sample 3 '(1 2 3 4 5 7 8 9) '(0.15 0.025 0.4 0.1 0.35 0.2 0.15 0.25 0.05 0.01)) ; (=> (lambda (result expected) (= (length result) expected))) 3) (test-group "descriptive statistics" (test "mean" 0 (mean '())) (test "mean" 1 (mean '(1))) (test "mean" 3 (mean '(1 2 3 4 5)) ) (test "median" 1 (median '(1))) (test "median" 3 (median '(1 2 3 4 5))) (test "median" 4 (median '(1 1 2 3 4 5 6 7 8))) (test "mode" '((1) 3) (let-values (((modes counts) (mode '(1 1 1 2 3 4 5)))) (list modes counts))) (test "mode" '((1 4) 3) (let-values (((modes counts) (mode '(1 1 1 2 3 4 4 4 5)))) (list modes counts))) (test "geometric mean" 10.0 (geometric-mean '(1 100))) (test-assert "geometric mean" (=5 2.60517 (geometric-mean '(1 2 3 4 5)))) (test "range" 4 (range '(1 2 3 4 5))) (test "range" 9 (range '(1 1 1 2 3 4 10))) (test "percentile" 7/2 (percentile '(1 2 3 4 5 6) 50)) (test "percentile" 2 (percentile '(1 2 3 4 5 6) 30)) (test "variance" 5/2 (variance '(1 2 3 4 5))) (test-assert "standard deviation" (=5 1.58113883 (standard-deviation '(1 2 3 4 5)))) (test-assert "coefficient of variation" (=5 52.704627 (coefficient-of-variation '(1 2 3 4 5)))) (test-assert (=5 (standard-error-of-the-mean '(1 2 3 4 5)) 0.707106781186548)) (let-values (((mean sd n) (mean-sd-n '(1 2 3 4 5)))) (test-assert "mean sd" (=5 sd 1.58113883008419))) ) (print (binomial-probability 10 0 0.5)) (test-group "distributional functions" (test-assert "binomial-probability" (every (lambda (pair) (=5 (binomial-probability 10 (car pair) 0.5) (cadr pair))) '((0 0.0009765625) (1 0.009765625) (2 0.0439453125) (3 0.1171875) (4 0.205078125) (5 0.24609375) (6 0.205078125) (7 0.1171875) (8 0.0439453125) (9 0.009765625) (10 0.0009765625)))) (test-assert "binomial-cumulative-probability" (every (lambda (pair) (=5 (binomial-cumulative-probability 10 (car pair) 0.5) (cadr pair))) '((0 0.0) (1 0.0009765625) (2 0.0107421875) (3 0.0546875) (4 0.171875) (5 0.376953125) (6 0.623046875) (7 0.828125) (8 0.9453125) (9 0.9892578125) (10 0.9990234375)))) (test-assert "binomial-cumulative-probability" (every (lambda (pair) (=4 (poisson-probability 10 (car pair)) (cadr pair))) '((0 0.0000) (1 0.0005) (2 0.0023) (3 0.0076) (4 0.0189) (5 0.0378) (6 0.0631) (7 0.0901) (8 0.1126) (9 0.1251) (10 0.1251) (11 0.1137) (12 0.0948) (13 0.0729) (14 0.0521) (15 0.0347) (16 0.0217) (17 0.0128) (18 0.0071) (19 0.0037)))) (test-assert "poisson-cumulative-probability" (every (lambda (pair) (=4 (poisson-cumulative-probability 10 (car pair)) (cadr pair))) '((0 0.0000) (1 0.0000) (2 0.0005) (3 0.0028) (4 0.0103) (5 0.0293) (6 0.0671) (7 0.1301) (8 0.2202) (9 0.3328) (10 0.4579) (11 0.5830) (12 0.6968) (13 0.7916) (14 0.8645) (15 0.9165) (16 0.9513) (17 0.9730) (18 0.9857) (19 0.9928)))) (test-assert "normal-pdf" (every (lambda (pair) (=4 (normal-pdf (car pair) 5 4) (cadr pair))) '((0 0.0088) (1 0.0270) (2 0.0648) (3 0.1210) (4 0.1760) (5 0.1995) (6 0.1760) (7 0.1210) (8 0.0648) (9 0.0270)(10 0.0088)))) (test-assert "phi" (every (lambda (pair) (=4 (phi (car pair)) (cadr pair))) '((-2.0 0.0228) (-1.6 0.0548) (-1.2 0.1151) (-0.8 0.2119) (-0.4 0.3446) ( 0.0 0.5000) ( 0.4 0.6554) ( 0.8 0.7881) ( 1.2 0.8849) ( 1.6 0.9452)))) (test-assert "z" (every (lambda (pair) (=5 (z (car pair)) (cadr pair))) '((0.1 -1.2815515713806909) (0.2 -0.8416212245351449)))) (test "convert-to-standard-normal" -1/2 (convert-to-standard-normal 5 6 2)) (test-assert "t-distribution" (=5 3.07763671875 (t-distribution 1 0.9))) (test-assert "t-distribution" (=5 1.3721923828125 (t-distribution 10 0.9))) (test-assert "chi-square" (=5 (chi-square 10 0.4405) 10)) (test-assert "chi-square" (=5 (chi-square 3 0.1718) 5)) (test-assert "chi-square-cdf" (=5 (chi-square-cdf 10 10) 0.559506714934786)) (test-assert "chi-square-cdf" (=5 (chi-square-cdf 5 3) 0.828202855703266)) ) (test-group "tests of confidence intervals" (let-values (((upper-bound lower-bound) (binomial-probability-ci 10 0.8 0.9))) (test-assert "binomial-probability-ci" (=5 upper-bound 0.724273681640625)) (test-assert "binomial-probability-ci" (=5 lower-bound 0.851547241210938)) ) (let-values (((upper-bound lower-bound) (poisson-mu-ci 10 0.9))) (test-assert "poisson-mu-ci" (=5 upper-bound 8.305419921875)) (test-assert "poisson-mu-ci" (=5 lower-bound 10.0635986328125))) (let-values (((upper-bound lower-bound) (normal-mean-ci 0.5 0.1 10 0.8))) (test-assert "normal-mean-ci" (=5 upper-bound 0.491747852700165)) (test-assert "normal-mean-ci" (=5 lower-bound 0.508252147299835))) (let-values (((upper-bound lower-bound) (normal-mean-ci-on-sequence '(1 2 3 4 5) 0.9))) (test-assert "normal-mean-ci-on-sequence" (=5 upper-bound 2.90535368828478)) (test-assert "normal-mean-ci-on-sequence" (=5 lower-bound 3.09464631171522))) ) (test-group "hypothesis testing" (test-assert "z-test" (=5 (z-test 40 1 #:mu 50 #:sigma 10 #:tails ':negative) 0.15865525)) (test-assert "z-test" (=5 (z-test 40 10 #:mu 50 #:sigma 10 #:tails ':negative) 0.000783)) (test-assert "z-test" (=5 (z-test 40 5 #:mu 50 #:sigma 10) 0.025347)) (test-assert "z-test" (=5 (z-test 11/5 5 #:mu 2 #:sigma 1) 0.65472085)) (test-assert "z-test-on-sequence" (=5 (z-test-on-sequence '(1 1 2 3 4) #:mu 2 #:sigma 1) 0.65472085)) (test-assert "t-test-one-sample" (=5 (t-test-one-sample 5 0.8 3 6) 0.162781641721079)) (test-assert "t-test-one-sample-on-sequence" (=5 (t-test-one-sample-on-sequence '(5 6 7) 5.9) 0.878433865229034)) (test-assert "correlation-test-two-sample" (=5 (correlation-test-two-sample 0.9 100 0.85 50) 0.224083300908794)) (test-assert "t-test-paired" (=5 (t-test-paired 7/3 0.57735 3) 0.0198039411803931)) (test-assert "t-test-paired-on-sequences" (=5 (t-test-paired-on-sequences '(4 3 5) '(1 1 3)) 0.0198039411803931)) ) (let-values (((s p) (spearman-rank-correlation '((4 5) (10 8) (3 6) (1 2) (9 10) (2 3) (6 9) (7 4) (8 7) (5 1))))) (test-assert "spearman-rank-correlation" (=5 s (/ 113 165))) (test-assert "spearman-rank-correlation" (=5 p 0.0288827975067328))) (test-group "sample size estimates" (test "t-test-one-sample-sse" 163 (t-test-one-sample-sse 5.0 5.2 0.5)) (let-values (((n1 n2) (t-test-two-sample-sse 5.1 0.5 5.2 0.3))) (test "t-test-two-sample-sse" 1040 n1) (test "t-test-two-sample-sse" 1040 n2)) (test "correlation-sse" 11 (correlation-sse 0.80 #:alpha 0.05 #:1-beta 0.9)) ) ;; NOTE: Not same as some on-line sites describe ... (test-group "correlation and regression" (let-values (((a b r r2 p) (linear-regression '((1.0 0.1) (2.0 0.3) (3.0 0.8))))) (test "linear-regression" -0.3 a) (test-assert "linear-regression" (=5 b 0.35)) (test-assert "linear-regression" (=5 r 0.970725343394151)) (test-assert "linear-regression" (=5 r2 0.942307692307692)) (test-assert "linear-regression" (=5 p 0.154420958311267)) ) (test-assert "correlation-coefficient" (=5 0.970725343394151 (correlation-coefficient '((1.0 0.1) (2.0 0.3) (3.0 0.8))))) ) (test-group "significance tests" (test-assert "t-significance" (=5 (t-significance 0.2 5) 0.849360513995829)) (test-assert "t-significance" (=5 (t-significance 0.2 5 #:tails ':both) 0.849360513995829)) (test-assert "t-significance" (=5 (t-significance 0.2 5 #:tails ':positive) 0.424680256997915)) (test-assert "t-significance" (=5 (t-significance 0.2 5 #:tails ':negative) 0.575319743002086)) (test-assert "f-significance" (=5 (f-significance 1.5 8 2) 0.920449812578091)) (test-assert "f-significance" (=5 (f-significance 1.5 8 2 #:one-tailed? #t) 0.460224906289046)) (test-assert "binomial-test-two-sample" (=5 (binomial-test-two-sample 0.2 100 0.3 50) 0.245930683028145)) (test-assert "fisher-exact-test" (=5 (fisher-exact-test 10 20 30 40) 0.5066621427235114)) (test-assert "lambert-W0" (=5 (lambert-W0 1.0) 0.567143290410)) (test-assert "lambert-Wm1" (=5 (lambert-Wm1 1.0) 0.567143290410)) ) (test-exit)