Skip to content

Commit 87f8340

Browse files
committed
Add memory.py and task_memory.py to analyze stationarity and long-memory effects, providing a more detailed analysis of the AR(p) model's ability to represent the data.
1 parent c145c8d commit 87f8340

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

src/lennart_epp/analysis/memory.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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())
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import pandas as pd
2+
3+
from lennart_epp.analysis.memory import (
4+
check_stat_diff_close,
5+
write_stationarity_results,
6+
)
7+
from lennart_epp.config import BLD
8+
9+
10+
def task_check_stat_diff_close(
11+
data=BLD / "data" / "cleaned_apple_data.pkl",
12+
produces=BLD / "memory" / "diff_close_stat_test.tex",
13+
):
14+
"""Task to check if the differenced 'close_price' is stationary, and saves as .tex.
15+
16+
Args:
17+
data (Path): Path to the cleaned Apple stock data (Pickle file).
18+
produces (Path): Path to the output LaTeX file.
19+
20+
Returns:
21+
None: Saves results to a .tex file.
22+
"""
23+
df = pd.read_pickle(data)
24+
25+
results = check_stat_diff_close(df, column="close_price")
26+
27+
write_stationarity_results(results, produces)
28+
29+
assert produces.exists(), f" Failed to produce {produces}"

0 commit comments

Comments
 (0)