Skip to main content

Class MetricUtil

Provides statistical metric calculations commonly used for model evaluation and data analysis.

Namespace: Workspace.XBR.Xperiflow.Utilities.Metrics

Assembly: Xperiflow.dll

Declaration
public static class MetricUtil

Examples

Basic metric calculations for model evaluation:

// Sample data: predictions vs actual values
var predictions = new[] { 10.5, 15.2, 8.7, 12.1, 9.8 };
var actuals = new[] { 10.0, 15.0, 9.0, 12.5, 10.0 };

// Calculate error metrics
var mse = MetricUtil.MeanSquaredError(predictions, actuals);
var rmse = MetricUtil.RootMeanSquareError(predictions, actuals);
var mae = MetricUtil.MeanAbsoluteError(predictions, actuals);

// Calculate accuracy
var accuracy = MetricUtil.AbsoluteAccuracy(predictions, actuals);

// Display results
Console.WriteLine($"MSE: `{mse:F3}`");
Console.WriteLine($"RMSE: `{rmse:F3}`");
Console.WriteLine($"MAE: `{mae:F3}`");
Console.WriteLine($"Accuracy: `{accuracy:P1}`");

Descriptive statistics for data analysis:

var dataset = new[] { 1.5, 2.3, 3.1, 2.7, 4.2, 1.8, 3.5, 2.9, 3.8, 2.1 };

// Calculate descriptive statistics
var stdDev = MetricUtil.StandardDeviation(dataset);
var median = MetricUtil.Median(sessionInfo, dataset);

Console.WriteLine($"Standard Deviation: `{stdDev:F3}`");
Console.WriteLine($"Median: `{median:F3}`");

Remarks

The Workspace.XBR.Xperiflow.Utilities.Metrics.MetricUtil class contains static methods for calculating various statistical metrics that are essential for evaluating model performance, analyzing prediction accuracy, and performing general statistical analysis in data science and machine learning workflows.

Metric Categories:

  • Error MetricsMean Squared Error (MSE), Root Mean Squared Error (RMSE), Mean Absolute Error (MAE)

  • Accuracy MetricsAbsolute accuracy calculations for prediction evaluation

  • Bias MetricsMean Bias Error (MBE), Percent Bias Error for systematic error analysis

  • Descriptive StatisticsStandard deviation, median, mode calculations

Input Requirements:

Most methods expect prediction and actual value collections to be of the same length. The methods handle empty collections gracefully, typically returning zero or appropriate default values. Null values in the collections should be handled by the caller.

Performance:

The methods use efficient enumeration patterns to minimize memory usage and provide good performance for large datasets. They use single-pass algorithms where possible to avoid multiple iterations over large collections.

Methods

MeanSquaredError(IEnumerable<double>, IEnumerable<double>)

Calculates the Mean Squared Error (MSE) between predicted and actual values.

Declaration
public static double MeanSquaredError(IEnumerable<double> prediction, IEnumerable<double> actuals)
Remarks

Mean Squared Error (MSE) is calculated as the average of the squared differences between predicted and actual values. It is defined mathematically as:

MSE = (1/n) * Σ(predicted_i - actual_i)²

MSE is particularly useful for:

Note: MSE is sensitive to outliers due to the squaring operation, which can disproportionately affect the result when large errors are present.

Returns

System.Double

The Mean Squared Error as a double value

Parameters
TypeNameDescription
System.Collections.Generic.IEnumerable<System.Double>predictionThe predicted values
System.Collections.Generic.IEnumerable<System.Double>actualsThe actual (observed) values
Exceptions

System.ArgumentNullException Thrown when prediction or actuals is null

RootMeanSquareError(IEnumerable<double>, IEnumerable<double>)

Calculates the Root Mean Squared Error (RMSE) between predicted and actual values.

Declaration
public static double RootMeanSquareError(IEnumerable<double> prediction, IEnumerable<double> actuals)
Remarks

Root Mean Squared Error (RMSE) is the square root of the Mean Squared Error. It is defined mathematically as:

RMSE = √(MSE) = √((1/n) * Σ(predicted_i - actual_i)²)

RMSE has several advantages over MSE:

Interpretation: Lower RMSE values indicate better model performance. RMSE = 0 indicates perfect predictions.

Returns

System.Double

The Root Mean Squared Error as a double value

Parameters
TypeNameDescription
System.Collections.Generic.IEnumerable<System.Double>predictionThe predicted values
System.Collections.Generic.IEnumerable<System.Double>actualsThe actual (observed) values
Exceptions

System.ArgumentNullException Thrown when prediction or actuals is null

MeanAbsoluteError(IEnumerable<double>, IEnumerable<double>)

Calculates the Mean Absolute Error (MAE) between predicted and actual values.

Declaration
public static double MeanAbsoluteError(IEnumerable<double> prediction, IEnumerable<double> actuals)
Remarks

Mean Absolute Error (MAE) is calculated as the average of the absolute differences between predicted and actual values. It is defined mathematically as:

MAE = (1/n) * Σ|predicted_i - actual_i|

MAE characteristics:

Use Cases: MAE is preferred when you want to treat all errors equally and when outliers should not disproportionately influence the metric.

Returns

System.Double

The Mean Absolute Error as a double value

Parameters
TypeNameDescription
System.Collections.Generic.IEnumerable<System.Double>predictionThe predicted values
System.Collections.Generic.IEnumerable<System.Double>actualsThe actual (observed) values
Exceptions

System.ArgumentNullException Thrown when prediction or actuals is null

AbsoluteAccuracy(IEnumerable<double>, IEnumerable<double>)

Calculates the absolute accuracy as a percentage based on Mean Absolute Error and actual values.

Declaration
public static double AbsoluteAccuracy(IEnumerable<double> prediction, IEnumerable<double> actuals)
Remarks

Absolute accuracy is calculated as one minus the ratio of Mean Absolute Error to the mean of actual values:

Accuracy = 1 - (MAE / mean(actuals))

Special Cases:

Interpretation:

Returns

System.Double

The absolute accuracy as a value between 0 and 1, where 1 represents perfect accuracy

Parameters
TypeNameDescription
System.Collections.Generic.IEnumerable<System.Double>predictionThe predicted values
System.Collections.Generic.IEnumerable<System.Double>actualsThe actual (observed) values

AbsoluteAccuracy(double, IEnumerable<double>)

Calculates the absolute accuracy based on a pre-computed Mean Absolute Error and actual values.

Declaration
public static double AbsoluteAccuracy(double meanAbsoluteError, IEnumerable<double> actuals)
Remarks

This overload is useful when you have already computed the Mean Absolute Error and want to avoid recalculating it. The accuracy calculation follows the same formula as the other overload:

Accuracy = 1 - (MAE / mean(actuals))

Performance Benefit: Use this method when you need both MAE and accuracy metrics to avoid redundant calculations.

Returns

System.Double

The absolute accuracy as a value between 0 and 1, where 1 represents perfect accuracy

Parameters
TypeNameDescription
System.DoublemeanAbsoluteErrorThe pre-computed Mean Absolute Error value
System.Collections.Generic.IEnumerable<System.Double>actualsThe actual (observed) values used to calculate the mean

SquaredError(IEnumerable<double>, IEnumerable<double>)

Calculates the total squared error between predicted and actual values.

Declaration
public static double SquaredError(IEnumerable<double> prediction, IEnumerable<double> actuals)
Remarks

The squared error is calculated as the sum of the squared differences between predicted and actual values:

SE = Σ(predicted_i - actual_i)²

Unlike MSE, this metric is not normalized by the number of observations, making it useful for scenarios where you need the total error magnitude rather than the average.

Use Cases:

Returns

System.Double

The sum of squared errors as a double value

Parameters
TypeNameDescription
System.Collections.Generic.IEnumerable<System.Double>predictionThe predicted values
System.Collections.Generic.IEnumerable<System.Double>actualsThe actual (observed) values

AbsoluteError(IEnumerable<double>, IEnumerable<double>)

Calculates the total absolute error between predicted and actual values.

