library(fpp2)
## Loading required package: ggplot2
## Loading required package: forecast
## Loading required package: fma
## Loading required package: expsmooth
library(gridExtra)
library(readxl)
library(TSA)
## 
## Attaching package: 'TSA'
## The following objects are masked from 'package:stats':
## 
##     acf, arima
## The following object is masked from 'package:utils':
## 
##     tar
library("foreign")

1.Encontrar la función de autocorrelación para el proceso definido por: \[ Y_t=5 + e - \frac {1} {2}e_{t-1} + \frac {1}{4}e_{t_2} \] Para un proceso general MA(q) se tiene que la función de Autocorrelación es:

\[ \rho_k= \frac{\theta_{k} + \theta_1 \theta_{k+1} + \theta_2 \theta_{k+2} + ... + \theta_{q-k} \theta_{q}} {1 + \theta_1^2 + \theta_2^2 + ... + \theta_q^2} \] y vale cero si k>q.

Para el proceso dado \[\theta =-\frac{1}{2}, \theta = \frac{1}{4}, q=2\] \[\rho_1= \frac{\frac{1}{2}+(\frac{-1}{2})(\frac{1}{4})}{1+(\frac{1}{2})^2+(\frac{1}{4})^2}=\frac{\frac{1}{2}-\frac{1}{8}}{\frac{21}{16}}=\frac{6}{21}\]

\[ \rho_2= \frac {\frac{1}{4}} {1 + \frac{1}{4} + \frac{1}{16}}=\frac{\frac{1}{4}}{\frac{21}{16}}=\frac{4}{21} \] ————————————————————————————————————————————————–

  1. Graficar la función de autocorrelacióon para los siguientes modelos MA(2)con los parámetros especificados:
  1. \(\theta_1=-0.5\) y \(\theta_2=-0.4\)
ARMAacf(ma=list(-.5,-.4))
##          0          1          2          3 
##  1.0000000 -0.2127660 -0.2836879  0.0000000
  1. \(\theta_1=-1.2\) y \(\theta_2=0.7\)
ARMAacf(ma=list(-1.2,0.7))
##          0          1          2          3 
##  1.0000000 -0.6962457  0.2389078  0.0000000
  1. \(\theta_1=1\) y \(\theta_2=0.6\)
ARMAacf(ma=list(1,.6))
##         0         1         2         3 
## 1.0000000 0.6779661 0.2542373 0.0000000

  1. Mostrar que cuando \(\theta\) se reemplaza por \(\frac{1}{\theta}\), la función de autocorrelación de un MA(1) no cambia.

Partimos de la expresión general del modelo MA(1)

\[ Y_{t} = e_{t} + \theta e_{t-1} \]

Su función de autocorrelación es:

\[ \rho_{1} = \frac {\theta}{1 + \theta^2} \]

Hacemos el cambio \[\theta \rightarrow \frac{1}{\theta}\]

\[ \rho_{1} = \frac {\frac{1}{\theta}} {1 + \frac{1}{\theta^2}}=\frac{1}{\theta+\frac{1}{\theta}}=\frac{\theta}{\theta^2+1} \] \[\rho_1= \frac{\theta}{\theta^2+1} \]

\(\rho_k = 0\)\(k>1\)

4. Calcular y graficar las funciones de autocorrelación para los siguientes modelos AR(1). Graficar con un número suficientes de rezagos para que la función de autocorrelación decrezca a casi cero.
a)
r ACFa<-ARMAacf(ar=.6,lag.max=8) plot(y=ACFa[-1],x=1:8,xlab='Lag',ylab='ACF',type='h'); abline(h=0)
b)
r ACFb<-ARMAacf(ar=-0.6,lag.max=8) plot(y=ACFb[-1],x=1:8,xlab='Lag',ylab='ACF',type='h'); abline(h=0)
c)
r ACF<-ARMAacf(ar=0.95,lag.max=20) plot(y=ACF[-1],x=1:20,xlab='Lag',ylab='ACF',type='h'); abline(h=0)
d)
r ACFc<-ARMAacf(ar=0.3,lag.max=8) plot(y=ACFc[-1],x=1:8,xlab='Lag',ylab='ACF',main='AR(1); theta1=0.3',type='h'); abline(h=0)
  1. Describa las caracter´isticas generales de la función de autocorrelación para los siguientes procesos: a) MA(1), b) MA(2), c) AR(1), d) AR(2) y e) ARMA(1,1)

