Sie sind auf Seite 1von 3

CARLOS ZURITA-CASTRO

APPENDIX A
Code in MATLAB for Q5 of PROBLEM SET#3
MAIN

%PROBLEM SET 3 QUESTION 5
clear all
close all

%% Parameters and settings
sigma=0.2; % standard deviation of the observation disturbance
tau=0.05; % standard deviation of the state disturbance
kappa=3.0; % standard deviation of the initial state
n=100; % time series length

%% Generating observations and states
y=zeros(n,1);
x=zeros(n,1);
time = 1:n;
time = time';

% Generating the initial state and observation
x(1)= exp(time(1)/n-0.5)/(1+exp(time(1)/n-0.5));
y(1)=normrnd(x(1),sigma);

% Looping to generate the remaining states and observations
for i=2:n
x(i)=exp(time(i-1)/n-0.5)/(1+exp(time(i-1)/n-0.5))+normrnd(0,tau);
y(i)=normrnd(x(i),sigma);
end

figure(1)
plot([x y])

%% Parameter estimation
theta=[0.8;0.5]; % initial parameters for the estimation (wrong on purpose)
theta=estimate(y,theta);
disp(theta)

% Now run Kalman filter and smoother using these values of sigma^2 and tau^2
% Kalman filter first
k=3;
fprintf('before call to KF\n\n')

figure(2)
[xf,sf] = kf(theta,y,k);
lpi = xf-2*sqrt(sf);
upi = xf+2*sqrt(sf);
plot(time, x, '.', time, xf, '+', time, lpi, '-',time, upi,'-')

% SMOOTHING
Xsmooth=zeros(n,1);
Ssmooth=zeros(n,1);
[Xsmooth,Ssmooth] = smooth(theta,xf,sf);

CARLOS ZURITA-CASTRO

figure(3)
lpi = Xsmooth-2*sqrt(Ssmooth);
upi = Xsmooth+2*sqrt(Ssmooth);
plot(time,Xsmooth,'+',time,lpi,'-',time,upi,'-')

figure(4)
plot(time,sf,'+',time,Ssmooth,'-')

figure(5)
%plot(time, xf,'+', time, Xsmooth,'o')
plot([xf Xsmooth], ['+' '-'])

LOG-LIKELIHOOD ESTIMATE
% function estimate
function theta=estimate(y,theta)

theta=thetain(theta);

%% Estimation
options=optimset('display','iter','TolFun',1e-5,'LargeScale','off','TolX',1e-
5,'HessUpdate','bfgs','FinDiffType','central',...
'maxiter',250,'MaxFunEvals',5000);
theta=fminunc(@(theta) likelihood(theta,y),theta,options);

%% Getting theta in the original scale
theta=thetaout(theta);

end

function theta=thetain(theta)
theta=log(theta);
end

function theta=thetaout(theta)
theta=exp(theta);
end

function loglik=likelihood(theta,y)
%% Preparation

% sample size
n=size(y,1);

% parameter transformation
theta=thetaout(theta);

% getting the individual parameters
k=3;
sigma=theta(1);
tau=theta(2);
xf = zeros(n,1); sf = zeros(n,1);
[xf,sf,loglik] = kf(theta,y,k );
CARLOS ZURITA-CASTRO


End

KALMAN FILTER
function [xf,sf,loglik] = kf(theta,y,k )
% KF routine
% returns xf, sf, loglik
sigma=theta(1);
tau=theta(2);
n = size(y,1);

% Now carry out KF
xf = zeros(n,1); sf = zeros(n,1);
xp = zeros(n,1); sp = zeros(n,1);
yp = zeros(n,1); vy = zeros(n,1);
eps = zeros(n,1);

xp(1) = exp(time(1)/n-0.5)/(1+exp(time(1)/n-0.5));
sp(1) = k^2;
yp(1) = xp(1);
vy(1) = sp(1) + sigma^2;
eps(1) = y(1) - yp(1);
xf(1) = xp(1)+ sp(1)/vy(1) *(y(1) - yp(1));
sf(1) = sp(1) - sp(1)^2/vy(1);
loglik = -1/2*( eps(1)^2/vy(1) + log(vy(1)));

for i=2:n
xp(i) = xf(i-1);
sp(i) = sf(i-1) + tau^2;
yp(i) = xp(i);
vy(i) = sp(i) + sigma^2;
xf(i) = xp(i) + (sp(i)/vy(i))*(y(i) - yp(i));
sf(i) = sp(i) - sp(i)^2/vy(i);
eps(i) = y(i) - yp(i);
loglik = loglik -1/2*( eps(i)^2/vy(i) + log(vy(i))) ;
end
loglik=loglik/n;

end

SMOOTHING
function [Xsmooth, Ssmooth] = smooth(theta,xf,sf)
n = size(xf,1);
sigma = theta(1);
tau = theta(2);
Xsmooth(n,1) = xf(n);
Ssmooth(n,1) = sf(n);
for i=n-1:-1:1
Xsmooth(i,1) = xf(i) + sf(i)/(sf(i) + tau^2)*(Xsmooth(i+1,1) - xf(i));
Ssmooth(i,1) = sf(i) - sf(i)^2 /(sf(i)+tau^2) +...
sf(i)^2/(sf(i)+tau^2)^2 * Ssmooth(i+1,1);
end
end

Das könnte Ihnen auch gefallen