Skip to content

Commit 1e5f2d2

Browse files
committed
Add files plot_memory.py and task_plot_memory.py for plotting the ACF of the differenced close price with confidence intervals.
1 parent a6697fe commit 1e5f2d2

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import numpy as np
2+
import plotly.graph_objects as go
3+
4+
5+
def plot_acf(acf_data: dict, save_path_html: str, save_path_pdf: str, n_obs: int):
6+
"""Plot the manually computed ACF using Plotly with confidence bands.
7+
8+
Args:
9+
acf_data (dict): Dictionary containing "acf" values and "lags".
10+
save_path_html (str): Path to save the interactive HTML plot.
11+
save_path_pdf (str): Path to save the static PDF plot.
12+
n_obs (int): Number of observations in the time series (for confidence bands).
13+
14+
Returns:
15+
None: Saves plots in the specified paths.
16+
"""
17+
max_lags = 200
18+
lags = acf_data["lags"][:max_lags]
19+
acf_values = acf_data["acf"][:max_lags]
20+
21+
confidence_band = 1.96 / np.sqrt(n_obs)
22+
23+
fig = go.Figure()
24+
25+
fig.add_trace(
26+
go.Bar(
27+
x=lags, y=acf_values, marker={"color": "rgba(0, 0, 255, 0.8)"}, name="ACF"
28+
)
29+
)
30+
31+
fig.add_trace(
32+
go.Scatter(
33+
x=lags,
34+
y=[confidence_band] * len(lags),
35+
mode="lines",
36+
line={"color": "red", "dash": "dash"},
37+
name="95% Confidence Interval",
38+
)
39+
)
40+
41+
fig.add_trace(
42+
go.Scatter(
43+
x=lags,
44+
y=[-confidence_band] * len(lags),
45+
mode="lines",
46+
line={"color": "red", "dash": "dash"},
47+
showlegend=False,
48+
)
49+
)
50+
51+
fig.update_layout(
52+
title="Autocorrelation Function (ACF) with Confidence Bands",
53+
xaxis_title="Lag",
54+
yaxis_title="ACF Value",
55+
template="plotly_white",
56+
yaxis={"range": [-0.15, 0.3]},
57+
plot_bgcolor="whitesmoke",
58+
xaxis={"gridcolor": "lightgray"},
59+
)
60+
61+
fig.write_html(save_path_html)
62+
fig.write_image(save_path_pdf)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import pandas as pd
2+
3+
from lennart_epp.config import BLD, SRC
4+
from lennart_epp.final.plot_memory import plot_acf
5+
6+
7+
def task_plot_acf(
8+
script=SRC / "final" / "plot_memory.py",
9+
acf_data=BLD / "memory" / "acf.pkl",
10+
data=BLD / "data" / "cleaned_apple_data.pkl",
11+
produces=(
12+
BLD / "plots" / "acf_plot.html",
13+
BLD / "plots" / "acf_plot.pdf",
14+
),
15+
):
16+
"""Task to generate and save the ACF plot for the differenced close price.
17+
18+
Args:
19+
script (Path): Path to the script that generates the ACF plot.
20+
acf_data (Path): Path to the computed ACF values stored in memory.
21+
data (Path): Path to the original cleaned Apple stock data.
22+
produces (tuple[Path, Path]): Paths to the output HTML and PDF plots.
23+
24+
Returns:
25+
None: Ensures the plots are successfully generated.
26+
"""
27+
acf_results = pd.read_pickle(acf_data)
28+
29+
df = pd.read_pickle(data)
30+
n_obs = df["close_price"].diff().dropna().shape[0]
31+
32+
plot_acf(acf_results, produces[0], produces[1], n_obs)
33+
34+
for file in produces:
35+
assert file.exists(), f"Failed to produce {file}"

0 commit comments

Comments
 (0)