Statistical Tests and Tools¶
This module is inspired by the exercises in the book “Advances in Financial Machine Learning” by Marcos Lopez de Prado.
Statistical tests and tools.
Functions:
Calculate the autocorrelation coefficient of the returns in a pandas DataFrame. |
|
|
Compute the hourly resampled bars variance and variance of variances for a given OHLC DataFrame. |
|
Perform the Jarque-Bera test for normality on the returns of an OHLC DataFrame. |
|
Calculate the Exponential Moving Average (EMA) of the given array using NumPy. |
|
Calculate the Simple Moving Average (SMA) of the given array using NumPy. |
- autocorrelation_coefficient(data: DataFrame) float[source]¶
Calculate the autocorrelation coefficient of the returns in a pandas DataFrame.
Parameters:
- datapandas.DataFrame
The DataFrame containing financial data in OHLC format.
Returns:
- float
The autocorrelation coefficient of the returns.
Notes:
The autocorrelation coefficient measures the strength of the linear relationship between the returns and their lagged values. A value of zero indicates no correlation, while a value of one indicates perfect positive correlation and a value of negative one indicates perfect negative correlation.
- compute_variances(data: DataFrame) tuple[Series, float][source]¶
Compute the hourly resampled bars variance and variance of variances for a given OHLC DataFrame.
Parameters:
- datapandas.DataFrame
The DataFrame containing financial data in OHLC format.
Returns:
- tuple
A tuple containing two pandas Series: - hourly_returns: The hourly resampled bars variance of the returns. - hourly_variances: The variance of the hourly variances.
Notes:
This function calculates the returns as the percentage change in the ‘Close’ column of the input DataFrame, and then groups the data by hour using the pd.Grouper method. The hourly resampled bars variance of the returns is then calculated for each hour, and the variance of these variances is calculated and returned as hourly_variances.
- jarque_bera_test(data) tuple[float, float][source]¶
Perform the Jarque-Bera test for normality on the returns of an OHLC DataFrame.
Parameters:
- datapandas.DataFrame
The DataFrame containing financial data in OHLC format.
Returns:
- tuple
A tuple containing the test statistic and p-value of the Jarque-Bera test.
Notes:
This function first calculates the returns as the percentage change in the ‘Close’ column of the input DataFrame, and then drops the first row, which will have a NaN return. Finally, the Jarque-Bera test for normality is performed on the returns using the jarque_bera function from the scipy.stats module. The test statistic and p-value are returned as a tuple.
The null hypothesis of the Jarque-Bera test is that the sample of data being tested is drawn from a normal distribution. Therefore, a small p-value (e.g., less than 0.05) indicates that the null hypothesis is rejected and the data is not normally distributed.
- ema_numpy(arr: ndarray, window: int) ndarray[source]¶
Calculate the Exponential Moving Average (EMA) of the given array using NumPy.
- Parameters:
arr (np.ndarray) – A NumPy array containing the input data.
window (int) – The window size for the EMA calculation.
- Returns:
A NumPy array containing the EMA values.
- Return type:
np.ndarray
Example:
import numpy as np arr = np.array([1.0, 2.5, 3.7, 4.2, 5.0, 6.3]) window = 3 ema_result = ema_numpy(arr, window) print(ema_result) [1. 1.66666667 2.61111111 3.40740741 4.27160494 5.18106996]
- sma_numpy(arr: ndarray, window: int) ndarray[source]¶
Calculate the Simple Moving Average (SMA) of the given array using NumPy.
- Parameters:
arr (np.ndarray) – A NumPy array containing the input data.
window (int) – The window size for the SMA calculation.
- Returns:
A NumPy array containing the SMA values.
- Return type:
np.ndarray
Example:
import numpy as np arr = np.array([1.0, 2.5, 3.7, 4.2, 5.0, 6.3]) window = 3 sma_result = sma_numpy(arr, window) print(sma_result) [ nan nan 2.4 3.46666667 4.3 5.16666667]