a)Estacionario en Media: \[ E(Y_t= E(a_t -\theta a_{t-1})=0 \] Estacionario en Covarianza: \[ \gamma_0= E(Y_t -E(Y_t))^2= E(Y)^2= E(a_t -\theta a_{t-1})^2= E(a_t)^2 +\theta^2 E(a_{t-1})^2 -2\theta E(a_t a_{t-1}) \] \[ =\sigma^2 +\theta^2 \sigma^2 -0= (1 +\theta^2) \sigma^2 < \infty \] \[ \gamma_2= E(Y_t-E(Y_t))(Y_{t-2} -E(Y_{t-2}))= E(a_t -\theta a_{t-1})(a_{t-2} -\theta a_{t-3})=0 \]

La función de Autocovarianzas de un MA(1) es: \[ \gamma_k \rightarrow \gamma_0= (1 +\theta^2)\sigma^2 \rightarrow k=0 \] \[ \gamma_k \rightarrow \gamma_1= -\theta \sigma^2 \rightarrow k=1 \] \[ \gamma_k=0 \rightarrow k>1 \] La función de Autocorrelación de un MA(1) es: \[ \rho_k \rightarrow 1 \rightarrow k=0 \] \[ \rho_k \rightarrow \frac{-\theta}{1+\theta^2} \rho_k \rightarrow k=1 \] \[ \rho_k \rightarrow 0 \rightarrow k>1 \] #Caracteristicas del Modelo MA(2). Consideremos el modelo de medias móviles de orden 2: \[ Y_t= a_t -\theta_1 a_{t-1} -\theta_2 a_{t-2} \] \[a_t \rightarrow RB(0,\sigma^2)\] Este proceso es estacionario para cualquier valor \[\theta_1, \theta_2\] y sus características son:

a)Media: \[E(Y_t)= E(a_t +\theta_1 a_{t-1} -\theta_2 a_{t-2})=0\]

b)Función de Autocovarianzas: \[\gamma_k, k=0, 1, 2, 3,...\]

\[ \gamma_0= E[Y_t -E(Y_t)^2]= E(Y_t)^2= E(a_t -\theta_1 a_{t-1}) -2\theta_2 E(a_t a_{t-2}) +2\theta_1 \theta_2 E(a_{t-1} a_{t-2}) \] \[ =\sigma^2 +\theta_1^2 \sigma^2 +\theta_2^2 \sigma^2= (1 +\theta_1^2 \sigma^2 +\theta_2^2 \sigma^2) \sigma^2 \]

\[ \gamma_1= E[(Y_t -E(Y_t))(Y_{t-1}-E(Y_t))]= E(Y_t Y_{t-1}) \] \[ =E[(a_t -\theta_1 a_{t-1} -\theta_2 a_{t-2})(a_{t-1} -\theta_1 a_{t-2} -\theta_2 a_{t-3})] \] \[ =E(a_t a_{t-1}) -\theta_1 E(a_t a_{t-2}) -\theta_2 E(a_t a_{t-3}) -\theta_1 E(a_{t-1})^2 +\theta_1^2 E(a_{t-2} a_{t-3})= -\theta_1 \sigma^2 +\theta_1 \theta_2 \sigma^2 \] \[ =(-\theta_1 + \theta_1 \theta_2) \sigma^2 \]

