use traquer::statistic::distribution::*; mod common; #[test] fn test_kurtosis() { let stats = common::test_data(); let result = kurtosis(&stats.close, 16).collect::>(); assert_eq!(stats.close.len(), result.len()); assert_eq!( vec![ 1.3103030757850638, 1.102747994753904, 3.06719774124363, 2.193365559939223, -0.9756569944812274, -0.5481498668335418, -0.2243968276571402, -0.9521890050850512, -0.37669821534673487, -0.2913451763689965, -0.7833694623815677, -0.5970746750197158, -0.6755548198272634, -0.6448793229503962, -0.9854966855493053, -1.1611158440747498, -1.00010972265145, -0.5654849882747519, -0.9958264091354074, ], result[16 - 1..] ); } #[test] fn test_skew() { let stats = common::test_data(); let result = skew(&stats.close, 16).collect::>(); assert_eq!(stats.close.len(), result.len()); assert_eq!( vec![ 1.3535582408038451, 1.298528601661446, 1.6254206574818177, 1.162585198346388, 0.19295159682513383, 0.40411386820995304, 0.6117924727238208, 0.285232085638137, 0.4564095498787098, 0.45766697953883545, 0.20396596725267968, 0.11996720728103474, 0.18545745211753306, 0.27852019344431006, 0.24824251094586902, 0.15953878421297624, 0.1407199604842263, 0.242851303974619, 0.2493021518683219, ], result[16 - 1..] ); } #[test] fn test_variance() { let stats = common::test_data(); let result = variance(&stats.close, 16).collect::>(); assert_eq!(stats.close.len(), result.len()); assert_eq!( vec![ 43.90139609932976, 46.75740616903386, 41.57331639003853, 20.183342535580266, 10.74866320257297, 9.298350945759921, 9.048442557741282, 5.976738957513533, 5.0368163517604785, 4.918951663781115, 4.269350375557906, 3.7288959446559033, 4.02582761118515, 5.394361230708228, 6.027443564058331, 7.401350057245281, 8.318585264683861, 10.545326561237516, 9.357811578119936, ], result[16 - 1..] ); assert_eq!( std_dev(&stats.close, 16).collect::>()[16 - 1..], result[16 - 1..] .iter() .map(|x| x.sqrt()) .collect::>() ); } #[test] fn test_std_dev() { let stats = common::test_data(); let result = std_dev(&stats.close, 16).collect::>(); assert_eq!(stats.close.len(), result.len()); assert_eq!( vec![ 6.625812863289285, 6.8379387368587805, 6.44773730777228, 4.49258751006369, 3.278515396116506, 3.049319751315024, 3.0080629244983026, 2.444736991480583, 2.244285265237126, 2.2178709754584722, 2.0662406383473115, 1.9310349413348022, 2.0064465134124934, 2.3225764208542694, 2.45508524578238, 2.720542235887045, 2.8841957743336115, 3.2473568576978904, 3.0590540332135254 ], result[15..] ); } #[test] fn test_zscore() { let stats = common::test_data(); let result = zscore(&stats.close, 16).collect::>(); assert_eq!(stats.close.len(), result.len()); assert_eq!( vec![ -1.271259155793192, -1.0206841722070752, -1.1880139694813163, -0.8120318127129288, -1.047348810068407, -0.5964443141822275, -0.2798729861249545, -0.022752455562283176, -0.2102560145932557, 1.1407336131745787, 1.6745380685199318, 0.8942736349721612, 1.5509389311366226, 1.9724865138719467, 1.3805525742768725, 1.7071506051200664, 1.7983787172684174, 2.015678119257573, 1.2447672598149504, ], result[15..] ); } #[test] fn test_mad() { let stats = common::test_data(); let result = mad(&stats.close, 16).collect::>(); assert_eq!(stats.close.len(), result.len()); assert_eq!( vec![ 5.039296746253967, 5.229140520095825, 4.5250003933906555, 3.3781254291534424, 2.8657814264297485, 2.695000171661377, 2.6033595204353333, 2.09703129529953, 1.829843819141388, 1.8025001883506775, 1.7024999856948853, 1.5822653472423553, 1.6441404223442078, 1.947343647480011, 2.1082810163497925, 2.407577931880951, 2.541874885559082, 2.770625114440918, 2.6209373474121094, ], result[16 - 1..] ); } #[test] fn test_cv() { let stats = common::test_data(); let result = cv(&stats.close, 16).collect::>(); assert_eq!(stats.close.len(), result.len()); assert_eq!( vec![ 0.13683158257033826, 0.14210364752937538, 0.13768390530536678, 0.09902519643982984, 0.07398415562575059, 0.06952591506700934, 0.06889449679876426, 0.05658376599695556, 0.052287679715081685, 0.0516986237343169, 0.04825410158953634, 0.04520940441061039, 0.04691453064000403, 0.05386465089357498, 0.05676415631458615, 0.06236110606004932, 0.0652339267899012, 0.07239777237919846, 0.06725089364652528, ], result[16 - 1..] ); } #[test] fn test_quantile_even() { let stats = common::test_data(); let result = quantile(&stats.close, 16, 90.0).collect::>(); assert_eq!(stats.close.len(), result.len()); assert_eq!( vec![ 58.77499961853027, 58.77499961853027, 54.10000038146973, 49.885000228881836, 48.44000053405762, 47.275001525878906, 47.275001525878906, 46.57000160217285, 45.96500015258789, 45.790000915527344, 45.790000915527344, 45.21500015258789, 45.65500068664551, 46.079999923706055, 46.459999084472656, 47.170000076293945, 47.98500061035156, 48.83500099182129, 49.34749984741211, ], result[16 - 1..] ); assert_eq!( quantile(&stats.close, 16, 50.0).collect::>()[16 - 1..], median(&stats.close, 16).collect::>()[16 - 1..] ); } #[test] fn test_quantile_odd() { let stats = common::test_data(); let result = quantile(&stats.close, 15, 90.0).collect::>(); assert_eq!(stats.close.len(), result.len()); assert_eq!( vec![ 58.97999954223633, 58.97999954223633, 54.83000030517577, 49.99800033569336, 48.61600036621094, 47.332001495361325, 47.332001495361325, 46.65400161743164, 46.002000427246095, 45.69000091552734, 45.258000183105466, 45.258000183105466, 45.06999969482422, 45.70000076293945, 46.119999694824216, 46.49599914550781, 47.27600021362305, 48.04200057983398, 48.948001098632815, 49.358000183105474, ], result[15 - 1..] ); assert_eq!( quantile(&stats.close, 15, 50.0).collect::>()[15 - 1..], median(&stats.close, 15).collect::>()[15 - 1..] ); } #[test] fn test_median_even() { let stats = common::test_data(); let result = median(&stats.close, 16).collect::>(); assert_eq!(stats.close.len(), result.len()); assert_eq!( vec![ 46.07500076293945, 46.060001373291016, 45.875, 45.38999938964844, 44.76499938964844, 43.39999961853027, 42.545000076293945, 42.545000076293945, 42.36000061035156, 42.36000061035156, 42.36000061035156, 42.36000061035156, 42.36000061035156, 42.635000228881836, 42.635000228881836, 42.98500061035156, 43.795000076293945, 44.93499946594238, 45.65500068664551, ], result[16 - 1..] ); } #[test] fn test_median_odd() { let stats = common::test_data(); let result = median(&stats.close, 15).collect::>(); assert_eq!(stats.close.len(), result.len()); assert_eq!( vec![ 46.150001525878906, 46.150001525878906, 45.970001220703125, 45.779998779296875, 45.0, 44.529998779296875, 42.27000045776367, 42.27000045776367, 42.27000045776367, 42.27000045776367, 42.27000045776367, 42.27000045776367, 42.27000045776367, 42.45000076293945, 42.45000076293945, 42.81999969482422, 43.150001525878906, 44.439998626708984, 45.43000030517578, 45.880001068115234, ], result[15 - 1..] ); } #[test] fn test_rank() { let input = &[0., 2., 3., 2., 0., 2.]; assert_eq!( rank(input, Some(RankMode::Min)).collect::>(), rank(input, None).collect::>() ); assert_eq!( vec![1., 3., 6., 3., 1., 3.], rank(input, None).collect::>() ); assert_eq!( vec![2., 5., 6., 5., 2., 5.], rank(input, Some(RankMode::Max)).collect::>() ); assert_eq!( vec![1., 3., 6., 4., 2., 5.], rank(input, Some(RankMode::Ordinal)).collect::>() ); assert_eq!( vec![1.5, 4.0, 6.0, 4.0, 1.5, 4.0], rank(input, Some(RankMode::Average)).collect::>() ); assert_eq!( vec![1., 2., 3., 2., 1., 2.], rank(input, Some(RankMode::Dense)).collect::>() ); } #[test] fn test_approx_entropy() { let stats = common::test_data(); let result = approx_entropy( &stats .close .iter() .zip(&stats.close[1..]) .map(|(x, y)| (y / x).ln()) .collect::>(), 16, Some(2), Some(0.1), ) .collect::>(); assert_eq!(stats.close.len() - 1, result.len()); assert_eq!( vec![ 0.3899689516358784, 0.40809929232328257, 0.25004368014823874, 0.12031030800877052, 0.17403528234884807, 0.15900363263070655, 0.13096809113760582, 0.1104630098022307, 0.1194853281985502, 0.12858861838106073, 0.15395314742114657, 0.11830103980290227, 0.11462052987046589, 0.0758266584781783, 0.06679983850538206, 0.036712468860765346, 0.020434877610718875, 0.016727783315437166 ], result[16 - 1..] ); let result = approx_entropy( &stats .close .iter() .zip(&stats.close[1..]) .map(|(x, y)| (y / x).ln()) .collect::>(), 16, Some(2), None, ) .collect::>(); assert_eq!( vec![ 0.06899287148695166, 0.06899287148695166, 0.06899287148695166, 0.06899287148695166, 0.023426752587707433, 0.023426752587707433, 0.11584637666236697, 0.11584637666236697, 0.023426752587707433, 0.023426752587707877, 0.11584637666236697, 0.11584637666236697, 0.11584637666236697, 0.11584637666236697, 0.20826600073702695, 0.18248579418728994, 0.18248579418728994, 0.18248579418728994 ], result[16 - 1..] ); } #[test] fn test_sample_entropy() { let stats = common::test_data(); let result = sample_entropy( &stats .close .iter() .zip(&stats.close[1..]) .map(|(x, y)| (y / x).ln()) .collect::>(), 16, Some(2), Some(0.1), ) .collect::>(); assert_eq!(stats.close.len() - 1, result.len()); assert_eq!( vec![ 0.5465437063680699, 0.503905180921417, 0.3764775712349121, 0.2876820724517809, 0.3483066942682157, 0.3184537311185346, 0.2799600263578706, 0.25452986513488046, 0.26469255422708216, 0.27570588140506525, 0.30010459245033816, 0.2635845208727204, 0.2612154992636177, 0.22040006536845885, 0.21007146974685703, 0.18026182383094402, 0.1643030512912763, 0.16077322058775173 ], result[16 - 1..] ); let result = sample_entropy( &stats .close .iter() .zip(&stats.close[1..]) .map(|(x, y)| (y / x).ln()) .collect::>(), 16, Some(2), None, ) .collect::>(); assert_eq!( vec![ -0.0, -0.0, -0.0, -0.0, 36.7368005696771, 36.7368005696771, 37.42994775023705, 37.42994775023705, 36.7368005696771, 36.7368005696771, 37.42994775023705, 37.42994775023705, 37.42994775023705, 37.42994775023705, 37.83541285834521, 1.3862943611198906, 1.3862943611198906, 1.3862943611198906 ], result[16 - 1..] ); }