Home > . > rfvar3.m

rfvar3

PURPOSE ^

function var=rfvar3(ydata,lags,xdata,breaks,lambda,mu)

SYNOPSIS ^

function var=rfvar3(ydata,lags,xdata,breaks,lambda,mu)

DESCRIPTION ^

function var=rfvar3(ydata,lags,xdata,breaks,lambda,mu)
 This algorithm goes for accuracy without worrying about memory requirements.
 ydata:   dependent variable data matrix
 xdata:   exogenous variable data matrix
 lags:    number of lags
 breaks:  rows in ydata and xdata after which there is a break.  This allows for
          discontinuities in the data (e.g. war years) and for the possibility of
          adding dummy observations to implement a prior.  This must be a column vector.
          Note that a single dummy observation becomes lags+1 rows of the data matrix,
          with a break separating it from the rest of the data.  The function treats the 
          first lags observations at the top and after each "break" in ydata and xdata as
          initial conditions. 
 lambda:  weight on "co-persistence" prior dummy observations.  This expresses
          belief that when data on *all* y's are stable at their initial levels, they will
          tend to persist at that level.  lambda=5 is a reasonable first try.  With lambda<0,
          constant term is not included in the dummy observation, so that stationary models
          with means equal to initial ybar do not fit the prior mean.  With lambda>0, the prior
          implies that large constants are unlikely if unit roots are present.
 mu:      weight on "own persistence" prior dummy observation.  Expresses belief
          that when y_i has been stable at its initial level, it will tend to persist
          at that level, regardless of the values of other variables.  There is
          one of these for each variable.  A reasonable first guess is mu=2.
      The program assumes that the first lags rows of ydata and xdata are real data, not dummies.
      Dummy observations should go at the end, if any.  If pre-sample x's are not available,
      repeating the initial xdata(lags+1,:) row or copying xdata(lags+1:2*lags,:) into 
      xdata(1:lags,:) are reasonable subsititutes.  These values are used in forming the
      persistence priors.
 Code written by Christopher Sims.  This version 6/15/03.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function var=rfvar3(ydata,lags,xdata,breaks,lambda,mu)
