CMP++: Uncertainty Quantification & Bayesian Calibration
Loading...
Searching...
No Matches
cmp::statistics::KFold Class Reference

K-Fold cross-validation partition generator. More...

#include <statistics.h>

Classes

class  KFoldIterator
 Iterator class to traverse folds for K-Fold cross-validation. More...
 

Public Member Functions

 KFold (Eigen::Index n_splits, Eigen::Index n_obs, bool shuffle=false, unsigned int random_state=42)
 Constructor for the KFold partitioner.
 
KFoldIterator begin () const
 Gets the iterator pointing to the first fold.
 
KFoldIterator end () const
 Gets the iterator pointing to the end of the folds.
 
std::pair< Eigen::VectorXs, Eigen::VectorXsoperator() (Eigen::Index split) const
 Directly accesses the train/test indices for a specific split without iterating.
 
size_t nFolds () const
 Gets the total number of splits/folds \( K \).
 

Private Attributes

Eigen::Index nSplits_
 Number of folds/splits \( K \).
 
Eigen::Index nObs_
 Total number of observations \( N \) in the dataset.
 
bool shuffle_
 Flag indicating if the dataset indices should be shuffled before splitting.
 
unsigned int rngState_
 Seed for the random number generator.
 
std::default_random_engine rng_
 Random number engine used for shuffling.
 
Eigen::VectorXs indices_
 Internal array storing the (potentially shuffled) sequence of indices.
 

Detailed Description

K-Fold cross-validation partition generator.

Mathematical Formulation

Partitions a dataset \(\mathcal{D}\) of size \(N\) into \(K\) mutually exclusive, approximately equal-sized subsets \(\{F_1, \dots, F_K\}\) such that:

\[ \bigcup_{k=1}^K F_k = \mathcal{D}, \quad F_i \cap F_j = \emptyset \quad \forall i \neq j \]

During the \(k\)-th fold iteration, the model is trained on \(\mathcal{D} \setminus F_k\) and validated on \(F_k\).

  • ### Implementation Algorithm
  1. Computes the base size of each fold: \(S = \lfloor N/K \rfloor\).
  2. Assigns indices to splits. The \(k\)-th split covers index range \([k \cdot S, (k+1) \cdot S)\), and the last fold covers any remainder.
  3. Returns the respective train and test index vectors.

Constructor & Destructor Documentation

◆ KFold()

cmp::statistics::KFold::KFold ( Eigen::Index  n_splits,
Eigen::Index  n_obs,
bool  shuffle = false,
unsigned int  random_state = 42 
)
inline

Constructor for the KFold partitioner.

Parameters
n_splitsNumber of folds \( K \). Must be at least 2 and at most n_obs.
n_obsTotal number of observations \( N \) in the dataset. Must be at least 2.
shuffleWhether to shuffle the data before splitting into folds.
random_stateSeed for the random number generator (used if shuffle is true).
Exceptions
std::invalid_argumentif the input parameters violate minimum constraints.

Member Function Documentation

◆ begin()

KFoldIterator cmp::statistics::KFold::begin ( ) const
inline

Gets the iterator pointing to the first fold.

Returns
A KFoldIterator initialized to split 0.

◆ end()

KFoldIterator cmp::statistics::KFold::end ( ) const
inline

Gets the iterator pointing to the end of the folds.

Returns
A KFoldIterator initialized to split K.

◆ nFolds()

size_t cmp::statistics::KFold::nFolds ( ) const
inline

Gets the total number of splits/folds \( K \).

Returns
The number of folds.

◆ operator()()

std::pair< Eigen::VectorXs, Eigen::VectorXs > cmp::statistics::KFold::operator() ( Eigen::Index  split) const
inline

Directly accesses the train/test indices for a specific split without iterating.

Parameters
splitThe specific split index \( k \) to extract.
Returns
A pair of training and test index vectors.
Exceptions
std::out_of_rangeif the requested split is invalid.

Member Data Documentation

◆ indices_

Eigen::VectorXs cmp::statistics::KFold::indices_
private

Internal array storing the (potentially shuffled) sequence of indices.

◆ nObs_

Eigen::Index cmp::statistics::KFold::nObs_
private

Total number of observations \( N \) in the dataset.

◆ nSplits_

Eigen::Index cmp::statistics::KFold::nSplits_
private

Number of folds/splits \( K \).

◆ rng_

std::default_random_engine cmp::statistics::KFold::rng_
private

Random number engine used for shuffling.

◆ rngState_

unsigned int cmp::statistics::KFold::rngState_
private

Seed for the random number generator.

◆ shuffle_

bool cmp::statistics::KFold::shuffle_
private

Flag indicating if the dataset indices should be shuffled before splitting.


The documentation for this class was generated from the following file: