Skip to content

Commit 3f5b1be

Browse files
committed
fixing ARIMA article, part 1
1 parent deb5c36 commit 3f5b1be

File tree

12 files changed

+1190
-17
lines changed

12 files changed

+1190
-17
lines changed

content/posts/finance/stock_prediction/ARIMA/arima_example.ipynb

+561
Large diffs are not rendered by default.

content/posts/finance/stock_prediction/ARIMA/arima_example.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,17 @@ def test_stationarity(timeseries):
3434

3535
# Forecast
3636
forecast = results.forecast(steps=30)
37-
help(forecast)
37+
# help(forecast)
38+
print(forecast)
3839

3940
# Plot the results
4041
plt.figure(figsize=(12,6))
4142
plt.plot(ts.index[-100:], ts.values[-100:], label='Observed')
4243
plt.plot(forecast.index, forecast.values, color='r', label='Forecast')
43-
plt.fill_between(forecast.index,
44-
forecast.conf_int().iloc[:, 0],
45-
forecast.conf_int().iloc[:, 1],
46-
color='pink', alpha=0.3)
44+
# plt.fill_between(forecast.index,
45+
# forecast.iloc[:, 0],
46+
# forecast.iloc[:, 1],
47+
# color='pink', alpha=0.3)
4748
plt.title(f'{ticker} Stock Price Prediction')
4849
plt.legend()
4950
plt.show()
Loading

content/posts/finance/stock_prediction/ARIMA/index.md

+19-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ menu:
88
identifier: ARIMA
99
parent: stock_prediction
1010
weight: 10
11-
hero:
11+
hero: images/find_d.png
1212
tags: ["Finance", "Statistics", "Forecasting"]
1313
categories: ["Finance"]
1414
---
@@ -93,6 +93,7 @@ def test_stationarity(timeseries):
9393
print('ADF Statistic:', result[0])
9494
print('p-value:', result[1])
9595

96+
# if p-value is > 0.05, it means the series is not stationary.
9697
test_stationarity(ts)
9798

9899
# If non-stationary, difference the series
@@ -134,6 +135,23 @@ Choosing the right ARIMA model involves selecting appropriate values for p, d, a
134135
2. **Grid search**: Trying different combinations of p, d, and q and selecting the best based on information criteria like AIC or BIC.
135136
3. **Diagnostic checking**: Analyzing residuals to ensure they resemble white noise.
136137

138+
### Finding ARIMA Parameters (p, d, q)
139+
Determining the optimal ARIMA parameters involves a combination of statistical tests, visual inspection, and iterative processes. Here's a systematic approach to finding p, d, and q:
140+
141+
* Determine d (Differencing Order):
142+
- Use the Augmented Dickey-Fuller test to check for stationarity.
143+
- If the series is not stationary, difference it and test again until stationarity is achieved.
144+
* Determine p (AR Order) and q (MA Order):
145+
- After differencing, use ACF (Autocorrelation Function) and PACF (Partial Autocorrelation Function) plots.
146+
- The lag where the ACF cuts off indicates the q value.
147+
- The lag where the PACF cuts off indicates the p value.
148+
* Fine-tune with Information Criteria:
149+
- Use AIC (Akaike Information Criterion) or BIC (Bayesian Information Criterion) to compare different models.
150+
151+
### Finding d values from plots
152+
153+
154+
### Grid Search
137155
Here's a Python function to perform a grid search:
138156

