test_t_test.cpp 5.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright Nick Thompson, 2019
  3. * Use, modification and distribution are subject to the
  4. * Boost Software License, Version 1.0. (See accompanying file
  5. * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #include "math_unit_test.hpp"
  8. #include <vector>
  9. #include <random>
  10. #include <boost/math/statistics/univariate_statistics.hpp>
  11. #include <boost/math/statistics/t_test.hpp>
  12. template<typename Real>
  13. void test_exact_mean()
  14. {
  15. // Of course this test seems obvious, but just for the sake of comfort, here's the Mathematica to demo this test:
  16. // data3 = RandomReal[NormalDistribution[], 1024];
  17. // NumberForm[TTest[data3, Mean[data3], "TestStatistic"], 16]
  18. // NumberForm[TTest[data3, Mean[data3], "PValue"], 16]
  19. std::mt19937 gen{5125122};
  20. std::normal_distribution<Real> dis{0,3};
  21. std::vector<Real> v(1024);
  22. for (auto & x : v) {
  23. x = dis(gen);
  24. }
  25. Real mu = boost::math::statistics::mean(v);
  26. auto [computed_statistic, computed_pvalue] = boost::math::statistics::one_sample_t_test(v, mu);
  27. CHECK_MOLLIFIED_CLOSE(Real(0), computed_statistic, 10*std::numeric_limits<Real>::epsilon());
  28. CHECK_ULP_CLOSE(Real(1), computed_pvalue, 9);
  29. }
  30. void test_agreement_with_mathematica()
  31. {
  32. // Reproduce via:
  33. //data = RandomReal[NormalDistribution[], 128];
  34. //NumberForm[data, 16]
  35. //NumberForm[TTest[data, 0.0, "TestStatistic"], 16]
  36. //NumberForm[TTest[data, 0.0, "PValue"], 16]
  37. std::vector<double> v{1.270498757865948,-0.7097895555907483,1.151445006538434,0.915648732663531,-0.7480131480881454,-0.6837323220203325,
  38. 2.362877076786142,2.438188734959438,0.1644154283470843,-0.980857299461513,-0.1448627006957758,0.04901437671768214,
  39. -0.3895499730435337,1.412356512608596,-0.3865249523080916,-0.6159322168089271,-0.1865107372684944,-0.152509328597876,
  40. 1.142603106429423,-1.358368106645048,0.2268475747885975,0.4029249376986136,0.1167407378850566,0.05532794835680535,
  41. -1.928794899326586,0.6496438708570567,0.269012797381103,-0.908168796067257,-0.6194990582309883,1.606256899489664,
  42. -0.903964536847682,-1.375889704354273,0.04906080087803202,0.2039077019578547,-0.4907377045195846,-0.4781929001716083,
  43. -0.2289802280011548,-1.339055086640687,-0.3120811524451416,0.06142580393246503,-0.140496390441262,-0.6482824149508374,
  44. -0.2944027976542998,1.619416512991051,0.6285648262611375,1.312636016409526,-1.109965359363169,-0.774547681114892,
  45. -0.344875897907528,0.816762481553918,0.1500701005574458,0.807790349338737,-0.2052962007348396,1.057657121384678,
  46. 0.836142529983228,0.3432803448381389,-0.01268905497569333,-1.144036865790547,-0.4530056923174255,-0.3061160863293071,
  47. -0.02963689772198411,-1.33671649419749,-0.06052105439831394,0.973282554859066,-1.643904288065807,-1.0293884110541,
  48. -0.5291066659852803,-0.3294227039691209,0.002993387508002654,0.2248580674319177,0.574521694409057,1.041337304293327,
  49. 0.4078548122453237,0.1112225876991191,-0.6448072259486091,-0.3051345048257077,0.089593933234481,0.4611768867673915,
  50. 0.7644315320471444,-0.341247840010495,0.0326958894744302,-0.05121900335567795,-0.06019531049352196,1.71234441194424,
  51. -0.04175157932686885,0.769694813995503,-1.080913235981393,0.5989354496438777,-0.84416230123901,0.03165655009402087,
  52. -0.7502374585144876,-2.734748382516766,1.541068679878993,0.1054620771416859,-0.6543692934553028,1.499220114211276,
  53. -0.342006571062175,-0.2053132127077213,0.5457125644270833,-0.7956250897267784,0.7320742348115779,0.4674423735122585,
  54. -0.3087396963145776,-1.53764162258267,0.2455449906251891,0.3795993803250636,-0.1195480230909131,0.137639511052913,
  55. 0.931721348902457,0.06704522870668304,-0.03773030445251862,0.3888322348695948,-0.06366757901233728,0.5563758371320388,
  56. -0.7918177216642121,-0.7566297580399533,-0.3740377818446702,-0.6065664299451118,-0.2341124269010213,2.028052675971757,
  57. 0.378550889251416,0.816911727914731,1.162652387697876,-0.3853743867873177,1.196620648443396,0.01265660717000745,
  58. 1.816698960862263,-0.972941421015463};
  59. double expected_statistic = 0.4587075249160456;
  60. double expected_pvalue = 0.6472282548266728;
  61. auto [computed_statistic, computed_pvalue] = boost::math::statistics::one_sample_t_test(v, 0.0);
  62. CHECK_ULP_CLOSE(expected_statistic, computed_statistic, 8);
  63. CHECK_ULP_CLOSE(expected_pvalue, computed_pvalue, 90);
  64. {
  65. std::vector<double> v = {0.7304375676969546,3.227250635039257,1.01821954205186};
  66. expected_statistic = 2.103013485037935;
  67. expected_pvalue = 0.1701790440880712;
  68. auto [computed_statistic, computed_pvalue] = boost::math::statistics::one_sample_t_test(v, 0.0);
  69. CHECK_ULP_CLOSE(expected_statistic, computed_statistic, 2);
  70. CHECK_ULP_CLOSE(expected_pvalue, computed_pvalue, 7);
  71. }
  72. }
  73. int main()
  74. {
  75. test_agreement_with_mathematica();
  76. test_exact_mean<float>();
  77. test_exact_mean<double>();
  78. return boost::math::test::report_errors();
  79. }