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

📄 sar_gcbma.m

📁 计量工具箱
💻 M
📖 第 1 页 / 共 2 页
字号:
function results = sar_gcbma(y,x,W,ndraw,prior)
% PURPOSE: MC^3 x-matrix specification for homoscedastic SAR model
%          where the model contains an intercept
% ------------------------------------------------------
% usage: results = sar_gcbma(y,x,W,ndraw,prior)
% where: % where: y = dependent variable vector (nobs x 1)
%        x = studentized or well-scaled x-matrix with no intercept 
%        W = spatial weight matrix (standardized, row-sums = 1)
%    ndraw = # of draws
%    prior = a structure variable with:
%            prior.g     = g-value for g-prior, default: max(1/n,1/k^2)
%            prior.nmodels = # of top models to save (default = 1000)
%            prior.nu    = informative Gamma(nu,d0) prior on sige
%            prior.d0    = default: nu=0,d0=0 (diffuse prior)
%            prior.a1    = parameter for beta(a1,a2) prior on rho (default = 1.01)
%            prior.a2    = (default = 1.01) see: 'help beta_prior'
%            prior.eig   = 0 for default rmin = -1,rmax = +1, 1 for eigenvalue calculation of these
%            prior.rmin  = (optional) min rho used in sampling (default = -1)
%            prior.rmax  = (optional) max rho used in sampling (default = 1)  
%            prior.lflag = 0 for full lndet computation (default = 1, fastest)
%                        = 1 for MC approx (fast for large problems)
%                        = 2 for Spline approx (medium speed)
%            prior.order = order to use with prior.lflag = 1 option (default = 50)
%            prior.iter  = iters to use with prior.lflag = 1 option (default = 30) 
%            prior.lndet = a matrix returned by sar, sar_g, sarp_g, etc.
%                          containing log-determinant information to save time
%-------------------------------------------------------------
% RETURNS:  a structure:
%          results.meth    = 'sar_gcbma'
%          results.g       = g-value for prior (from input, or default value)
%          results.munique = # of unique models found
%          results.nmodels = # of top models to save from input or (default = 1000)
%          results.freq    = a matrix with frequency distribution of
%                            variables appearing in all unique models found
%          results.models  = a matrix with only information for top nmodels
%          results.allmodels = a matrix with information on all unique models found
%          results.mprob   = a vector of posterior model probabilities based on all unique models
%          results.vprob   = a vector of variable probabilities based on all unique models
%          results.nobs    = # of observations
%          results.nvar    = # of variables in x-matrix
%          results.ndraw   = # of draws
%          results.y       = y-vector from input (nobs x 1)
%          results.nu      = nu prior parameter
%          results.d0      = d0 prior parameter
%          results.a1      = a1 parameter for beta prior on rho from input, or default value
%          results.a2      = a2 parameter for beta prior on rho from input, or default value
%          results.time1   = time for eigenvalue calculation
%          results.time2   = time for log determinant calcluation
%          results.time3   = time for BMA sampling
%          results.time    = total time taken  
%          results.rmax    = 1/max eigenvalue of W (or rmax if input)
%          results.rmin    = 1/min eigenvalue of W (or rmin if input)          
%          results.lflag   = lflag from input
%          results.iter    = prior.iter option from input
%          results.order   = prior.order option from input
%          results.limit   = matrix of [rho lower95,logdet approx, upper95] 
%                           intervals for the case of lflag = 1
%          results.dflag   = dflag value from input (or default value used)
%          results.lndet  = a matrix containing log-determinant information
%                           (for use in later function calls to save time)
% --------------------------------------------------------------
% NOTES: 1) the log-marginals for the top 1000 models are written to a file lmarginal.sar
%        as vectors over a grid of rho values for later use 
%        2) the model descriptions for the top 1000 models are written to a file models.sar
% --------------------------------------------------------------
% SEE ALSO: prt_bmas(), for printing results
%           model_averages() function, that can be used to construct
%           estimates based on averaging over models using posterior
%           model probabilities as weights
% --------------------------------------------------------------
% REFERENCES: James P. LeSage and Olivier Parent, 
% `Bayesian Model Averaging for Spatial Econometric Models', working paper 
%
% Fernandez,Carmen, Eduardo Ley, and Mark F. J. Steel, (2001a)
%'Model uncertainty in cross-country growth regressions,'
% Journal of Applied Econometrics}, Volume 16, number 5, pp. 563 - 576.
%
% Fernandez,Carmen, Eduardo Ley, and Mark F. J. Steel, (2001b)
% 'Benchmark priors for Bayesian model averaging', Journal of Econometrics
% Volume 100, number 2, pp. 381-427.
%----------------------------------------------------------------

% written by:
% James P. LeSage, last updated 6/2004
% Dept of Economics
% University of Toledo
% 2801 W. Bancroft St,
% Toledo, OH 43606
% jlesage@spatial-econometrics.com

% NOTE: some of the speed for large problems comes from:
% the use of methods pioneered by Pace and Barry.
% R. Kelley Pace was kind enough to provide functions
% lndetmc, and lndetint from his spatial statistics toolbox
% for which I'm very grateful.


% error checking on inputs
[n junk] = size(y);
[n1 k] = size(x);
[n3 n4] = size(W);
time1 = 0;
time2 = 0;
time3 = 0;
time4 = 0;

nobsa = n;

results.nobs  = n;
results.nvar  = k;
results.y = y;      

if n1 ~= n
error('sar_gcbma: x-matrix contains wrong # of observations');
elseif n3 ~= n4
error('sar_gcbma: W matrix is not square');
elseif n3~= n
error('sar_gcbma: W matrix is not the same size at y,x');
end;

if nargin == 4
    prior.lflag = 1;
end;

[nu,d0,rho,sige,rmin,rmax,detval,ldetflag,eflag,order,iter,a1,a2,g,gmodels] = sar_parse(prior,x);


results.order = order;
results.iter = iter;

timet = clock; % start the timer

[rmin,rmax,time1] = sar_eigs(eflag,W,rmin,rmax,n);
results.time1 = time1;

[detval,time2] = sar_lndet(ldetflag,W,rmin,rmax,detval,order,iter);
results.time2 = time2;
na = length(detval(:,1));
% overide nmodels to speed things up
nmodels = min([gmodels 10]);
gmodels = max([1000 gmodels]);
% storage for draws
          nvar = k;
          maxlog = ones(gmodels,1)*(-Inf);
          vsave = zeros(ndraw,nvar);
          msave = zeros(1,nvar);
          visits = zeros(ndraw,1);
          lsave = zeros(na,1);
          vfreqs = zeros(1,nvar);
          
          modsave = zeros(gmodels,nvar); % storage for definition of 1000 best models 
          logsave = zeros(gmodels,na);

% ====== initializations
% compute this stuff once to save time
in = ones(n,1);
Wy = sparse(W)*y;

% start with a random selection of variables
vtmp=zeros(nvar,1);
for v=1:nvar
    vtmp(v)=bino_rnd(1,0.5,1,1);
end
vin=selif(seqa(1,1,nvar).*vtmp,vtmp)';
vout=delif(seqa(1,1,nvar)-seqa(1,1,nvar).*vtmp,vtmp)'; 

cnt = 0;

%<====================== start draws
t0 = clock;
hwait = waitbar(0,'SAR BMA : MCMC sampling ...');
for i=1:ndraw; 
    
% choose new model
[vinew vonew] = sample(vin,vout);
for j=1:length(vinew);
vsave(i,vinew(1,j)) = 1.0;
end;
vfreqs = vfreqs + vsave(i,:);

[j visits] = find_new(i,vsave,vinew,visits);

if i == 1 % first draw, compute logpost for initial model
lmarg = bmapost(y,x,vinew,W,detval,a1,a2,nu,d0,g);
maxl = max(lmarg);
maxlog(i,1) = maxl;
lsave = [lsave lmarg];
msave = [msave
         vsave(i,:)];
modsave(i,:) =  vsave(i,:);     
logsave(i,:) = lmarg';
cnt = cnt+1; % count of unique models found
  lmarg_old = lmarg;
end; % end of if i == 1


if visits(i,1) ~= 0 % we found a new model
cnt = cnt+1; % increase counter for new models
lmarg_new = bmapost(y,x,vinew,W,detval,a1,a2,nu,d0,g);

  if cnt < gmodels
  lsave = [lsave lmarg_new];
  msave = [msave
           vsave(i,:)];
  end;
maxl = max(lmarg_new);
  if cnt < gmodels
    maxlog(cnt,1) = maxl;
    modsave(cnt,:) = vsave(i,:);
    logsave(cnt,:) = lmarg_new';
  elseif cnt == gmodels
    maxlog(cnt,1) = maxl;
    modsave(cnt,:) = vsave(i,:);
    logsave(cnt,:) = lmarg_new';
    % sort the maxlog vector
    [maxlog,maxi] = sort(maxlog);
    tmp = logsave(maxi,:); % bring along the log-marginals matrix
    logsave = tmp;
    tmp = modsave(maxi,:); % bring along the model definitions
    modsave = tmp;
  else
    ind = find(maxl > maxlog);
    if length(ind) == 0 
    %  we don't save this log-marginal
    else
    % insert this log-marginal vector into the 1000 best models
    % pitch lowest log-marginal
      if length(ind) == 1 % special case where we replace just the lowest
       logsave(1,:) = lmarg_new';
       modsave(1,:) = vsave(i,:); % replace model definitions
      else
      logsave(1:ind(end-1),:) = logsave(2:ind(end),:); % pitch the lowest
      logsave(ind(end),:) = lmarg_new'; % insert the new one
      modsave(1:ind(end-1),:) = modsave(2:ind(end),:); % pitch the lowest model defintiion
      modsave(ind(end),:) = vsave(i,:); % insert the new model definition
      end;
    end;
  end;

else % we have a model that we have already computed the log-marginal for
     % so, we could look it up.
  if cnt < gmodels
  tmps = matsub(vsave(i,:),msave);
  ind = ~any(tmps');
  chk = find(ind == 1);
  lmarg_new = lsave(:,chk(1,1));
  else
% recompute it
  lmarg_new = bmapost(y,x,vinew,W,detval,a1,a2,nu,d0,g);
  end;
end;

bf = model_odds(detval(:,1),lmarg_new,lmarg_old);
 if bf >=1; 
     flag = 1; 
 else 
     flag = bino_rnd(1,bf,1,1); 
 end;

 if flag == 1 % change models
 vin = vinew;
 vout = vonew;
 lmarg_old = lmarg_new;
 end;
 
waitbar(i/ndraw);         
end; % end of sampling loop
close(hwait);

time3 = abs(etime(clock,t0));
results.time3 = time3;

vfreqs = vfreqs/ndraw;

results.munique = cnt;

% write out results for 1000 best models

tmp =  logsave(1:cnt,:);
save lmarginal.sar tmp  -ascii;
tmp = modsave(1:cnt,:);
save models.sar tmp -ascii;

psum = 0.0;
posts = [];
na = length(detval);

load lmarginal.sar;
load models.sar;

[nlog,na] = size(lmarginal);
adj = max(max(lmarginal));
madj = lmarginal - adj;
  tmp = exp(madj);
  % trapezoid rule integration
  for i=1:nlog
  xx = tmp(i,:)';
  yy = detval(:,1).*ones(na,1);
  isum = 0.0;
  isum = sum(diff(yy).*(xx(1:end-1)+xx(2:end))/2);
  % compute posterior probability
  psum = psum + isum;
  posts = [posts
          isum];
  end;

postprob = posts/psum;

[probs_sort,pind] = sort(postprob);
results.mprob = probs_sort;
msort = models(pind,:);
out = [msort probs_sort];
results.allmodels = out; % put out results for all unique models found
[no,junk] = size(out);

% just pull out nmodels requested by the user
if (nmodels > no) % the user wants more models output than were found
    nmodels = no; % we can only output what was found
else
    % the user is okay
end;


results.vprob = vfreqs; % compute variable probabilities based on 
                            % all unique models found

results.models = msort(no-nmodels+1:no,:);

results.ndraw = ndraw;
results.nu = nu;
results.d0 = d0;
results.a1 = a1;
results.a2 = a2;
results.rmax = rmax; 
results.rmin = rmin;
results.lflag = ldetflag;
results.lndet = detval;
results.g = g;



function [nu,d0,rho,sige,rmin,rmax,detval,ldetflag,eflag,order,iter,a1,a2,g,gmodels] = sar_parse(prior,x)
% PURPOSE: parses input arguments for sar_gcbma models
% ---------------------------------------------------
%  USAGE: [nu,d0,rho,sige,rmin,rmax,detval,ldetflag,eflag,mflag,order,iter,a1,a2,g] = 
%                           sar_parse(prior,x)
% where prior contains the structure variable with inputs,
% x is the matrix of explanatory variables input
% and the outputs are either user-inputs or default values
% ---------------------------------------------------

% set defaults
[n,k] = size(x);
g1 = (k*k);
g2 = n;
g = 1/max([g1 g2]); % Fernandez, Ley Steel default g-value
gmodels = 1000;

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;
nu = 0;
d0 = 0;
a1 = 1.01;
a2 = 1.01;

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},'g')
       g = prior.g; 
    elseif strcmp(fields{i},'nmodels')
       gmodels = prior.nmodels; 
    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},'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; 

⌨️ 快捷键说明

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