|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +import nolds |
| 4 | +import numpy as np |
1 | 5 | import pandas as pd |
2 | 6 |
|
3 | 7 | from lennart_epp.analysis.fit_ar_model import _check_stationarity, _difference_series |
@@ -76,3 +80,116 @@ def write_stationarity_results(results: dict, file_path): |
76 | 80 |
|
77 | 81 | with file_path.open("w", encoding="utf-8") as f: |
78 | 82 | f.write(latex_content.strip()) |
| 83 | + |
| 84 | + |
| 85 | +def _compute_mean(series: np.ndarray) -> float: |
| 86 | + """Compute the mean of a given time series. |
| 87 | +
|
| 88 | + Args: |
| 89 | + series (np.ndarray): The time series data as a NumPy array. |
| 90 | +
|
| 91 | + Returns: |
| 92 | + float: The mean of the time series. |
| 93 | + """ |
| 94 | + return np.mean(series) |
| 95 | + |
| 96 | + |
| 97 | +def _compute_variance(series: np.ndarray, mean_series: float) -> float: |
| 98 | + """Compute the variance of a time series. |
| 99 | +
|
| 100 | + Args: |
| 101 | + series (np.ndarray): The time series data as a NumPy array. |
| 102 | + mean_series (float): The precomputed mean of the series. |
| 103 | +
|
| 104 | + Returns: |
| 105 | + float: The variance of the time series. |
| 106 | + """ |
| 107 | + return np.sum((series - mean_series) ** 2) |
| 108 | + |
| 109 | + |
| 110 | +def _compute_autocovariance(series: np.ndarray, mean_series: float, lag: int) -> float: |
| 111 | + """Compute the autocovariance for a given lag in a time series. |
| 112 | +
|
| 113 | + Args: |
| 114 | + series (np.ndarray): The time series data as a NumPy array. |
| 115 | + mean_series (float): The precomputed mean of the series. |
| 116 | + lag (int): The lag at which to compute the autocovariance. |
| 117 | +
|
| 118 | + Returns: |
| 119 | + float: The autocovariance value for the specified lag. |
| 120 | + """ |
| 121 | + n = len(series) |
| 122 | + return np.sum((series[lag:] - mean_series) * (series[: n - lag] - mean_series)) |
| 123 | + |
| 124 | + |
| 125 | +def compute_acf( |
| 126 | + df: pd.DataFrame, column: str = "close_price", lags: int = 1000 |
| 127 | +) -> dict: |
| 128 | + """Compute the ACF manually for the first-differenced time series. |
| 129 | +
|
| 130 | + Args: |
| 131 | + df (pd.DataFrame): The dataframe containing the time series. |
| 132 | + column (str, optional): Column to analyze. Defaults to "close_price". |
| 133 | + lags (int, optional): Number of lags for ACF. |
| 134 | +
|
| 135 | + Returns: |
| 136 | + dict: A dictionary containing ACF values and corresponding lags. |
| 137 | + """ |
| 138 | + df_diff = _difference_series(df, column) |
| 139 | + series = df_diff[f"diff_{column}"].dropna().to_numpy() |
| 140 | + |
| 141 | + lags = min(len(series) - 1, lags) |
| 142 | + mean_series = _compute_mean(series) |
| 143 | + variance = _compute_variance(series, mean_series) |
| 144 | + |
| 145 | + acf_values = [] |
| 146 | + for lag in range(lags + 1): |
| 147 | + autocovariance = _compute_autocovariance(series, mean_series, lag) |
| 148 | + acf_values.append(autocovariance / variance) |
| 149 | + |
| 150 | + return {"acf": np.array(acf_values), "lags": np.arange(len(acf_values))} |
| 151 | + |
| 152 | + |
| 153 | +def compute_hurst_exponent(df: pd.DataFrame, column: str = "close_price") -> dict: |
| 154 | + """Compute the Hurst exponent to assess long-memory effects. |
| 155 | +
|
| 156 | + Args: |
| 157 | + df (pd.DataFrame): The dataframe containing the time series. |
| 158 | + column (str, optional): Column to analyze. Defaults to "close_price". |
| 159 | +
|
| 160 | + Returns: |
| 161 | + dict: A dictionary containing the computed Hurst exponent. |
| 162 | + """ |
| 163 | + series = df[column].dropna().to_numpy() |
| 164 | + hurst_value = nolds.hurst_rs(series) |
| 165 | + |
| 166 | + return {"Hurst Exponent": hurst_value} |
| 167 | + |
| 168 | + |
| 169 | +def write_hurst_result_to_tex(results: dict, file_path: Path): |
| 170 | + """Write the computed Hurst exponent results to a LaTeX file. |
| 171 | +
|
| 172 | + Args: |
| 173 | + results (dict): Dictionary containing the Hurst exponent. |
| 174 | + file_path (Path): Path where the LaTeX file will be saved. |
| 175 | + """ |
| 176 | + file_path.parent.mkdir(parents=True, exist_ok=True) |
| 177 | + |
| 178 | + hurst_value = results["Hurst Exponent"] |
| 179 | + |
| 180 | + latex_content = f""" |
| 181 | + \\begin{{table}}[H] |
| 182 | + \\centering |
| 183 | + \\caption{{Hurst Exponent Analysis}} |
| 184 | + \\label{{tab:hurst_exponent}} |
| 185 | + \\begin{{tabular}}{{l c}} |
| 186 | + \\toprule |
| 187 | + \\textbf{{Metric}} & \\textbf{{Value}} \\\\ |
| 188 | + \\midrule |
| 189 | + Hurst Exponent & {hurst_value:.4f} \\\\ |
| 190 | + \\bottomrule |
| 191 | + \\end{{tabular}} |
| 192 | + \\end{{table}} |
| 193 | + """ |
| 194 | + |
| 195 | + file_path.write_text(latex_content.strip(), encoding="utf-8") |
0 commit comments