⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 semip_g.m

📁 计量工具箱
💻 M
📖 第 1 页 / 共 2 页
字号:
            p = min(1,ratio);
          end;
         
          ru = unif_rnd(1,0,1);
          if (ru < p)
              rho = rho2;
              acc = acc + 1;
          end;
      acc_rate(iter,1) = acc/iter;
      % update cc based on std of rho draws
       if acc_rate(iter,1) < 0.4
       cc = cc/1.1;
       end;
       if acc_rate(iter,1) > 0.6
       cc = cc*1.1;
       end;

      end; % end of if metflag == 1

      if (metflag == 0) & (inform_flag == 0)

          % UPDATE: rho (using univariate integration)
          C0 = a'*a;
          Wa = W*a;
          C1 = a'*Wa;
          C2 = Wa'*Wa;
          
		  rho = draw_rho(detval,C0,C1,C2,sige,rho,a1,a2);
end; % end  sampling for rho
 
	  	  Bp = speye(m) - rho*sparse(W); 

   % UPDATE: z

      lp = x*bhat + tvec;
	       ind = find(y == 0);
		    tmp = ones(n,1)./inV;
          z(ind,1) = normrt_rnd(lp(ind,1),tmp(ind,1),0);
          %z(ind,1) = rtanorm_combo(lp(ind,1),tmp(ind,1),zeros(length(ind),1));
	       ind = find(y == 1);
          z(ind,1) = normlt_rnd(lp(ind,1),tmp(ind,1),0);
          %z(ind,1) = rtbnorm_combo(lp(ind,1),tmp(ind,1),zeros(length(ind),1));

    if iter > nomit % if we are past burn-in, save the draws
    	bsave(iter-nomit,1:k) = bhat';
    	asave(iter-nomit,1:m) = a'; % should just return the mean
    	ssave(iter-nomit,1) = sige;
    	psave(iter-nomit,1) = rho;
    	amean = amean + a;
    	yhat = yhat + lp;
    	zmean = zmean + z;
    	vmean = vmean + vi;

    	if mm~= 0
        rsave(iter-nomit,1) = rval;
     	end;
    end;
    
           
    iter = iter + 1;
       
waitbar(iter/ndraw);     

end; % end of sampling loop

close(hwait);

time3 = etime(clock,t0);

vmean = vmean/(ndraw-nomit);
yhat = yhat/(ndraw-nomit);
amean = amean/(ndraw-nomit);
zmean = zmean/(ndraw-nomit);


time = etime(clock,timet);

results.meth  = 'semip_g';
results.bdraw = bsave;
results.adraw = asave;
results.pdraw = psave;
results.sdraw = ssave;
results.vmean = vmean;
results.amean = amean;
results.yhat  = yhat;
results.zmean = zmean;
results.bmean = c;
results.bstd  = sqrt(diag(T));
results.bflag = inform_flag;
results.dflag = metflag;
results.eflag = eflag;
results.nobs  = n;
results.nvar  = k;
results.ndraw = ndraw;
results.nomit = nomit;
results.time = time;
results.time1 = time1;
results.time2 = time2;
results.time3 = time3;
results.nu = nu;
results.d0 = d0;
results.tflag = 'plevel';
results.lflag = ldetflag;
results.nreg = m;
results.mobs = mobs;
results.acc = acc_rate;
results.rmin = rmin;
results.rmax = rmax;
results.a1 = a1;
results.a2 = a2;


if mm~= 0
results.rdraw = rsave;
results.m     = mm;
results.k     = kk;
else
results.r     = rval;
results.rdraw = 0;
end;



function cout = c_semip(rho,a,sige,W,detval,a1,a2)
% PURPOSE: evaluate the conditional distribution of rho for semip model
% ---------------------------------------------------
%  USAGE:cout = c_semip(rho,a,W,detval,sige,a1,a2)
%  where:  rho  = spatial autoregressive parameter
%          a    = regional effects vector
%          W    = spatial weight matrix
%        detval = an (ngrid,2) matrix of values for det(I-rho*W) 
%                 over a grid of rho values 
%                 detval(:,1) = rho values
%                 detval(:,2) = associated log det values
%          a1    = optional beta prior for rho
%          a2    = optional beta prior for rho
% ---------------------------------------------------
%  RETURNS: a conditional used in Metropolis-Hastings sampling
%  NOTE: called only by semip_g
%  --------------------------------------------------