\[ \gamma_2= E[(Y_t -E(Y_t))(Y_{t-2} -E(Y_t))]= E(Y_t Y_{t-2}) \] \[ =E[(a_t -\theta_1 a_{t-1} -\theta_2 a_{t-2})(a_{t-2} -\theta_1 a_{t-3} +\theta_2 a_{t-4})] \] \[ =E(a_t a_{t-2}) -\theta_1 E(a_t a_{t-3}) -\theta_2 E(a_t a_{t-4}) -\theta_1 E(a_{t-1} a_{t-2}) +\theta_2 \theta_1 E(a_{t-2} a_{t-3}) +\theta_2^2 E(a_{t-2} a_{t-4})= -\theta_2 \sigma^2 \]

\[ \gamma_3= E[(y_t -E(Y_t))(Y_{t-3} -E(Y_t))]= E(Y_t Y_{t-3})= \] \[ =E[(a_t -\theta_1 a_{t-1} -\theta_2 a_{t-2})(a_{t-3} -\theta_1 a_{t-4} -\theta_2 a_{t-5})]= E[(a_t a_{t-3}) -\theta_1 E(a_t a_{t-4}) -\theta_2 E(a_t a_{t-5})] \] \[ -\theta_1 E(a_{t-1} a_{t-3}) +\theta_1^2(a_{t-1} a{t-4}) +\theta_1 \theta_2 E(a_{t-1} a_{t-5}) -\theta_2 E(a_{t-2} a_{t-3}) +\theta_2 \theta_1 E(a_{t-2} a_{t-4}) +\theta_2^2 E(a_{t-2} a_{t-5})=0 \]

La función de Autocovarianza de un MA(2) es:

\[ \gamma_k\rightarrow \gamma_0= (1+ \theta_1^2 +\theta_2^2) \sigma^2 \rightarrow k=0 \] \[ \gamma_k\rightarrow \gamma_1= (-\theta_1 +\theta_1 \theta_2) \sigma^2 \rightarrow k=1 \] \[ \gamma_k\rightarrow \gamma_2= -\theta_2 \sigma^2 \rightarrow k=2 \] \[ \gamma_k=0 \rightarrow k>2 \] La función de Autocorrelación de un MA(2) es: \[ \rho_k \rightarrow \rho_1= \frac{-\theta_1 +\theta_1 \theta_2}{1 +\theta_1^2 +\theta_2^2} \rightarrow k=1 \] —————————————————————————————————————————————————-

  1. Usar la fórmula recursiva \(\rho_k = \phi_1 \rho_{k-1}\) (ecuación Yule-Walker) para calcular y graficar las funciones de autocorrelación para los siguientes procesos AR(2) con los parámetros especificados. En cada caso especifique si las raíces de la ecuaci´on característica son reales o complejas.
  1. \(\phi_1=0.6\) , \(\phi_2=0.3\)
  2. \(f1=-0.4\) y \(f2=0.5\)
  3. \(f1=1.2\) y \(f2=-0.7\)
  4. \(f1=-1\) y \(f2=-0.6\)
  5. \(f1=0.5\) y \(f2=-0.9\) f ) \(f1=-0.5\) y \(f2=-0.6\)
rho=NULL; phi1=.6; phi2=.3; max.lag=50
rho1=phi1/(1-phi2); rho2=(phi2*(1-phi2)+phi1^2)/(1-phi2)
rho[1]=rho1; rho[2]=rho2
for (k in 3:max.lag) rho[k]=phi1*rho[k-1]+phi2*rho[k-2]
rho # to display the values
##  [1] 0.85714286 0.81428571 0.74571429 0.69171429 0.63874286 0.59076000
##  [7] 0.54607886 0.50487531 0.46674885 0.43151190 0.39893179 0.36881265
## [13] 0.34096713 0.31522407 0.29142458 0.26942197 0.24908056 0.23027492
## [19] 0.21288912 0.19681595 0.18195631 0.16821857 0.15551803 0.14377639
## [25] 0.13292124 0.12288566 0.11360777 0.10503036 0.09710055 0.08976944
## [31] 0.08299183 0.07672593 0.07093310 0.06557764 0.06062652 0.05604920
## [37] 0.05181748 0.04790525 0.04428839 0.04094461 0.03785328 0.03499535
## [43] 0.03235320 0.02991052 0.02765227 0.02556452 0.02363439 0.02184999
## [49] 0.02020031 0.01867519
plot(y=rho,x=1:max.lag,type='h',ylab='ACF',xlab='Lag',ylim=c(-1,+1)); abline(h=0)

