Skip to content

Commit 663f520

Browse files
committed
Add docstrings for plot_ar.py and plot_forecast_ar.py and improve naming of the plots.
1 parent 8cbc4b4 commit 663f520

File tree

4 files changed

+48
-9
lines changed

4 files changed

+48
-9
lines changed

src/lennart_epp/analysis/task_forecast_ar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
def task_forecast_ar(
1515
data=BLD / "data" / "cleaned_apple_data.pkl",
1616
produces=BLD / "forecasts" / "multistep_forecast.pkl",
17-
lags=50,
17+
lags=30,
1818
):
1919
"""Generate multi-step forecasts using an AR model.
2020

src/lennart_epp/final/plot_ar.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,21 @@
77

88
from lennart_epp.analysis.evaluate_ar_model import _predict_ar
99

10-
msg = "Fehler beim Erstellen des PDF-Plots"
10+
msg = "Pdf could not be created."
1111

1212

1313
def _predict_ar_from_coefficients(
1414
df: pd.DataFrame, integrated_coefficients: pd.DataFrame
1515
) -> pd.Series:
16+
"""Generate in-sample predictions using AR model coefficients.
17+
18+
Args:
19+
df (pd.DataFrame): The DataFrame containing the time series data.
20+
integrated_coefficients (pd.DataFrame): DataFrame of integrated AR coefficients.
21+
22+
Returns:
23+
pd.Series: A series of in-sample predictions, aligned with the original index.
24+
"""
1625
model_results = {"integrated_coefficients": integrated_coefficients}
1726

1827
predictions = _predict_ar(df, model_results)
@@ -32,6 +41,18 @@ def plot_top_ar_models(
3241
*,
3342
export_as_pdf: bool = False,
3443
):
44+
"""Plot and save the top AR models' approximations of the Apple stock price.
45+
46+
Args:
47+
top_models (pd.DataFrame): DataFrame containing the top AR(p) models,
48+
including their integrated coefficients.
49+
df (pd.DataFrame): The DataFrame containing the stock price time series.
50+
plot_path (str): The path to save the generated HTML plot.
51+
export_as_pdf (bool, optional): If True, also exports the plots as a PDF.
52+
53+
Returns:
54+
str: The path where the plot is saved.
55+
"""
3556
fig = go.Figure()
3657
fig.add_trace(
3758
go.Scatter(
@@ -87,8 +108,6 @@ def plot_top_ar_models(
87108
height=300,
88109
)
89110
combined_html = (
90-
"<h1>Top 3 AR(p) Models vs. Original Data (Zoom on Last 100 Days, "
91-
"Y-axis 140-200)</h1>\n"
92111
f"{fig.to_html(full_html=False, include_plotlyjs='cdn')}\n"
93112
"<h2>Residuals of AR(p) Models</h2>\n"
94113
f"{residual_fig.to_html(full_html=False, include_plotlyjs='cdn')}"

src/lennart_epp/final/plot_forecast_ar.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
missing_close_msg = "Close price column is missing in the original data."
66
empty_forecast_msg = "Forecast data is empty."
77
invalid_index_msg = "Forecast data does not have a DatetimeIndex."
8-
msg_pdf = "Fehler beim Erstellen des PDF-Plots"
8+
msg_pdf = "Pdf_plot could not be created."
99

1010

1111
def plot_forecast_ar(
@@ -15,6 +15,23 @@ def plot_forecast_ar(
1515
*,
1616
export_as_pdf: bool = True,
1717
):
18+
"""Plot and save the original Apple stock price alongside the AR model forecast.
19+
20+
Args:
21+
data_path (str): Path to the cleaned stock price data.
22+
forecast_path (str): Path to the AR model forecast data.
23+
output_path (str): Path to save the generated plot.
24+
export_as_pdf (bool, optional): If True, also exports the plot as a PDF.
25+
26+
Returns:
27+
str: The path where the plot is saved.
28+
29+
Raises:
30+
KeyError: If the 'close_price' column is missing in the input data.
31+
ValueError: If the forecast data is empty.
32+
TypeError: If the forecast index is not a DatetimeIndex.
33+
RuntimeError: If exporting to PDF fails.
34+
"""
1835
df = pd.read_pickle(data_path)
1936
forecast = pd.read_pickle(forecast_path)
2037

@@ -53,10 +70,10 @@ def plot_forecast_ar(
5370
)
5471

5572
fig.update_layout(
56-
title="Apple Aktienkurs: Original vs. AR-Forecast (2023)",
73+
title="Apple Stock Price: Original vs. AR-Multi-Step-Forecast",
5774
xaxis_title="Datum",
5875
yaxis_title="Kurs (USD)",
59-
legend_title="Legende",
76+
legend_title="Legend",
6077
template="plotly_white",
6178
autosize=True,
6279
yaxis={"range": [143, 180]},

src/lennart_epp/final/plot_memory.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ def plot_acf(acf_data: dict, save_path_html: str, save_path_pdf: str, n_obs: int
2424

2525
fig.add_trace(
2626
go.Bar(
27-
x=lags, y=acf_values, marker={"color": "rgba(0, 0, 255, 0.8)"}, name="ACF"
27+
x=lags,
28+
y=acf_values,
29+
marker={"color": "rgba(0, 0, 255, 0.8)"},
30+
name="ACF Value",
2831
)
2932
)
3033

@@ -49,7 +52,7 @@ def plot_acf(acf_data: dict, save_path_html: str, save_path_pdf: str, n_obs: int
4952
)
5053

5154
fig.update_layout(
52-
title="Autocorrelation Function (ACF) with Confidence Bands",
55+
title="ACF of differenced Close Price with Confidence Bands",
5356
xaxis_title="Lag",
5457
yaxis_title="ACF Value",
5558
template="plotly_white",

0 commit comments

Comments
 (0)