% written by: James P. LeSage 2/98
% University of Toledo
% Department of Economics
% Toledo, OH 43606
% jpl@jpl.econ.utoledo.edu

gsize = detval(2,1) - detval(1,1);
i1 = find(detval(:,1) <= rho + gsize);
i2 = find(detval(:,1) <= rho - gsize);
i1 = max(i1);
i2 = max(i2);
index = round((i1+i2)/2);
if isempty(index)
index = 1;
end;
detm = detval(index,2);
if nargin == 7
bprior = beta_prior(detval(:,1),a1,a2);
detm = detm + log(bprior);
end;
n = length(a);
z = speye(n) - rho*sparse(W);
epe = (a'*z'*z*a)/(2*sige);
cout = detm - epe;

function rho = draw_rho(detval,epe0,eped,epe0d,sige,rho,a1,a2)
% update rho via univariate numerical integration

nrho = length(detval(:,1));
iota = ones(nrho,1);

z = epe0*iota - 2*detval(:,1)*eped + detval(:,1).*detval(:,1)*epe0d;
den = detval(:,2) - (z/2*sige);
bprior = beta_prior(detval(:,1),a1,a2);
den = den + log(bprior);

n = length(den);
y = detval(:,1);
adj = max(den);
den = den - adj;
x = exp(den);

% trapezoid rule
isum = sum((y(2:n,1) + y(1:n-1,1)).*(x(2:n,1) - x(1:n-1,1))/2);
z = abs(x/isum);
den = cumsum(z);

rtmp = unif_rnd(1,0,1);
rnd = rtmp*sum(z);
ind = find(den <= rnd);
idraw = max(ind);
if (idraw > 0 & idraw < nrho)
rho = detval(idraw,1);
end;

function [rmin,rmax,time2] = semip_eigs(eflag,W,rmin,rmax,n);
% PURPOSE: compute the eigenvalues for the weight matrix
% ---------------------------------------------------
%  USAGE: [rmin,rmax,time2] = semip_eigs(eflag,W,rmin,rmax,W)
% where eflag is an input flag, W is the weight matrix
%       rmin,rmax may be used as default outputs
% and the outputs are either user-inputs or default values
% ---------------------------------------------------


if eflag == 1 % compute eigenvalues
t0 = clock;
opt.tol = 1e-3; opt.disp = 0;
lambda = eigs(sparse(W),speye(n),1,'SR',opt);  
rmin = 1/real(lambda);   
rmax = 1;
time2 = etime(clock,t0);
else
time2 = 0;
end;



function [nu,d0,rval,mm,kk,rho,sige,rmin,rmax,detval,ldetflag,eflag,order,iter,c,T,inform_flag,cc,metflag,a1,a2] = semip_parse(prior,k)
% PURPOSE: parses input arguments 
% ---------------------------------------------------
%  USAGE: [nu,d0,rval,mm,kk,rho,sige,rmin,rmax,detval, ...
%         ldetflag,eflag,order,iter,novi_flag,c,T,prior_beta,cc,metflag],a1,a2 = 
%                           semip_parse(prior,k)
% where info contains the structure variable with inputs 
% and the outputs are either user-inputs or default values
% ---------------------------------------------------

% set defaults

eflag = 0;     % default to not computing eigenvalues
ldetflag = 1;  % default to 1999 Pace and Barry MC determinant approx
order = 50;    % there are parameters used by the MC det approx
iter = 30;     % defaults based on Pace and Barry recommendation
rmin = -1;     % use -1,1 rho interval as default
rmax = 1;
detval = 0;    % just a flag
rho = 0.5;
sige = 1.0;
rval = 4;
mm = 0;
kk = 0;
nu = 0;
d0 = 0;
a1 = 1.01;
a2 = 1.01;
c = zeros(k,1);   % diffuse prior for beta
T = eye(k)*1e+12;
prior_beta = 0;   % flag for diffuse prior on beta
cc = 0.2;
metflag = 0; % use integration instead of M-H sampling for rho
inform_flag = 0;