rho=NULL; phi1=-0.4; phi2=0.5; max.lag=50
rho1=phi1/(1-phi2); rho2=(phi2*(1-phi2)+phi1^2)/(1-phi2)
rho[1]=rho1; rho[2]=rho2
for (k in 3:max.lag) rho[k]=phi1*rho[k-1]+phi2*rho[k-2]
rho # to display the values
##  [1] -0.80000000  0.82000000 -0.72800000  0.70120000 -0.64448000
##  [6]  0.60839200 -0.56559680  0.53043472 -0.49497229  0.46320628
## [11] -0.43276865  0.40471060 -0.37826857  0.35366273 -0.33059937
## [16]  0.30907111 -0.28892813  0.27010681 -0.25250679  0.23605612
## [21] -0.22067584  0.20629840 -0.19285728  0.18029211 -0.16854548
## [26]  0.15756425 -0.14729844  0.13770150 -0.12872982  0.12034268
## [31] -0.11250198  0.10517213 -0.09831984  0.09191400 -0.08592552
## [36]  0.08032721 -0.07509365  0.07020106 -0.06562725  0.06135143
## [41] -0.05735420  0.05361739 -0.05012406  0.04685832 -0.04380536
## [46]  0.04095130 -0.03828320  0.03578893 -0.03345717  0.03127733
plot(y=rho,x=1:max.lag,type='h',ylab='ACF',xlab='Lag',ylim=c(-1,+1)); abline(h=0)

rho=NULL; phi1=1.2; phi2=-0.7; max.lag=30
rho1=phi1/(1-phi2); rho2=(phi2*(1-phi2)+phi1^2)/(1-phi2)
rho[1]=rho1; rho[2]=rho2
for (k in 3:max.lag) rho[k]=phi1*rho[k-1]+phi2*rho[k-2]
rho # to display the values
##  [1]  0.705882353  0.147058824 -0.317647059 -0.484117647 -0.358588235
##  [6] -0.091423529  0.141303529  0.233560706  0.181360376  0.054139958
## [11] -0.061984314 -0.112279148 -0.091345957 -0.031019745  0.026718476
## [16]  0.053775993  0.045828258  0.017350715 -0.011258923 -0.025656208
## [21] -0.022906203 -0.009528099  0.004600624  0.012190418  0.011408065
## [26]  0.005156385 -0.001797983 -0.005767049 -0.005661871 -0.002757311
plot(y=rho,x=1:max.lag,type='h',ylab='ACF',xlab='Lag',ylim=c(-1,+1)); abline(h=0)

rho=NULL; phi1=-1; phi2=-0.6; max.lag=20
rho1=phi1/(1-phi2); rho2=(phi2*(1-phi2)+phi1^2)/(1-phi2)
rho[1]=rho1; rho[2]=rho2
for (k in 3:max.lag) rho[k]=phi1*rho[k-1]+phi2*rho[k-2]
rho # to display the values
##  [1] -0.6250000000  0.0250000000  0.3500000000 -0.3650000000  0.1550000000
##  [6]  0.0640000000 -0.1570000000  0.1186000000 -0.0244000000 -0.0467600000
## [11]  0.0614000000 -0.0333440000 -0.0034960000  0.0235024000 -0.0214048000
## [16]  0.0073033600  0.0055395200 -0.0099215360  0.0065978240 -0.0006449024
plot(y=rho,x=1:max.lag,type='h',ylab='ACF',xlab='Lag',ylim=c(-1,+1)); abline(h=0)

