Half-Moments
Half-moment measures for data analysis.
This module provides functions for: 1. Half-moment based measures: hm, ham, asymmetry, extremity, cextremity 2. Mean Root Error (MRE)
References
Svetunkov I., Kourentzes N., Svetunkov S. “Half Central Moment for Data Analysis”. Working Paper of Department of Management Science, Lancaster University, 2023:3, 1-21.
Kourentzes N. (2014). The Bias Coefficient: a new metric for forecast bias.
- greybox.hm.asymmetry(x: ndarray, center: float | None = None) float[source]
Asymmetry coefficient.
The asymmetry coefficient measures the asymmetry of a distribution around a center point based on the half moment.
- Formula:
asymmetry = 1 - Arg(hm(x, C)) / (pi/4)
- Values:
1: Maximum negative asymmetry (all values below center)
0: Symmetric distribution
-1: Maximum positive asymmetry (all values above center)
- Parameters:
x (np.ndarray) – Data vector.
center (float, optional) – Centering parameter. If None, uses mean(x).
- Returns:
Asymmetry coefficient.
- Return type:
float
Examples
>>> x = np.array([1, 2, 3, 4, 5]) >>> asymmetry(x) 0.0 >>> x_skewed = np.array([1, 1, 1, 4, 5]) >>> asymmetry(x_skewed) -0.20...
References
Svetunkov I., Kourentzes N., Svetunkov S. “Half Central Moment for Data Analysis”.
- greybox.hm.cextremity(x: ndarray, center: float | None = None) complex[source]
Complex Extremity coefficient.
The complex extremity coefficient extends the extremity coefficient to capture both magnitude and phase information.
- Formula:
cextremity = 2 * (CH * 2)^(log(0.5)/log(2*3^(-0.75))) - 1
where CH = hm(x, C) / (std(x)^0.25)
- Parameters:
x (np.ndarray) – Data vector.
center (float, optional) – Centering parameter. If None, uses mean(x).
- Returns:
Complex extremity coefficient.
- Return type:
complex
Examples
>>> x = np.array([1, 2, 3, 4, 5]) >>> cextremity(x) (-0.04...+0...j)
References
Svetunkov I., Kourentzes N., Svetunkov S. “Half Central Moment for Data Analysis”.
- greybox.hm.extremity(x: ndarray, center: float | None = None) float[source]
Extremity coefficient.
The extremity coefficient measures how extreme the values in a distribution are relative to the center.
- Formula:
extremity = 2 * (ham(x, C) / (std(x)^0.5))^(log(0.5)/log(2*3^(-0.75))) - 1
- Parameters:
x (np.ndarray) – Data vector.
center (float, optional) – Centering parameter. If None, uses mean(x).
- Returns:
Extremity coefficient.
- Return type:
float
Examples
>>> x = np.array([1, 2, 3, 4, 5]) >>> extremity(x) -0.04...
References
Svetunkov I., Kourentzes N., Svetunkov S. “Half Central Moment for Data Analysis”.
- greybox.hm.ham(x: ndarray, center: float | None = None) float[source]
Half Absolute Moment (HAM).
The half absolute moment is the absolute value of the half moment. It captures the magnitude of deviations without direction.
- Formula:
ham = mean(sqrt(abs(x - C)))
- Parameters:
x (np.ndarray) – Data vector.
center (float, optional) – Centering parameter. If None, uses mean(x).
- Returns:
Half absolute moment.
- Return type:
float
Examples
>>> x = np.array([1, 2, 3, 4, 5]) >>> ham(x) 1.0
References
Svetunkov I., Kourentzes N., Svetunkov S. “Half Central Moment for Data Analysis”.
- greybox.hm.hm(x: ndarray, center: float | None = None) complex[source]
Half Moment (HM).
The half moment is a measure that captures the asymmetry of a distribution by considering only the positive or negative deviations from a center point.
- Formula:
hm = mean(sqrt(x - C))
where C is the centering parameter (default is mean of x).
- Parameters:
x (np.ndarray) – Data vector.
center (float, optional) – Centering parameter. If None, uses mean(x).
- Returns:
Half moment (complex number when mean of sqrt is taken).
- Return type:
complex
Examples
>>> x = np.array([1, 2, 3, 4, 5]) >>> hm(x) (0.79...+0.79...j)
References
Svetunkov I., Kourentzes N., Svetunkov S. “Half Central Moment for Data Analysis”.
- greybox.hm.mre(actual: ndarray, forecast: ndarray, na_rm: bool = True) float[source]
Mean Root Error (MRE).
The MRE measures the average signed root error. It is used as a bias coefficient (Kourentzes, 2014) and can detect systematic over or under-forecasting.
- Formula:
MRE = mean(sqrt(actual - forecast))
Note: When actual < forecast, the result is complex. The function returns the real part of the mean.
- Parameters:
actual (np.ndarray) – Actual (observed) values.
forecast (np.ndarray) – Forecasted values.
na_rm (bool, default=True) – Remove NA values.
- Returns:
Mean Root Error.
- Return type:
float
Examples
>>> actual = np.array([1, 2, 3, 4, 5]) >>> forecast = np.array([1.1, 2.0, 3.2, 3.9, 5.1]) >>> mre(actual, forecast)
References
Kourentzes N. (2014). The Bias Coefficient: a new metric for forecast bias. https://kourentzes.com/forecasting/2014/12/17/the-bias-coefficient-a-new-metric-for-forecast-bias/