Vous êtes sur la page 1sur 5

C:\Documents and Settings\soumya1\Desktop\BayRegimeChangev1\GIBBS_2_REGIME.

Monday, January 21, 2013 3:29 PM

function [Beta_draws,sigma2_draws,change_draws] = GIBBS_2_REGIME(Y,X,lb,ub,ndraws,burn_in, add_constant) % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % Purpose: Bayesian inference of regime change regression, with unknown switch time ----------------------------------Model: Yt = Xt * Beta1 + ut , ut ~ N(0,s1^2) if t <= lambda Yt = Xt * Beta2 + ut , ut ~ N(0,s2^2) if t >= lambda ----------------------------------Algorithm: Gibbs sampler with Lindley and Smith (1972) proper priors Beta1 ~ N(mu,V), s1^2 ~ IG(a,b), Beta2 ~ N(mu,V), s2^2 ~ IG(a,b), lambda ~ U(lb,ub) Conditional posteriors are conjugate normal and inverted gamma respectively. Conditional posteriors of lambda is proportional to the likelihood of the model in which regime switch occurs at a given time. Hyperparameters are specified below ----------------------------------Usage: Y = dependent variable (n * 1 vector) X = regressors (n * k matrix) lb = the lower bound of the time (obs. index) when regime switch might occur ub = the upper bound of the time (obs. index) when regime switch might occur ndraws = number of draws in MCMC burn_in = number of burn-in draws in MCMC add_constant = whether to add a constant to X (default = 0) ----------------------------------Returns: Beta_draws = posterior draws of coefficients corresponding to the k regressors sigma2_draws = posterior draws of variance of disturbances change_draws = posterior draws of regime switch time Written by Hang Qian, Iowa State University Contact me: matlabist@gmail.com

if if if if if if

nargin nargin nargin nargin nargin nargin

< < < < < <

2; 3; 4; 5; 6; 7;

error('Incomplete data.'); lb = []; ub = []; ndraws = 50000; burn_in = ndraws * 0.2; add_constant = 0;

end end end end end end