0002 %function var=rfvar3(ydata,lags,xdata,breaks,lambda,mu)
0003 % This algorithm goes for accuracy without worrying about memory requirements.
0004 % ydata:   dependent variable data matrix
0005 % xdata:   exogenous variable data matrix
0006 % lags:    number of lags
0007 % breaks:  rows in ydata and xdata after which there is a break.  This allows for
0008 %          discontinuities in the data (e.g. war years) and for the possibility of
0009 %          adding dummy observations to implement a prior.  This must be a column vector.
0010 %          Note that a single dummy observation becomes lags+1 rows of the data matrix,
0011 %          with a break separating it from the rest of the data.  The function treats the
0012 %          first lags observations at the top and after each "break" in ydata and xdata as
0013 %          initial conditions.
0014 % lambda:  weight on "co-persistence" prior dummy observations.  This expresses
0015 %          belief that when data on *all* y's are stable at their initial levels, they will
0016 %          tend to persist at that level.  lambda=5 is a reasonable first try.  With lambda<0,
0017 %          constant term is not included in the dummy observation, so that stationary models
0018 %          with means equal to initial ybar do not fit the prior mean.  With lambda>0, the prior
0019 %          implies that large constants are unlikely if unit roots are present.
0020 % mu:      weight on "own persistence" prior dummy observation.  Expresses belief
0021 %          that when y_i has been stable at its initial level, it will tend to persist
0022 %          at that level, regardless of the values of other variables.  There is
0023 %          one of these for each variable.  A reasonable first guess is mu=2.
0024 %      The program assumes that the first lags rows of ydata and xdata are real data, not dummies.
0025 %      Dummy observations should go at the end, if any.  If pre-sample x's are not available,
0026 %      repeating the initial xdata(lags+1,:) row or copying xdata(lags+1:2*lags,:) into
0027 %      xdata(1:lags,:) are reasonable subsititutes.  These values are used in forming the
0028 %      persistence priors.
0029 % Code written by Christopher Sims.  This version 6/15/03.
0030 [T,nvar]=size(ydata);
0031 nox=isempty(xdata);
0032 if ~nox
0033    [T2,nx]=size(xdata);
0034 else
0035    T2=T;nx=0;xdata=zeros(T2,0);
0036 end
0037 % note that x must be same length as y, even though first part of x will not be used.
0038 % This is so that the lags parameter can be changed without reshaping the xdata matrix.
0039 if T2 ~= T, disp('Mismatch of x and y data lengths'),end
0040 if nargin<4
0041    nbreaks=0;breaks=[];
0042 else
0043    nbreaks=length(breaks);
0044 end
0045 breaks=[0;breaks;T];
0046 smpl=[];
0047 for nb=1:nbreaks+1
0048    smpl=[smpl;[breaks(nb)+lags+1:breaks(nb+1)]'];
0049 end
0050 Tsmpl=size(smpl,1);
0051 X=zeros(Tsmpl,nvar,lags);
0052 for is=1:length(smpl)
0053     X(is,:,:)=ydata(smpl(is)-(1:lags),:)';
0054 end
0055 X=[X(:,:) xdata(smpl,:)];
0056 y=ydata(smpl,:);
0057 % Everything now set up with input data for y=Xb+e
0058 % ------------------Form persistence dummies-------------------
0059 if lambda~=0 | mu>0
0060    ybar=mean(ydata(1:lags,:),1);
0061    if ~nox 
0062       xbar=mean(xdata(1:lags,:),1);
0063    else
0064       xbar=[];
0065    end
0066    if lambda~=0
0067       if lambda>0
0068          xdum=lambda*[repmat(ybar,1,lags) xbar];
0069       else
0070          lambda=-lambda;
0071          xdum=lambda*[repmat(ybar,1,lags) zeros(size(xbar))];
0072       end
0073       ydum=zeros(1,nvar);
0074       ydum(1,:)=lambda*ybar;
0075       y=[y;ydum];
0076       X=[X(:,:);xdum];
0077    end
0078    if mu>0
0079       xdum=[repmat(diag(ybar),1,lags) zeros(nvar,nx)]*mu;
0080       ydum=mu*diag(ybar);
0081       X=[X;xdum];
0082       y=[y;ydum];
0083    end
0084 end
0085 [vl,d,vr]=svd(X(:,:),0);
0086 di=1../diag(d);
0087 B=vl'*y;
0088 B=(vr.*repmat(di',nvar*lags+nx,1))*B;
0089 u=y-X(:,:)*B;
0090 xxi=vr.*repmat(di',nvar*lags+nx,1);
0091 xxi=xxi*xxi';
0092 B=reshape(B,[nvar*lags+nx,nvar]); % rhs variables, equations
0093 By=B(1:nvar*lags,:);
0094 By=reshape(By,nvar,lags,nvar);% variables, lags, equations
0095 By=permute(By,[3,1,2]); %equations, variables, lags to match impulsdt.m
0096 if nox
0097    Bx=[];
0098 else
0099    Bx=B(nvar*lags+(1:nx),:)';
0100 end
0101 %logintlh=matrictint(u'*u,xxi,size(X,1)-nvar-1)-.5*nvar*(nvar+1)*log(2*pi);
0102 var.By=By;var.Bx=Bx;var.u=u;var.xxi=xxi;%var.logintlh=logintlh;
0103 % Desired features: 1) automatic dummies for vcv prior
0104 %                   2) automatic calculation of integrated pdf, accounting
0105 %                      for the dummy variables as a prior
0106 %                   3) automatic dummies for "Minnesota prior"

Generated on Fri 16-Jun-2006 09:09:06 by m2html © 2003