| 15 | |
| 16 | Y_UNIT_TEST_SUITE(TLinearRegressionTest) { |
| 17 | Y_UNIT_TEST(MeanAndDeviationTest) { |
| 18 | TVector<double> arguments; |
| 19 | TVector<double> weights; |
| 20 | |
| 21 | const size_t argumentsCount = 100; |
| 22 | for (size_t i = 0; i < argumentsCount; ++i) { |
| 23 | arguments.push_back(i); |
| 24 | weights.push_back(i); |
| 25 | } |
| 26 | |
| 27 | TDeviationCalculator deviationCalculator; |
| 28 | TMeanCalculator meanCalculator; |
| 29 | for (size_t i = 0; i < arguments.size(); ++i) { |
| 30 | meanCalculator.Add(arguments[i], weights[i]); |
| 31 | deviationCalculator.Add(arguments[i], weights[i]); |
| 32 | } |
| 33 | |
| 34 | double actualMean = InnerProduct(arguments, weights) / Accumulate(weights, 0.0); |
| 35 | double actualDeviation = 0.; |
| 36 | for (size_t i = 0; i < arguments.size(); ++i) { |
| 37 | double deviation = arguments[i] - actualMean; |
| 38 | actualDeviation += deviation * deviation * weights[i]; |
| 39 | } |
| 40 | |
| 41 | UNIT_ASSERT(IsValidFloat(meanCalculator.GetMean())); |
| 42 | UNIT_ASSERT_DOUBLES_EQUAL(meanCalculator.GetMean(), actualMean, 1e-10); |
| 43 | |
| 44 | UNIT_ASSERT(IsValidFloat(deviationCalculator.GetDeviation())); |
| 45 | UNIT_ASSERT_DOUBLES_EQUAL(meanCalculator.GetMean(), deviationCalculator.GetMean(), 0); |
| 46 | |
| 47 | UNIT_ASSERT(IsValidFloat(meanCalculator.GetSumWeights())); |
| 48 | UNIT_ASSERT(IsValidFloat(deviationCalculator.GetSumWeights())); |
| 49 | UNIT_ASSERT_DOUBLES_EQUAL(meanCalculator.GetSumWeights(), deviationCalculator.GetSumWeights(), 0); |
| 50 | UNIT_ASSERT_DOUBLES_EQUAL(meanCalculator.GetSumWeights(), Accumulate(weights, 0.0), 0); |
| 51 | |
| 52 | ValueIsCorrect(deviationCalculator.GetDeviation(), actualDeviation, 1e-5); |
| 53 | |
| 54 | TMeanCalculator checkRemovingMeanCalculator; |
| 55 | TDeviationCalculator checkRemovingDeviationCalculator; |
| 56 | |
| 57 | const size_t argumentsToRemoveCount = argumentsCount / 3; |
| 58 | for (size_t i = 0; i < argumentsCount; ++i) { |
| 59 | if (i < argumentsToRemoveCount) { |
| 60 | meanCalculator.Remove(arguments[i], weights[i]); |
| 61 | deviationCalculator.Remove(arguments[i], weights[i]); |
| 62 | } else { |
| 63 | checkRemovingMeanCalculator.Add(arguments[i], weights[i]); |
| 64 | checkRemovingDeviationCalculator.Add(arguments[i], weights[i]); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | UNIT_ASSERT(IsValidFloat(meanCalculator.GetMean())); |
| 69 | UNIT_ASSERT(IsValidFloat(checkRemovingMeanCalculator.GetMean())); |
| 70 | |
| 71 | UNIT_ASSERT(IsValidFloat(deviationCalculator.GetDeviation())); |
| 72 | UNIT_ASSERT(IsValidFloat(checkRemovingDeviationCalculator.GetDeviation())); |
| 73 | |
| 74 | UNIT_ASSERT_DOUBLES_EQUAL(meanCalculator.GetMean(), deviationCalculator.GetMean(), 0); |
nothing calls this directly
no test coverage detected