139157
```python

public/index.json

+1-1
Large diffs are not rendered by default.

public/posts/finance/stock_prediction/arima/arima_example.ipynb

+561
Large diffs are not rendered by default.

public/posts/finance/stock_prediction/arima/arima_example.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,17 @@ def test_stationarity(timeseries):
3434

3535
# Forecast
3636
forecast = results.forecast(steps=30)
37-
help(forecast)
37+
# help(forecast)
38+
print(forecast)
3839

3940
# Plot the results
4041
plt.figure(figsize=(12,6))
4142
plt.plot(ts.index[-100:], ts.values[-100:], label='Observed')
4243
plt.plot(forecast.index, forecast.values, color='r', label='Forecast')
43-
plt.fill_between(forecast.index,
44-
forecast.conf_int().iloc[:, 0],
45-
forecast.conf_int().iloc[:, 1],
46-
color='pink', alpha=0.3)
44+
# plt.fill_between(forecast.index,
45+
# forecast.iloc[:, 0],
46+
# forecast.iloc[:, 1],
47+
# color='pink', alpha=0.3)
4748
plt.title(f'{ticker} Stock Price Prediction')
4849
plt.legend()
4950
plt.show()
Loading

public/posts/finance/stock_prediction/arima/index.html

+34-3
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@
438438
<div class="content">
439439
<div class="container p-0 read-area">
440440

441-
<div class="hero-area col-sm-12" id="hero-area" style='background-image: url(/images/default-hero.jpg);'>
441+
<div class="hero-area col-sm-12" id="hero-area" style='background-image: url(/posts/finance/stock_prediction/arima/images/find_d.png);'>
442442
</div>
443443

444444

@@ -447,7 +447,7 @@
447447
<div class="author-profile ms-auto align-self-lg-center">
448448
<img class="rounded-circle" src='/images/author/profile_hu8a567cefac8c1a165d433ac0796ac418_3088978_120x120_fit_q75_box.jpg' alt="Author Image">
449449
<h5 class="author-name">Stefano Giannini</h5>
450-
<p class="text-muted">Friday, June 28, 2024 | 5 minutes</p>
450+
<p class="text-muted">Friday, June 28, 2024 | 6 minutes</p>
451451
</div>
452452

453453

@@ -541,6 +541,7 @@ <h2 id="4-implementing-arima-models-in-python">4. Implementing ARIMA Models in P
541541
</span></span><span style="display:flex;"><span> print(<span style="color:#e6db74">&#39;ADF Statistic:&#39;</span>, result[<span style="color:#ae81ff">0</span>])
542542
</span></span><span style="display:flex;"><span> print(<span style="color:#e6db74">&#39;p-value:&#39;</span>, result[<span style="color:#ae81ff">1</span>])
543543
</span></span><span style="display:flex;"><span>
544+
</span></span><span style="display:flex;"><span><span style="color:#75715e"># if p-value is &gt; 0.05, it means the series is not stationary.</span>
544545
</span></span><span style="display:flex;"><span>test_stationarity(ts)
545546
</span></span><span style="display:flex;"><span>
546547
</span></span><span style="display:flex;"><span><span style="color:#75715e"># If non-stationary, difference the series</span>
@@ -578,6 +579,30 @@ <h2 id="5-model-selection-and-diagnostic-checking">5. Model Selection and Diagno
578579
<li><strong>Grid search</strong>: Trying different combinations of p, d, and q and selecting the best based on information criteria like AIC or BIC.</li>
579580
<li><strong>Diagnostic checking</strong>: Analyzing residuals to ensure they resemble white noise.</li>
580581
</ol>
582+
<h3 id="finding-arima-parameters-p-d-q">Finding ARIMA Parameters (p, d, q)</h3>
583+
<p>Determining the optimal ARIMA parameters involves a combination of statistical tests, visual inspection, and iterative processes. Here&rsquo;s a systematic approach to finding p, d, and q:</p>
584+
<ul>
585+
<li>Determine d (Differencing Order):
586+
<ul>
587+
<li>Use the Augmented Dickey-Fuller test to check for stationarity.</li>
588+
<li>If the series is not stationary, difference it and test again until stationarity is achieved.</li>
589+
</ul>
590+
</li>
591+
<li>Determine p (AR Order) and q (MA Order):
592+
<ul>
593+
<li>After differencing, use ACF (Autocorrelation Function) and PACF (Partial Autocorrelation Function) plots.</li>
594+
<li>The lag where the ACF cuts off indicates the q value.</li>
595+
<li>The lag where the PACF cuts off indicates the p value.</li>
596+
</ul>
597+
</li>
598+
<li>Fine-tune with Information Criteria:
599+
<ul>
600+
<li>Use AIC (Akaike Information Criterion) or BIC (Bayesian Information Criterion) to compare different models.</li>
601+
</ul>
602+
</li>
603+
</ul>
604+
<h3 id="finding-d-values-from-plots">Finding d values from plots</h3>
605+
<h3 id="grid-search">Grid Search</h3>
581606
<p>Here&rsquo;s a Python function to perform a grid search:</p>
582607
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#66d9ef">def</span> <span style="color:#a6e22e">grid_search_arima</span>(ts, p_range, d_range, q_range):
583608
</span></span><span style="display:flex;"><span> best_aic <span style="color:#f92672">=</span> float(<span style="color:#e6db74">&#39;inf&#39;</span>)
@@ -788,7 +813,13 @@ <h5 class="text-center ps-3">Table of Contents</h5>
788813
</ul>
789814
</li>
790815
<li><a href="#4-implementing-arima-models-in-python">4. Implementing ARIMA Models in Python</a></li>
791-
<li><a href="#5-model-selection-and-diagnostic-checking">5. Model Selection and Diagnostic Checking</a></li>
816+
<li><a href="#5-model-selection-and-diagnostic-checking">5. Model Selection and Diagnostic Checking</a>
817+
<ul>
818+
<li><a href="#finding-arima-parameters-p-d-q">Finding ARIMA Parameters (p, d, q)</a></li>
819+
<li><a href="#finding-d-values-from-plots">Finding d values from plots</a></li>
820+
<li><a href="#grid-search">Grid Search</a></li>
821+
</ul>
822+
</li>
792823
<li><a href="#6-limitations-and-considerations">6. Limitations and Considerations</a></li>
793824
<li><a href="#7-advanced-topics-and-extensions">7. Advanced Topics and Extensions</a></li>
794825
<li><a href="#8-conclusion">8. Conclusion</a></li>

public/posts/index.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@
430430
<div class="card">
431431
<div class="card-head">
432432
<a href="/posts/finance/stock_prediction/arima/" class="post-card-link">
433-
<img class="card-img-top" src='/images/default-hero.jpg' alt="Hero Image">
433+
<img class="card-img-top" src='/posts/finance/stock_prediction/arima/images/find_d.png' alt="Hero Image">
434434
</a>
435435
</div>
436436
<div class="card-body">
@@ -460,7 +460,7 @@ <h5 class="card-title">Time Series Analysis and ARIMA Models for Stock Price Pre
460460
<div class="card-footer">
461461
<span class="float-start">
462462
Friday, June 28, 2024
463-
| 5 minutes </span>
463+
| 6 minutes </span>
464464
<a
465465
href="/posts/finance/stock_prediction/arima/"
466466
class="float-end btn btn-outline-info btn-sm">Read</a>

0 commit comments

Comments
 (0)