\documentclass[notitlepage]{article}
\usepackage{../../Math545}
\usepackage{listings}
\usepackage{numprint}
\usepackage{enumerate}
\usepackage{bbm}

\usepackage{amsfonts,amsmath}

\setlength{\parindent}{0in}


\lstloadlanguages{R}

\definecolor{keywordcolor}{rgb}{0,0.6,0.6}
\definecolor{delimcolor}{rgb}{0.461,0.039,0.102}
\definecolor{Rcommentcolor}{rgb}{0.101,0.043,0.432}

\lstdefinestyle{Rsettings}{
  basicstyle=\ttfamily,
  breaklines=true,
  showstringspaces=false,
  keywords={if, else, function, theFunction, tmp}, % Write as many keywords
  otherkeywords={},
  commentstyle=\itshape\color{Rcommentcolor},
  keywordstyle=\color{keywordcolor},
  moredelim=[s][\color{delimcolor}]{"}{"},
}

\lstset{basicstyle=\ttfamily, numbers=none, literate={~} {$\sim$}{2}}

\def\ctitle{MATH 545: Introduction to Time Series Analysis}
\def\ttitle{Winter 2025}
\def\stitle{Notation and Simulation for Time Series Models}

\begin{document}

\maketitle

We can define time series models using simple constructions.  Consider first the sequence of random variables $\{Z_t\}_{t \geq 1}$, where
$Z_t \sim Normal(0,1)$ with the random variables (mutually) independent.  Using these variables, we may define the simple time series $\{X_t\}_{t \geq 1}$. For example, suppose
\[
X_t = Z_t - 5 Z_{t-1} + 4 Z_{t-2}.
\]
We define the backshift operator, $\B$, essentially, as the operator that shifts time back by one unit; in particular, we have
\[
\B Z_t = Z_{t-1}.
\]
This operator exhibits standard arithmetic and algebraic properties: for example
\begin{itemize}

\item $\B^2 Z_t = \B (\B Z_t) = \B (Z_{t-1}) = Z_{t-2}$

\smallskip

\item $(c_1+c_2 \B) Z_t = c_1  Z_t + c_2 Z_{t-1}$

\smallskip

\item $(1-\B)^2 Z_t = (1-2\B + \B^2)Z_t = Z_t - 2 Z_{t-1} + Z_{t-2}$

\end{itemize}
and so on.  Therefore we may write
\[
X_t = Z_t - 5 Z_{t-1} + 4 Z_{t-2} = \left(1 -5 \B + 4 \B^2 \right) Z_t.
\]
For simulation, we generate $Z_{-1}$ and $Z_{-2}$ from the same $Normal(0,1)$ distribution.
<<sim0,comment='+', fig.width=7.25, fig.height=5.25, fig.align='center'>>=
set.seed(2101)                   #Set the random number generator seed value
n<-200                           #Set the sample size
Z<-rnorm(n)                      #Generate Z[1],...Z[n]
Z.1<-rnorm(1)                    #Generate Z[-1]
Z.2<-rnorm(1)                    #Generate Z[-2]
Z.extra<-c(Z.2,Z.1,Z)            #Concatenate
X<-Z.extra[3:(n+2)]-5*Z.extra[2:(n+1)]+4*Z.extra[1:n]   #Define the time series X
@
An alternate way to do this is to use the \texttt{filter} function:
<<sim1,comment='+', fig.width=7.25, fig.height=4.25, fig.align='center'>>=
X.f<-filter(Z.extra,c(1,-5,4))[2:(n+1)]
sum(X.f-X)                       #X and X.f are identical
@
We can also create a special time series object in \texttt{R} using the \texttt{ts} function:  for example, if we wanted the observations to represent quarterly data, starting from 2010, 3rd quarter, we would write
<<sim2,comment='+', fig.width=7.25, fig.height=4.25, fig.align='center'>>=
X.t<-ts(X, frequency = 4, start = c(2010, 3))
@
With 200 observations, this amounts to 40 years worth of data.

\medskip