rho=NULL; phi1=0.5; phi2=-0.9; max.lag=50
rho1=phi1/(1-phi2); rho2=(phi2*(1-phi2)+phi1^2)/(1-phi2)
rho[1]=rho1; rho[2]=rho2
for (k in 3:max.lag) rho[k]=phi1*rho[k-1]+phi2*rho[k-2]
rho # to display the values
##  [1]  0.263157895 -0.768421053 -0.621052632  0.381052632  0.749473684
##  [6]  0.031789474 -0.658631579 -0.357926316  0.413805263  0.529036316
## [11] -0.107906579 -0.530085974 -0.167927066  0.393113843  0.347691281
## [16] -0.179956819 -0.402900562 -0.039489144  0.342865934  0.206973197
## [21] -0.205092742 -0.288822248  0.040172344  0.280026195  0.103857988
## [26] -0.200094582 -0.193519480  0.083325383  0.215830224  0.032922267
## [31] -0.177786068 -0.118523074  0.100745924  0.157043729 -0.012149467
## [36] -0.147414090 -0.062772524  0.101286418  0.107138481 -0.037588536
## [41] -0.115218901 -0.023779768  0.091807127  0.067305355 -0.048973737
## [46] -0.085061688  0.001545519  0.077328279  0.037273172 -0.050958865
plot(y=rho,x=1:max.lag,type='h',ylab='ACF',xlab='Lag',ylim=c(-1,+1)); abline(h=0)

rho=NULL; phi1=-0.5; phi2=-0.6; max.lag=20
rho1=phi1/(1-phi2); rho2=(phi2*(1-phi2)+phi1^2)/(1-phi2)
rho[1]=rho1; rho[2]=rho2
for (k in 3:max.lag) rho[k]=phi1*rho[k-1]+phi2*rho[k-2]
rho # to display the values
##  [1] -0.3125000000 -0.4437500000  0.4093750000  0.0615625000 -0.2764062500
##  [6]  0.1012656250  0.1152109375 -0.1183648437 -0.0099441406  0.0759909766
## [11] -0.0320290039 -0.0295800840  0.0340074443  0.0007443282 -0.0207766307
## [16]  0.0099417184  0.0074951192 -0.0097125907  0.0003592238  0.0056479425
plot(y=rho,x=1:max.lag,type='h',ylab='ACF',xlab='Lag',ylim=c(-1,+1)); abline(h=0)

7.-Graficar las funciones de autocorrelacion para cada uno de los siguientes procesos ARMA: a) ARMA(1,1) con f=0.7 y q=-0.4 b) ARMA(1,1) con f=0.7 y q=0.4
a)
r ACF<-ARMAacf(ar=0.7,ma=-0.4,lag.max=20) plot(y=ACF[-1],x=1:20,xlab='Lag',ylab='ACF',type='h'); abline(h=0)
b)
r ACF<-ARMAacf(ar=0.7,ma=0.4,lag.max=20) plot(y=ACF[-1],x=1:20,xlab='Lag',ylab='ACF',type='h'); abline(h=0)
  1. Considere dos procesos MA(2), uno con \(\theta_1\) = \(\theta_2\) =-1/6 y otro con \(\theta_1=1\) y \(\theta_2=-6\)
  1. Mostrar que estos procesos tienen la misma función de autocorrelación.

Para k=1 la función de autocorrelacion es \[ \rho_1 = \frac{\theta_1 + \theta_1 \theta_2}{1 + \theta_1^2 + \theta_2^2} \] y para k=2 \[ \rho_2 = \frac {\theta^2}{1 + \theta_1^2 + \theta_2^2} \] \(\rho_k=0\) para \(k>2\)

Calculamos primero \(\theta = \theta_2 = \frac{1}{6}\)

\[ \rho_1 = \frac {-\frac{1}{6} + \frac{1}{6} \frac{1}{6}} {1 + \frac{1}{36} + \frac{1}{36}} \]

