aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/codecs/greedy_dict/gd_stats.h
blob: b63c4c38d23b1b383c422282bdeeb28b289dba6f (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
#pragma once

#include <util/generic/ymath.h>
#include <util/generic/algorithm.h>
#include <util/generic/yexception.h>

namespace NGreedyDict {
    enum EEntryScore {
        ES_COUNT,
        ES_LEN_COUNT,
        ES_SIMPLE,
        ES_LEN_SIMPLE,
        ES_SOLAR
    };

    enum EEntryStatTest {
        EST_NONE = 0,
        EST_SIMPLE_NORM = 2
    };

    inline float ModelP(ui32 countA, ui32 countB, ui32 total) {
        return float(countA) * countB / total / total;
    }

    // P (ab | dependent)
    inline float SimpleTest(float modelp, ui32 countAB, ui32 total) {
        float realp = float(countAB) / total;
        return modelp >= realp ? 0 : (realp - modelp);
    }

    inline float SolarTest(float modelp, ui32 countAB, ui32 total) {
        float realp = float(countAB) / total;
        return modelp >= realp ? 0 : (modelp + realp * (log(realp / modelp) - 1));
    }

    // P (ab | dependent) / P (ab)
    inline float SimpleTestNorm(float modelp, ui32 countAB, ui32 total) {
        float realp = float(countAB) / total;
        return modelp >= realp ? 0 : (realp - modelp) / realp;
    }

    inline float StatTest(EEntryStatTest test, float modelp, ui32 countAB, ui32 total) {
        if (!total) {
            return 0;
        }
        switch (test) {
            case EST_NONE:
                return 1;
            case EST_SIMPLE_NORM:
                return SimpleTestNorm(modelp, countAB, total);
        }
        Y_FAIL("no way!");
        return 0;
    }

    inline float Score(EEntryScore score, ui32 len, float modelp, ui32 count, ui32 total) {
        if (!total) {
            return 0;
        }
        ui32 m = 1;
        switch (score) {
            case ES_LEN_COUNT:
                m = len;
                [[fallthrough]];
            case ES_COUNT:
                return m * count;
            case ES_LEN_SIMPLE:
                m = len;
                [[fallthrough]];
            case ES_SIMPLE:
                return m * SimpleTest(modelp, count, total);
            case ES_SOLAR:
                return SolarTest(modelp, count, total);
        }
        Y_FAIL("no way!");
        return 0;
    }

}