Skip to content

math_utils

def HP_two_sided(
    s: pd.Series | np.ndarray,
    lamb: float = 1600,
) -> tuple[pd.Series | np.ndarray, pd.Series | np.ndarray]
Apply the Two-Sided Hodrick-Prescott filter to a time series to separate the trend and cyclical components.

Parameters:

Name Type Description
s pd.Series | np.ndarray The input time series data.
lamb float The smoothing parameter. Default is 1600, commonly used for quarterly data.

Returns:

Type Description
pd.Series | np.ndarray Tuple index 0. The trend component of the series.
pd.Series | np.ndarray Tuple index 1. The cyclical component of the series.

 

def HP_one_sided(
    s: pd.Series | np.ndarray,
    lamb: float = 1600,
) -> tuple[pd.Series | np.ndarray, pd.Series | np.ndarray]
Apply the One-Sided Hodrick-Prescott filter to a time series to separate the trend and cyclical components.

Parameters:

Name Type Description
s pd.Series | np.ndarray The input time series data.
lamb float The smoothing parameter. Default is 1600, commonly used for quarterly data.

Returns:

Type Description
pd.Series | np.ndarray Tuple index 0. The trend component of the series.
pd.Series | np.ndarray Tuple index 1. The cyclical component of the series.

 

def annualized_log_percent(
    s: pd.Series | np.ndarray, periods_per_year: int = 4
) -> pd.Series | np.ndarray
Calculate the annualized log percent change of a time series.

Parameters:

Name Type Description
s pd.Series | np.ndarray The input time series data.
periods_per_year int Number of periods in a year. Default is 4 for quarterly data.

Returns:

Type Description
pd.Series | np.ndarray The annualized log percent change of the series.

 

def demean(
    s: pd.Series | np.ndarray
) -> pd.Series | np.ndarray
Parameters:

Name Type Description
s pd.Series | np.ndarray The input series.

Returns:

Type Description
pd.Series | np.ndarray The demeaned series.

 

def detrend(
    s: pd.Series | np.ndarray
) -> pd.Series | np.ndarray
Detrend a time series by removing its linear trend.

Detrending

This process subtracts the "Line of Best Fit" (LoBF) from the data. LoBF is computed as the slope and intercept that satisfy: $$ \underset{m,b}{\operatorname{argmin}}\,\ \sum_{i=0}^n (y_i - (mx_i+b))^2 $$

Parameters:

Name Type Description
s pd.Series | np.ndarray The input series.

Returns:

Type Description
pd.Series | np.ndarray The detrended series.