38 std::map<std::string, double>
params;
106 {
"gamma", { 0.0 } },
124std::vector<std::map<std::string, double>>
125createGrid(
const std::map<std::string, std::vector<double>>& param_values);
164template<
typename MatrixType>
168 const Eigen::MatrixXd& y_in,
177 auto y = loss->preprocessResponse(y_in);
180 auto hyperparams = config.default_hyperparams;
183 for (
const auto& [key, values] : config.hyperparams) {
184 hyperparams[key] = values;
191 config.predefined_folds.has_value()
192 ?
Folds(config.predefined_folds.value())
193 :
Folds(n, config.n_folds, config.n_repeats, config.random_seed);
197 for (
const auto& params : grid) {
201 double q = params.at(
"q");
202 double gamma = params.at(
"gamma");
206 auto initial_path = model.
path(x, y);
208 result.
alphas = initial_path.getAlpha();
209 int n_alpha = result.
alphas.size();
211 assert((result.
alphas > 0).all());
213 Eigen::MatrixXd scores = Eigen::MatrixXd::Zero(n_evals, n_alpha);
215 Eigen::setNbThreads(1);
218 std::vector<std::string> thread_errors(n_evals);
219 bool had_exception =
false;
222 omp_set_max_active_levels(1);
223#pragma omp parallel for num_threads(Threads::get()) \
224 shared(scores, thread_errors, had_exception)
226 for (
int i = 0; i < n_evals; ++i) {
228 auto [rep, fold] = std::div(i, folds.
numFolds());
230 Slope thread_model = model;
234 auto [x_train, y_train, x_test, y_test] = folds.
split(x, y, fold, rep);
236 auto path = thread_model.
path(x_train, y_train, result.
alphas);
239 path = thread_model.
relax(path, x_train, y_train, gamma);
242 for (
int j = 0; j < n_alpha; ++j) {
243 auto eta = path(j).
predict(x_test,
"linear");
244 scores(i, j) = scorer->eval(eta, y_test, loss);
246 }
catch (
const std::exception& e) {
247 thread_errors[i] = e.what();
249#pragma omp atomic write
251 had_exception =
true;
253 thread_errors[i] =
"Unknown exception";
255#pragma omp atomic write
257 had_exception =
true;
262 std::string error_message =
"Exception(s) during cross-validation:\n";
263 for (
int i = 0; i < n_evals; ++i) {
264 if (!thread_errors[i].empty()) {
266 "Fold " + std::to_string(i) +
": " + thread_errors[i] +
"\n";
269 throw std::runtime_error(error_message);
274 result.
score = std::move(scores);
275 cv_result.
results.push_back(result);
279 Eigen::setNbThreads(0);
Manages data partitioning for cross-validation.
size_t numEvals() const
Get the total number of folds (repetitions * folds)
size_t numFolds() const
Get the number of folds.
std::tuple< MatrixType, Eigen::MatrixXd, MatrixType, Eigen::MatrixXd > split(MatrixType &x, const Eigen::MatrixXd &y, size_t fold_idx, size_t rep_idx=0) const
Split data into training and test sets for a specific fold and repetition.
static std::unique_ptr< Score > create(const std::string &metric)
Eigen::MatrixXd predict(T &x, const std::string &type="response") const
Predict the response for a given input matrix.
SlopePath path(T &x, const Eigen::MatrixXd &y_in, Eigen::ArrayXd alpha=Eigen::ArrayXd::Zero(0), Eigen::ArrayXd lambda=Eigen::ArrayXd::Zero(0))
Computes SLOPE regression solution path for multiple alpha and lambda values.
const std::string & getLossType()
Get currently defined loss type.
SlopeFit relax(const SlopeFit &fit, T &x, const Eigen::VectorXd &y_in, const double gamma=0.0, Eigen::VectorXd beta0=Eigen::VectorXd(0), Eigen::VectorXd beta=Eigen::VectorXd(0))
Relaxes a fitted SLOPE model.
void setModifyX(const bool modify_x)
Controls if x should be modified-in-place.
void setQ(double q)
Sets the q value.
Cross-validation fold management for SLOPE models.
Namespace containing SLOPE regression implementation.
std::unique_ptr< Loss > setupLoss(const std::string &loss)
Factory function to create the appropriate loss function based on the distribution family.
void findBestParameters(CvResult &cv_result, const std::unique_ptr< Score > &scorer)
Identifies the best parameters from cross-validation results.
Eigen::VectorXd stdDevs(const Eigen::SparseMatrix< double > &x)
Computes the standard deviation for each column of a matrix.
std::vector< std::map< std::string, double > > createGrid(const std::map< std::string, std::vector< double > > ¶m_values)
Creates a grid of parameter combinations from parameter value ranges.
CvResult crossValidate(Slope model, MatrixType &x, const Eigen::MatrixXd &y_in, const CvConfig &config=CvConfig())
Performs cross-validation on a SLOPE model to select optimal hyperparameters.
Scoring metrics for model evaluation.
SLOPE (Sorted L-One Penalized Estimation) optimization.
Configuration settings for cross-validation.
int n_repeats
Number of times to repeat the cross-validation (default: 1)
std::map< std::string, std::vector< double > > hyperparams
Map of hyperparameter names to vectors of values to evaluate.
std::optional< std::vector< std::vector< std::vector< int > > > > predefined_folds
Optional user-defined fold assignments for custom cross-validation splits.
int n_folds
Number of folds for cross-validation (default: 10)
std::string metric
Evaluation metric used for model assessment (default: "mse")
std::map< std::string, std::vector< double > > default_hyperparams
Map of hyperparameter names to vectors of values to evaluate.
uint64_t random_seed
Seed for random number generator to ensure reproducibility (default: 42)
Contains overall results from a cross-validation process.
double best_score
The score achieved by the optimal hyperparameter configuration.
std::map< std::string, double > best_params
std::vector< GridResult > results
int best_ind
Index of the best performing configuration in the results vector.
Stores cross-validation results for a specific set of hyperparameters.
Eigen::ArrayXd mean_scores
Array of scores averaged across all folds for each alpha value.
std::map< std::string, double > params
Map of hyperparameter names to their values for the configuration.
Eigen::ArrayXd std_errors
Eigen::ArrayXd alphas
Array of regularization parameters used in the regularization path.