aboutsummaryrefslogtreecommitdiffstats
path: root/util/random/normal_ut.cpp
blob: 8968f9e571c9f132692ceef9e900bf3e14ad6618 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "normal.h"
#include "fast.h"

#include <library/cpp/testing/unittest/registar.h>

#include <util/generic/vector.h>

#include <functional>

Y_UNIT_TEST_SUITE(TestNormalDistribution) {
    Y_UNIT_TEST(TestDefined) {
        volatile auto x = NormalRandom<float>(0, 1) + NormalRandom<double>(0, 1) + NormalRandom<long double>(0, 1);

        (void)x;
    }

    template <class T>
    static void TestMD(std::function<T()> f, T m, T d) {
        TVector<T> 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<float>(&StdNormalRandom<float>, 0, 1);
        TestMD<double>(&StdNormalRandom<double>, 0, 1);
        TestMD<long double>(&StdNormalRandom<long double>, 0, 1);
    }

    template <class T>
    std::function<T()> GenFunc1(T m, T d) {
        return [m, d]() {
            return NormalRandom<T>(m, d);
        };
    }

    template <class T>
    std::function<T()> GenFunc2(T m, T d) {
        TFastRng<ui64> rng(17);

        return [rng, m, d]() mutable {
            return NormalDistribution<T>(rng, m, d);
        };
    }

    Y_UNIT_TEST(Test2) {
        TestMD<float>(GenFunc1<float>(2, 3), 2, 3);
        TestMD<double>(GenFunc1<double>(3, 4), 3, 4);
        TestMD<long double>(GenFunc1<long double>(4, 5), 4, 5);
    }

    Y_UNIT_TEST(Test3) {
        TestMD<float>(GenFunc2<float>(20, 30), 20, 30);
        TestMD<double>(GenFunc2<double>(30, 40), 30, 40);
        TestMD<long double>(GenFunc2<long double>(40, 50), 40, 50);
    }
}