The plotting outcomes are slightly different:
<<sim3,comment='+', fig.width=7.25, fig.height=4.25, fig.align='center'>>=
par(mar=c(4,4,1,0))              #Set up the plotting margins
plot(1:n,X,xlab='t',type='l')    #Construct a line plot
points(1:n,X,pch=19,cex=0.5)     #Add the points on top of the line
plot(X.t,xlab='t',type='l')      #Construct a line plot
points(X.t,pch=19,cex=0.5)       #Add the points on top of the line
@
Note that the time axis is now plotted in years.

\pagebreak

We can use elementary functions in \texttt{R} to summarize the simulated data:
<<sim4,comment='+', fig.width=7.25, fig.height=4.25, fig.align='center'>>=
mean(X)                          #Mean
var(X)                           #Variance
@
In time series, we are interested in covariance and correlation at different \textit{lags}, $h=0,1,2,\ldots$; recall that for a stationary process
\[
\gamma_X(h) = \Cov[X_t,X_{t+h}] \qquad h=0,\pm 1, \pm 2,\ldots
\]
with $\gamma_X(-h) = \gamma_X(h)$, and
\[
\rho_X(h) = \Corr[X_t,X_{t+h}] = \frac{\gamma_X(h)}{\gamma_X(0)} \qquad h=0,\pm 1, \pm 2,\ldots
\]
define the \textit{autocovariance function} (ACVF) and \textit{autocorrelation function} (ACF).  We can compute both using the \texttt{acf} function:
<<sim5,comment='+', fig.width=7.25, fig.height=4.25, fig.align='center'>>=
acf(X,type='covariance',plot=F)  #Covariance
acf(X,type='correlation',plot=F) #Correlation
@
Notice that the variance calculation using \texttt{acf} at lag $h=0$ gives a different answer to that obtained using \texttt{var}:
<<sim6,comment='+', fig.width=7.25, fig.height=4.25, fig.align='center'>>=
(v0<-var(X))                                      #Variance using var
(v1<-acf(X,type='covariance',plot=F)$acf[1])      #Variance using acf
@
This is because the \texttt{acf} uses the formula
\begin{equation} \label{eq:gammahat}
\widehat \gamma_X(h) = \frac{1}{n-h} \sum_{t=1}^{n-h} (x_{t} - \overline{x})(x_{t+h} - \overline{x})
\end{equation}
that is, dividing by $n$ not $n-1$ for the variance calculation (with $h=0$).  We may plot the ACVF or ACF using the \texttt{'plot'} argument to \texttt{acf}
<<sim7,comment='+', fig.width=7.25, fig.height=4.25, fig.align='center'>>=
par(mar=c(4,4,1,0))                               #Set up the plotting margins
acf(X,type='correlation',plot=T,ylim=range(-1,1)) #Autocorrelation plot
@
The dashed horizontal lines either side of zero give an indication of statistical significance; if the ACF estimate $\widehat \rho_X(h)$ exceeds the horizontal dashed line, then they correlation at that lag may be considered significantly different from zero.

\medskip 
\textbf{Note:} Recall that a random walk time series process is formed by setting 
\[
X_t = \sum\limits_{i=1}^t Z_i;
\]
with $\{Z_t\} \sim \WN(0,\sigma_Z^2)$.  Recall that for such a process
\[
\rho_X(t,h) = \Corr[X_t,X_{t+h}] = \frac{t}{\sqrt{t(t+h)}}.
\]
This process is non-stationary and because of this, the estimates derived using equation \eqref{eq:gammahat} -- which is derived assuming stationarity -- are inappropriate.  Because of this, the random walk has a characteristic sample ACF pattern with very slow decay.
<<sim8,comment='+', fig.width=7.25, fig.height=4.25, fig.align='center'>>=
n<-2000
Z<-rnorm(n)
X.rw<-cumsum(Z)                                           #Form the cumulative sum of the Zs
par(mar=c(4,4,1,0))                                       #Set up the plotting margins
plot(1:n,X.rw,xlab='t',type='l',ylim=range(-50,50))       #Construct a line plot
points(1:n,X.rw,pch=19,cex=0.5)                           #Add the points on top of the line
acf(X.rw,type='correlation',plot=T,ylim=range(-1,1),lag.max=100) #Autocorrelation plot
@
An ACF plot such as this is strongly indicative of non-stationarity.
\end{document}
