#include "normal.h" #include "fast.h" #include #include #include Y_UNIT_TEST_SUITE(TestNormalDistribution) { Y_UNIT_TEST(TestDefined) { volatile auto x = NormalRandom(0, 1) + NormalRandom(0, 1) + NormalRandom(0, 1); (void)x; } template static void TestMD(std::function f, T m, T d) { TVector v; v.reserve(20000); for (size_t i = 0; i < 20000; ++i) { v.push_back(f()); } long double mm = 0; long double vv = 0; for (auto x : v) { mm += x; } mm /= v.size(); for (auto x : v) { vv += (mm - x) * (mm - x); } vv /= v.size(); long double dd = std::sqrt(vv); UNIT_ASSERT_DOUBLES_EQUAL(m, mm, (m + 1) * 0.05); UNIT_ASSERT_DOUBLES_EQUAL(d, dd, (d + 1) * 0.05); } Y_UNIT_TEST(Test1) { TestMD(&StdNormalRandom, 0, 1); TestMD(&StdNormalRandom, 0, 1); TestMD(&StdNormalRandom, 0, 1); } template std::function GenFunc1(T m, T d) { return [m, d]() { return NormalRandom(m, d); }; } template std::function GenFunc2(T m, T d) { TFastRng rng(17); return [rng, m, d]() mutable { return NormalDistribution(rng, m, d); }; } Y_UNIT_TEST(Test2) { TestMD(GenFunc1(2, 3), 2, 3); TestMD(GenFunc1(3, 4), 3, 4); TestMD(GenFunc1(4, 5), 4, 5); } Y_UNIT_TEST(Test3) { TestMD(GenFunc2(20, 30), 20, 30); TestMD(GenFunc2(30, 40), 30, 40); TestMD(GenFunc2(40, 50), 40, 50); } }