text
stringlengths 12
786k
|
---|
let map_nz ( ) = Alcotest . ( check bool ) " map_nz " true ( To_test . map_nz ( ) ) |
let fold ( ) = Alcotest . ( check bool ) " fold " true ( To_test . fold ( ) ) |
let foldi ( ) = Alcotest . ( check bool ) " foldi " true ( To_test . foldi ( ) ) |
let fold_nz ( ) = Alcotest . ( check bool ) " fold_nz " true ( To_test . fold_nz ( ) ) |
let foldi_nz ( ) = Alcotest . ( check bool ) " foldi_nz " true ( To_test . foldi_nz ( ) ) |
let add ( ) = Alcotest . ( check bool ) " add " true ( To_test . add ( ) ) |
let mul ( ) = Alcotest . ( check bool ) " mul " true ( To_test . mul ( ) ) |
let add_scalar ( ) = Alcotest . ( check bool ) " add_scalar " true ( To_test . add_scalar ( ) ) |
let mul_scalar ( ) = Alcotest . ( check bool ) " mul_scalar " true ( To_test . mul_scalar ( ) ) |
let abs ( ) = Alcotest . ( check bool ) " abs " true ( To_test . abs ( ) ) |
let neg ( ) = Alcotest . ( check bool ) " neg " true ( To_test . neg ( ) ) |
let sum ( ) = Alcotest . ( check bool ) " sum " true ( To_test . sum ( ) ) |
let min ( ) = Alcotest . ( check bool ) " min " true ( To_test . min ( ) ) |
let max ( ) = Alcotest . ( check bool ) " max " true ( To_test . max ( ) ) |
let is_zero ( ) = Alcotest . ( check bool ) " is_zero " false ( To_test . is_zero ( ) ) |
let is_positive ( ) = Alcotest . ( check bool ) " is_positive " false ( To_test . is_positive ( ) ) |
let is_negative ( ) = Alcotest . ( check bool ) " is_negative " false ( To_test . is_negative ( ) ) |
let is_nonnegative ( ) = Alcotest . ( check bool ) " is_nonnegative " true ( To_test . is_nonnegative ( ) ) |
let equal ( ) = Alcotest . ( check bool ) " equal " false ( To_test . equal ( ) ) |
let greater ( ) = Alcotest . ( check bool ) " greater " false ( To_test . greater ( ) ) |
let greater_equal ( ) = Alcotest . ( check bool ) " greater_equal " true ( To_test . greater_equal ( ) ) |
let filter ( ) = Alcotest . ( check bool ) " filter " true ( To_test . filter ( ) ) |
let filteri ( ) = Alcotest . ( check bool ) " filteri " true ( To_test . filteri ( ) ) |
let filteri_nz ( ) = Alcotest . ( check bool ) " filteri_nz " true ( To_test . filteri_nz ( ) ) |
let transpose ( ) = Alcotest . ( check bool ) " transpose " true ( To_test . transpose ( ) ) |
let flatten ( ) = Alcotest . ( check bool ) " flatten " true ( To_test . flatten ( ) ) |
let reshape ( ) = Alcotest . ( check bool ) " reshape " true ( To_test . reshape ( ) ) |
let of_array ( ) = Alcotest . ( check bool ) " of_array " true ( To_test . of_array ( ) ) |
let save_load ( ) = Alcotest . ( check bool ) " save_load " true ( To_test . save_load ( ) ) |
let test_set = [ " shape " , ` Slow , shape ; " num_dims " , ` Slow , num_dims ; " nth_dim " , ` Slow , nth_dim ; " numel " , ` Slow , numel ; " nnz " , ` Slow , nnz ; " density " , ` Slow , density ; " get " , ` Slow , get ; " set " , ` Slow , set ; " slice " , ` Slow , slice ; " copy " , ` Slow , copy ; " map " , ` Slow , map ; " map_nz " , ` Slow , map_nz ; " fold " , ` Slow , fold ; " foldi " , ` Slow , foldi ; " fold_nz " , ` Slow , fold_nz ; " foldi_nz " , ` Slow , foldi_nz ; " add " , ` Slow , add ; " mul " , ` Slow , mul ; " add_scalar " , ` Slow , add_scalar ; " mul_scalar " , ` Slow , mul_scalar ; " abs " , ` Slow , abs ; " neg " , ` Slow , neg ; " sum " , ` Slow , sum ; " min " , ` Slow , min ; " max " , ` Slow , max ; " is_zero " , ` Slow , is_zero ; " is_positive " , ` Slow , is_positive ; " is_negative " , ` Slow , is_negative ; " is_nonnegative " , ` Slow , is_nonnegative ; " equal " , ` Slow , equal ; " greater " , ` Slow , greater ; " greater_equal " , ` Slow , greater_equal ; " filter " , ` Slow , filter ; " filteri " , ` Slow , filteri ; " filteri_nz " , ` Slow , filteri_nz ; " transpose " , ` Slow , transpose ; " flatten " , ` Slow , flatten ; " reshape " , ` Slow , reshape ; " of_array " , ` Slow , of_array ; " save_load " , ` Slow , save_load ] |
module To_test = struct let mannwhitneyu_both_side x y = let h = M . mannwhitneyu x y in h . p_value let mannwhitneyu_right_side x y = let h = M . mannwhitneyu ~ side : M . RightSide x y in h . p_value let mannwhitneyu_left_side x y = let h = M . mannwhitneyu ~ side : M . LeftSide x y in h . p_value let wilcoxon_both_side x y = let h = M . wilcoxon x y in h . p_value let wilcoxon_right_side x y = let h = M . wilcoxon ~ side : M . RightSide x y in h . p_value let wilcoxon_left_side x y = let h = M . wilcoxon ~ side : M . LeftSide x y in h . p_value let fisher_test_both_side a b c d = let h = M . fisher_test a b c d in h . p_value let fisher_test_right_side a b c d = let h = M . fisher_test ~ side : M . RightSide a b c d in h . p_value let fisher_test_left_side a b c d = let h = M . fisher_test ~ side : M . LeftSide a b c d in h . p_value let ks_test_pval a b = let h = M . ks_test a b in h . p_value let ks_test_statistic a b = let h = M . ks_test a b in h . score let ks2_test_pval a b = let h = M . ks2_test a b in h . p_value let ks2_test_statistic a b = let h = M . ks2_test a b in h . score let hist_uni n x = let h = M . histogram ( ` N n ) x in h . counts let hist_uni_sorted n x = let h = M . histogram_sorted ( ` N n ) x in h . counts let hist_bin bins x = let h = M . histogram ( ` Bins bins ) x in h . counts let hist_bin_sorted bins x = let h = M . histogram_sorted ( ` Bins bins ) x in h . counts let hist_bin_weighted bins weights x = let h = M . histogram ( ` Bins bins ) ~ weights x in h . counts , h . weighted_counts let hist_bin_weighted_sorted bins weights x = let h = M . histogram_sorted ( ` Bins bins ) ~ weights x in h . counts , h . weighted_counts let hist_normalise bins ? weights x = let h = M . histogram ( ` Bins bins ) ? weights x |> M . normalise |> M . normalise_density in h . normalised_counts , h . density let tukey_fences x = M . tukey_fences x let quantiles ~ p x = let f = M . quantile x in Some ( Array . map f p ) end |
module Data = struct let x1 = [ | 0 . ; - 1 . ; 0 . ; sqrt 0 . 5 ; 2 . ] | let x1_sorted = [ | - 1 . ; 0 . ; 0 . ; sqrt 0 . 5 ; 2 . ] | let w1 = [ | 0 . ; 2 . ; 1 . ; 1 . ; 0 . 2 ] | let w1_sorted = [ | 2 . ; 0 . ; 1 . ; 1 . ; 0 . 2 ] | let bin1 = [ | - 1 . ; 0 . ] | let bin_wrong = [ | 4 . ] | let bin_low = [ | - 3 . ; - 2 . ] | let bin_high = [ | 3 . ; 4 . ] | let x2 = Array . append x1 [ | infinity ] | let x2_sorted = Array . append x1_sorted [ | infinity ] | let w2 = Array . append w1 [ | 0 . 2 ] | let bin2 = [ | - 1 . ; 0 . ; sqrt 0 . 5 ; 3 . ] | let bin2_inf = [ | . - infinity ; 0 . ; sqrt 0 . 5 ; infinity ] | let with_outliers = [ | - 10000 . 0 ; 0 . 0 ; 1 . 0 ; 2 . 0 ; 10000 . 0 ] | end |
let mannwhitneyu_test_both_side_asym ( ) = Alcotest . ( check bool ) " mannwhitneyu_test_both_side_asym " true ( abs_float ( To_test . mannwhitneyu_both_side [ | 4 . ; 5 . ; 6 . ; 7 . ; 7 . ; 7 . ] | [ | 1 . ; 2 . ; 3 . ; 3 . ] ) | . - 0 . 0093747684594348759 < eps ) |
let mannwhitneyu_test_right_side_asym ( ) = Alcotest . ( check bool ) " mannwhitneyu_test_right_side_asym " true ( abs_float ( To_test . mannwhitneyu_right_side [ | 4 . ; 5 . ; 6 . ; 7 . ; 7 . ; 7 . ] | [ | 1 . ; 2 . ; 3 . ; 3 . ] ) | . - 0 . 0046873842297174379 < eps ) |
let mannwhitneyu_test_left_side_asym ( ) = Alcotest . ( check bool ) " mannwhitneyu_test_left_side_asym " true ( abs_float ( To_test . mannwhitneyu_left_side [ | 4 . ; 5 . ; 6 . ; 7 . ; 7 . ; 7 . ] | [ | 1 . ; 2 . ; 3 . ; 3 . ] ) | . - 0 . 99531261577028252 < eps ) |
let mannwhitneyu_test_both_side_exact ( ) = Alcotest . ( check bool ) " mannwhitneyu_test_both_side_asym " true ( abs_float ( To_test . mannwhitneyu_both_side [ | 5 . ; 6 . ; 7 . ; 4 . ; 25 . ; 12 . ; 14 . ] | [ | 1 . ; 2 . ; 3 . ; 103 . ] ) | . - 0 . 2303 < 0 . 001 ) |
let mannwhitneyu_test_right_side_exact ( ) = Alcotest . ( check bool ) " mannwhitneyu_test_right_side_asym " true ( abs_float ( To_test . mannwhitneyu_right_side [ | 5 . ; 6 . ; 7 . ; 4 . ; 25 . ; 12 . ; 14 . ] | [ | 1 . ; 2 . ; 3 . ; 103 . ] ) | . - 0 . 1152 < 0 . 001 ) |
let mannwhitneyu_test_left_side_exact ( ) = Alcotest . ( check bool ) " mannwhitneyu_test_left_side_asym " true ( abs_float ( To_test . mannwhitneyu_left_side [ | 5 . ; 6 . ; 7 . ; 4 . ; 25 . ; 12 . ; 14 . ] | [ | 1 . ; 2 . ; 3 . ; 103 . ] ) | . - 0 . 9182 < 0 . 001 ) |
let fisher_test_both_side ( ) = Alcotest . ( check bool ) " fisher_test_both_side " true ( abs_float ( To_test . fisher_test_both_side 45 25 10 15 . - 0 . 057776279489461277 ) < eps ) |
let fisher_test_right_side ( ) = Alcotest . ( check bool ) " fisher_test_right_side " true ( abs_float ( To_test . fisher_test_right_side 45 25 10 15 . - 0 . 0307852082455 ) < eps ) |
let fisher_test_left_side ( ) = Alcotest . ( check bool ) " fisher_test_left_side " true ( abs_float ( To_test . fisher_test_left_side 45 25 10 15 . - 0 . 990376800656 ) < eps ) |
let wilcoxon_test_both_side_exact ( ) = Alcotest . ( check bool ) " wilcoxon_test_both_side_exact " true ( abs_float ( To_test . wilcoxon_both_side [ | 1 . ; 2 . ; 3 . ; 4 . ; 5 . ] | [ | 10 . ; 9 . ; 8 . ; 7 . ; 6 . ] | . - 0 . 0625 ) < 0 . 001 ) |
let wilcoxon_test_right_side_exact ( ) = Alcotest . ( check bool ) " wilcoxon_test_right_side_exact " true ( abs_float ( To_test . wilcoxon_right_side [ | 1 . ; 2 . ; 3 . ; 4 . ; 5 . ] | [ | 10 . ; 9 . ; 8 . ; 7 . ; 6 . ] | . - 1 . ) < 0 . 001 ) |
let wilcoxon_test_left_side_exact ( ) = Alcotest . ( check bool ) " wilcoxon_test_left_side_exact " true ( abs_float ( To_test . wilcoxon_left_side [ | 1 . ; 2 . ; 3 . ; 4 . ; 5 . ] | [ | 10 . ; 9 . ; 8 . ; 7 . ; 6 . ] | . - 0 . 03125 ) < 0 . 001 ) |
let wilcoxon_test_both_side_asymp ( ) = Alcotest . ( check bool ) " wilcoxon_test_both_side_asymp " true ( abs_float ( To_test . wilcoxon_both_side [ | 10 . ; 9 . ; 8 . ; 7 . ; 6 . ] | [ | 10 . ; 3 . ; 1 . ; 3 . ; 2 . ] | . - 0 . 0656 ) < 0 . 001 ) |
let wilcoxon_test_right_side_asymp ( ) = Alcotest . ( check bool ) " wilcoxon_test_right_side_asymp " true ( abs_float ( To_test . wilcoxon_right_side [ | 10 . ; 9 . ; 8 . ; 7 . ; 6 . ] | [ | 10 . ; 3 . ; 1 . ; 3 . ; 2 . ] | . - 0 . 9672 ) < 0 . 001 ) |
let wilcoxon_test_left_side_asymp ( ) = Alcotest . ( check bool ) " wilcoxon_test_left_side_asymp " true ( abs_float ( To_test . wilcoxon_left_side [ | 10 . ; 9 . ; 8 . ; 7 . ; 6 . ] | [ | 10 . ; 3 . ; 1 . ; 3 . ; 2 . ] | . - 0 . 0328 ) < 0 . 001 ) |
let ks_teststat1 ( ) = Alcotest . ( check bool ) " ks_test test stat " true ( abs_float ( To_test . ks_test_statistic [ | 1 . ; 2 . ; 3 . ] | ( fun x -> M . gaussian_cdf ~ mu : 0 . ~ sigma : 1 . x ) . - 0 . 8413 ) < 0 . 0001 ) |
let ks_teststat2 ( ) = Alcotest . ( check bool ) " ks_test pval 4 " true ( abs_float ( To_test . ks_test_statistic [ | 0 . ; 0 . ; 0 . ] | ( fun x -> M . gaussian_cdf ~ mu : 0 . ~ sigma : 1 . x ) . - 0 . 5 ) < 0 . 0001 ) |
let ks_pval_test1 ( ) = Alcotest . ( check bool ) " ks_test pval 1 " true ( abs_float ( To_test . ks_test_pval [ | 1 . ; 2 . ; 3 . ] | ( fun x -> M . gaussian_cdf ~ mu : 0 . ~ sigma : 1 . x ) . - 0 . 0079 ) < 0 . 0001 ) |
let ks_pval_test2 ( ) = Alcotest . ( check bool ) " ks_test pval 2 " true ( abs_float ( To_test . ks_test_pval [ | 0 . ; 0 . 25 ; 0 . 15 ; 0 . 05 ] | ( fun x -> M . gaussian_cdf ~ mu : 0 . ~ sigma : 1 . x ) . - 0 . 1875 ) < 0 . 0001 ) |
let ks_pval_test3 ( ) = Alcotest . ( check bool ) " ks_test pval 3 " true ( abs_float ( To_test . ks_test_pval ( Array . mapi ( fun i _ -> 0 . 99 . / 2000 . . * float_of_int i ) ( Array . create_float 2000 ) ) ( fun x -> M . uniform_cdf ~ a : 0 . ~ b : 1 . x ) . - 0 . 98025 ) < 0 . 00001 ) |
let ks_pval_test4 ( ) = Alcotest . ( check bool ) " ks_test pval 4 " true ( abs_float ( To_test . ks_test_pval ( Array . mapi ( fun i _ -> 0 . 99 . / 10_000 . . * float_of_int i ) ( Array . create_float 10_000 ) ) ( fun x -> M . uniform_cdf ~ a : 0 . ~ b : 1 . x ) . - 0 . 25953 ) < 0 . 00001 ) |
let ks_pval_test5 ( ) = Alcotest . check_raises " ks_test exception " Owl_exception . EMPTY_ARRAY ( fun _ -> ignore ( To_test . ks_test_pval [ ] || ( fun x -> M . gaussian_cdf ~ mu : 0 . ~ sigma : 1 . x ) ) ) |
let ks2_teststat ( ) = Alcotest . ( check bool ) " ks2_test test statistic " true ( abs_float ( To_test . ks2_test_statistic [ | 0 . ; 2 . ; 3 . ; 1 . ; 4 . ] | [ | 6 . ; 5 . ; 3 . ; 4 . ] | . - 0 . 6 ) < 0 . 0001 ) |
let ks2_pval_test1 ( ) = Alcotest . ( check bool ) " ks2_test p value " true ( abs_float ( To_test . ks2_test_pval [ | 0 . ; 2 . ; 3 . ; 1 . ; 4 . ] | [ | 6 . ; 5 . ; 3 . ; 4 . ] | . - 0 . 2587 ) < 0 . 0001 ) |
let ks2_pval_test2 ( ) = Alcotest . ( check bool ) " ks2_test p value " true ( abs_float ( To_test . ks2_test_pval [ | 8 . 3653642 ; 9 . 39604144 ; 9 . 567219 ; 8 . 95912556 ; 9 . 97116074 ] | [ | 1 . 7835817 ; - 0 . 37877421 ; - 1 . 21761325 ; 0 . 91270459 ; - 1 . 06491371 ] | . - 0 . 003781 ) < 0 . 0001 ) |
let ks2_pval_test3 ( ) = Alcotest . check_raises " ks2_test exception " Owl_exception . EMPTY_ARRAY ( fun _ -> ignore ( To_test . ks2_test_pval [ ] || [ | 1 . ; 2 . ; 3 . ] ) ) | |
let hist_uni_1 ( ) = Alcotest . ( check ( array int ) ) " uniform histogram " [ | 1 ; 3 ; 1 ] | ( To_test . hist_uni 3 Data . x1 ) |
let hist_uni_sorted_1 ( ) = Alcotest . ( check ( array int ) ) " uniform histogram sorted " [ | 1 ; 3 ; 1 ] | ( To_test . hist_uni_sorted 3 Data . x1_sorted ) |
let hist_bins_1 ( ) = Alcotest . ( check ( array int ) ) " histogram with bins " [ | 3 ] | ( To_test . hist_bin Data . bin1 Data . x1 ) |
let hist_bins_low ( ) = Alcotest . ( check ( array int ) ) " bin below " [ | 0 ] | ( To_test . hist_bin Data . bin_low Data . x1 ) |
let hist_bins_high ( ) = Alcotest . ( check ( array int ) ) " bin above " [ | 0 ] | ( To_test . hist_bin Data . bin_high Data . x1 ) |
let hist_bins_sorted_low ( ) = Alcotest . ( check ( array int ) ) " bin below sorted " [ | 0 ] | ( To_test . hist_bin_sorted Data . bin_low Data . x1_sorted ) |
let hist_bins_sorted_high ( ) = Alcotest . ( check ( array int ) ) " bin above sorted " [ | 0 ] | ( To_test . hist_bin Data . bin_high Data . x1_sorted ) |
let hist_bins_wrong ( ) = Alcotest . ( check_raises ) " histogram with bins " ( Failure " Need at least two bin boundaries " ) ! ( fun ( ) -> To_test . hist_bin Data . bin_wrong Data . x1 |> ignore ) |
let hist_bins_2 ( ) = Alcotest . ( check ( array int ) ) " histogram with infinite bins " [ | 1 ; 2 ; 2 ] | ( To_test . hist_bin Data . bin2 Data . x2 ) |
let hist_bins_sorted_2 ( ) = Alcotest . ( check ( array int ) ) " histogram with infinite bins sorted " [ | 1 ; 2 ; 2 ] | ( To_test . hist_bin_sorted Data . bin2 Data . x2_sorted ) |
let fao = let open Alcotest in let fl = testable Fmt . float ( fun x y -> x = y || abs_float ( x . - y ) <= eps ) in option ( array fl ) |
let hist_bins_weights ( ) = Alcotest . ( check ( pair ( array int ) fao ) ) " weighted histogram with bins " ( [ | 3 ] , | Some [ | 3 . ] ) | ( To_test . hist_bin_weighted Data . bin1 Data . w1 Data . x1 ) |
let hist_bins_weights_sorted ( ) = Alcotest . ( check ( pair ( array int ) fao ) ) " weighted histogram with bins sorted " ( [ | 3 ] , | Some [ | 3 . ] ) | ( To_test . hist_bin_weighted_sorted Data . bin1 Data . w1_sorted Data . x1_sorted ) |
let hist_bins_normalise ( ) = Alcotest . ( check ( pair fao fao ) ) " normalisation test unweighted " ( Some [ | 1 . . / 5 . ; 2 . . / 5 . ; 2 . . / 5 . ] | , Some [ | 1 . . / 5 . ; 2 . . / 5 . . / sqrt 0 . 5 ; 2 . . / 5 . . / ( 3 . . - sqrt 0 . 5 ) ] | ) ( To_test . hist_normalise Data . bin2 Data . x2 ) |
let hist_bins_normalise_weights ( ) = Alcotest . ( check ( pair fao fao ) ) " normalisation test with weights " ( Some [ | 2 . . / 4 . 2 ; 1 . . / 4 . 2 ; 1 . 2 . / 4 . 2 ] | , Some [ | 2 . . / 4 . 2 ; 1 . . / 4 . 2 . / sqrt 0 . 5 ; 1 . 2 . / 4 . 2 . / ( 3 . . - sqrt 0 . 5 ) ] | ) ( To_test . hist_normalise Data . bin2 ~ weights : Data . w2 Data . x2 ) |
let hist_bins_normalise_binf ( ) = Alcotest . ( check ( pair fao fao ) ) " normalisation test unweighted , infinite bins " ( Some [ | 1 . . / 6 . ; 2 . . / 6 . ; 3 . . / 6 . ] , | Some [ | 0 . ; 2 . . / 6 . . / sqrt 0 . 5 ; 0 . ] ) | ( To_test . hist_normalise Data . bin2_inf Data . x2 ) |
let tukey_fences ( ) = Alcotest . ( check ( pair ( float 0 . 0 ) ( float 0 . 0 ) ) ) " tukey fences test with data containing outliers " ( - 3 . 0 , 5 . 0 ) ( To_test . tukey_fences Data . with_outliers ) |
let quantiles ( ) = Alcotest . ( check fao ) " quantiles calculation " ( Some [ | 0 . 106500 ; 0 . 190700 ; 0 . 376750 ; 0 . 767625 ; 0 . 978800 ] ) | ( To_test . quantiles ~ p [ :| 0 . ; 0 . 25 ; 0 . 5 ; 0 . 75 ; 1 . ] | [ | 0 . 1182 ; 0 . 8388 ; 0 . 4569 ; 0 . 1902 ; 0 . 8652 ; 0 . 2966 ; 0 . 9788 ; 0 . 5541 ; 0 . 1065 ; 0 . 1922 ] ) | |
let test_set = [ " mannwhitneyu_test_left_side_asym " , ` Slow , mannwhitneyu_test_left_side_asym ; " mannwhitneyu_test_right_side_asym " , ` Slow , mannwhitneyu_test_right_side_asym ; " mannwhitneyu_test_both_side_asym " , ` Slow , mannwhitneyu_test_both_side_asym ; " mannwhitneyu_test_both_side_exact " , ` Slow , mannwhitneyu_test_both_side_exact ; " mannwhitneyu_test_right_side_exact " , ` Slow , mannwhitneyu_test_right_side_exact ; " mannwhitneyu_test_left_side_exact " , ` Slow , mannwhitneyu_test_left_side_exact ; " wilcoxon_test_both_side_exact " , ` Slow , wilcoxon_test_both_side_exact ; " wilcoxon_test_right_side_exact " , ` Slow , wilcoxon_test_right_side_exact ; " wilcoxon_test_left_side_exact " , ` Slow , wilcoxon_test_left_side_exact ; " wilcoxon_test_both_side_asymp " , ` Slow , wilcoxon_test_both_side_asymp ; " wilcoxon_test_right_side_asymp " , ` Slow , wilcoxon_test_right_side_asymp ; " wilcoxon_test_left_side_asymp " , ` Slow , wilcoxon_test_left_side_asymp ; " fisher_test_both_side " , ` Slow , fisher_test_both_side ; " fisher_test_right_side " , ` Slow , fisher_test_right_side ; " fisher_test_left_side " , ` Slow , fisher_test_left_side ; " ks_teststat1 " , ` Slow , ks_teststat1 ; " ks_teststat2 " , ` Slow , ks_teststat2 ; " ks_pval_test1 " , ` Slow , ks_pval_test1 ; " ks_pval_test2 " , ` Slow , ks_pval_test2 ; " ks_pval_test3 " , ` Slow , ks_pval_test3 ; " ks_pval_test4 " , ` Slow , ks_pval_test4 ; " ks_pval_test5 " , ` Slow , ks_pval_test5 ; " ks2_pval_test1 " , ` Slow , ks2_pval_test1 ; " ks2_pval_test2 " , ` Slow , ks2_pval_test2 ; " ks2_pval_test3 " , ` Slow , ks2_pval_test3 ; " ks2_teststat " , ` Slow , ks2_teststat ; " hist_uni_1 " , ` Slow , hist_uni_1 ; " hist_uni_sorted_1 " , ` Slow , hist_uni_sorted_1 ; " hist_bins_1 " , ` Slow , hist_bins_1 ; " hist_bins_low " , ` Slow , hist_bins_low ; " hist_bins_high " , ` Slow , hist_bins_high ; " hist_bins_sorted_low " , ` Slow , hist_bins_low ; " hist_bins_sorted_high " , ` Slow , hist_bins_high ; " hist_bins_wrong " , ` Slow , hist_bins_wrong ; " hist_bins_2 " , ` Slow , hist_bins_2 ; " hist_bins_sorted_2 " , ` Slow , hist_bins_sorted_2 ; " hist_bins_weights " , ` Slow , hist_bins_weights ; " hist_bins_weights_sorted " , ` Slow , hist_bins_weights_sorted ; " hist_bins_normalise " , ` Slow , hist_bins_normalise ; " hist_bins_normalise_weights " , ` Slow , hist_bins_normalise_weights ; " hist_bins_normalise_binf " , ` Slow , hist_bins_normalise_binf ; " tukey_fences " , ` Slow , tukey_fences ; " quantiles " , ` Slow , quantiles ] |
type ( ' a , ' b ) t_iter = ( ' a -> int -> ' b -> ' a ) -> ' a -> int * ' a |
let trace pos = let a , b , c , d = pos in Printf . printf " trace :% s :% d :% d :% d \ n " %! a b c d |
module Cubic = struct type t = { lx : float ; rx : float ; dx : float ; p0 : float ; p1 : float ; p ' 0 : float ; p ' 1 : float ; poly : float array } let f t x = let u = ( x . - t . lx ) . / t . dx in let u2 = u . * u in let u3 = u2 . * u in ( t . poly . ( 0 ) . * u3 ) . + ( t . poly . ( 1 ) . * u2 ) . + ( t . poly . ( 2 ) . * u ) . + t . poly . ( 3 ) let df t x = let u = ( x . - t . lx ) . / t . dx in let u2 = u . * u in ( ( t . poly . ( 0 ) . * u2 . * 3 . ) . + ( t . poly . ( 1 ) . * u . * 2 . ) . + t . poly . ( 2 ) ) . / t . dx let find_x_of_value t p = let rec iter n x = if n > 0 then ( let f ' = df t x in let f ' = max f ' 0 . 01 in let dx = ( f t x . - p ) . / f ' in let x ' = x . - dx in iter ( n - 1 ) x ' ) else x in iter 40 ( ( t . lx . + t . rx ) . / 2 . ) let fit lx rx p0 p ' 0 p1 p ' 1 = let dx = rx . - lx in let f0 = p0 in let f1 = p1 in let f ' 0 = dx . * p ' 0 in let f ' 1 = dx . * p ' 1 in let d = f0 in let c = f ' 0 in let a = ( 2 . . * ( f0 . - f1 ) ) . + ( f ' 1 . + f ' 0 ) in let b = ( 3 . . * ( f1 . - f0 ) ) . - ( f ' 1 . + ( 2 . . * f ' 0 ) ) in let t = { lx ; rx ; dx ; p0 ; p1 ; p ' 0 ; p ' 1 ; poly = [ | a ; b ; c ; d ] | } in t end |
module CdfTree = struct type binary_node = | Leaf of int | Balance of ( float * binary_node * binary_node ) let rec create_node cdf_x_of_p logn p dp i di = if logn <= 0 then Leaf ( i / 2 ) else ( let ndp = dp . / 2 . in let ndi = di / 2 in let node_l = create_node cdf_x_of_p ( logn - 1 ) ( p . - ndp ) ndp ( i - ndi ) ndi in let node_r = create_node cdf_x_of_p ( logn - 1 ) ( p . + ndp ) ndp ( i + ndi ) ndi in let x = cdf_x_of_p p in Balance ( x , node_l , node_r ) ) let create cdf_x_of_p logn = create_node cdf_x_of_p logn 0 . 5 0 . 5 ( 1 lsl logn ) ( 1 lsl logn ) let rec display ( ? prefix = " " ) ( ? dirn = 0 ) t = match t with | Leaf n -> Printf . printf " % s +-- % n \ n " prefix n | Balance ( x , l , r ) -> let subprefix_0 = prefix ^ ( if dirn > 0 then " " | else " " ) ^ " " in let subprefix_1 = prefix ^ ( if dirn < 0 then " " | else " " ) ^ " " in display ~ prefix : subprefix_0 ~ dirn ( :- 1 ) l ; Printf . printf " % s + % 8f -+\ n " prefix x ; display ~ prefix : subprefix_1 ~ dirn : 1 r let cdf_x_of_p ( ? min_x = - 1 . E4 ) ( ? max_x = 1 . E4 ) cdf p = let rec find_x_of_p lx rx = let dx = rx . - lx in let mx = ( lx . + rx ) . / 2 . in let mp = cdf mx in if dx < 1E - 8 then mx else if mp > p then find_x_of_p lx mx else find_x_of_p mx rx in find_x_of_p min_x max_x let rec value t x = match t with | Leaf n -> n | Balance ( nx , nl , nr ) -> if x < nx then value nl x else value nr x let create_from_rv rv logn = create ( cdf_x_of_p rv ) logn end |
let _ = let tree = CdfTree . create_from_rv ( M . gaussian_cdf ~ mu : 0 . ~ sigma : 1 . ) 5 in CdfTree . display tree ; List . iter ( fun x -> Printf . printf " bucket of % f : % d \ n " x ( CdfTree . value tree x ) ) [ - 1 . ; - 0 . 5 ; 0 . ; 0 . 5 ; 1 . ] |
module Distribution = struct type t = { name : string ; num_buckets : int ; cdf_x_array : float array ; pdf_x_array : float array ; cubics : t_cubic array ; dut_cdf : float -> float ; dut_pdf : float -> float } let create name dut_cdf dut_pdf cdf_x_array pdf_x_array = let num_buckets = Array . length cdf_x_array - 1 in let num_buckets_f = float num_buckets in let cubics = let rec add_cubic acc i = if i >= 0 then ( let lx = cdf_x_array . ( i ) in let rx = cdf_x_array . ( i + 1 ) in let p0 = float i . / num_buckets_f in let p1 = float ( i + 1 ) . / num_buckets_f in let p ' 0 = pdf_x_array . ( i ) in let p ' 1 = pdf_x_array . ( i + 1 ) in let c = Cubic . fit lx rx p0 p ' 0 p1 p ' 1 in add_cubic ( c :: acc ) ( i - 1 ) ) else acc in Array . of_list ( add_cubic [ ] ( num_buckets - 1 ) ) in { name ; num_buckets ; dut_cdf ; dut_pdf ; pdf_x_array ; cdf_x_array ; cubics } let value t i = t . cdf_x_array . ( i ) let bucket_of_p t p = let i = int_of_float ( p . * float t . num_buckets ) in min ( t . num_buckets - 1 ) ( max i 0 ) let interp_x_of_p t p = let i = bucket_of_p t p in Cubic . find_x_of_value t . cubics . ( i ) p let search_for_x t x = let rec find_x li ri = if ri <= li + 1 then li , ri else ( let mi = ( ri + li ) / 2 in let mx = t . cdf_x_array . ( mi ) in if x < mx then find_x li mi else find_x mi ri ) in let li , ri = if x <= t . cdf_x_array . ( 0 ) then 0 , 1 else if x >= t . cdf_x_array . ( t . num_buckets ) then t . num_buckets - 1 , t . num_buckets else find_x 0 t . num_buckets in li , ri , t . num_buckets let dut_cdf t = t . dut_cdf let dut_pdf t = t . dut_pdf let interp_cdf t x = let li , _ , _ = search_for_x t x in Cubic . f t . cubics . ( li ) x let interp_pdf t x = let li , _ , _ = search_for_x t x in Cubic . df t . cubics . ( li ) x end |
module RandomTest = struct type test_hypothesis = { test_name : string ; score_name : string ; hypothesis : M . hypothesis } let make_hypothesis reject p_value score : M . hypothesis = { reject ; p_value ; score } let make_test_hypothesis test_name score_name hypothesis = { test_name ; score_name ; hypothesis } let is_fail t = t . hypothesis . reject let assert_hypothesis ( ? should_reject = false ) rv_name test_hypothesis = let hypothesis = test_hypothesis . hypothesis in let name = test_hypothesis . test_name in let data_name = test_hypothesis . score_name in Printf . printf " ********************************************************************************\ n " ; Printf . printf " % s % s test \ n " rv_name name ; Printf . printf " % s = % f \ n " data_name hypothesis . score ; Printf . printf " p_value = % f \ n " hypothesis . p_value ; Alcotest . ( check bool ) ( sfmt " Statistical test ' % s ' for random variable ' % s ' " name rv_name ) should_reject hypothesis . reject ; ( ) let iterate ( sampler : int -> int -> int * ( int -> ' b ) ) ( start : int ) ( length : int ) ( f : ' c -> int -> ' b -> ' c ) ( acc : ' c ) : int * ' c = let actual_length , sampler_next = sampler start length in let rec x acc i n = if n <= 0 then acc else ( let new_acc = f acc i ( sampler_next i ) in x new_acc ( i + 1 ) ( n - 1 ) ) in length , x acc 0 actual_length let match_distribution ( ? significance = 0 . 01 ) ( dist : Distribution . t ) = let diff i = let x = float i . / 32 . in Distribution . interp_cdf dist x . - Distribution . dut_cdf dist x in let diff2 i = diff i ** 2 . in let rec sum_diff2 acc i = if i >= 0 then sum_diff2 ( acc . + diff2 i ) ( i - 1 ) else acc in let p = sum_diff2 0 . 32 . / 32 . in let hypothesis = make_hypothesis ( p < significance ) p p in hypothesis module C = Owl . Dense . Matrix . C let do_fft ( ? significance = 0 . 01 ) ( ? n = 1024 ) f iter = let x = C . zeros 1 n in let blah acc i b = if i < n then C . set x 0 i { Complex . re = f b ; im = 0 . } ; acc in let _ , _ = iter blah 0 in C . print ~ max_col : 64 x ; let f = Owl . Fft . S . fft x in C . print ~ max_col : 64 f ; let p = 0 . 95 in let q = 1 . . - p in let t2 = float n . * . - log q in Printf . printf " t2 % f \ n " t2 ; let rec count_above_threshold i acc = if i < 0 then acc else ( let c = C . get f 0 i in let new_acc = if Complex . norm2 c < t2 then acc + 1 else acc in count_above_threshold ( i - 1 ) new_acc ) in let n_over = count_above_threshold ( ( n / 2 ) - 1 ) 0 in let x = float n_over . - ( float ( n / 2 ) . * p ) in let x = x . / sqrt ( float ( 2 * n ) . * p . * q ) in let x = abs_float x in let p = Maths . erfc x in let hypothesis = make_hypothesis ( p < significance ) p ( float n_over . / float ( n / 2 ) ) in hypothesis end |
module BinaryTest = struct include RandomTest let frequency ( ? significance = 0 . 01 ) ( ? p = 0 . 5 ) iter = let q = 1 . . - p in let inc_if_true_else_dec acc _ b = if b then acc + 1 else acc - 1 in let n , sum = iter inc_if_true_else_dec 0 in let x = float sum . - ( ( ( 2 . . * p ) . - 1 . ) . * float n ) in let x = x . / sqrt ( 8 . . * p . * q . * float n ) in let x = abs_float x in let p = Maths . erfc x in let hypothesis = make_hypothesis ( p < significance ) p x in make_test_hypothesis ( sfmt " Frequency with % d samples " n ) " Partial sum " hypothesis let block_frequency ( ? significance = 0 . 01 ) ( ? p = 0 . 5 ) m iter = let number_ones_in_block ( i_of_m , acc , chi2 , n ) _ b = let v = if b then 1 else 0 in if i_of_m + 1 == m then ( let chi2_obs = ( ( float ( acc + v ) . / float m ) . - p ) ** 2 . in 0 , 0 , chi2_obs . + chi2 , n + 1 ) else i_of_m + 1 , acc + v , chi2 , n in let _ , ( _ , _ , chi2 , n ) = iter number_ones_in_block ( 0 , 0 , 0 . , 0 ) in let chi2 = chi2 . * float m . / p in let p = 1 . . - M . chi2_cdf chi2 ~ df ( : float n ) in let hypothesis = make_hypothesis ( p < significance ) p chi2 in make_test_hypothesis ( sfmt " Block frequency of % d run lengths of size % d " n m ) " Chi ^ 2 " hypothesis let occurrence_of_patterns ( ? significance = 0 . 01 ) m iter = let num_pats = 1 lsl m in let mask = num_pats - 1 in let obs_counts = Array . make num_pats 0 in let rec count_ones_in_n acc i v = if i == 0 then acc else count_ones_in_n ( acc + ( v land 1 ) ) ( i - 1 ) ( v lsr 1 ) in let count_observations ( sum_z , sum_o , m_seen , pat ) _ b = let v = if b then 1 else 0 in let new_pat = ( ( pat lsl 1 ) land mask ) + v in if m_seen == m then ( obs_counts . ( new_pat ) <- obs_counts . ( new_pat ) + 1 ; sum_z + 1 - v , sum_o + v , m_seen , new_pat ) else sum_z + 1 - v , sum_o + v , m_seen + 1 , new_pat in let n , ( sum_z , sum_o , _ , _ ) = iter count_observations ( 0 , 0 , 0 , 0 ) in let prob_z = float sum_z . / float ( n - m + 1 ) in let prob_o = float sum_o . / float ( n - m + 1 ) in let set_exp_counts i = let num_o = count_ones_in_n 0 m i in let num_z = m - num_o in ( prob_o ** float num_o ) . * ( prob_z ** float num_z ) . * float ( n - m + 1 ) in let exp_counts = Array . init num_pats set_exp_counts in let rec sum_chi2 acc i = if i == num_pats then acc else ( let obs_m = float obs_counts . ( i ) in let exp_m = exp_counts . ( i ) in sum_chi2 ( acc . + ( ( ( obs_m . - exp_m ) ** 2 . ) . / exp_m ) ) ( i + 1 ) ) in let chi2 = sum_chi2 0 . 0 in let p = 1 . . - M . chi2_cdf chi2 ~ df ( : float ( num_pats - 1 ) ) in let hypothesis = make_hypothesis ( p < significance ) p chi2 in make_test_hypothesis ( sfmt " Occurrences of % d - bit patterns in run length of size % d " m n ) " Chi ^ 2 " hypothesis let occurrences_of_patterns ( ? significance = 0 . 01 ) n m iter = let rec accum_n acc f n = if n <= 0 then acc else accum_n ( f acc ) f ( n - 1 ) in let accum acc = occurrence_of_patterns ~ significance : 0 . 01 m iter :: acc in let run_results = accum_n [ ] accum n in let num_passing = List . fold_left ( fun a i -> if is_fail i then a else a . + 1 . ) 0 . run_results in let chi2 = ( ( num_passing . - ( 0 . 99 . * float n ) ) ** 2 . ) . / ( 0 . 99 . * float n ) in let p = 1 . . - M . chi2_cdf chi2 ~ df : 1 . in let hypothesis = make_hypothesis ( p < significance ) p num_passing in make_test_hypothesis ( sfmt " % d iterations of ' % s ' " n ( List . hd run_results ) . test_name ) " Num passing " hypothesis let fft ( ? significance = 0 . 01 ) ~ n iter = let hypothesis = do_fft ~ significance ~ n ( fun b -> if b then 1 . else - 1 . ) iter in make_test_hypothesis ( sfmt " FFT of size % d " n ) " # peaks above 95 % power " hypothesis end |
let test_rough_cdf pydist = let ( name , imprecision , dut_cdf , dut_pdf , dut_logcdf , dut_logpdf , dut_opt_ppf , dut_sf , dut_logsf , dut_opt_isf , cdf_x_array , pdf_x_array ) = pydist in let dist = Distribution . create name dut_cdf dut_pdf cdf_x_array pdf_x_array in let sum_diff_cdf_2 = ref 0 . in let sum_diff_pdf_2 = ref 0 . in let max_diff_cdf_2 = ref 0 . in let max_diff_pdf_2 = ref 0 . in let sum_err_log_cdf_2 = ref 0 . in let sum_err_log_pdf_2 = ref 0 . in let sum_err_ppf_2 = ref 0 . in let sum_err_isf_2 = ref 0 . in let sum_err_sf_2 = ref 0 . in let sum_err_log_sf_2 = ref 0 . in let n = 1000 in let total = float n . * 0 . 96 in for i = n * 2 / 100 to n * 98 / 100 do let p = float i . / float n in let x = Distribution . interp_x_of_p dist p in let cdf_x = Distribution . interp_cdf dist x in let pdf_x = Distribution . interp_pdf dist x in let dut_cdf_x = Distribution . dut_cdf dist x in let dut_pdf_x = Distribution . dut_pdf dist x in let diff_cdf_2 = ( cdf_x . - dut_cdf_x ) ** 2 . in let diff_pdf_2 = ( ( pdf_x . / dut_pdf_x ) . - 1 . ) ** 2 . in let err_log_cdf_2 = ( log dut_cdf_x . - dut_logcdf x ) ** 2 . in let err_log_pdf_2 = ( log dut_pdf_x . - dut_logpdf x ) ** 2 . in let option_get o = match o with | Some x -> x | None -> raise Owl_exception . TEST_FAIL in let dut_cdf_ppf_p , dut_ppf_p , dut_isf_o_m_p = match dut_opt_ppf with | None -> p , 0 . , 0 . | Some dut_ppf -> let dut_isf = option_get dut_opt_isf in dut_cdf ( dut_ppf p ) , dut_ppf p , dut_isf ( 1 . . - p ) in let err_ppf_2 , err_isf_2 = ( dut_cdf_ppf_p . - p ) ** 2 . , ( dut_ppf_p . - dut_isf_o_m_p ) ** 2 . in let err_sf_2 = ( dut_cdf x . + dut_sf x . - 1 . ) ** 2 . in let err_log_sf_2 = ( dut_logsf x . - log ( dut_sf x ) ) ** 2 . in if ! max_diff_cdf_2 < diff_cdf_2 then max_diff_cdf_2 := diff_cdf_2 ; if ! max_diff_pdf_2 < diff_pdf_2 then max_diff_pdf_2 := diff_pdf_2 ; sum_diff_cdf_2 := ! sum_diff_cdf_2 . + diff_cdf_2 ; sum_diff_pdf_2 := ! sum_diff_pdf_2 . + diff_pdf_2 ; sum_err_log_cdf_2 := ! sum_err_log_cdf_2 . + err_log_cdf_2 ; sum_err_log_pdf_2 := ! sum_err_log_pdf_2 . + err_log_pdf_2 ; sum_err_ppf_2 := ! sum_err_ppf_2 . + err_ppf_2 ; sum_err_isf_2 := ! sum_err_isf_2 . + err_isf_2 ; sum_err_sf_2 := ! sum_err_sf_2 . + err_sf_2 ; sum_err_log_sf_2 := ! sum_err_log_sf_2 . + err_log_sf_2 done ; let avg_diff_cdf_2 = ! sum_diff_cdf_2 . / total in let avg_diff_pdf_2 = ! sum_diff_pdf_2 . / total in let avg_err_log_cdf_2 = ! sum_err_log_cdf_2 . / total in let avg_err_log_pdf_2 = ! sum_err_log_pdf_2 . / total in let avg_err_ppf_2 = ! sum_err_ppf_2 . / total in let avg_err_isf_2 = ! sum_err_isf_2 . / total in let avg_err_sf_2 = ! sum_err_sf_2 . / total in let avg_err_log_sf_2 = ! sum_err_log_sf_2 . / total in Printf . printf " % s : Avg sq diff in cdf % g pdf % g max cdf % g pdf % g \ n " name avg_diff_cdf_2 avg_diff_pdf_2 ! max_diff_cdf_2 ! max_diff_pdf_2 ; Printf . printf " Avg err logcdf % g logpdf % g ppf % g isf % g \ n " avg_err_log_cdf_2 avg_err_log_pdf_2 avg_err_ppf_2 avg_err_isf_2 ; Printf . printf " Avg err sf % g logsf % g \ n " avg_err_sf_2 avg_err_log_sf_2 ; Alcotest . ( check bool ) ( sfmt " Avg sq diff in ' % s ' cdf < 1E - 8 " name ) true ( avg_diff_cdf_2 < 1E - 8 . * imprecision ) ; Alcotest . ( check bool ) ( sfmt " Max sq diff in ' % s ' cdf < 1E - 5 " name ) true ( ! max_diff_cdf_2 < 1E - 5 . * imprecision ) ; Alcotest . ( check bool ) ( sfmt " Avg sq diff in ' % s ' pdf < 1E - 3 " name ) true ( avg_diff_pdf_2 < 1E - 3 . * imprecision ) ; Alcotest . ( check bool ) ( sfmt " Max sq diff in ' % s ' pdf < 1E - 1 " name ) true ( ! max_diff_pdf_2 < 1E - 1 . * imprecision ) ; Alcotest . ( check bool ) ( sfmt " Avg logcdf error in ' % s ' < 1E - 28 " name ) true ( avg_err_log_cdf_2 < 1E - 28 ) ; Alcotest . ( check bool ) ( sfmt " Avg logpdf error in ' % s ' < 1E - 28 " name ) true ( avg_err_log_pdf_2 < 1E - 28 ) ; Alcotest . ( check bool ) ( sfmt " Avg ppf error in ' % s ' < 1E - 28 " name ) true ( avg_err_ppf_2 < 1E - 28 ) ; Alcotest . ( check bool ) ( sfmt " Avg isf error in ' % s ' < 1E - 28 " name ) true ( avg_err_isf_2 < 1E - 28 ) ; Alcotest . ( check bool ) ( sfmt " Avg sf error in ' % s ' < 1E - 28 " name ) true ( avg_err_sf_2 < 1E - 28 ) ; Alcotest . ( check bool ) ( sfmt " Avg log sf error in ' % s ' < 1E - 28 " name ) true ( avg_err_log_sf_2 < 1E - 28 ) ; ( ) |
let test_rough_cdf_matches _ = List . iter test_rough_cdf cdf_approximations ; ( ) |
let _ = Owl_stats_prng . self_init ( ) ; Owl_stats_prng . sfmt_seed 1 |
let one_third = 1 . . / 3 . |
type t_bin_iter = | IOfBool of ( ( int , bool ) t_iter -> RandomTest . test_hypothesis ) | IIIIOfBool of ( ( int * int * int * int , bool ) t_iter -> RandomTest . test_hypothesis ) | IIFIOfBool of ( ( int * int * float * int , bool ) t_iter -> RandomTest . test_hypothesis ) |
let bin_frequency = IOfBool BinaryTest . frequency |
let bin_frequency_1_of_3 = IOfBool ( BinaryTest . frequency ~ p : one_third ) |
let bin_block_frequency_7 = IIFIOfBool ( BinaryTest . block_frequency 7 ) |
let bin_block_frequency_70 = IIFIOfBool ( BinaryTest . block_frequency 70 ) |
let bin_block_frequency_700 = IIFIOfBool ( BinaryTest . block_frequency 700 ) |
let bin_block_frequency_1_of_3_7 = IIFIOfBool ( BinaryTest . block_frequency ~ p : one_third 7 ) |
let bin_block_frequency_1_of_3_70 = IIFIOfBool ( BinaryTest . block_frequency ~ p : one_third 70 ) |
let bin_block_frequency_1_of_3_700 = IIFIOfBool ( BinaryTest . block_frequency ~ p : one_third 700 ) |
let bin_patterns_2 = IIIIOfBool ( BinaryTest . occurrences_of_patterns 8 2 ) |
let bin_patterns_3 = IIIIOfBool ( BinaryTest . occurrences_of_patterns 8 3 ) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.