\section*{Time Series}
Recall that we had just looked at some examples of simple white noise
series with several different distributions.  Of what possible use is
a white-noise sequence?  Well, at the least it is a base sequence that
we can use to compare with other series---series that we believe have
significant signals.


\subsection*{A nonstationary example}

While we will be assuming stationarity (a lot), it is worthwhile
looking a simple time-series that is {\bf not} stationary, but when
generated looks like it has a strong trend.
Consider the process
$$ Y_t = Y_{t-1} + Z_t:\quad t=1,2,\ldots $$
where $\{Z_t\}$ is just white noise, and $Y_0$ is $0$.
 What might this sequence look like
(we'll generate some examples, so hold on).  What are the properties
of this random sequence?

For starters,  $\mu(t) = \rm{E}[Y_t]$ can be easily derived as
$$ \mu_t = \mu_{t-1} + \rm{E}[Z_t] = \mu_{t-1} $$
which is quickly seen to be zero.  
But a calculation that is just as simple shows that
$\gamma{t,t} = t \sigma_2$, which violates the stationarity
assumption.  
(Digression, here is the calculation:
$$
\begin{array}{r c l}
  \rm{E}( (Y_t) (Y_t)) &=& \rm{E}( (Y_t + Z_t) (Y_t + Z_t)) \\
                       &=& \rm{E}( 2 Y_t Z_t + (Y_t)^2 + (Z_t)^2 ) \\
                       &=& 0 + \rm{E}( (Y_t)^2) + \sigma^2
\end{array}
$$
So what about some pictures?  Note all the different forms that a
random walk (what this process is called) can take!

\centerline{\psfig{figure=rwalk.eps,angle=-90,width=6in}}


\section*{Simple Descriptive tools}


\subsection*{Plots}

We have already seen ample plots.


\subsection*{Smoothing}

Often the signal in the data is hard to see because of all the
underlying variability.  It is quite useful to be able to see a
(slightly) smoothed version of the data, to help us see the signal.

A very simple smoother is a moving average.  This is defined as a new
series
 $$ s_t = (y_{t-1} + y_t + y_{t+1})/3 $$
(obviously we can average over a larger number of observations, and
get ``more'' smoothing).  Lets look at an example (based on the
uk.female data) that we saw before.
\begin{verbatim}
> par(mfrow=c(4,1))
## Create a simple smoothing function
> mean3 <- function(y) { (lag(y,-1) + y + lag(y,1))/3}
> tsplot( uk.female, mean3(uk.female), type="pl")
> title("UK females, and smooth 3")
> tsplot( uk.female - mean3(uk.female))
> title("Residuals")
> mean5 <- function(y) { (lag(y,-2) + lag(y,-1) + lag(y) + lag(y,1)      
+ + lag(y,2) )/5}
> tsplot( uk.female, mean5(uk.female), type="pl")                  
> title("UK females, smooth 5")
> tsplot( uk.female - mean5(uk.female))                            
> title("Residuals")                   
\end{verbatim}
\centerline{\psfig{figure=smooth.eps,angle=-90,width=6in}}


Notice that the longer the moving average, the more we have smoothed
the data?  What are the residuals telling us?  Have we captured the
bulk of the data?  The residuals allow us to concentrate on the random
fluctuations, partially ignoring the underlying signal.

The simple 3 and 5 point averages are just examples of what is called
a moving average of a time series, defined as
$$
    s_t = \sum_{j=-p}^{p}{w_j y_{t+j}} \quad t=p+1, \ldots, n-p $$
where $p$ is a positive integer and the $w_j$ are weights which sum to
1. Typically each $w_j$ is positive.  The {\em order\/} of the moving
average is $2p+1$.  There are many different choices for the order of
smoothing and the exact weights.

A second type of smoothing is {\em spline smoothing\/}, where the
series is approximated by a spline (piecwise polynomial) function.
The details are left to the reader, but here are some examples.
\begin{verbatim}
> plot(1:length(uk.female), uk.female)                              
> jj <- smooth.spline( 1:length(uk.female), uk.female,spar=.0000001)
> lines(predict(jj))                                                
> plot(1:length(uk.female), uk.female - predict(jj)$y)              
> title("Residuals")

> plot(1:length(uk.female), uk.female)                              
> jj <- smooth.spline( 1:length(uk.female), uk.female,spar=.0001)   
> lines(predict(jj))                                             
> jj <- smooth.spline( 1:length(uk.female), uk.female,spar=.00001)
> lines(predict(jj))                                              
> plot(1:length(uk.female), uk.female - predict(jj)$y)            
> title("Residuals")                                              
\end{verbatim}

\centerline{\psfig{figure=spline.eps,angle=-90,width=6in}}

Both of these smoothing techniques are an attempt to decompose the
series into (smooth) signal plus (random) noise.  

\subsection*{Differencing}

Differencing provides a simple approach to removing, rather than
highlighting, trends in time-series data.

The first difference of a time series $\{y_t\}$, written as 
$\{Dy_t\}$ is defined by the transformation
$$
       Dy_t = y_t - y_{t-1} $$

Higher order differences are defined by repeated use of the above
definition.  Thus the second order difference $\{D^2y_t\}$ is
$$
      D^2y_t = D(Dy_t) = Dy_t - Dy_{t-1} = y_t - 2y_{t-1} + y_{t-2} $$

Why would one ever difference?  Suppose that the series is
deterministic, and a linear function of time, say
$y_t = \alpha + \beta t$, then
$$Dy_t = (\alpha + \beta t) - \{ \alpha + \beta (t-1) \} = \beta $$
a constant for all t.  Similarly the k'th order difference will
completely remove a k'th order polynomial effect.

Differencing also has the special property that it will remove the
sort of nonstationarity that we generated earlier
$$ Y_t = Y_{t-1} + Z_t:\quad t=1,2,\ldots $$

If we consider differences of this series, we end up with just white
noise ($Z_t$) --- generally a desirable thing. 

If differencing is used to remove a trend in a time series, the usual
approach is to construct $\{Dy_t\}$, $\{D^2y_t\}$, etc until visual
inspection indicated the differenced series is stationary.  Generally
first or second order differences are enough.  
A simple calculation shows that a first order difference is the same
as subtracting a moving average from the series.  
As an example, lets generate some non-stationary data (as above), and
difference it, to see the effect.
\begin{verbatim}
> dd <- ts(cumsum(rnorm(200)))
> tsplot(dd)
> title("Nonstationary series")
> tsplot(diff(dd))
> title ("DIfferenced series")
> tsplot(dd<- ts(cumsum(rnorm(200))))
> title("Nonstationary series")      
> tsplot(diff(dd))                   
> title ("DIfferenced series")       
\end{verbatim}

\centerline{\psfig{figure=difference.eps,angle=-90,width=6in}}


\subsection*{Autocovariance and autocorrelation function}

The {\em autocovariance function\/} of a stationary random function
$\{Y(t)\}$ is
$$
          \gamma(k) = \rm{cov}\{Y(t), Y(t-k)\} $$
This does not not depend on $t$ because of the the assumption of
stationarity.  Since $\gamma(0)$ is the variance, the
{\em autocorrelation function\/} is
$$
          \rho(k) = \gamma(k)/\gamma(0)  $$


\subsubsection*{An example}

Consider the first-order autoregressive process defined by
$$
          Y_t = \alpha Y_{t-1} + Z_t $$
where $Z_t$ is white noise and $-1 < \alpha < 1 $.
Some calculations (you do them!) show that
$$
        \rho_k = \alpha \rho_{k-1} $$
Inserting $\rho_0 = 1 $ gives
$$
        \rho_k = \alpha^k $$

Lets look at some (generated) data, and correlations/covariances.

\centerline{\psfig{figure=ar.eps,angle=-90,width=6in}}



% Local Variables: 
% mode: latex
% eval: (LaTeX-math-mode t)
% eval: (my-latex-mode)
% eval: (read-abbrev-file "~/.abbrev_defs")
% abbrev-mode: t
% TeX-master: "class"
% End: 
