Skip to content

Cross Validation Library

Omega Joctan edited this page Mar 3, 2024 · 2 revisions

cross_validation.mqh: MQL5 Library for K-Fold Cross-Validation

The CrossValidation.mqh library provides the CCrossValidation class, specifically designed for implementing K-Fold Cross-Validation in MQL5. This technique is essential for robustly evaluating the performance of machine learning models by dividing the data into training and validation sets multiple times.

Key Functionalities:

  • K-Fold Cross-Validation:
    • CTensors* KFoldCV(matrix &data, uint n_spilts=5): Performs K-Fold cross-validation on the provided data matrix (data).
      • n_spilts: The number of folds (default: 5).
      • Returns a CTensors object containing the split data for each fold.

Internal Management:

  • The CCrossValidation class maintains an internal array of CTensors objects (tensors[]) to store the generated folds during cross-validation.
  • The destructor (~CCrossValidation) ensures proper memory management by checking and deleting any allocated CTensors objects within the array.

KFoldCV Function Breakdown:

  1. Memory Allocation:
    • Resizes the tensors array to accommodate a new CTensors object.
    • Creates a new CTensors object with the specified number of folds (n_spilts).
  2. Splitting Data:
    • Calculates the fold size based on the data size and number of folds.
    • Iterates through the data:
      • Extracts a specific data chunk using MatrixExtend::Get for the current fold.
      • Adds the extracted data chunk to the corresponding fold in the newly created CTensors object.
  3. Returning Results:
    • Returns the newly created CTensors object containing the split data for each fold.

Usage Example:

// Example: Performing K-Fold Cross-Validation (K=5)

matrix my_data;
// ... populate data matrix ...

CCrossValidation cv;
CTensors* folds = cv.KFoldCV(my_data, 5); // 5 folds

// Access data for each fold (example: accessing data for fold 2)
matrix fold_data = folds.Get(2);

// ... use fold_data for training and validation ...

cv.MemoryClear(); // Optionally, release memory used by the cross-validation object

This documentation clarifies the purpose and functionality of the CCrossValidation class, enabling MQL5 users to effectively implement K-Fold Cross-Validation in their machine-learning pipelines.