12 #include <OpenMS/config.h>
90 std::vector<std::vector<Size>> folds(K);
91 for (
Size i = 0; i < n; ++i) folds[i % K].push_back(i);
123 template <
typename CandIter,
typename TrainEval,
typename ScoreFn>
124 static std::pair<typename std::iterator_traits<CandIter>::value_type,
double>
126 const std::vector<std::vector<Size>>& folds,
127 TrainEval train_eval,
129 double tie_tol = 1e-12,
132 using CandT =
typename std::iterator_traits<CandIter>::value_type;
139 CandT best_cand = *cbegin;
140 double best_score = std::numeric_limits<double>::infinity();
143 for (
auto it = cbegin; it != cend; ++it)
145 const CandT cand = *it;
147 std::vector<double> abs_errs;
148 abs_errs.reserve(256);
149 train_eval(cand, folds, abs_errs);
151 const double s = score(abs_errs);
154 const bool better = (s < best_score - tie_tol);
155 const bool tie = (std::fabs(s - best_score) <= tie_tol);
157 bool wins_on_tie =
false;
168 if (first || better || wins_on_tie)
176 return {best_cand, best_score};
Lightweight K-fold / LOO cross-validation utilities and 1-D grid search.
Definition: CrossValidation.h:45
Invalid range exception.
Definition: Exception.h:257
Invalid value exception.
Definition: Exception.h:305
A more convenient string class.
Definition: String.h:34
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:97
CandidateTieBreak
Tie-breaking preference for equal (within tolerance) CV scores.
Definition: CrossValidation.h:57
static std::pair< typename std::iterator_traits< CandIter >::value_type, double > gridSearch1D(CandIter cbegin, CandIter cend, const std::vector< std::vector< Size >> &folds, TrainEval train_eval, ScoreFn score, double tie_tol=1e-12, CandidateTieBreak tie_break=CandidateTieBreak::PreferLarger)
One-dimensional grid search with external cross-validation evaluation.
Definition: CrossValidation.h:125
static std::vector< std::vector< Size > > makeKFolds(Size n, Size K)
Build K folds for indices [0, n).
Definition: CrossValidation.h:76
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19