aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/linear_regression/linear_regression_ut.cpp
blob: 80e1d9af8234ce1a21b70efaea88c04674683e3a (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include <library/cpp/linear_regression/linear_regression.h> 
#include <library/cpp/testing/unittest/registar.h>

#include <util/generic/vector.h>
#include <util/generic/ymath.h>
#include <util/random/random.h>

#include <util/system/defaults.h>

namespace {
    void ValueIsCorrect(const double value, const double expectedValue, double possibleRelativeError) {
        UNIT_ASSERT_DOUBLES_EQUAL(value, expectedValue, possibleRelativeError * expectedValue);
    }
}

Y_UNIT_TEST_SUITE(TLinearRegressionTest) {
    Y_UNIT_TEST(MeanAndDeviationTest) {
        TVector<double> arguments;
        TVector<double> weights;

        const size_t argumentsCount = 100;
        for (size_t i = 0; i < argumentsCount; ++i) {
            arguments.push_back(i);
            weights.push_back(i);
        }

        TDeviationCalculator deviationCalculator;
        TMeanCalculator meanCalculator;
        for (size_t i = 0; i < arguments.size(); ++i) {
            meanCalculator.Add(arguments[i], weights[i]);
            deviationCalculator.Add(arguments[i], weights[i]);
        }

        double actualMean = InnerProduct(arguments, weights) / Accumulate(weights, 0.0);
        double actualDeviation = 0.;
        for (size_t i = 0; i < arguments.size(); ++i) {
            double deviation = arguments[i] - actualMean;
            actualDeviation += deviation * deviation * weights[i];
        }

        UNIT_ASSERT(IsValidFloat(meanCalculator.GetMean()));
        UNIT_ASSERT_DOUBLES_EQUAL(meanCalculator.GetMean(), actualMean, 1e-10);

        UNIT_ASSERT(IsValidFloat(deviationCalculator.GetDeviation()));
        UNIT_ASSERT_DOUBLES_EQUAL(meanCalculator.GetMean(), deviationCalculator.GetMean(), 0);

        UNIT_ASSERT(IsValidFloat(meanCalculator.GetSumWeights()));
        UNIT_ASSERT(IsValidFloat(deviationCalculator.GetSumWeights()));
        UNIT_ASSERT_DOUBLES_EQUAL(meanCalculator.GetSumWeights(), deviationCalculator.GetSumWeights(), 0);
        UNIT_ASSERT_DOUBLES_EQUAL(meanCalculator.GetSumWeights(), Accumulate(weights, 0.0), 0);

        ValueIsCorrect(deviationCalculator.GetDeviation(), actualDeviation, 1e-5);

        TMeanCalculator checkRemovingMeanCalculator;
        TDeviationCalculator checkRemovingDeviationCalculator;

        const size_t argumentsToRemoveCount = argumentsCount / 3;
        for (size_t i = 0; i < argumentsCount; ++i) {
            if (i < argumentsToRemoveCount) {
                meanCalculator.Remove(arguments[i], weights[i]);
                deviationCalculator.Remove(arguments[i], weights[i]);
            } else {
                checkRemovingMeanCalculator.Add(arguments[i], weights[i]);
                checkRemovingDeviationCalculator.Add(arguments[i], weights[i]);
            }
        }

        UNIT_ASSERT(IsValidFloat(meanCalculator.GetMean()));
        UNIT_ASSERT(IsValidFloat(checkRemovingMeanCalculator.GetMean()));

        UNIT_ASSERT(IsValidFloat(deviationCalculator.GetDeviation()));
        UNIT_ASSERT(IsValidFloat(checkRemovingDeviationCalculator.GetDeviation()));

        UNIT_ASSERT_DOUBLES_EQUAL(meanCalculator.GetMean(), deviationCalculator.GetMean(), 0);
        UNIT_ASSERT_DOUBLES_EQUAL(meanCalculator.GetMean(), checkRemovingMeanCalculator.GetMean(), 1e-10);

        ValueIsCorrect(deviationCalculator.GetDeviation(), checkRemovingDeviationCalculator.GetDeviation(), 1e-5);
    }

    Y_UNIT_TEST(CovariationTest) {
        TVector<double> firstValues;
        TVector<double> secondValues;
        TVector<double> weights;

        const size_t argumentsCount = 100;
        for (size_t i = 0; i < argumentsCount; ++i) {
            firstValues.push_back(i);
            secondValues.push_back(i * i);
            weights.push_back(i);
        }

        TCovariationCalculator covariationCalculator;
        for (size_t i = 0; i < argumentsCount; ++i) {
            covariationCalculator.Add(firstValues[i], secondValues[i], weights[i]);
        }

        const double firstValuesMean = InnerProduct(firstValues, weights) / Accumulate(weights, 0.0);
        const double secondValuesMean = InnerProduct(secondValues, weights) / Accumulate(weights, 0.0);

        double actualCovariation = 0.;
        for (size_t i = 0; i < argumentsCount; ++i) {
            actualCovariation += (firstValues[i] - firstValuesMean) * (secondValues[i] - secondValuesMean) * weights[i];
        }

        UNIT_ASSERT(IsValidFloat(covariationCalculator.GetCovariation()));
        UNIT_ASSERT(IsValidFloat(covariationCalculator.GetFirstValueMean()));
        UNIT_ASSERT(IsValidFloat(covariationCalculator.GetSecondValueMean()));

        UNIT_ASSERT_DOUBLES_EQUAL(covariationCalculator.GetFirstValueMean(), firstValuesMean, 1e-10);
        UNIT_ASSERT_DOUBLES_EQUAL(covariationCalculator.GetSecondValueMean(), secondValuesMean, 1e-10);

        UNIT_ASSERT(IsValidFloat(covariationCalculator.GetSumWeights()));
        UNIT_ASSERT_DOUBLES_EQUAL(covariationCalculator.GetSumWeights(), Accumulate(weights, 0.0), 0);

        ValueIsCorrect(covariationCalculator.GetCovariation(), actualCovariation, 1e-5);

        TCovariationCalculator checkRemovingCovariationCalculator;

        const size_t argumentsToRemoveCount = argumentsCount / 3;
        for (size_t i = 0; i < argumentsCount; ++i) {
            if (i < argumentsToRemoveCount) {
                covariationCalculator.Remove(firstValues[i], secondValues[i], weights[i]);
            } else {
                checkRemovingCovariationCalculator.Add(firstValues[i], secondValues[i], weights[i]);
            }
        }

        ValueIsCorrect(covariationCalculator.GetCovariation(), checkRemovingCovariationCalculator.GetCovariation(), 1e-5);
    }

    template <typename TSLRSolverType>
    void SLRTest() {
        TVector<double> arguments;
        TVector<double> weights;
        TVector<double> goals;

        const double factor = 2.;
        const double intercept = 105.;
        const double randomError = 0.01;

        const size_t argumentsCount = 10;
        for (size_t i = 0; i < argumentsCount; ++i) {
            arguments.push_back(i);
            weights.push_back(i);
            goals.push_back(arguments.back() * factor + intercept + 2 * (i % 2 - 0.5) * randomError);
        }

        TSLRSolverType slrSolver;
        for (size_t i = 0; i < argumentsCount; ++i) {
            slrSolver.Add(arguments[i], goals[i], weights[i]);
        }

        for (double regularizationThreshold = 0.; regularizationThreshold < 0.05; regularizationThreshold += 0.01) {
            double solutionFactor, solutionIntercept;
            slrSolver.Solve(solutionFactor, solutionIntercept, regularizationThreshold);

            double predictedSumSquaredErrors = slrSolver.SumSquaredErrors(regularizationThreshold);

            UNIT_ASSERT(IsValidFloat(solutionFactor));
            UNIT_ASSERT(IsValidFloat(solutionIntercept));
            UNIT_ASSERT(IsValidFloat(predictedSumSquaredErrors));

            UNIT_ASSERT_DOUBLES_EQUAL(solutionFactor, factor, 1e-2);
            UNIT_ASSERT_DOUBLES_EQUAL(solutionIntercept, intercept, 1e-2);

            double sumSquaredErrors = 0.;
            for (size_t i = 0; i < argumentsCount; ++i) {
                double error = goals[i] - arguments[i] * solutionFactor - solutionIntercept;
                sumSquaredErrors += error * error * weights[i];
            }

            if (!regularizationThreshold) {
                UNIT_ASSERT(predictedSumSquaredErrors < Accumulate(weights, 0.0) * randomError * randomError);
            }
            UNIT_ASSERT_DOUBLES_EQUAL(predictedSumSquaredErrors, sumSquaredErrors, 1e-8);
        }
    }

    Y_UNIT_TEST(FastSLRTest) {
        SLRTest<TFastSLRSolver>();
    }

    Y_UNIT_TEST(KahanSLRTest) {
        SLRTest<TKahanSLRSolver>();
    }

    Y_UNIT_TEST(SLRTest) {
        SLRTest<TSLRSolver>();
    }

    template <typename TLinearRegressionSolverType>
    void LinearRegressionTest() {
        const size_t featuresCount = 10;
        const size_t instancesCount = 10000;
        const double randomError = 0.01;

        TVector<double> coefficients;
        for (size_t featureNumber = 0; featureNumber < featuresCount; ++featureNumber) {
            coefficients.push_back(featureNumber);
        }
        const double intercept = 10;

        TVector<TVector<double>> featuresMatrix;
        TVector<double> goals;
        TVector<double> weights;

        for (size_t instanceNumber = 0; instanceNumber < instancesCount; ++instanceNumber) {
            TVector<double> features;
            for (size_t featureNumber = 0; featureNumber < featuresCount; ++featureNumber) {
                features.push_back(RandomNumber<double>());
            }
            featuresMatrix.push_back(features);

            const double goal = InnerProduct(coefficients, features) + intercept + 2 * (instanceNumber % 2 - 0.5) * randomError;
            goals.push_back(goal);
            weights.push_back(instanceNumber);
        }

        TLinearRegressionSolverType lrSolver;
        for (size_t instanceNumber = 0; instanceNumber < instancesCount; ++instanceNumber) {
            lrSolver.Add(featuresMatrix[instanceNumber], goals[instanceNumber], weights[instanceNumber]);
        }
        const TLinearModel model = lrSolver.Solve();

        for (size_t featureNumber = 0; featureNumber < featuresCount; ++featureNumber) {
            UNIT_ASSERT_DOUBLES_EQUAL(model.GetCoefficients()[featureNumber], coefficients[featureNumber], 1e-2);
        }
        UNIT_ASSERT_DOUBLES_EQUAL(model.GetIntercept(), intercept, 1e-2);

        const double expectedSumSquaredErrors = randomError * randomError * Accumulate(weights, 0.0);
        UNIT_ASSERT_DOUBLES_EQUAL(lrSolver.SumSquaredErrors(), expectedSumSquaredErrors, expectedSumSquaredErrors * 0.01);
    }

    Y_UNIT_TEST(FastLRTest) {
        LinearRegressionTest<TFastLinearRegressionSolver>();
    }

    Y_UNIT_TEST(LRTest) {
        LinearRegressionTest<TLinearRegressionSolver>();
    }

    void TransformationTest(const ETransformationType transformationType, const size_t pointsCount) {
        TVector<float> arguments;
        TVector<float> goals;

        const double regressionFactor = 10.;
        const double regressionIntercept = 100;

        const double featureOffset = -1.5;
        const double featureNormalizer = 15;

        const double left = -100.;
        const double right = +100.;
        const double step = (right - left) / pointsCount;

        for (double argument = left; argument <= right; argument += step) {
            const double goal = regressionIntercept + regressionFactor * (argument - featureOffset) / (fabs(argument - featureOffset) + featureNormalizer);

            arguments.push_back(argument);
            goals.push_back(goal);
        }

        TFastFeaturesTransformerLearner learner(transformationType);
        for (size_t instanceNumber = 0; instanceNumber < arguments.size(); ++instanceNumber) {
            learner.Add(arguments[instanceNumber], goals[instanceNumber]);
        }
        TFeaturesTransformer transformer = learner.Solve();

        double sse = 0.;
        for (size_t instanceNumber = 0; instanceNumber < arguments.size(); ++instanceNumber) {
            const double error = transformer.Transformation(arguments[instanceNumber]) - goals[instanceNumber];
            sse += error * error;
        }
        const double rmse = sqrt(sse / arguments.size());
        UNIT_ASSERT_DOUBLES_EQUAL(rmse, 0., 1e-3);
    }

    Y_UNIT_TEST(SigmaTest100) {
        TransformationTest(ETransformationType::TT_SIGMA, 100);
    }

    Y_UNIT_TEST(SigmaTest1000) {
        TransformationTest(ETransformationType::TT_SIGMA, 1000);
    }

    Y_UNIT_TEST(SigmaTest10000) {
        TransformationTest(ETransformationType::TT_SIGMA, 10000);
    }

    Y_UNIT_TEST(SigmaTest100000) {
        TransformationTest(ETransformationType::TT_SIGMA, 100000);
    }

    Y_UNIT_TEST(SigmaTest1000000) {
        TransformationTest(ETransformationType::TT_SIGMA, 1000000);
    }

    Y_UNIT_TEST(SigmaTest10000000) {
        TransformationTest(ETransformationType::TT_SIGMA, 10000000);
    }

    Y_UNIT_TEST(ResetCalculatorTest) {
        TVector<double> arguments;
        TVector<double> weights;
        const double eps = 1e-10;

        const size_t argumentsCount = 100;
        for (size_t i = 0; i < argumentsCount; ++i) {
            arguments.push_back(i);
            weights.push_back(i);
        }

        TDeviationCalculator deviationCalculator1, deviationCalculator2;
        TMeanCalculator meanCalculator1, meanCalculator2;
        TCovariationCalculator covariationCalculator1, covariationCalculator2;
        for (size_t i = 0; i < arguments.size(); ++i) {
            meanCalculator1.Add(arguments[i], weights[i]);
            meanCalculator2.Add(arguments[i], weights[i]);
            deviationCalculator1.Add(arguments[i], weights[i]);
            deviationCalculator2.Add(arguments[i], weights[i]);
            covariationCalculator1.Add(arguments[i], arguments[arguments.size() - i - 1], weights[i]);
            covariationCalculator2.Add(arguments[i], arguments[arguments.size() - i - 1], weights[i]);
        }

        UNIT_ASSERT_DOUBLES_EQUAL(meanCalculator1.GetMean(), meanCalculator2.GetMean(), eps);
        UNIT_ASSERT_DOUBLES_EQUAL(meanCalculator1.GetSumWeights(), meanCalculator2.GetSumWeights(), eps);

        UNIT_ASSERT_DOUBLES_EQUAL(deviationCalculator1.GetMean(), deviationCalculator2.GetMean(), eps);
        UNIT_ASSERT_DOUBLES_EQUAL(deviationCalculator1.GetDeviation(), deviationCalculator2.GetDeviation(), eps);
        UNIT_ASSERT_DOUBLES_EQUAL(deviationCalculator1.GetStdDev(), deviationCalculator2.GetStdDev(), eps);
        UNIT_ASSERT_DOUBLES_EQUAL(deviationCalculator1.GetSumWeights(), deviationCalculator2.GetSumWeights(), eps);

        UNIT_ASSERT_DOUBLES_EQUAL(covariationCalculator1.GetFirstValueMean(), covariationCalculator2.GetFirstValueMean(), eps);
        UNIT_ASSERT_DOUBLES_EQUAL(covariationCalculator1.GetSecondValueMean(), covariationCalculator2.GetSecondValueMean(), eps);
        UNIT_ASSERT_DOUBLES_EQUAL(covariationCalculator1.GetCovariation(), covariationCalculator2.GetCovariation(), eps);
        UNIT_ASSERT_DOUBLES_EQUAL(covariationCalculator1.GetSumWeights(), covariationCalculator2.GetSumWeights(), eps);

        meanCalculator2.Reset();
        deviationCalculator2.Reset();
        covariationCalculator2.Reset();

        UNIT_ASSERT_DOUBLES_EQUAL(0.0, meanCalculator2.GetMean(), eps);
        UNIT_ASSERT_DOUBLES_EQUAL(0.0, meanCalculator2.GetSumWeights(), eps);

        UNIT_ASSERT_DOUBLES_EQUAL(0.0, deviationCalculator2.GetMean(), eps);
        UNIT_ASSERT_DOUBLES_EQUAL(0.0, deviationCalculator2.GetDeviation(), eps);
        UNIT_ASSERT_DOUBLES_EQUAL(0.0, deviationCalculator2.GetStdDev(), eps);
        UNIT_ASSERT_DOUBLES_EQUAL(0.0, deviationCalculator2.GetSumWeights(), eps);

        UNIT_ASSERT_DOUBLES_EQUAL(0.0, covariationCalculator2.GetFirstValueMean(), eps);
        UNIT_ASSERT_DOUBLES_EQUAL(0.0, covariationCalculator2.GetSecondValueMean(), eps);
        UNIT_ASSERT_DOUBLES_EQUAL(0.0, covariationCalculator2.GetCovariation(), eps);
        UNIT_ASSERT_DOUBLES_EQUAL(0.0, covariationCalculator2.GetSumWeights(), eps);

        for (size_t i = 0; i < arguments.size(); ++i) {
            meanCalculator2.Add(arguments[i], weights[i]);
            deviationCalculator2.Add(arguments[i], weights[i]);
            covariationCalculator2.Add(arguments[i], arguments[arguments.size() - i - 1], weights[i]);
        }

        UNIT_ASSERT_DOUBLES_EQUAL(meanCalculator1.GetMean(), meanCalculator2.GetMean(), 1e-10);
        UNIT_ASSERT_DOUBLES_EQUAL(meanCalculator1.GetSumWeights(), meanCalculator2.GetSumWeights(), 1e-10);

        UNIT_ASSERT_DOUBLES_EQUAL(deviationCalculator1.GetMean(), deviationCalculator2.GetMean(), eps);
        UNIT_ASSERT_DOUBLES_EQUAL(deviationCalculator1.GetDeviation(), deviationCalculator2.GetDeviation(), eps);
        UNIT_ASSERT_DOUBLES_EQUAL(deviationCalculator1.GetStdDev(), deviationCalculator2.GetStdDev(), eps);
        UNIT_ASSERT_DOUBLES_EQUAL(deviationCalculator1.GetSumWeights(), deviationCalculator2.GetSumWeights(), eps);

        UNIT_ASSERT_DOUBLES_EQUAL(covariationCalculator1.GetFirstValueMean(), covariationCalculator2.GetFirstValueMean(), eps);
        UNIT_ASSERT_DOUBLES_EQUAL(covariationCalculator1.GetSecondValueMean(), covariationCalculator2.GetSecondValueMean(), eps);
        UNIT_ASSERT_DOUBLES_EQUAL(covariationCalculator1.GetCovariation(), covariationCalculator2.GetCovariation(), eps);
        UNIT_ASSERT_DOUBLES_EQUAL(covariationCalculator1.GetSumWeights(), covariationCalculator2.GetSumWeights(), eps);
    }
}