\[ \rho_1= \frac {-\frac{6}{36} + \frac{1}{36}} {\frac{38}{36}}=\frac{-5}{38} \] \[ \rho_1 = -\frac{5}{38} \] \[ \rho_2 = \frac {-\frac{1}{6}} {1 + \frac{1}{36} + \frac{1}{36}}=-\frac{\frac{6}{36}}{\frac{38}{36}}=-\frac{6}{38} \] \[ \rho_2= -\frac{6}{38} \] si \(\theta = 1\) y \(\theta_2 = -6\)

\[ \rho_1 = \frac {1 - 6 } {1 + 1 + 36}=-\frac{5}{38} \]

\[ \rho_2 = \frac {-6} {1 + 1 + 36}=-\frac{6}{38} \]

  1. ¿Cómo se comparan las raíces de los polinomios característicos?

El ecuación caracteristica es \[1+\theta_1x+\theta_2x ^2=0\] Para \(\theta_1=\theta_2=-\frac{1}{6}\)

\[1-\frac{1}{6}x-\frac{1}{6}x ^2=0\] \[x=\frac{\frac{1}{6}\pm\sqrt{(\frac{1}{6})^2-4(-\frac{1}{6})}}{2(-\frac{1}{6})}=\frac{\frac{1}{6}\pm\sqrt{(\frac{1}{6})^2(1+4(6))}}{2(\frac{-1}{6})}==\frac{1\pm\sqrt{(1+24}}{2}=\frac{1\pm 5}{-2}\] \(x_1=-3\) y \(x_2=2\)

Para \(\theta_1=1\) y \(\theta_2=-6\)

\[1+x-6x ^2=0\] \[x=\frac{-1\pm\sqrt{(1-4(-6)}}{2(-6)}=\frac{-1\pm\sqrt{(1+24)}}{2(-6)}=\frac{-1\pm 5}{2(-6)}\] \[x_1=\frac{-1+5}{-12}=-\frac{4}{12}=-\frac{2}{6}=-\frac{1}{3}\] \[x_2=\frac{-1-5}{-12}=\frac{6}{12}=\frac{1}{2}\] Las raices del polinomio son numeros reciprocos


  1. Considere un proceso AR(1) \(Y_t = \phi Y_{t-1} + et\). Mostrar que si \(|\phi| = 1\) el proceso no puede ser estacionario. (Pista: Tomar varianzas de ambos lados).

Mostraremos el enunciado por contradiccion. Supodremos que es un proceso estacionario y \(|\phi| = 1\)

Calculando la varianza de ambos lados tenemos

\[Var(Y_t)=\phi^2 Var(Y_{t-1})+Var(e_t)\]

Dado que suponemos es estacionario se cumple \(Var(Y_t)=Var(Y_{t-1})\)

Por lo tanto \(Var(Y_t)=\frac{\sigma_e^2}{1-\phi^2}\), si \(|\phi| = 1\) tendremos que \(Var(Y_t)\) es infinita lo cual es imposible por tanto tenemos uan una contradiccion, por lo tanto esto implica que el proceso no puede ser estacionario y tener \(|\phi| = 1\).

10.Considere un proceso MA(6) con \(\theta_1=-0.5\), \(\theta_2=0.25\), \(\theta_3=-0.125\), \(\theta_4=0.0625\), \(\theta_5=-0.0325\) y \(\theta_6=0.015625\). Encontrar un modelo más simple que tenga casi los mismos ??-pesos.

ARMAacf(ma=c(-0.5, 0.25, -0.125, 0.0625, -0.0325, 0.015625))
##           0           1           2           3           4           5 
##  1.00000000 -0.49995181  0.24987336 -0.12474625  0.06199227 -0.03023441 
##           6           7 
##  0.01171876  0.00000000
ARMAacf(ar= -0.5, lag.max = 7)
##          0          1          2          3          4          5 
##  1.0000000 -0.5000000  0.2500000 -0.1250000  0.0625000 -0.0312500 
##          6          7 
##  0.0156250 -0.0078125

Se puede observar que para un si se elige un modelo AR(1) se tienen coeficientes de autocorrelacion muy cercanos.