|
| 1 | +import pandas as pd |
| 2 | + |
| 3 | +from lennart_epp.analysis.fit_ar_model import _check_stationarity, _difference_series |
| 4 | + |
| 5 | +error_msg = "Column '{column}' not found in dataframe." |
| 6 | + |
| 7 | + |
| 8 | +def check_stat_diff_close(df: pd.DataFrame, column: str = "close_price") -> dict: |
| 9 | + """Check if the first-differenced close_price is stationary. |
| 10 | +
|
| 11 | + Args: |
| 12 | + df (pd.DataFrame): The dataframe containing the time series. |
| 13 | + column (str, optional): Column to test for stationarity. |
| 14 | +
|
| 15 | + Returns: |
| 16 | + dict: A dictionary containing the ADF test results. |
| 17 | + """ |
| 18 | + if column not in df.columns: |
| 19 | + raise ValueError(error_msg) |
| 20 | + |
| 21 | + df_diff = _difference_series(df, column) |
| 22 | + |
| 23 | + diff_column = f"diff_{column}" |
| 24 | + |
| 25 | + is_stationary, p_value, test_statistic_adf = _check_stationarity( |
| 26 | + df_diff, diff_column |
| 27 | + ) |
| 28 | + |
| 29 | + return { |
| 30 | + "ADF Test Statistic": test_statistic_adf, |
| 31 | + "P-Value": p_value, |
| 32 | + "Is Stationary": is_stationary, |
| 33 | + } |
| 34 | + |
| 35 | + |
| 36 | +def write_stationarity_results(results: dict, file_path): |
| 37 | + """Generate and save a LaTeX table for Augmented Dickey-Fuller (ADF) test results. |
| 38 | +
|
| 39 | + Args: |
| 40 | + results (dict): A dictionary containing the ADF test results with keys: |
| 41 | + - "ADF Test Statistic" (float or None): The test statistic value. |
| 42 | + - "P-Value" (float): The p-value from the ADF test. |
| 43 | + - "Is Stationary" (bool): Whether the series is stationary. |
| 44 | + file_path (Path): The file path where the LaTeX table will be saved. |
| 45 | +
|
| 46 | + Returns: |
| 47 | + None: The function writes the LaTeX table to the specified file. |
| 48 | + """ |
| 49 | + file_path.parent.mkdir(parents=True, exist_ok=True) |
| 50 | + |
| 51 | + test_stat = results["ADF Test Statistic"] |
| 52 | + test_stat_str = f"{test_stat:.4f}" if test_stat is not None else "Not Available" |
| 53 | + |
| 54 | + conclusion_text = ( |
| 55 | + "The differenced series is likely stationary." |
| 56 | + if results["Is Stationary"] |
| 57 | + else "The differenced series may not be stationary." |
| 58 | + ) |
| 59 | + |
| 60 | + latex_content = f""" |
| 61 | + \\begin{{table}}[H] |
| 62 | + \\centering |
| 63 | + \\caption{{Results of the Augmented Dickey-Fuller (ADF) Test}} |
| 64 | + \\label{{tab:stationarity_test}} |
| 65 | + \\begin{{tabular}}{{l c}} |
| 66 | + \\toprule |
| 67 | + \\textbf{{Test Statistic}} & \\textbf{{Value}} \\\\ |
| 68 | + \\midrule |
| 69 | + ADF Test Statistic & {test_stat_str} \\\\ |
| 70 | + P-Value & {results["P-Value"]:.4f} \\\\ |
| 71 | + Conclusion & {conclusion_text} \\\\ |
| 72 | + \\bottomrule |
| 73 | + \\end{{tabular}} |
| 74 | + \\end{{table}} |
| 75 | + """ |
| 76 | + |
| 77 | + with file_path.open("w", encoding="utf-8") as f: |
| 78 | + f.write(latex_content.strip()) |
0 commit comments