[nrow_x,ncol_x] = size(X); [nrow_y,ncol_y] = size(Y); if nrow_x < ncol_x; X = X'; ncol_x = nrow_x;end if nrow_y < ncol_y; Y = Y'; ncol_y = nrow_y;end if ncol_x < ncol_y; Y_temp = Y; Y = X; X = Y_temp;end MissingValue = any(isnan([Y,X]),2); if any(MissingValue) disp('There are missing values in your data.') disp(['Discard observations: ',num2str(find(MissingValue'))]) FullValue = ~MissingValue; Y = Y(FullValue); X = X(FullValue,:); end [nobs,nreg] = size(X);
-1-

C:\Documents and Settings\soumya1\Desktop\BayRegimeChangev1\GIBBS_2_REGIME.m

Monday, January 21, 2013 3:29 PM

if add_constant == 1 disp('A constant is added to X') X = [ones(nobs,1),X]; nreg = nreg + 1; end if isempty(lb) || isnan(lb) lb = floor(nobs*0.1); end if isempty(ub) || isnan(ub) ub = floor(nobs*0.9); end if lb > nobs [lb,tempo] = find(X==lb); if isempty(lb) lb = floor(nobs*0.1); error(['Incorrect specification of regime switch lower bound, the defalut value is used instead: ',num2str(lb)]) else disp(['Regime switch lower bound should be an index of observations. Automatic translation: ',num2str(lb)]) end end if ub > nobs [ub,tempo] = find(X==ub); if isempty(ub) ub = floor(nobs*0.1); error(['Incorrect specification of regime switch upper bound, the defalut value is used instead: ',num2str(ub)]) else disp(['Regime switch upper bound should be an index of observations. Automatic translation: ',num2str(ub)]) end end

%---------------------------------------% Prior distribution settings % Beta ~ N(mu,V) % s1^2,s2^2 ~ IG(a,b) % lambda ~ U(lower,upper) % You may change the hyperparameters here if needed prior_mu = zeros(nreg,1); prior_V = 100 * eye(nreg); prior_a = 3; prior_b = 2.5; %-----------------------------------------

nregime = 2; Beta_draws = zeros(nreg,ndraws - burn_in,nregime); sigma2_draws = zeros(ndraws - burn_in,nregime); change_draws = zeros(1,ndraws - burn_in);
-2-

C:\Documents and Settings\soumya1\Desktop\BayRegimeChangev1\GIBBS_2_REGIME.m

Monday, January 21, 2013 3:29 PM

logL = -Inf(1,nobs); change_use = floor(0.5*(lb + ub)); X1 = X(1:change_use,:); Y1 = Y(1:change_use); Beta_use = zeros(nreg,nregime); sigma2_use = zeros(1,nregime); Beta_use(:,1) = (X1'*X1)\(X1'*Y1); sigma2_use(1) = (Y1-X1*Beta_use(:,1))'*(Y1-X1*Beta_use(:,1))/nobs/2; X2 = X(change_use + 1:nobs,:); Y2 = Y(change_use + 1:nobs); Beta_use(:,2) = (X2'*X2)\(X2'*Y2); sigma2_use(2) = (Y2-X2*Beta_use(:,2))'*(Y2-X2*Beta_use(:,2))/nobs/2; try JustTest = gamrnd(3,1); catch disp(' ') disp('Oooopse, Matlab statistics toolbox is not installed.') disp('You may download a compatibility package on my website.') disp('http://www.public.iastate.edu/~hqi/toolkit') error('Program exits.') end inv_prior_V = inv(prior_V);

H_status_bar = waitbar(0,'MCMC in progress percentage = floor(ndraws/100); for r = 2:ndraws

0%');

if mod(r,percentage) == 0 waitbar(r/ndraws,H_status_bar,['MCMC in progress ]); end

',num2str(floor(r/ndraws*100)),'%'

beta_var = inv(X1'*X1/sigma2_use(1) + inv_prior_V); beta_mean = beta_var * (X1'*Y1/sigma2_use(1) + inv_prior_V*prior_mu); P = chol(beta_var); Beta_use(:,1) = beta_mean + P'*randn(nreg,1); beta_var = inv(X2'*X2/sigma2_use(2) + inv_prior_V); beta_mean = beta_var * (X2'*Y2/sigma2_use(2) + inv_prior_V*prior_mu); P = chol(beta_var); Beta_use(:,2) = beta_mean + P'*randn(nreg,1);

resid = Y1 - X1 * Beta_use(:,1); RSS = resid' * resid; gam_a = length(Y1)/2 + prior_a; gam_b = 1/(1/prior_b + 0.5*RSS); sigma2_use(1) = 1/gamrnd(gam_a,gam_b);

-3-

C:\Documents and Settings\soumya1\Desktop\BayRegimeChangev1\GIBBS_2_REGIME.m

Monday, January 21, 2013 3:29 PM

resid = Y2 - X2 * Beta_use(:,2); RSS = resid' * resid; gam_a = length(Y2)/2 + prior_a; gam_b = 1/(1/prior_b + 0.5*RSS); sigma2_use(2) = 1/gamrnd(gam_a,gam_b);

resid1_sq = ( Y - X*Beta_use(:,1) ).^2; resid2_sq = ( Y - X*Beta_use(:,2) ).^2; for n = lb:ub RSS = sum(resid1_sq(1:n))/sigma2_use(1) + sum(resid2_sq(n + 1:nobs))/sigma2_use(2); logL(n) = -0.5*n*log(sigma2_use(1))-0.5*(nobs-n)*log(sigma2_use(2))-0.5*RSS; end

W = exp(logL - max(logL)); W = W / sum(W); change_use = find(cumsum(W)>rand,1);

X1 Y1 X2 Y2

= = = =

X(1:change_use,:); Y(1:change_use); X(change_use + 1:nobs,:); Y(change_use + 1:nobs);

if r > burn_in Beta_draws(:, r - burn_in, 1) = Beta_use(:,1); Beta_draws(:, r - burn_in, 2) = Beta_use(:,2); sigma2_draws(r - burn_in, 1) = sigma2_use(1); sigma2_draws(r - burn_in, 2) = sigma2_use(2); change_draws(r - burn_in) = change_use; end end

beta_mean = mean(Beta_draws,2); beta_std = std(Beta_draws,0,2); sigma2_mean = mean(sigma2_draws); sigma2_std = std(sigma2_draws); change_mean = mean(change_draws); change_std = std(change_draws); eval([char([81 72 49 61]),'[87 114 105 116 116 101 110 32 98 121];']) eval([char([81 72 50 61]),'[32 72 97 110 103 32 81 105 97 110];'])

disp('------- Before Regime Change ----------') result = cell(nreg + 2,3); result(1,:) = {'Coeff.','Post. mean','Post. std'}; for m = 1:nreg result(m + 1,1) = {['C(',num2str(m-add_constant),')']}; result(m + 1,2:3) = {beta_mean(m,:,1),beta_std(m,:,1)}; end result(nreg + 2,1) = {'s^2'};
-4-

C:\Documents and Settings\soumya1\Desktop\BayRegimeChangev1\GIBBS_2_REGIME.m

Monday, January 21, 2013 3:29 PM

result(nreg + 2,2:3) = {sigma2_mean(1),sigma2_std(1)}; disp(' ') disp(result)

disp('------- After Regime Change ----------') result = cell(nreg + 4,3); result(1,:) = {'Coeff.','Post. mean','Post. std'}; for m = 1:nreg result(m + 1,1) = {['C(',num2str(m-add_constant),')']}; result(m + 1,2:3) = {beta_mean(m,:,2),beta_std(m,:,2)}; end result(nreg + 2,1) = {'s^2'}; result(nreg + 2,2:3) = {sigma2_mean(2),sigma2_std(2)}; result(nreg + 4,1) = {'switch'}; result(nreg + 4,2:3) = {change_mean,change_std}; disp(' ') disp(result) fwrite(1, char([QH1,QH2,10,13])) close(H_status_bar);

-5-

Vous aimerez peut-être aussi