Skip to content

Commit

Permalink
ACF plot analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
s2t2 committed Oct 31, 2024
1 parent f9b0758 commit 2ec1728
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,65 @@ In addition to interpreting the autocorrelation values themselves, we can examin
+ Slow decay or oscillation often suggests non-stationarity, which may require differencing to stabilize the series.


```{python}
#| code-fold: true
import numpy as np
import matplotlib.pyplot as plt
from statsmodels.tsa.arima_process import ArmaProcess
import statsmodels.api as sm
# Setting random seed for reproducibility
np.random.seed(42)
# Generating AR(1) process (exponential decay in ACF)
ar1 = np.array([1, -0.8]) # AR coefficient
ma1 = np.array([1]) # MA coefficient
ar1_process = ArmaProcess(ar1, ma1).generate_sample(nsample=100)
ar1_acf = sm.tsa.acf(ar1_process, nlags=20)
# Generating MA(1) process (significant spike followed by rapid decay)
ar2 = np.array([1]) # AR coefficient
ma2 = np.array([1, 0.8]) # MA coefficient
ma1_process = ArmaProcess(ar2, ma2).generate_sample(nsample=100)
ma1_acf = sm.tsa.acf(ma1_process, nlags=20)
# Generating non-stationary series with slow decay in ACF
non_stat_process = np.cumsum(np.random.randn(100)) # Random walk process
non_stat_acf = sm.tsa.acf(non_stat_process, nlags=20)
```

```{python}
#| code-fold: true
import plotly.graph_objects as go
# Creating the ACF plots using Plotly
# Plot for AR(1) process
fig = go.Figure()
fig.add_trace(go.Scatter(x=list(range(21)), y=ar1_acf, mode='markers+lines', name='AR(1)'))
fig.update_layout(title='ACF of AR(1) Process (Exponential Decay)',
xaxis_title='Lag',
yaxis_title='ACF')
# Plot for MA(1) process
fig.add_trace(go.Scatter(x=list(range(21)), y=ma1_acf, mode='markers+lines', name='MA(1)'))
fig.update_layout(title='ACF of MA(1) Process (Significant Spike then Rapid Decay)',
xaxis_title='Lag',
yaxis_title='ACF')
# Plot for non-stationary process
fig.add_trace(go.Scatter(x=list(range(21)), y=non_stat_acf, mode='markers+lines', name='Non-Stationary'))
fig.update_layout(title='ACF Comparison for AR(1), MA(1), and Non-Stationary Processes',
xaxis_title='Lag',
yaxis_title='ACF',
height=400, # You can set this to any number of pixels you prefer
legend_title='Process Type',
template='plotly_white')
fig.show()
```



Expand Down
12 changes: 12 additions & 0 deletions docs/references.bib
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@
# AUTOCORRELATION
#
# https://www.investopedia.com/terms/a/autocorrelation.asp
# https://www.ibm.com/topics/autocorrelation
# https://machinelearningmastery.com/gentle-introduction-autocorrelation-partial-autocorrelation/
# Robert Nau
# https://people.duke.edu/~rnau/411arim.htm
# An Introductory Study on Time Series Modeling and Forecasting
# Ratnadip Adhikari and R.K. Agrawal.
# 2013
Expand Down Expand Up @@ -88,6 +97,9 @@ @Inbook{Smit1984
url="https://doi.org/10.1007/978-94-017-1026-8_6"
}




#
# ARIMA
#
Expand Down

0 comments on commit 2ec1728

Please sign in to comment.