fields = fieldnames(prior);
nf = length(fields);
if nf > 0
 for i=1:nf
    if strcmp(fields{i},'nu')
        nu = prior.nu;
    elseif strcmp(fields{i},'d0')
        d0 = prior.d0;  
    elseif strcmp(fields{i},'rval')
        rval = prior.rval; 
    elseif strcmp(fields{i},'dflag')
       metflag = prior.dflag; 
    elseif strcmp(fields{i},'a1')
       a1 = prior.a1; 
    elseif strcmp(fields{i},'a2')
       a2 = prior.a2; 
    elseif strcmp(fields{i},'m')
        mm = prior.m;
        kk = prior.k;
        rval = gamm_rnd(1,1,mm,kk);    % initial value for rval   
    elseif strcmp(fields{i},'beta')
        c = prior.beta; inform_flag = 1; % flag for informative prior on beta
    elseif strcmp(fields{i},'bcov')
        T = prior.bcov; inform_flag = 1;
    elseif strcmp(fields{i},'rmin')
        rmin = prior.rmin; eflag = 0;
    elseif strcmp(fields{i},'rmax')
        rmax = prior.rmax;  eflag = 0;
    elseif strcmp(fields{i},'lndet')
    detval = prior.lndet;
    ldetflag = -1;
    eflag = 0;
    rmin = detval(1,1);
    nr = length(detval);
    rmax = detval(nr,1);
    elseif strcmp(fields{i},'lflag')
        tst = prior.lflag;
        if tst == 0,
        ldetflag = 0; 
        elseif tst == 1,
        ldetflag = 1; 
        elseif tst == 2,
        ldetflag = 2; 
        else
        error('sar_g: unrecognizable lflag value on input');
        end;
    elseif strcmp(fields{i},'order')
        order = prior.order;  
    elseif strcmp(fields{i},'iter')
        iter = prior.iter; 
    elseif strcmp(fields{i},'dflag')
        metflag = prior.dflag;
    elseif strcmp(fields{i},'eig')
        eflag = prior.eig;
    end;
 end;

 
else, % the user has input a blank info structure
      % so we use the defaults
end; 


function [detval,time1] = semip_lndet(ldetflag,W,rmin,rmax,detval,order,iter);
% PURPOSE: compute the log determinant |I_n - rho*W|
% using the user-selected (or default) method
% ---------------------------------------------------
%  USAGE: detval = far_lndet(lflag,W,rmin,rmax)
% where eflag,rmin,rmax,W contains input flags 
% and the outputs are either user-inputs or default values
% ---------------------------------------------------


% do lndet approximation calculations if needed
if ldetflag == 0 % no approximation
t0 = clock;    
out = lndetfull(W,rmin,rmax);
time1 = etime(clock,t0);
tt=rmin:.001:rmax; % interpolate a finer grid
outi = interp1(out.rho,out.lndet,tt','spline');
detval = [tt' outi];
    
elseif ldetflag == 1 % use Pace and Barry, 1999 MC approximation

t0 = clock;    
out = lndetmc(order,iter,W,rmin,rmax);
time1 = etime(clock,t0);
results.limit = [out.rho out.lo95 out.lndet out.up95];
tt=rmin:.001:rmax; % interpolate a finer grid
outi = interp1(out.rho,out.lndet,tt','spline');
detval = [tt' outi];

elseif ldetflag == 2 % use Pace and Barry, 1998 spline interpolation

t0 = clock;
out = lndetint(W,rmin,rmax);
time1 = etime(clock,t0);
tt=rmin:.001:rmax; % interpolate a finer grid
outi = interp1(out.rho,out.lndet,tt','spline');
detval = [tt' outi];

elseif ldetflag == -1 % the user fed down a detval matrix
    time1 = 0;
        % check to see if this is right
        if detval == 0
            error('sar_g: wrong lndet input argument');
        end;
        [n1,n2] = size(detval);
        if n2 ~= 2
            error('sar_g: wrong sized lndet input argument');
        elseif n1 == 1
            error('sar_g: wrong sized lndet input argument');
        end;          
end;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -