| 130 | |
| 131 | template <typename TSLRSolverType> |
| 132 | void SLRTest() { |
| 133 | TVector<double> arguments; |
| 134 | TVector<double> weights; |
| 135 | TVector<double> goals; |
| 136 | |
| 137 | const double factor = 2.; |
| 138 | const double intercept = 105.; |
| 139 | const double randomError = 0.01; |
| 140 | |
| 141 | const size_t argumentsCount = 10; |
| 142 | for (size_t i = 0; i < argumentsCount; ++i) { |
| 143 | arguments.push_back(i); |
| 144 | weights.push_back(i); |
| 145 | goals.push_back(arguments.back() * factor + intercept + 2 * (i % 2 - 0.5) * randomError); |
| 146 | } |
| 147 | |
| 148 | TSLRSolverType slrSolver; |
| 149 | for (size_t i = 0; i < argumentsCount; ++i) { |
| 150 | slrSolver.Add(arguments[i], goals[i], weights[i]); |
| 151 | } |
| 152 | |
| 153 | for (double regularizationThreshold = 0.; regularizationThreshold < 0.05; regularizationThreshold += 0.01) { |
| 154 | double solutionFactor, solutionIntercept; |
| 155 | slrSolver.Solve(solutionFactor, solutionIntercept, regularizationThreshold); |
| 156 | |
| 157 | double predictedSumSquaredErrors = slrSolver.SumSquaredErrors(regularizationThreshold); |
| 158 | |
| 159 | UNIT_ASSERT(IsValidFloat(solutionFactor)); |
| 160 | UNIT_ASSERT(IsValidFloat(solutionIntercept)); |
| 161 | UNIT_ASSERT(IsValidFloat(predictedSumSquaredErrors)); |
| 162 | |
| 163 | UNIT_ASSERT_DOUBLES_EQUAL(solutionFactor, factor, 1e-2); |
| 164 | UNIT_ASSERT_DOUBLES_EQUAL(solutionIntercept, intercept, 1e-2); |
| 165 | |
| 166 | double sumSquaredErrors = 0.; |
| 167 | for (size_t i = 0; i < argumentsCount; ++i) { |
| 168 | double error = goals[i] - arguments[i] * solutionFactor - solutionIntercept; |
| 169 | sumSquaredErrors += error * error * weights[i]; |
| 170 | } |
| 171 | |
| 172 | if (!regularizationThreshold) { |
| 173 | UNIT_ASSERT(predictedSumSquaredErrors < Accumulate(weights, 0.0) * randomError * randomError); |
| 174 | } |
| 175 | UNIT_ASSERT_DOUBLES_EQUAL(predictedSumSquaredErrors, sumSquaredErrors, 1e-8); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | Y_UNIT_TEST(FastSLRTest) { |
| 180 | SLRTest<TFastSLRSolver>(); |
nothing calls this directly
no test coverage detected