Declaration
public static double AbsoluteError(IEnumerable<double> prediction, IEnumerable<double> actuals)
Remarks

The absolute error is calculated as the sum of the absolute differences between predicted and actual values:

AE = Σ|predicted_i - actual_i|

Unlike MAE, this metric is not normalized by the number of observations, providing the total magnitude of error across all predictions.

Applications:

Returns

System.Double

The sum of absolute errors as a double value

Parameters
TypeNameDescription
System.Collections.Generic.IEnumerable<System.Double>predictionThe predicted values
System.Collections.Generic.IEnumerable<System.Double>actualsThe actual (observed) values

NetError(IEnumerable<double>, IEnumerable<double>)

Calculate the net error between the prediction and the actuals. Note it is expected that the prediction and actuals are of the same length.

Declaration
public static double NetError(IEnumerable<double> prediction, IEnumerable<double> actuals)
Returns

System.Double

Parameters
TypeName
System.Collections.Generic.IEnumerable<System.Double>prediction
System.Collections.Generic.IEnumerable<System.Double>actuals

MeanBiasError(IEnumerable<double>, IEnumerable<double>)

Calculate the Mean Bias Error (MBE) between the prediction and the actuals. Note it is expected that the prediction and actuals are of the same length.

Declaration
public static double MeanBiasError(IEnumerable<double> prediction, IEnumerable<double> actuals)
Returns

System.Double

Parameters
TypeNameDescription
System.Collections.Generic.IEnumerable<System.Double>predictionThe predicted values.
System.Collections.Generic.IEnumerable<System.Double>actualsThe actual values.

PercentBiasError(IEnumerable<double>, IEnumerable<double>)

Calculate the Percent Bias Error between the prediction and the actuals. Note it is expected that the prediction and actuals are of the same length.

Declaration
public static double PercentBiasError(IEnumerable<double> prediction, IEnumerable<double> actuals)
Returns

System.Double

Parameters
TypeNameDescription
System.Collections.Generic.IEnumerable<System.Double>predictionThe predicted values.
System.Collections.Generic.IEnumerable<System.Double>actualsThe actual values.

MeanAbsolutePercentageError(IEnumerable<double>, IEnumerable<double>)

Calculate the Mean Absolute Percentage Error (MAPE) between the prediction and the actuals. Note it is expected that the prediction and actuals are of the same length.

Declaration
public static double MeanAbsolutePercentageError(IEnumerable<double> prediction, IEnumerable<double> actuals)
Returns

System.Double

Parameters
TypeNameDescription
System.Collections.Generic.IEnumerable<System.Double>predictionThe predicted values.
System.Collections.Generic.IEnumerable<System.Double>actualsThe actual values.

StandardDeviation(IEnumerable<double>)

Calculate the Standard Deviation of a series of numbers.

Declaration
public static double StandardDeviation(IEnumerable<double> series)
Returns

System.Double

Parameters
TypeNameDescription
System.Collections.Generic.IEnumerable<System.Double>seriesThe series of numbers.

ModeCalculation(IEnumerable<int>)

Calculates the Mode of a series of numbers.

Declaration
public static int ModeCalculation(IEnumerable<int> series)
Returns

System.Int32

Parameters
TypeNameDescription
System.Collections.Generic.IEnumerable<System.Int32>seriesThe series of numbers.

Median(SessionInfo, IEnumerable<double>)

Calculate the Median of a series of numbers.

Declaration
public static double Median(SessionInfo si, IEnumerable<double> series)
Returns

System.Double

The median value.

Parameters
TypeNameDescription
OneStream.Shared.Common.SessionInfosiThe OneStream session information.
System.Collections.Generic.IEnumerable<System.Double>seriesThe series of numbers.

Inherited Members

  • System.Object.Equals(System.Object)
  • System.Object.Equals(System.Object,System.Object)
  • System.Object.GetHashCode
  • System.Object.GetType
  • System.Object.MemberwiseClone
  • System.Object.ReferenceEquals(System.Object,System.Object)
  • System.Object.ToString

Was this page helpful?