aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/lwtrace/mon/analytics/transform.h
blob: 60f7aac3793be5efc86f8116f392279f0af4d266 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#pragma once

#include "data.h"

namespace NAnalytics {

template <class TSkip, class TX, class TY>
inline TTable Histogram(const TTable& in, TSkip skip,
                 const TString& xn_out, TX x_in, 
                 const TString& yn_out, TY y_in, 
                 double x1, double x2, double dx)
{
    long buckets = (x2 - x1) / dx;
    TTable out;
    TString yn_sum = yn_out + "_sum"; 
    TString yn_share = yn_out + "_share"; 
    double ysum = 0.0;
    out.resize(buckets);
    for (size_t i = 0; i < out.size(); i++) {
        double lb = x1 + dx*i;
        double ub = lb + dx;
        out[i].Name = "[" + ToString(lb) + ";" + ToString(ub) + (ub==x2? "]": ")");
        out[i][xn_out] = (lb + ub) / 2;
        out[i][yn_sum] = 0.0;
    }
    for (const auto& row : in) {
        if (skip(row)) {
            continue;
        }
        double x = x_in(row);
        long i = (x - x1) / dx;
        if (x == x2) { // Special hack to include right edge
            i--;
        }
        double y = y_in(row);
        ysum += y;
        if (i >= 0 && i < buckets) {
            out[i][yn_sum] = y + out[i].GetOrDefault(yn_sum, 0.0);
        }
    }
    for (TRow& row : out) {
        if (ysum != 0.0) {
            row[yn_share] = row.GetOrDefault(yn_sum, 0.0) / ysum;
        }
    }
    return out;
}

inline TTable HistogramAll(const TTable& in, const TString& xn, double x1, double x2, double dx) 
{
    long buckets = (dx == 0.0? 1: (x2 - x1) / dx);
    TTable out;
    THashMap<TString, double> colSum; 
    out.resize(buckets);

    TSet<TString> cols; 
    for (auto& row : in) {
        for (auto& kv : row) {
            cols.insert(kv.first);
        }
    }
    cols.insert("_count");
    cols.erase(xn);

    for (const TString& col : cols) { 
        colSum[col] = 0.0;
    }

    for (size_t i = 0; i < out.size(); i++) {
        double lb = x1 + dx*i;
        double ub = lb + dx;
        TRow& row = out[i];
        row.Name = "[" + ToString(lb) + ";" + ToString(ub) + (ub==x2? "]": ")");
        row[xn] = (lb + ub) / 2;
        for (const TString& col : cols) { 
            row[col + "_sum"] = 0.0;
        }
    }
    for (const TRow& row_in : in) {
        double x;
        if (!row_in.Get(xn, x)) {
            continue;
        }
        long i = (dx == 0.0? 0: (x - x1) / dx);
        if (x == x2 && dx > 0.0) { // Special hack to include right edge
            i--;
        }
        for (const auto& kv : row_in) {
            const TString& yn = kv.first; 
            if (yn == xn) {
                continue;
            }
            double y;
            if (!row_in.Get(yn, y)) {
                continue;
            }
            colSum[yn] += y;
            if (i >= 0 && i < buckets) {
                out[i][yn + "_cnt"] = out[i].GetOrDefault(yn + "_cnt") + 1;
                out[i][yn + "_sum"] = out[i].GetOrDefault(yn + "_sum") + y;
                if (out[i].contains(yn + "_min")) {
                    out[i][yn + "_min"] = Min(y, out[i].GetOrDefault(yn + "_min"));
                } else {
                    out[i][yn + "_min"] = y;
                }
                if (out[i].contains(yn + "_max")) {
                    out[i][yn + "_max"] = Max(y, out[i].GetOrDefault(yn + "_max"));
                } else {
                    out[i][yn + "_max"] = y;
                }
            }
        }
        colSum["_count"]++;
        if (i >= 0 && i < buckets) {
            out[i]["_count_sum"] = out[i].GetOrDefault("_count_sum") + 1;
        }
    }
    for (TRow& row : out) {
        for (const TString& col : cols) { 
            double ysum = colSum[col];
            if (col != "_count") {
                if (row.GetOrDefault(col + "_cnt") != 0.0) {
                    row[col + "_avg"] = row.GetOrDefault(col + "_sum") / row.GetOrDefault(col + "_cnt");
                }
            }
            if (ysum != 0.0) {
                row[col + "_share"] = row.GetOrDefault(col + "_sum") / ysum;
            }
        }
    }
    return out;
}

inline TMatrix CovarianceMatrix(const TTable& in)
{
    TSet<TString> cols; 
    for (auto& row : in) {
        for (auto& kv : row) {
            cols.insert(kv.first);
        }
    }

    struct TAggregate {
        size_t Idx = 0;
        double Sum = 0;
        size_t Count = 0;
        double Mean = 0;
    };

    THashMap<TString, TAggregate> colAggr; 

    size_t colCount = 0;
    for (const TString& col : cols) {
        TAggregate& aggr = colAggr[col];
        aggr.Idx = colCount++;
    }

    for (const TRow& row : in) {
        for (const auto& kv : row) {
            const TString& xn = kv.first;
            double x;
            if (!row.Get(xn, x)) {
                continue;
            }
            TAggregate& aggr = colAggr[xn];
            aggr.Sum += x;
            aggr.Count++;
        }
    }

    for (auto& kv : colAggr) {
        TAggregate& aggr = kv.second;
        aggr.Mean = aggr.Sum / aggr.Count;
    }

    TMatrix covCount(cols.size(), cols.size());
    TMatrix cov(cols.size(), cols.size());
    for (const TRow& row : in) {
        for (const auto& kv1 : row) {
            double x;
            if (!row.Get(kv1.first, x)) {
                continue;
            }
            TAggregate& xaggr = colAggr[kv1.first];
            for (const auto& kv2 : row) {
                double y;
                if (!row.Get(kv2.first, y)) {
                    continue;
                }                
                TAggregate& yaggr = colAggr[kv2.first];
                covCount.Cell(xaggr.Idx, yaggr.Idx)++;
                cov.Cell(xaggr.Idx, yaggr.Idx) += (x - xaggr.Mean) * (y - yaggr.Mean);
            }
        }
    }

    for (size_t idx = 0; idx < cov.size(); idx++) {
        cov[idx] /= covCount[idx];
    }

    return cov;
}

}