matlab的arsin,BVARs using Independent Normal-Wishart Prior in Matlab

% VAR using the Gibbs sampler, based on independent Normal Wishar prior

clear all;

clc;

randn('seed',2); %#ok

rand('seed',2); %#ok

%------------------------------LOAD DATA---------------------------------

load Yraw.dat;

% Define specification of the VAR model

constant = 1;        % 1: if you desire intercepts, 0: otherwise

p = 2;               % Number of lags on dependent variables

forecasting = 1;     % 1: Compute h-step ahead predictions, 0: no prediction

forecast_method = 1; % 0: Direct forecasts

% 1: Iterated forecasts

repfor = 10;         % Number of times to obtain a draw from the predictive

% density, for each generated draw of the parameters

h = 1;               % Number of forecast periods

impulses = 1;        % 1: compute impulse responses, 0: no impulse responses

ihor = 21;           % Horizon to compute impulse responses

% Set prior for BVAR model:

prior = 1;  % prior = 1 --> Indepependent Normal-Whishart Prior

% prior = 2 --> Indepependent Minnesota-Whishart Prior

% Gibbs-related preliminaries

nsave = 2000;         % Final number of draws to save

nburn = 200;         % Draws to discard (burn-in)

ntot = nsave + nburn;  % Total number of draws

it_print = 2000;      % Print on the screen every "it_print"-th iteration

%--------------------------DATA HANDLING-----------------------------------

[Traw M] = size(Yraw);

if forecasting==1

if h<=0

error('You have set forecasting, but the forecast horizon choice is wrong')

end

% Now create VAR specification according to forecast method

if forecast_method==0       % Direct forecasts

Y1 = Yraw(h+1:end,:);

Y2 = Yraw(2:end-h,:);

Traw = Traw - h - 1;

elseif forecast_method==1   % Iterated forecasts

Y1 = Yraw;

Y2 = Yraw;

else

error('Wrong choice of forecast_method')

end

else

Y1 = Yraw;

Y2 = Yraw;

end

% Generate lagged Y matrix. This will be part of the X matrix

Ylag = mlag2(Y2,p); % Y is [T x M]. ylag is [T x (Mp)]

% Now define matrix X which has all the R.H.S. variables (constant, lags of

% the dependent variable and exogenous regressors/dummies)

if constant

X1 = [ones(Traw-p,1) Ylag(p+1:Traw,:)];

else

X1 = Ylag(p+1:Traw,:);  %#ok

end

% Get size of final matrix X

[Traw3 K] = size(X1);

% Create the block diagonal matrix Z

Z1 = kron(eye(M),X1);

% Form Y matrix accordingly

% Delete first "LAGS" rows to match the dimensions of X matrix

Y1 = Y1(p+1:Traw,:); % This is the final Y matrix used for the VAR

% Traw was the dimesnion of the initial data. T is the number of actual

% time series observations of Y and X (we lose the p-lags)

T = Traw - p;

%========= FORECASTING SET-UP:

% Now keep also the last "h" or 1 observations to evaluate (pseudo-)forecasts

if forecasting==1

Y_pred = zeros(nsave*repfor,M); % Matrix to save prediction draws

PL =zeros(nsave,1);             % Matrix to save Predictive Likelihood

if forecast_method==0  % Direct forecasts, we only need to keep the

Y = Y1(1:end-1,:);                             % last observation

X = X1(1:end-1,:);

Z = kron(eye(M),X);

T = T - 1;

else              % Iterated forecasts, we keep the last h observations

Y = Y1(1:end-h,:);

X = X1(1:end-h,:);

Z = kron(eye(M),X);

T = T - h;

end

else

Y = Y1;

X = X1;

Z = Z1;

end

%========= IMPULSE RESPONSES SET-UP:

% Create matrices to store forecasts

if impulses == 1;

% Impulse response horizon

imp = zeros(nsave,M,ihor);

bigj = zeros(M,M*p);

bigj(1:M,1:M) = eye(M);

end

%-----------------------------PRELIMINARIES--------------------------------

% First get ML estimators

A_OLS = inv(X'*X)*(X'*Y); % This is the matrix of regression coefficients

a_OLS = A_OLS(:);         % This is the vector of parameters, i.e. it holds

% that a_OLS = vec(A_OLS)

