| 11 | |
| 12 | template <typename TLRSolver> |
| 13 | void QualityBenchmark(const TPool& originalPool) { |
| 14 | auto measure = [&](const double injureFactor, const double injureOffset) { |
| 15 | TPool injuredPool = originalPool.InjurePool(injureFactor, injureOffset); |
| 16 | |
| 17 | static const size_t runsCount = 10; |
| 18 | static const size_t foldsCount = 10; |
| 19 | |
| 20 | TMeanCalculator determinationCoefficientCalculator; |
| 21 | |
| 22 | TPool::TCVIterator learnIterator = injuredPool.CrossValidationIterator(foldsCount, TPool::LearnIterator); |
| 23 | TPool::TCVIterator testIterator = injuredPool.CrossValidationIterator(foldsCount, TPool::TestIterator); |
| 24 | |
| 25 | for (size_t runNumber = 0; runNumber < runsCount; ++runNumber) { |
| 26 | for (size_t foldNumber = 0; foldNumber < foldsCount; ++foldNumber) { |
| 27 | learnIterator.ResetShuffle(); |
| 28 | learnIterator.SetTestFold(foldNumber); |
| 29 | testIterator.ResetShuffle(); |
| 30 | testIterator.SetTestFold(foldNumber); |
| 31 | |
| 32 | TLRSolver solver; |
| 33 | for (; learnIterator.IsValid(); ++learnIterator) { |
| 34 | solver.Add(learnIterator->Features, learnIterator->Goal, learnIterator->Weight); |
| 35 | } |
| 36 | TLinearModel model = solver.Solve(); |
| 37 | |
| 38 | TDeviationCalculator goalsCalculator; |
| 39 | TKahanAccumulator<double> errorsCalculator; |
| 40 | for (; testIterator.IsValid(); ++testIterator) { |
| 41 | const double prediction = model.Prediction(testIterator->Features); |
| 42 | const double goal = testIterator->Goal; |
| 43 | const double weight = testIterator->Weight; |
| 44 | const double error = goal - prediction; |
| 45 | |
| 46 | goalsCalculator.Add(goal, weight); |
| 47 | errorsCalculator += error * error * weight; |
| 48 | } |
| 49 | |
| 50 | const double determinationCoefficient = 1 - errorsCalculator.Get() / goalsCalculator.GetDeviation(); |
| 51 | determinationCoefficientCalculator.Add(determinationCoefficient); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | return determinationCoefficientCalculator.GetMean(); |
| 56 | }; |
| 57 | |
| 58 | Cout << TypeName<TLRSolver>() << ":\n"; |
| 59 | Cout << "\t" << Sprintf("base : %.10lf\n", measure(1., 0.)); |
| 60 | Cout << "\t" << Sprintf("injure1 : %.10lf\n", measure(1e-1, 1e+1)); |
| 61 | Cout << "\t" << Sprintf("injure2 : %.10lf\n", measure(1e-3, 1e+4)); |
| 62 | Cout << "\t" << Sprintf("injure3 : %.10lf\n", measure(1e-3, 1e+5)); |
| 63 | Cout << "\t" << Sprintf("injure4 : %.10lf\n", measure(1e-3, 1e+6)); |
| 64 | Cout << "\t" << Sprintf("injure5 : %.10lf\n", measure(1e-4, 1e+6)); |
| 65 | Cout << "\t" << Sprintf("injure6 : %.10lf\n", measure(1e-4, 1e+7)); |
| 66 | Cout << Endl; |
| 67 | } |
| 68 | |
| 69 | template <typename TLRSolver> |
| 70 | void SpeedBenchmark(const TPool& originalPool) { |
nothing calls this directly
no test coverage detected