Skip to content

Metrics library

Omega Joctan edited this page Mar 3, 2024 · 1 revision

Metrics.mqh: MQL5 Library for Evaluation Metrics

The metrics.mqh library provides an array of useful functions for calculating and evaluating various performance metrics in MQL5, particularly for machine learning tasks. These metrics help assess the effectiveness of your model's predictions compared to the ground truth (actual values).

Metrics Supported:

  • Regression Metrics:
    • R-squared
    • Adjusted R-squared
    • Residual Sum of Squares (RSS)
    • Mean Squared Error (MSE)
    • Root Mean Squared Error (RMSE)
    • Mean Absolute Error (MAE)
  • Classification Metrics:
    • Accuracy score
    • Accuracy per class
    • Precision per class
    • Recall per class
    • F1-score per class
    • Specificity per class
    • Receiver Operating Characteristic (ROC) curve

Functionalities:

  1. Regression Metrics:

    • r_squared(const vector &True, const vector &Pred): Calculates the R-squared value for regression models.
    • adjusted_r(const vector &True, const vector &Pred, uint indep_vars = 1): Calculates the adjusted R-squared value.
    • rss(const vector &True, const vector &Pred): Calculates the Residual Sum of Squares.
    • mse(const vector &True, const vector &Pred): Calculates the Mean Squared Error.
    • rmse(const vector &True, const vector &Pred): Calculates the Root Mean Squared Error.
    • mae(const vector &True, const vector &Pred): Calculates the Mean Absolute Error.
    • RegressionMetric(const vector &True, const vector &Pred, regression_metrics METRIC_): Calculates a specific regression metric based on the provided enum value (METRIC_).
  2. Classification Metrics:

    • accuracy_score(const vector &True, const vector &Pred): Calculates the overall accuracy (correctly classified instances).
    • accuracy(const vector &True, const vector &Preds): Calculates the accuracy per class (vector of values).
    • precision(const vector &True, const vector &Preds): Calculates the precision per class (vector of values).
    • recall(const vector &True, const vector &Preds): Calculates the recall per class (vector of values).
    • f1_score(const vector &True, const vector &Preds): Calculates the F1-score per class (vector of values).
    • specificity(const vector &True, const vector &Preds): Calculates the specificity per class (vector of values).
    • roc_curve(const vector &True, const vector &Preds, bool show_roc_curve=false): Calculates and returns an roc_curve_struct containing True Positive Rates (TPR), False Positive Rates (FPR), and thresholds. Optional argument to display the ROC curve graphically.
    • classification_report(const vector &True, const vector &Pred, bool show_roc_curve=false): Generates a comprehensive classification report with various metrics and optionally displays the ROC curve.
  3. Helper Functions:

    • SearchPatterns(const vector &True, int value_A, const vector &B, int value_B): Internal helper function used for calculations.

Additional Notes:

  • The vector type is commonly used to represent data in MQL5.
  • Ensure correct data sizes and types when using these functions (e.g., True and Pred vectors should have the same length).
  • Refer to the metrics.mqh file for the specific implementation and detailed comments within the code for more nuanced understanding.

Usage Example (Regression):

// Example: Calculating R-squared for a regression model

vector true_values;
vector predicted_values;

// ... populate vectors with true and predicted values ...

double r2_score = Metrics::r_squared(true_values, predicted_values);

Print("R-squared:", r2_score);

Usage Example (Classification):

// Example: Calculating accuracy and F1-score per class

vector true_labels;
vector predicted_labels;

// ... populate vectors with true and predicted labels ...

vector accuracy_per_class = Metrics::accuracy(true_labels, predicted_labels);
vector f1_score_per_class = Metrics::f1_score(true_labels, predicted_labels);

Print("Accuracy per class:", accuracy_per_class);
Print("F1-score per class:",
Clone this wiki locally