SSE = (Y - X*A_OLS)'*(Y - X*A_OLS);   % Sum of squared errors

SIGMA_OLS = SSE./(T-K+1);

% Initialize Bayesian posterior parameters using OLS values

alpha = a_OLS;     % This is the single draw from the posterior of alpha

ALPHA = A_OLS;     % This is the single draw from the posterior of ALPHA

SSE_Gibbs = SSE;   % This is the single draw from the posterior of SSE

SIGMA = SIGMA_OLS; % This is the single draw from the posterior of SIGMA

% Storage space for posterior draws

alpha_draws = zeros(nsave,K*M);

ALPHA_draws = zeros(nsave,K,M);

SIGMA_draws = zeros(nsave,M,M);

%-----------------Prior hyperparameters for bvar model

n = K*M; % Total number of parameters (size of vector alpha)

% Define hyperparameters

if prior == 1 % Normal-Wishart

a_prior = 0*ones(n,1);   %

V_prior = 10*eye(n);     %

% Hyperparameters on inv(SIGMA) ~ W(v_prior,inv(S_prior))

v_prior = M;             %

S_prior = eye(M);            %

inv_S_prior = inv(S_prior);

elseif prior == 2 % Minnesota-Whishart

% Prior mean on VAR regression coefficients

A_prior = [zeros(1,M); 0.9*eye(M); zeros((p-1)*M,M)];  %

a_prior = A_prior(:);               %

% Minnesota Variance on VAR regression coefficients

% First define the hyperparameters 'a_bar_i'

a_bar_1 = 0.5;

a_bar_2 = 0.5;

a_bar_3 = 10^2;

% Now get residual variances of univariate p-lag autoregressions. Here

% we just run the AR(p) model on each equation, ignoring the constant

% and exogenous variables (if they have been specified for the original

% VAR model)

sigma_sq = zeros(M,1); % vector to store residual variances

for i = 1:M

% Create lags of dependent variable in i-th equation

Ylag_i = mlag2(Yraw(:,i),p);

Ylag_i = Ylag_i(p+1:Traw,:);

% Dependent variable in i-th equation

Y_i = Yraw(p+1:Traw,i);

% OLS estimates of i-th equation

alpha_i = inv(Ylag_i'*Ylag_i)*(Ylag_i'*Y_i);

sigma_sq(i,1) = (1./(Traw-p+1))*(Y_i - Ylag_i*alpha_i)'*(Y_i - Ylag_i*alpha_i);

end

% Now define prior hyperparameters.

% Create an array of dimensions K x M, which will contain the K diagonal

% elements of the covariance matrix, in each of the M equations.

V_i = zeros(K,M);

% index in each equation which are the own lags

ind = zeros(M,p);

for i=1:M

ind(i,:) = constant+i:M:K;

end

for i = 1:M  % for each i-th equation

for j = 1:K   % for each j-th RHS variable

if constant==1 % if there is constant in the model use this code:

if j==1

V_i(j,i) = a_bar_3*sigma_sq(i,1); % variance on constant

elseif find(j==ind(i,:))>0

V_i(j,i) = a_bar_1./(p^2); % variance on own lags

else

for kj=1:M

if find(j==ind(kj,:))>0

ll = kj;

end

end                 % variance on other lags

V_i(j,i) = (a_bar_2*sigma_sq(i,1))./((p^2)*sigma_sq(ll,1));

end

else  % if there is no constant in the model use this:

if find(j==ind(i,:))>0

V_i(j,i) = a_bar_1./(p^2); % variance on own lags

else

for kj=1:M

if find(j==ind(kj,:))>0

ll = kj;

end

end                 % variance on other lags

V_i(j,i) = (a_bar_2*sigma_sq(i,1))./((p^2)*sigma_sq(ll,1));

end

end

end

end

% Now V is a diagonal matrix with diagonal elements the V_i

V_prior = diag(V_i(:));  % this is the prior variance of the vector alpha

% Hyperparameters on inv(SIGMA) ~ W(v_prior,inv(S_prior))

v_prior = M;

S_prior = eye(M);

inv_S_prior = inv(S_prior);

end

%========================== Start Sampling ================================

tic;

disp('Number of iterations');

for irep = 1:ntot  %Start the Gibbs "loop"

