Documentos de Académico
Documentos de Profesional
Documentos de Cultura
)
Spectrum del ARMA(2,2)
EXPOSITOR: Miguel Ataurima Arellano 5 mataurimaa@uni.pe
UNIVERSIDAD NACIONAL DE INGENIERA
Facultad de Ingenieria Econmica, Estadstica y Ciencias Sociales
MATLAB para el Anlisis Econmico y Financiero
NIVEL BSICO
3. Construya un portafolio de 4 componentes, considere un Equity Portfolio como el visto en clase.
Reealice las mismas opperaciones en el caso anterior, adicionalmente realice un anlisis riesgo rentabilidad
considerando el riesgo como la desviacin estndar derivada del modelo GARCH previamente estimado.
SOLUCIN:
ES_4_5.m (script)
1 % Problema No. 4.5
2 % Series Utilizadas :
3 % NIKE
4 % AMEXP
5 % COCACOLA
6 % MCDONALD
7
8 clc; clear;
9
10 % Cargamos las 4 series (incluyendo fechas y encabezados)
11 [p, texto] = xlsread(series4,1,A1:E3486);
12
13 % Seleccionamos fechas y quotes
14 strfechas = texto(2:end,1);
15 strquotes = texto(1,2:end);
16
17 % Convertimos fechas texto a fechas numericas
18 formatIn = dd/mm/yyyy;
19 tiempo = datenum(strfechas,formatIn);
20
21 % Grafica de precios
22 figure(1);
23 plot(tiempo, p);
24 legend(strquotes);
25 datetick(x,mmmyy);
26
27 % Obtenemos retornos
28 R = price2ret(p);
29 figure(2);
30 plot(tiempo(2:end), R);
31 legend(strquotes);
32 datetick(x,mmmyy);
33
34 [T,n] = size(p);
35
36 % Pesos
37 w = (1/n)*ones(n,1);
38
39 % Retorno del Portafolio
40 Rp = R*w;
41 figure(3);
42 plot(tiempo(2:end), Rp);
43 legend(Retorno del Portafolio);
44 datetick(x,mmmyy);
45
46 % Autocorrelacion de Rp
47 figure(4);
48 autocorr(Rp);
49
50 % Autocorrelacion de Rp^2
51 figure(5);
EXPOSITOR: Miguel Ataurima Arellano 6 mataurimaa@uni.pe
UNIVERSIDAD NACIONAL DE INGENIERA
Facultad de Ingenieria Econmica, Estadstica y Ciencias Sociales
MATLAB para el Anlisis Econmico y Financiero
NIVEL BSICO
52 autocorr(Rp.^2);
53
54 % Estimacion del GARCH
55
56 spec = garchset(Distribution,T,P,1,Q,1,R,1,VarianceModel,...
57 GARCH,Display,off)
58 [spec, error, like, residuos, desviaciones] = garchfit(spec,Rp);
59
60 % Nota: error, es el error filtrado del modelo
61 garchdisp(spec,error);
62
63 % Graficamos los residuos filtrados
64 figure(6)
65 subplot(2,1,1); plot(tiempo(2:end),residuos);
66 title(Residuos filtrados por ARIMA-GARCH);
67 datetick(x,mmmyy);
68
69 subplot(2,1,2); plot(tiempo(2:end),desviaciones);
70 title(Des. Est. Condicionales (filtradas));
71 datetick(x,mmmyy);
EXPOSITOR: Miguel Ataurima Arellano 7 mataurimaa@uni.pe
UNIVERSIDAD NACIONAL DE INGENIERA
Facultad de Ingenieria Econmica, Estadstica y Ciencias Sociales
MATLAB para el Anlisis Econmico y Financiero
NIVEL BSICO
Salida
Jan00 Jan02 Jan04 Jan06 Jan08 Jan10 Jan12 Jan14
0
50
100
150
200
NIKE
AMEXP
COCACOLA
MCDONALD
Jan00 Jan02 Jan04 Jan06 Jan08 Jan10 Jan12 Jan14
1.2
1
0.8
0.6
0.4
0.2
0
0.2
NIKE
AMEXP
COCACOLA
MCDONALD
Jan00 Jan02 Jan04 Jan06 Jan08 Jan10 Jan12 Jan14
0.3
0.25
0.2
0.15
0.1
0.05
0
0.05
0.1
Retorno del Portafolio
0 2 4 6 8 10 12 14 16 18 20
0.2
0
0.2
0.4
0.6
0.8
Lag
S
a
m
p
l
e
A
u
t
o
c
o
r
r
e
l
a
t
i
o
n
Sample Autocorrelation Function
EXPOSITOR: Miguel Ataurima Arellano 8 mataurimaa@uni.pe
UNIVERSIDAD NACIONAL DE INGENIERA
Facultad de Ingenieria Econmica, Estadstica y Ciencias Sociales
MATLAB para el Anlisis Econmico y Financiero
NIVEL BSICO
0 2 4 6 8 10 12 14 16 18 20
0.2
0
0.2
0.4
0.6
0.8
Lag
S
a
m
p
l
e
A
u
t
o
c
o
r
r
e
l
a
t
i
o
n
Sample Autocorrelation Function
Jan00 Jan02 Jan04 Jan06 Jan08 Jan10 Jan12 Jan14
0.3
0.2
0.1
0
0.1
Residuos filtrados por ARIMAGARCH
Jan00 Jan02 Jan04 Jan06 Jan08 Jan10 Jan12 Jan14
0
0.05
0.1
Des. Est. Condicionales (filtradas)
EXPOSITOR: Miguel Ataurima Arellano 9 mataurimaa@uni.pe
UNIVERSIDAD NACIONAL DE INGENIERA
Facultad de Ingenieria Econmica, Estadstica y Ciencias Sociales
MATLAB para el Anlisis Econmico y Financiero
NIVEL BSICO
4. Producir datos simulados:
a) Elaborar la recta m
t
= at +b y la senoidal s
50,t
= 250 sin
2t
T
2
= 10
0 10 20 30
1
0
1
Rezagos (j)
A
C
F
d
e
s
5
0
,
t
2
= 10
0 10 20 30
1
0
1
Rezagos (j)
A
C
F
d
e
m
t
2
= 100
0 10 20 30
1
0
1
Rezagos (j)
A
C
F
d
e
s
5
0
,
t
2
= 100
0 10 20 30
1
0
1
Rezagos (j)
A
C
F
d
e
m
t
2
= 200
0 10 20 30
1
0
1
Rezagos (j)
A
C
F
d
e
s
5
0
,
t
2
= 200
Interpretacin:
Se observa que las variaciones en la desviacin estndar no afectan a las funciones de autocorrelacin
EXPOSITOR: Miguel Ataurima Arellano 12 mataurimaa@uni.pe
UNIVERSIDAD NACIONAL DE INGENIERA
Facultad de Ingenieria Econmica, Estadstica y Ciencias Sociales
MATLAB para el Anlisis Econmico y Financiero
NIVEL BSICO
c) Agregar simultneamente la recta, el ruido y la senoidal. Obtenga la funcin de autocorrelacin de
esta serie agregada. Interprete.
SOLUCIN:
ES_5_1_1_c.m (script)
1 clc; clear;
2
3 % parametros de la tendencia
4 a = 0.5;
5 b = 1;
6
7 % parametros de la onda sinusoidal
8 A = 250; % Amplitud
9 T = 50; % Periodo de la Onda Sinusoidal (distancia pico a pico)
10
11 % Intervalo de simulacion
12 t = (0:999);
13
14 % Generacion de Series
15 m = a*t + b;
16 s50 = A*sin(2*pi*t/T);
17
18 %% % % %Generamos un ruido gaussiano N(0,10)
19 sigma = sqrt(10);
20 u = sigma*randn(size(t));
21
22 %% % % %Todos juntos
23 y = m + s50 + u;
24
25 T = length(t);
26 tc = 1.96;
27 stderror = sqrt(1/T);
28 bw = tc*stderror;
29
30 % Grafica de las series
31 figure(1); clf;
32 subplot(2,1,1);
33 plot(t, y, t, u);
34 grid on;
35 xlabel(Periodos);
36 legend(y_t = m_t + s_{50,t} + u_t; u_t~N(0,10), ...
37 u_t~N(0,10));
38
39 % Grafica de la autocorrelacion
40 J = 100;
41 acf_y = correlograma(y, J);
42 subplot(2,1,2);
43 h = bar(acf_y); axis([0 J -1.5 1.5]);
44 xlabel(Rezagos (j)); ylabel(ACF de y_t); hold on;
45 plot([0 J],[bw bw], --k, [0 J],[-bw -bw], --k); hold off;
46 set(h,FaceColor, [0.7 0.8 1], EdgeColor, k);
47 grid on;
48 shg;
EXPOSITOR: Miguel Ataurima Arellano 13 mataurimaa@uni.pe
UNIVERSIDAD NACIONAL DE INGENIERA
Facultad de Ingenieria Econmica, Estadstica y Ciencias Sociales
MATLAB para el Anlisis Econmico y Financiero
NIVEL BSICO
Salida
0 100 200 300 400 500 600 700 800 900 1000
500
0
500
1000
Periodos
y
t
= m
t
+ s
50,t
+ u
t
; u
t
~N(0,10)
u
t
~N(0,10)
0 10 20 30 40 50 60 70 80 90 100
1
0
1
Rezagos (j)
A
C
F
d
e
y
t
Interpretacin:
Se observa que la ACF es cclica debido a la persistencia de las oscilaciones que la seal sinusoidal
adhieren a la seal resultante y
t
.
EXPOSITOR: Miguel Ataurima Arellano 14 mataurimaa@uni.pe
UNIVERSIDAD NACIONAL DE INGENIERA
Facultad de Ingenieria Econmica, Estadstica y Ciencias Sociales
MATLAB para el Anlisis Econmico y Financiero
NIVEL BSICO
5. Extrayendo la tendencia de una serie.
El operador de diferencias de orden p se dene por
p
x
n
= x
n
x
np
. Aplicar este operador para varios
valores de p con el n de eliminar la tendencia de la serie del ejercicio anterior.
SOLUCIN:
ES_5_1_2.m (script)
1 clc; clear;
2
3 % parametros de la tendencia
4 a = 0.5;
5 b = 1;
6
7 % parametros de la onda sinusoidal
8 A = 250; % Amplitud
9 T = 50; % Periodo de la Onda Sinusoidal (distancia pico a pico)
10
11 % Intervalo de simulacion
12 t = (0:999);
13
14 % Generacion de Series
15 m = a*t + b;
16 s50 = A*sin(2*pi*t/T);
17
18 %% % % %Generamos un ruido gaussiano N(0,10)
19 sigma = sqrt(10);
20 u = sigma*randn(size(t));
21
22 %% % % %Todos juntos
23 y = m + s50 + u;
24
25 T = length(t);
26 tc = 1.96;
27 stderror = sqrt(1/T);
28 bw = tc*stderror;
29
30 % Aplicamos el operador de diferencia de orden p para varios valores de p
31 k = 1;
32 for p = 47:53
33 ysa = zeros(T,1);
34 for n = p+1:T
35 ysa(n) = y(n) - y(n-p);
36 end
37 ysa(1:p) = NaN;
38 YSA(:,k) = ysa;
39 strlegenda{k} = [p = num2str(p)];
40 k = k + 1;
41 end
42
43 plot(YSA(1:500,:),LineWidth,2)
44 legend(strlegenda);
45 grid on;
46 xlabel(Periodos);
47 ylim([-150 200]);
48 title(Grfica de \nabla_px_n para distintos valores de p);
49 shg;
EXPOSITOR: Miguel Ataurima Arellano 15 mataurimaa@uni.pe
UNIVERSIDAD NACIONAL DE INGENIERA
Facultad de Ingenieria Econmica, Estadstica y Ciencias Sociales
MATLAB para el Anlisis Econmico y Financiero
NIVEL BSICO
Salida:
0 50 100 150 200 250 300 350 400 450 500
150
100
50
0
50
100
150
200
Periodos
Grfica de
p
x
n
para distintos valores de p
p = 47
p = 48
p = 49
p = 50
p = 51
p = 52
p = 53
EXPOSITOR: Miguel Ataurima Arellano 16 mataurimaa@uni.pe
UNIVERSIDAD NACIONAL DE INGENIERA
Facultad de Ingenieria Econmica, Estadstica y Ciencias Sociales
MATLAB para el Anlisis Econmico y Financiero
NIVEL BSICO
6. Sea Z
t
N
0, 100
2
2t
50
+Z
t
Y
(3)
t
= 1 + 0,5t + 250 sin
2t
50
+Z
t
a) Simular tres series para t [0, 999].
SOLUCIN:
ES_5_1_3_a.m (script)
1 clc; clear;
2
3 T = 1000;
4 t = (0:T-1);
5
6 % Generamos el ruido blanco gaussiano Z
7 sigma = 100;
8 Z = sigma*randn(T,1);
9
10 % Generamos las series de tiempo pedidas
11 Y1 = 1 + 0.5*t + Z;
12 Y2 = 250*sin(2*pi*t/50) + Z;
13 Y3 = 1 + 0.5*t + 250*sin(2*pi*t/50) + Z;
14
15 % Grafica de las series
16 subplot(3,1,1);
17 plot(t, [Y1, Z]);
18 grid on;
19 xlabel(Periodos);
20 legend(Y_{1,t}, ...
21 Z_t~N(0,100^2));
22
23 subplot(3,1,2);
24 plot(t, [Y2 Z]);
25 grid on;
26 xlabel(Periodos);
27 legend(Y_{2,t}, ...
28 Z_t~N(0,100^2));
29
30 subplot(3,1,3);
31 plot(t, [Y3 Z]);
32 grid on;
33 xlabel(Periodos);
34 legend(Y_{3,t}, ...
35 Z_t~N(0,100^2));
36 shg;
EXPOSITOR: Miguel Ataurima Arellano 17 mataurimaa@uni.pe
UNIVERSIDAD NACIONAL DE INGENIERA
Facultad de Ingenieria Econmica, Estadstica y Ciencias Sociales
MATLAB para el Anlisis Econmico y Financiero
NIVEL BSICO
Salida:
0 100 200 300 400 500 600 700 800 900 1000
500
0
500
1000
Periodos
Y
1,t
Z
t
~N(0,100
2
)
0 100 200 300 400 500 600 700 800 900 1000
500
0
500
Periodos
Y
2,t
Z
t
~N(0,100
2
)
0 100 200 300 400 500 600 700 800 900 1000
500
0
500
1000
Periodos
Y
3,t
Z
t
~N(0,100
2
)
EXPOSITOR: Miguel Ataurima Arellano 18 mataurimaa@uni.pe
UNIVERSIDAD NACIONAL DE INGENIERA
Facultad de Ingenieria Econmica, Estadstica y Ciencias Sociales
MATLAB para el Anlisis Econmico y Financiero
NIVEL BSICO
b) Realizar un ltrado (medias mviles) de tres series con sus ruidos.
SOLUCIN:
ES_5_1_3_b.m (script)
1 clc; clear;
2
3 T = 1000;
4 t = (0:T-1);
5
6 % Generamos el ruido blanco gaussiano Z
7 sigma = 100;
8 Z = sigma*randn(T,1);
9
10 % Generamos las series de tiempo pedidas
11 Y1 = 1 + 0.5*t + Z;
12 Y2 = 250*sin(2*pi*t/50) + Z;
13 Y3 = 1 + 0.5*t + 250*sin(2*pi*t/50) + Z;
14
15 % Aplicando el filtro MA
16 q = 12;
17 wts = [1/(2*q);repmat(1/q,11,1);1/(2*q)];
18 Ys1 = conv(Y1,wts,valid);
19 Ys2 = conv(Y2,wts,valid);
20 Ys3 = conv(Y3,wts,valid);
21
22 % Grafica de las Series
23 subplot(3,1,1);
24 plot(t, Y1,b,...
25 7:T-6,Ys1,r,LineWidth,2);
26 grid on;
27 legend(Y_{1,t}, Y_{1,t} filtrado (MA));
28 xlabel(Periodos);
29
30 subplot(3,1,2);
31 Ys = conv(Y2,wts,valid);
32 plot(t, Y2,b,...
33 7:T-6,Ys2,r,LineWidth,2);
34 grid on;
35 legend(Y_{2,t}, Y_{2,t} filtrado (MA));
36 xlabel(Periodos);
37
38 subplot(3,1,3);
39 plot(t, Y3,b,...
40 7:T-6,Ys3,r,LineWidth,2);
41 grid on;
42 legend(Y_{3,t}, Y_{3,t} filtrado (MA));
43 xlabel(Periodos);
44
45 shg;
EXPOSITOR: Miguel Ataurima Arellano 19 mataurimaa@uni.pe
UNIVERSIDAD NACIONAL DE INGENIERA
Facultad de Ingenieria Econmica, Estadstica y Ciencias Sociales
MATLAB para el Anlisis Econmico y Financiero
NIVEL BSICO
Salida:
0 100 200 300 400 500 600 700 800 900 1000
200
0
200
400
600
800
Periodos
Y
1,t
Y
1,t
filtrado (MA)
0 100 200 300 400 500 600 700 800 900 1000
500
0
500
1000
Periodos
Y
2,t
Y
2,t
filtrado (MA)
0 100 200 300 400 500 600 700 800 900 1000
500
0
500
1000
Periodos
Y
3,t
Y
3,t
filtrado (MA)
EXPOSITOR: Miguel Ataurima Arellano 20 mataurimaa@uni.pe
UNIVERSIDAD NACIONAL DE INGENIERA
Facultad de Ingenieria Econmica, Estadstica y Ciencias Sociales
MATLAB para el Anlisis Econmico y Financiero
NIVEL BSICO
7. Simular los siguientes modelos MA y analizar su funcin covarianza emprica
a) X
t
= Z
t
+ 0,3Z
t1
0,4Z
t1
con Z
t
RB(0, 1)
b) Y
t
= W
t
1,2W
t1
1,6W
t1
con W
t
RB(0, 0,25)
SOLUCIN:
ES_5_1_4.m (script)
1 clc; clear;
2
3 T = 1000;
4 tiempo = (0:T-1);
5
6 %T = length(t);
7 tc = 1.96;
8 stderror = sqrt(1/T);
9 bw = tc*stderror;
10
11 % Generamos los ruidos blancos gaussianos Z y W
12 sigmaZ = 1;
13 sigmaW = sqrt(0.25);
14 Z = sigmaZ*randn(T,1);
15 W = sigmaW*randn(T,1);
16
17 % Asumimos
18 X = zeros(T,1);
19 Y = zeros(T,1);
20
21 % Generamos las series de tiempo pedidas
22 for t = 3:T
23 X(t) = Z(t) + 0.3*Z(t-1) - 0.4*Z(t-2);
24 Y(t) = W(t) - 1.2*W(t-1) - 1.6*W(t-2);
25 end
26
27 % Grafica de las series
28 subplot(2,2,1);
29 plot(tiempo, X);
30 grid on; ylim([-6 6]);
31 xlabel(Periodos);
32 ylabel(X_{t});
33
34 subplot(2,2,2);
35 plot(tiempo, Y);
36 grid on; ylim([-6 6]);
37 xlabel(Periodos);
38 ylabel(Y_{t});
39
40 J = 100;
41 [acf_X, pacf_X, gamma_X] = correlograma(X, J);
42 [acf_Y, pacf_Y, gamma_Y] = correlograma(Y, J);
43
44 % Grafica de la Autocovarianzas de X
45 subplot(2,2,3); h(1) = bar(gamma_X); axis([0 J -0.5 0.5]);
46 xlabel(Rezagos (j)); ylabel(Autocovarianzas de X_t);
47 grid on;
48
49 % Grafica de la Autocovarianzas de Y
EXPOSITOR: Miguel Ataurima Arellano 21 mataurimaa@uni.pe
UNIVERSIDAD NACIONAL DE INGENIERA
Facultad de Ingenieria Econmica, Estadstica y Ciencias Sociales
MATLAB para el Anlisis Econmico y Financiero
NIVEL BSICO
50 subplot(2,2,4); h(2) = bar(gamma_Y); axis([0 J -0.5 0.5]);
51 xlabel(Rezagos (j)); ylabel(Autocovarianzas de Y_t);
52 grid on;
Salida:
0 200 400 600 800 1000
6
4
2
0
2
4
6
Periodos
X
t
0 200 400 600 800 1000
6
4
2
0
2
4
6
Periodos
Y
t
0 20 40 60 80 100
0.5
0
0.5
Rezagos (j)
A
u
t
o
c
o
v
a
r
i
a
n
z
a
s
d
e
X
t
0 20 40 60 80 100
0.5
0
0.5
Rezagos (j)
A
u
t
o
c
o
v
a
r
i
a
n
z
a
s
d
e
Y
t
EXPOSITOR: Miguel Ataurima Arellano 22 mataurimaa@uni.pe
UNIVERSIDAD NACIONAL DE INGENIERA
Facultad de Ingenieria Econmica, Estadstica y Ciencias Sociales
MATLAB para el Anlisis Econmico y Financiero
NIVEL BSICO
8. Simular la trayectoria de un modelo AR(1): X
t
= X
t1
+ Z
t
con Z
t
RB(0, 1). Vea la funcion de
covarianzas y las realizaciones del modelo basados en diferentes condiciones iniciales.
a) = 0,9
b) = 0,9
c) = 1,001
d) = 1,001
SOLUCIN:
ES_5_1_5.m (script)
1 clc; clear;
2
3 T = 100;
4 tiempo = (0:T-1);
5
6 % Generamos el ruido blanco gaussiano Z
7 sigmaZ = 1;
8 Z = sigmaZ*randn(T,1);
9
10 % Utilizamos 4 condiciones iniciales diferentes
11 X0 = [0 5 10 15];
12
13 % Utilizamos 4 valores del parametro autoregresivo phi
14 PHI = [0.9 -0.9 1.001 -1.001];
15
16 X = zeros(T,1);
17 i = 1;
18 for x0 = X0
19 j = 1;
20 for phi = PHI
21
22 % Generamos la serie
23 X(1) = x0;
24 for t = 2:T
25 X(t) = phi*X(t-1) + Z(t);
26 end
27
28 % Graficamos las series
29 figure(1);
30 subplot(4, 4, 4*(i-1) + j);
31 plot(tiempo, X);
32 title([x_0 = num2str(x0)]);
33 ylabel([\phi = num2str(phi)]);
34
35 % Graficamos las funciones de covarianza
36 figure(2);
37 subplot(4, 4, 4*(i-1) + j);
38 J = 100;
39 [ACF_X, PACF_X, gamma_X] = correlograma(X, J);
40 h(4*(i-1) + j) = bar(gamma_X); xlim([0 J]);
41 title([x_0 = num2str(x0)]);
42 ylabel(\gamma_j);
43
44 j = j + 1;
45 end
46 i = i + 1;
47 end
48
EXPOSITOR: Miguel Ataurima Arellano 23 mataurimaa@uni.pe
UNIVERSIDAD NACIONAL DE INGENIERA
Facultad de Ingenieria Econmica, Estadstica y Ciencias Sociales
MATLAB para el Anlisis Econmico y Financiero
NIVEL BSICO
49 set(h,FaceColor, [0.7 0.8 1], EdgeColor, k);
Salida 1: Funciones de Autocovarianza
Salida 2: Series en Niveles
0 50 100
-5
0
5
x
0
= 0
=
0
.
9
0 50 100
-10
0
10
x
0
= 0
=
-
0
.
9
0 50 100
0
5
10
15
x
0
= 0
=
1
.
0
0
1
0 50 100
-50
0
50
x
0
= 0
=
-
1
.
0
0
1
0 50 100
-5
0
5
10
x
0
= 5
=
0
.
9
0 50 100
-10
0
10
x
0
= 5
=
-
0
.
9
0 50 100
0
10
20
30
x
0
= 5
=
1
.
0
0
1
0 50 100
-50
0
50
x
0
= 5
=
-
1
.
0
0
1
0 50 100
-10
0
10
20
x
0
= 10
=
0
.
9
0 50 100
-10
0
10
x
0
= 10
=
-
0
.
9
0 50 100
10
20
30
x
0
= 10
=
1
.
0
0
1
0 50 100
-20
0
20
x
0
= 10
=
-
1
.
0
0
1
0 50 100
-10
0
10
20
x
0
= 15
=
0
.
9
0 50 100
-20
0
20
x
0
= 15
=
-
0
.
9
0 50 100
10
20
30
40
x
0
= 15
=
1
.
0
0
1
0 50 100
-20
0
20
x
0
= 15
=
-
1
.
0
0
1
EXPOSITOR: Miguel Ataurima Arellano 24 mataurimaa@uni.pe
UNIVERSIDAD NACIONAL DE INGENIERA
Facultad de Ingenieria Econmica, Estadstica y Ciencias Sociales
MATLAB para el Anlisis Econmico y Financiero
NIVEL BSICO
9. Sea el modelo Y
t
= X
t
+ W
t
con X
t
un modelo AR(1) y W
t
RB(0, 0,25). El modelo AR(1) est
dado por X
t
= 0,5X
t1
+Z
t
con Z
t
RB(0, 1).
a) Estudiar la funcin de correlacin y la funcin de correlacin parcial de X
t
.
ES_5_1_6_a.m (script)
1 clc; clear;
2
3 T = 1000;
4 tiempo = (0:T-1);
5
6 sigmaW = sqrt(0.25);
7 W = sigmaW*randn(T,1);
8
9 sigmaZ = 1;
10 Z = sigmaZ*randn(T,1);
11
12 % Generamos X
13 X = zeros(T,1);
14 for t = 2:T
15 X(t) = 0.5*X(t-1) + Z(t);
16 end
17
18 J = 36;
19 [acf_X, pacf_X] = correlograma(X, J);
20
21 % Graficamos las series
22 figure(1); clf;
23 subplot(3,1,1);
24 plot(tiempo, X);
25 xlabel(Periodos);
26 ylabel(X_t);
27 legend(X_t);
28 grid on;
29
30 tc = 1.96;
31 stderror = sqrt(1/T);
32 bw = tc*stderror;
33
34 subplot(3,1,2);
35 h(1) = bar(acf_X); axis([0 J -0.5 0.5]);
36 xlabel(Rezagos (j)); ylabel(ACF de X_t); hold on;
37 plot([0 J],[bw bw], --k, [0 J],[-bw -bw], --k); hold off;
38 grid on;
39
40 subplot(3,1,3);
41 h(2) = bar(pacf_X); axis([0 J -0.5 0.5]);
42 xlabel(Rezagos (j)); ylabel(PACF de X_t); hold on;
43 plot([0 J],[bw bw], --k, [0 J],[-bw -bw], --k); hold off;
44 grid on;
45
46 set(h,FaceColor, [0.7 0.8 1], EdgeColor, k);
47 shg;
EXPOSITOR: Miguel Ataurima Arellano 25 mataurimaa@uni.pe
UNIVERSIDAD NACIONAL DE INGENIERA
Facultad de Ingenieria Econmica, Estadstica y Ciencias Sociales
MATLAB para el Anlisis Econmico y Financiero
NIVEL BSICO
Salida:
0 100 200 300 400 500 600 700 800 900 1000
5
0
5
Periodos
X
t
X
t
0 5 10 15 20 25 30 35
0.5
0
0.5
Rezagos (j)
A
C
F
d
e
X
t
0 5 10 15 20 25 30 35
0.5
0
0.5
Rezagos (j)
P
A
C
F
d
e
X
t
EXPOSITOR: Miguel Ataurima Arellano 26 mataurimaa@uni.pe
UNIVERSIDAD NACIONAL DE INGENIERA
Facultad de Ingenieria Econmica, Estadstica y Ciencias Sociales
MATLAB para el Anlisis Econmico y Financiero
NIVEL BSICO
b) Simular Y
t
. Analizar su FAS y FAP.
SOLUCIN:
ES_5_1_6_b.m (script)
1 clc; clear;
2
3 T = 1000;
4 tiempo = (0:T-1);
5
6 sigmaW = sqrt(0.25);
7 W = sigmaW*randn(T,1);
8
9 sigmaZ = 1;
10 Z = sigmaZ*randn(T,1);
11
12 % Generamos X
13 X = zeros(T,1);
14 for t = 2:T
15 X(t) = 0.5*X(t-1) + Z(t);
16 end
17
18 % Generamos Y
19 Y = zeros(T,1);
20 for t = 1:T
21 Y(t) = X(t) + W(t);
22 end
23
24 J = 36;
25 [acf_Y, pacf_Y] = correlograma(Y, J);
26
27 % Graficamos las series
28 figure(1); clf;
29 subplot(3,1,1);
30 plot(tiempo, Y);
31 xlabel(Periodos);
32 ylabel(Y_t);
33 grid on;
34
35 tc = 1.96;
36 stderror = sqrt(1/T);
37 bw = tc*stderror;
38
39 subplot(3,1,2);
40 h(1) = bar(acf_Y); axis([0 J -0.5 0.5]);
41 xlabel(Rezagos (j)); ylabel(ACF de Y_t); hold on;
42 plot([0 J],[bw bw], --k, [0 J],[-bw -bw], --k); hold off;
43 grid on;
44
45 subplot(3,1,3);
46 h(2) = bar(pacf_Y); axis([0 J -0.5 0.5]);
47 xlabel(Rezagos (j)); ylabel(PACF de Y_t); hold on;
48 plot([0 J],[bw bw], --k, [0 J],[-bw -bw], --k); hold off;
49 grid on;
50
51 set(h,FaceColor, [0.7 0.8 1], EdgeColor, k);
52 shg;
EXPOSITOR: Miguel Ataurima Arellano 27 mataurimaa@uni.pe
UNIVERSIDAD NACIONAL DE INGENIERA
Facultad de Ingenieria Econmica, Estadstica y Ciencias Sociales
MATLAB para el Anlisis Econmico y Financiero
NIVEL BSICO
Salida:
0 100 200 300 400 500 600 700 800 900 1000
5
0
5
Periodos
Y
t
0 5 10 15 20 25 30 35
0.5
0
0.5
Rezagos (j)
A
C
F
d
e
Y
t
0 5 10 15 20 25 30 35
0.5
0
0.5
Rezagos (j)
P
A
C
F
d
e
Y
t
EXPOSITOR: Miguel Ataurima Arellano 28 mataurimaa@uni.pe
UNIVERSIDAD NACIONAL DE INGENIERA
Facultad de Ingenieria Econmica, Estadstica y Ciencias Sociales
MATLAB para el Anlisis Econmico y Financiero
NIVEL BSICO
c) Simular U
t
= Y
t
0,5Y
t1
por dos mtodos y analizar su FAS y FAP. El primer mtodo es directo, el
segundo requiere la identicacin del modelo MA(1).
SOLUCIN:
ES_5_1_6c_.m (script)
1 clc; clear;
2
3 T = 1000;
4 tiempo = (0:T-1);
5
6 sigmaW = sqrt(0.25);
7 W = sigmaW*randn(T,1);
8
9 sigmaZ = 1;
10 Z = sigmaZ*randn(T,1);
11
12 % Generamos X
13 X = zeros(T,1);
14 for t = 2:T
15 X(t) = 0.5*X(t-1) + Z(t);
16 end
17
18 % Generamos Y
19 Y = zeros(T,1);
20 for t = 1:T
21 Y(t) = X(t) + W(t);
22 end
23
24 % Generamos U
25 % Asumimos que Y(0) = 0
26 U = zeros(T,1);
27 U(1) = Y(1);
28 for t = 2:T
29 U(t) = Y(t) - 0.5*Y(t-1);
30 end
31
32 J = 36;
33 [acf_U, pacf_U] = correlograma(U, J);
34
35 % Graficamos las series
36 figure(1); clf;
37 subplot(3,1,1);
38 plot(tiempo, U);
39 xlabel(Periodos);
40 ylabel(Y_t);
41 grid on;
42
43 tc = 1.96;
44 stderror = sqrt(1/T);
45 bw = tc*stderror;
46
47 subplot(3,1,2);
48 h(1) = bar(acf_U); axis([0 J -0.5 0.5]);
49 xlabel(Rezagos (j)); ylabel(ACF de U_t); hold on;
50 plot([0 J],[bw bw], --k, [0 J],[-bw -bw], --k); hold off;
51 grid on;
52
EXPOSITOR: Miguel Ataurima Arellano 29 mataurimaa@uni.pe
UNIVERSIDAD NACIONAL DE INGENIERA
Facultad de Ingenieria Econmica, Estadstica y Ciencias Sociales
MATLAB para el Anlisis Econmico y Financiero
NIVEL BSICO
53 subplot(3,1,3);
54 h(2) = bar(pacf_U); axis([0 J -0.5 0.5]);
55 xlabel(Rezagos (j)); ylabel(PACF de U_t); hold on;
56 plot([0 J],[bw bw], --k, [0 J],[-bw -bw], --k); hold off;
57 grid on;
58
59 set(h,FaceColor, [0.7 0.8 1], EdgeColor, k);
60 shg;
Salida:
0 100 200 300 400 500 600 700 800 900 1000
4
2
0
2
4
Periodos
Y
t
0 5 10 15 20 25 30 35
0.5
0
0.5
Rezagos (j)
A
C
F
d
e
U
t
0 5 10 15 20 25 30 35
0.5
0
0.5
Rezagos (j)
P
A
C
F
d
e
U
t
EXPOSITOR: Miguel Ataurima Arellano 30 mataurimaa@uni.pe
UNIVERSIDAD NACIONAL DE INGENIERA
Facultad de Ingenieria Econmica, Estadstica y Ciencias Sociales
MATLAB para el Anlisis Econmico y Financiero
NIVEL BSICO
d) Simular Y
t
como un ARMA(1,1).
SOLUCIN:
ES_5_1_6_d.m (script)
1 clc; clear;
2
3 T = 1000;
4 tiempo = (0:T-1);
5
6 sigmaW = sqrt(0.25);
7 W = sigmaW*randn(T,1);
8
9 sigmaZ = 1;
10 Z = sigmaZ*randn(T,1);
11
12 % Generamos X
13 X = zeros(T,1);
14 for t = 2:T
15 X(t) = 0.5*X(t-1) + Z(t);
16 end
17
18 % Generamos Y
19 Y = zeros(T,1);
20 for t = 1:T
21 Y(t) = X(t) + W(t);
22 end
23
24 % Simulacin de Y_t como un ARMA(1,1)
25 model = arima(1,0,1);
26 fit = estimate(model,Y);
27
28 % Recogemos los parmetros
29 phi = fit.AR{1};
30 theta = fit.MA{1};
31 c = fit.Constant;
32 sigma2 = fit.Variance;
33
34 % Graficamos
35 figure(2);
36 Y_BJ = zeros(T,1);
37 e = sqrt(sigma2)*randn(T,1);
38 for t = 2:T
39 Y_BJ(t) = c + phi*Y_BJ(t) + e(t) + theta*e(t-1);
40 end
41 plot(tiempo, Y, tiempo, Y_BJ, LineWidth, 2);
42 legend(Y_t, Y_t estimado como ARMA(1,1));
43 xlabel(Periodos);
44 ylim([-6 6]);
45 grid on;
EXPOSITOR: Miguel Ataurima Arellano 31 mataurimaa@uni.pe
UNIVERSIDAD NACIONAL DE INGENIERA
Facultad de Ingenieria Econmica, Estadstica y Ciencias Sociales
MATLAB para el Anlisis Econmico y Financiero
NIVEL BSICO
Salida:
0 100 200 300 400 500 600 700 800 900 1000
6
4
2
0
2
4
6
Periodos
Y
t
Y
t
estimado como ARMA(1,1)
Salida por Consola:
ARIMA(1,0,1) Model:
--------------------
Conditional Probability Distribution: Gaussian
Standard t
Parameter Value Error Statistic
----------- ----------- ------------ -----------
Constant -0.047973 0.0383039 -1.25243
AR{1} 0.367577 0.0742372 4.95139
MA{1} 0.0303972 0.0797297 0.381254
Variance 1.3437 0.0594129 22.6163
EXPOSITOR: Miguel Ataurima Arellano 32 mataurimaa@uni.pe
UNIVERSIDAD NACIONAL DE INGENIERA
Facultad de Ingenieria Econmica, Estadstica y Ciencias Sociales
MATLAB para el Anlisis Econmico y Financiero
NIVEL BSICO
Cdigos MATLAB Auxiliares
Para la obtencin de la funcin de autocorrelacin, funcin de autocorrelacin parcial y la funcin
de autocovarianzas, se implement la rutina correlograma.m tomando como referencia el libro Time
Series Analysis : Univariate and Multivariate Methods (2nd Edition) de William W.S. Wei.
correlograma.m (script)
1 function [ACF, PACF, gamma] = correlograma(y,J)
2
3 T = length(y);
4 ybarra = mean(y);
5
6 % Calculo de las autocovarianzas muestrales
7 gamma = zeros(J,1);
8 gamma0 = (1/T)*(y(1:T)-ybarra)*(y(1:T)-ybarra);
9 for jj=1:J
10 gamma(jj) = (1/T)*(y(jj+1:T)-ybarra)*(y(1:T-jj)-ybarra);
11 end
12
13 % Calculo de la funcin de autocorrelacin muestral (ACF)
14 ACF = zeros(J,1);
15 for jj=1:J
16 ACF(jj) = gamma(jj)/gamma0;
17 end
18
19 % Calculo de la funcin de autocorrelacin parcial muestral (PACF)
20 PACF = zeros(J,1);
21 PACF(1) = ACF(1);
22 for j=2:J
23 rhoj = ACF(1:j);
24 M = ones(j-1,j-1);
25 for i=1:j-2
26 M(i,i+1:end) = rhoj(1:j-(i+1));
27 M(i+1,1:i) = rhoj(i:-1:1);
28 end
29 num = [[M; rhoj(j-1:-1:1)] rhoj(1:j)];
30 den = [M rhoj(j-1:-1:1); rhoj(j-1:-1:1) 1];
31 PACF(j) = det(num)/det(den);
32 end
EXPOSITOR: Miguel Ataurima Arellano 33 mataurimaa@uni.pe