<jupyter_start><jupyter_text>Linear Difference Equationsbased on <NAME>$\newcommand{\bm}[1]{\boldsymbol{1}}$  2nd-order Difference EquationStandard form:$$y_t = \phi_1 y_{t-1} + \phi_2 y_{t-2} + w_t$$ Vector Form\begin{equation}\bm{\xi}_t = \bm{F}\ \bm{\xi}_{t-1} + \bm{v_t}\end{equation}where\begin{equation}\bm{\xi}_t = \left[\begin{array}{c}y_t \\y_{t-1}\end{array}\right],\quad\bm{F} = \left[\begin{array}{cc}\phi_1 & \phi_2 \\1 & 0\end{array}\right],\quad\bm{v}_t = \left[\begin{array}{c}w_t \\0\end{array}\right],\end{equation} Using Lag Operator$$(1 - \phi_1 L - \phi_2 L^2)y_t = w_t$$ Eigenvalues:$|F - \lambda I| = 0$ lead to $$\lambda^2 - \phi_1 \lambda - \phi_2 = 0$$* Let $\lambda_1$ and $\lambda_2$ be eignevalues. Then, $$(\lambda - \lambda_1) (\lambda - \lambda_2) = 0$$* Factorise the difference equation using the eigenvalues$$(1 - \lambda_1 L)(1-\lambda_2 L) = w_t$$* Not to be confused with $1- \phi_1 z - \phi_2 z^2 = 0$, obtained by replacing $L$ by $z$. $z$ and $\lambda$ are reciprocal each other:  $$z = \lambda^{-1}$$  Eigenvalue Diagram* The dynamics of a 2nd-order difference equation can be summarised in terms of the eigenvalues as in the figure below. * The eigenvalues are district unless $\phi_1^2 + 4\phi^2 = 0$. * It is reproduction of Figure 1.5 of Hamilton.<jupyter_code>def fn_q(p):
    return -1.0/4*p**2

# basic variables
p_l = np.arange(-3,-2,0.01)
p_m = np.arange(-2,2,0.01)
p_r = np.arange(2,3,0.01)
p_lm = np.hstack((p_l,p_m))
p_mr = np.hstack((p_m,p_r))
p_lmr = np.hstack((p_l,p_m,p_r))

# plots
xrange = [np.min(p_lmr), np.max(p_lmr)]
yrange = xrange
pwidth = 800

ago = [] # collection of plots
ago.append(go.Scatter(x=np.hstack((p_l,p_mr,np.flip(p_lmr))),
                      y=np.hstack((fn_q(p_l),1+p_mr,3*np.ones(p_lmr.shape))),fill='toself',fillcolor='rgba(150,150,100,0.5)'))
ago.append(go.Scatter(x=np.hstack((p_lm,p_r,np.flip(p_lmr))),
                      y=np.hstack((1-p_lm,fn_q(p_r),3*np.ones(p_lmr.shape))),fill='toself',fillcolor='rgba(150,100,150,0.5)'))
ago.append(go.Scatter(x=np.hstack((p_l,p_m,p_r,np.flip(p_lmr))),
                      y=np.hstack((fn_q(p_l),-np.ones(p_m.shape),fn_q(p_r),-3*np.ones(p_lmr.shape))),fill='toself',fillcolor='rgba(100,150,150,0.5)'))
ago.append(go.Scatter(x=p_lmr, y=fn_q(p_lmr)))
ago.append(go.Scatter(x=p_lm, y=1.0 - p_lm))
ago.append(go.Scatter(x=p_mr, y=1.0 + p_mr))
ago.append(go.Scatter(x=p_m, y=-np.ones(p_m.shape)))

annotations=[
    dict(x=-2.3,y=-1,text=r'(-2,-1)',showarrow=False),
    dict(x=+2.3,y=-1,text=r'(2,-1)',showarrow=False),
    dict(x=0,y=1.1,text=r'(0,1)',showarrow=False),
    dict(x=0.5,y=0.5,text=r'$\phi_2=1-\phi_1$',ax=40),
    dict(x=-0.5,y=0.5,text=r'$\phi_2=1+\phi_1$',ax=-40),
    dict(x=2.5,y=-2.5**2/4,text=r'$\phi_1^2+4\phi_2=0$', ax=-60, ay=20),
    dict(x=-2,y=1.5,text=r'$\text{real eigenvalues}\\ \quad\quad\lambda_2<-1\\$',showarrow=False),
    dict(x=2,y=1.5,text=r'$\text{real eigenvalues}\\ \quad\quad\lambda_1>1\\$',showarrow=False),
    dict(x=0,y=0.3,text=r'$\text{real eigenvalues}\\ \quad|\lambda_j|<1\\$',showarrow=False),
    dict(x=0,y=-0.5,text=r'$\text{complex eigenvalues}\\ \quad\quad|\lambda_j|<1\\$',showarrow=False),
    dict(x=0,y=-2,text=r'$\text{complex eigenvalues}\\ \quad\quad|\lambda_j|>1\\$',showarrow=False),
    dict(x=xrange[1],y=0,text=r'$\phi_1$',showarrow=False),
    dict(x=0,y=2,text=r'$\phi_2$',showarrow=False),
]

# update with selected font
[d.update(dict(font=dict(family='Courier New, monospace', size=16))) for d in annotations]

# layout and plot
layout = go.Layout(height=pwidth, width=pwidth, 
                   xaxis=dict(range=xrange,showgrid=False, showticklabels=False), 
                   yaxis=dict(range=yrange,showgrid=False, showticklabels=False), 
                   annotations=annotations,
                   showlegend=False, 
                   hovermode=False)
py.iplot(go.Figure(data=ago, layout=layout))<jupyter_output><empty_output><jupyter_text>Autoregressive Processes  AR(1)\begin{equation}Y_t = c + \phi Y_{t-1} + \sigma \epsilon_t\end{equation}<jupyter_code>def fn_ar1(c,phi,sigma,y0, eps1):
    return c + phi * y0 + sigma * eps1

c = 0.1
phi = 0.99
sigma = 0.1

n_scen = 10
dim_scen = 'scen'
y0 = 0.0
dim_t = 't'
n_t = 100

aY = [y0 * xr.DataArray(np.ones(n_path), dims=(dim_scen))]
for n in range(n_t):
    y0 = aY[-1]
    eps1 = xr.DataArray(np.random.normal(0.0, 1.0, n_path), dims=(dim_scen))
    y1 = fn_ar1(c, phi, sigma, y0, eps1)
    aY.append(y1)

T = xr.DataArray(np.arange(0,n_t+1), dims=(dim_t))
Y = xr.concat(aY, dim=(dim_t))


    
    
    
[x for x in Y.get_index(dim_scen)]
Y[dict(scen=0)]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.21866265319286174, 'fraction_numerical': 0.04837669318426145, 'mean_word_length': 4.226966292134832, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '374467', 'license': 'Apache-2.0', 'n_tokens_mistral': 2170, 'n_tokens_neox': 2070, 'n_words': 296, 'path': 'notebook/learning/time_series_hamilton/Note_TimeSeries.ipynb', 'repo_name': 'xyise/xyise', 'size': '4662'}