if mod(irep,it_print) == 0

disp(irep);

toc;

end

VARIANCE = kron(inv(SIGMA),speye(T));

V_post = inv(inv(V_prior) + Z'*VARIANCE*Z);

a_post = V_post*(inv(V_prior)*a_prior + Z'*VARIANCE*Y(:));

alpha = a_post + chol(V_post)'*randn(n,1); % Draw of alpha

ALPHA = reshape(alpha,K,M); % Draw of ALPHA

% Posterior of SIGMA|ALPHA,Data ~ iW(inv(S_post),v_post)

v_post = T + v_prior;

S_post = S_prior + (Y - X*ALPHA)'*(Y - X*ALPHA);

SIGMA = inv(wish(inv(S_post),v_post));% Draw SIGMA

% Store results

if irep > nburn

%=========FORECASTING:

if forecasting==1

if forecast_method == 0   % Direct forecasts

Y_temp = zeros(repfor,M);

% compute 'repfor' predictions for each draw of ALPHA and SIGMA

for ii = 1:repfor

X_fore = [1 Y(T,:) X(T,2:M*(p-1)+1)];

% Forecast of T+1 conditional on data at time T

Y_temp(ii,:) = X_fore*ALPHA + randn(1,M)*chol(SIGMA);

end

% Matrix of predictions

Y_pred(((irep-nburn)-1)*repfor+1:(irep-nburn)*repfor,:) = Y_temp;

% Predictive likelihood

PL(irep-nburn,:) = mvnpdf(Y1(T+1,:),X(T,:)*ALPHA,SIGMA);

if PL(irep-nburn,:) == 0

PL(irep-nburn,:) = 1;

end

elseif forecast_method == 1   % Iterated forecasts

for ii = 1:repfor

% Forecast of T+1 conditional on data at time T

X_fore = [1 Y(T,:) X(T,2:M*(p-1)+1)];

Y_hat = X_fore*ALPHA + randn(1,M)*chol(SIGMA);

Y_temp = Y_hat;

X_new_temp = X_fore;

for i = 1:h-1  % Predict T+2, T+3 until T+h

if i <= p

% Create matrix of dependent variables for

% predictions. Dependent on the horizon, use the previous

% forecasts to create the new right-hand side variables

% which is used to evaluate the next forecast.

X_new_temp = [1 Y_hat X_fore(:,2:M*(p-i)+1)];

% This gives the forecast T+i for i=1,..,p

Y_temp = X_new_temp*ALPHA + randn(1,M)*chol(SIGMA);

Y_hat = [Y_hat Y_temp];

else

X_new_temp = [1 Y_hat(:,1:M*p)];

Y_temp = X_new_temp*ALPHA + randn(1,M)*chol(SIGMA);

Y_hat = [Y_hat Y_temp];

end

end %  the last value of 'Y_temp' is the prediction T+h

Y_temp2(ii,:) = Y_temp;

end

% Matrix of predictions

Y_pred(((irep-nburn)-1)*repfor+1:(irep-nburn)*repfor,:) = Y_temp2;

% Predictive likelihood

PL(irep-nburn,:) = mvnpdf(Y1(T+h,:),X_new_temp*ALPHA,SIGMA);

if PL(irep-nburn,:) == 0

PL(irep-nburn,:) = 1;

end

end

end % end forecasting

%=========Forecasting ends here

%=========IMPULSE RESPONSES:

if impulses==1

biga = zeros(M*p,M*p);

for j = 1:p-1

biga(j*M+1:M*(j+1),M*(j-1)+1:j*M) = eye(M);

end

atemp = ALPHA(2:end,:);

atemp = atemp(:);

splace = 0;

for ii = 1:p

for iii = 1:M

biga(iii,(ii-1)*M+1:ii*M) = atemp(splace+1:splace+M,1)';

splace = splace + M;

end

end

% St dev matrix for structural VAR

STCO = chol(SIGMA);

% Now get impulse responses for 1 through nhor future periods

impresp = zeros(M,M*ihor);

impresp(1:M,1:M) = STCO;

bigai = biga;

for j = 1:ihor-1

impresp(:,j*M+1:(j+1)*M) = bigj*bigai*bigj'*STCO;

bigai = bigai*biga;

end

% Get the responses of all M variables to a shock imposed on

% the 'equatN'- th equation:

equatN = M; %this assumes that the interest rate is sorted last in Y

impf_m = zeros(M,ihor);

jj=0;

for ij = 1:ihor

jj = jj + equatN;

impf_m(:,ij) = impresp(:,jj);

end

imp(irep-nburn,:,:) = impf_m;

end

%----- Save draws of the parameters

alpha_draws(irep-nburn,:) = alpha;

ALPHA_draws(irep-nburn,:,:) = ALPHA;

SIGMA_draws(irep-nburn,:,:) = SIGMA;

end % end saving results

end %end the main Gibbs for loop

%====================== End Sampling Posteriors ===========================

%Posterior mean of parameters:

ALPHA_mean = squeeze(mean(ALPHA_draws,1)); %posterior mean of ALPHA

SIGMA_mean = squeeze(mean(SIGMA_draws,1)); %posterior mean of SIGMA

% mean prediction and log predictive likelihood

if forecasting == 1

Y_pred_mean = mean(Y_pred,1);

log_PL = mean((log(PL)),1);

%This are the true value of Y at T+h:

if forecast_method == 0

true_value = Y1(T+1,:);

elseif forecast_method == 1

true_value = Y1(T+h,:);

end

%(subsequently you can easily also get MSFE and MAFE)

end

% You can also get other quantities, like impulse responses

if impulses==1;

qus = [.1, .5, .90];

imp_resp = squeeze(quantile(imp,qus));

end

clc;

toc;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
后台采用apache服务器下的cgi处理c语言做微信小程序后台逻辑的脚本映射。PC端的服务器和客户端都是基于c语言写的。采用mysql数据库进行用户数据和聊天记录的存储。.zip C语言是一种广泛使用的编程语言,它具有高效、灵活、可移植性强等特点,被广泛应用于操作系统、嵌入式系统、数据库、编译器等领域的开发。C语言的基本语法包括变量、数据类型、运算符、控制结构(如if语句、循环语句等)、函数、指针等。下面详细介绍C语言的基本概念和语法。 1. 变量和数据类型 在C语言中,变量用于存储数据,数据类型用于定义变量的类型和范围。C语言支持多种数据类型,包括基本数据类型(如int、float、char等)和复合数据类型(如结构体、联合等)。 2. 运算符 C语言中常用的运算符包括算术运算符(如+、、、/等)、关系运算符(如==、!=、、=、<、<=等)、逻辑运算符(如&&、||、!等)。此外,还有位运算符(如&、|、^等)和指针运算符(如、等)。 3. 控制结构 C语言中常用的控制结构包括if语句、循环语句(如for、while等)和switch语句。通过这些控制结构,可以实现程序的分支、循环和多路选择等功能。 4. 函数 函数是C语言中用于封装代码的单元,可以实现代码的复用和模块化。C语言中定义函数使用关键字“void”或返回值类型(如int、float等),并通过“{”和“}”括起来的代码块来实现函数的功能。 5. 指针 指针是C语言中用于存储变量地址的变量。通过指针,可以实现对内存的间接访问和修改。C语言中定义指针使用星号()符号,指向数组、字符串和结构体等数据结构时,还需要注意数组名和字符串常量的特殊性质。 6. 数组和字符串 数组是C语言中用于存储同类型数据的结构,可以通过索引访问和修改数组中的元素。字符串是C语言中用于存储文本数据的特殊类型,通常以字符串常量的形式出现,用双引号("...")括起来,末尾自动添加'\0'字符。 7. 结构体和联合 结构体和联合是C语言中用于存储不同类型数据的复合数据类型。结构体由多个成员组成,每个成员可以是不同的数据类型;联合由多个变量组成,它们共用同一块内存空间。通过结构体和联合,可以实现数据的封装和抽象。 8. 文件操作 C语言中通过文件操作函数(如fopen、fclose、fread、fwrite等)实现对文件的读写操作。文件操作函数通常返回文件指针,用于表示打开的文件。通过文件指针,可以进行文件的定位、读写等操作。 总之,C语言是一种功能强大、灵活高效的编程语言,广泛应用于各种领域。掌握C语言的基本语法和数据结构,可以为编程学习和实践打下坚实的基础。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值