📄 semip_g.m
字号:
function results = semip_g(y,x,W,m,mobs,ndraw,nomit,prior)
% PURPOSE: Bayesian Probit model with individual effects exhibiting spatial dependence:
% Y = (Yi, i=1,..,m) with each vector, Yi = (yij:j=1..Ni) consisting of individual
% dichotomous observations in regions i=1..m, as defined by yij = Indicator(zij>0),
% where latent vector Z = (zij) is given by the linear model:
%
% Z = x*b + del*a + e with:
%
% x = n x k matrix of explanatory variables [n = sum(Ni: i=1..m)];
% del = n x m indicator matrix with del(j,i) = 1 iff indiv j is in reg i;
% a = (ai: i=1..m) a vector of random regional effects modeled by
%
% a = rho*W*a + U, U ~ N[0,sige*I_m] ; (I_m = m-square Identity matrix)
%
% and with e ~ N(0,V), V = diag(del*v) where v = (vi:i=1..m).
%
% The priors for the above parameters are of the form:
% r/vi ~ ID chi(r), r ~ Gamma(m,k)
% b ~ N(c,T),
% 1/sige ~ Gamma(nu,d0),
% rho ~ beta(a1,a2)
%-----------------------------------------------------------------
% USAGE: results = semip_g(y,x,W,m,mobs,ndraw,nomit,prior)
% where: y = dependent variable vector (nobs x 1) [must be zero-one]
% x = independent variables matrix (nobs x nvar)
% W = 1st order contiguity matrix (standardized, row-sums = 1)
% m = # of regions
% mobs = an m x 1 vector containing the # of observations in each
% region [= (Ni:i=1..m) above]
% ndraw = # of draws
% nomit = # of initial draws omitted for burn-in
% prior = a structure variable with:
% prior.beta = prior means for beta, (= c above)
% (default = 0)
% prior.bcov = prior beta covariance , (= T above)
% [default = 1e+12*I_k ]
% prior.rval = r prior hyperparameter, default=4
% prior.a1 = parameter for beta(a1,a2) prior on rho (default = 1.01)
% prior.a2 = (default = 1.01) see: 'help beta_prior'
% prior.m = informative Gamma(m,k) prior on r
% prior.k = (default: not used)
% prior.nu = informative Gamma(nu,d0) prior on sige
% prior.d0 = default: nu=0,d0=0 (diffuse prior)
% 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.dflag = 0 for numerical integration, 1 for Metropolis-Hastings (default = 0)
% prior.eig = 0 for default rmin = -1,rmax = +1, 1 for eigenvalue calculation of these
% prior.order = order to use with prior.lflag = 1 option (default = 50)
% prior.iter = iters to use with prior.lflag = 1 option (default = 30)
%---------------------------------------------------
% RETURNS: a structure:
% results.meth = 'semip_g'
% results.bdraw = bhat draws (ndraw-nomit x nvar)
% results.pdraw = rho draws (ndraw-nomit x 1)
% results.adraw = a draws (ndraw-nomit x m)
% results.amean = mean of a draws (m x 1)
% results.sdraw = sige draws (ndraw-nomit x 1)
% results.vmean = mean of vi draws (m x 1)
% results.rdraw = r draws (ndraw-nomit x 1) (if m,k input)
% results.bmean = b prior means, prior.beta from input
% results.bstd = b prior std deviations sqrt(diag(prior.bcov))
% results.r = value of hyperparameter r (if input)
% results.rsqr = R-squared
% results.nobs = # of observations
% results.mobs = mobs vector from input
% results.nreg = # of regions
% results.nvar = # of variables in x-matrix
% results.ndraw = # of draws
% results.nomit = # of initial draws omitted
% results.y = actual (0,1) observations (nobs x 1)
% results.zmean = mean of latent z-draws (nobs x 1)
% results.yhat = mean of posterior y-predicted (nobs x 1)
% results.nu = nu prior parameter
% results.d0 = d0 prior parameter
% results.time1 = time for eigenvalue calculation
% results.time2 = time for log determinant calcluation
% results.time3 = time for 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.tflag = 'plevel' (default) for printing p-levels
% = 'tstat' for printing bogus t-statistics
% results.rflag = 1, if a beta(a1,a2) prior for rho, 0 otherwise
% results.lflag = lflag from input
% results.dflag = dflag from input
% results.bflag = 1 for informative prior on beta, 0 otherwise
% 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.acc = acceptance rate for M-H sampling (ndraw x 1 vector)
% ----------------------------------------------------
% SEE ALSO: sem_gd, prt, semp_g, coda
% ----------------------------------------------------
% REFERENCES: Tony E. Smith "A Bayesian Probit Model with Spatial Dependencies" unpublished manuscript
% For lndet information see: Ronald Barry and R. Kelley Pace, "A Monte Carlo Estimator
% of the Log Determinant of Large Sparse Matrices", Linear Algebra and
% its Applications", Volume 289, Number 1-3, 1999, pp. 41-54.
% and: R. Kelley Pace and Ronald P. Barry "Simulating Mixed Regressive
% Spatially autoregressive Estimators", Computational Statistics, 1998,
% Vol. 13, pp. 397-418.
%----------------------------------------------------------------
% written by:
% James P. LeSage, Dept of Economics
% University of Toledo
% 2801 W. Bancroft St,
% Toledo, OH 43606
% jpl@jpl.econ.utoledo.edu
% NOTE: much 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.
timet = clock;
time1 = 0;
time2 = 0;
time3 = 0;
% error checking on inputs
[n junk] = size(y);
results.y = y;
[n1 k] = size(x);
[n3 n4] = size(W);
if n1 ~= n
error('semip_g: x-matrix contains wrong # of observations');
elseif n3 ~= n4
error('semip_g: W matrix is not square');
elseif n3~= m
error('semip_g: W matrix is not the same size as # of regions');
end;
% check that mobs vector is correct
obs_chk = sum(mobs);
if obs_chk ~= n
error('semip_g: wrong # of observations in mobs vector');
end;
if length(mobs) ~= m
error('semip_g: wrong size mobs vector -- should be m x 1');
end;
[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);
% error checking on prior information inputs
[checkk,junk] = size(c);
if checkk ~= k
error('sar_g: prior means are wrong');
elseif junk ~= 1
error('sar_g: prior means are wrong');
end;
[checkk junk] = size(T);
if checkk ~= k
error('sar_g: prior bcov is wrong');
elseif junk ~= k
error('sar_g: prior bcov is wrong');
end;
results.order = order;
results.iter = iter;
[rmin,rmax,time1] = semip_eigs(eflag,W,rmin,rmax,m);
[detval,time2] = semip_lndet(ldetflag,W,rmin,rmax,0,order,iter);
rv = detval(:,1);
nr = length(rv);
% storage for draws
bsave = zeros(ndraw-nomit,k);
asave = zeros(ndraw-nomit,m);
if mm~= 0
rsave = zeros(ndraw-nomit,1);
end;
ssave = zeros(ndraw-nomit,1);
psave = zeros(ndraw-nomit,1);
vmean = zeros(m,1);
amean = zeros(m,1);
zmean = zeros(n,1);
yhat = zeros(n,1);
acc_rate = zeros(ndraw,1);
% ====== initializations
% compute this once to save time
TI = inv(T);
TIc = TI*c;
iter = 1;
inV0 = ones(n,1); % default starting value for inV [= inv(V)]
a0 = ones(m,1);
z0 = y; %default starting value for latent vector z
a = a0;
z = z0;
in = ones(n,1);
inV = inV0;
vi = ones(m,1);
tvec = ones(n,1); % initial values
evec = ones(m,1);
b1 = ones(m,1);
Bp = speye(m) - rho*sparse(W);
%Computations for updating vector a
if(m > 100)
W1 = zeros(m,m-1);
W2 = zeros(m-1,m);
W3 = zeros(m,m-1);
for i = 1:m
w1(i) = W(:,i)'*W(:,i);
if i == 1
W1(1,:) = W(1,[2:m]); %W-rows minus entry i
W2(:,1) = W([2:m],1); %W-columns minus entry i
W3(1,:) = W(:,1)'*W(:,[2:m]);
elseif i == m
W1(m,:) = W(m,[1:m-1]);
W2(:,m) = W([1:m-1],m);
W3(m,:) = W(:,m)'*W(:,[1:m-1]);
else
W1(i,:) = W(i,[1:i-1,i+1:m]);
W2(:,i) = W([1:i-1,i+1:m],i);
W3(i,:) = W(:,i)'*W(:,[1:i-1,i+1:m]);
end
end
end %end if(m > 10)
%*********************************
% START SAMPLING
%*********************************
dmean = zeros(length(detval),1);
hwait = waitbar(0,'MCMC sampling ...');
t0 = clock;
iter = 1;
acc = 0;
cc = 0.1;
cntr = 1;
while (iter <= ndraw); % start sampling;
% UPDATE: beta
xs = matmul(sqrt(inV),x);
zs = sqrt(inV).*z;
A0i = inv(xs'*xs + TI);
zmt = sqrt(inV).*(z-tvec);
b = xs'*zmt + TIc;
b0 = A0i*b;
bhat = norm_rnd(A0i) + b0;
%Update b1
e0 = z - x*bhat;
cobs = 0;
for i=1:m;
obs = mobs(i,1);
b1(i,1) = sum(e0(cobs+1:cobs+obs,1)/vi(i,1));
cobs = cobs + obs;
end;
% UPDATE: a
if m <= 100 %Procedure for small m
vii = ones(m,1)./vi;
A1i = inv((1/sige)*Bp'*Bp + diag(vii.*mobs));
a = norm_rnd(A1i) + A1i*b1;
else %Procedure for large m
cobs = 0;
for i = 1:m
obs = mobs(i,1);
if i == 1 %Form complementary vector
ai = a(2:m);
elseif i == m
ai = a(1:m-1);
else
ai = a([1:i-1,i+1:m]);
end
di = (1/sige) + ((rho^2)/sige)*w1(i) + (obs/vi(i));
zi = z(cobs+1:cobs+obs,1);
xbi = x([cobs+1:cobs+obs],:)* bhat;
phi = (1/vi(i))*(ones(1,obs)*(zi - xbi));
awi = ai'*(W1(i,:)' + W2(:,i));
bi = phi + (rho/sige)*awi - ((rho^2)/sige)*(W3(i,:)*ai) ;
a(i) = (bi/di) + sqrt(1/di)*randn(1,1);
cobs = cobs + obs;
end %end for i = 1:m
end %end if on m
% Update tvec = del*a
cobs = 0;
for i=1:m;
obs = mobs(i,1);
tvec(cobs+1:cobs+obs,1) = a(i,1)*ones(obs,1);
cobs = cobs + obs;
end;
% UPDATE: sige
term1 = a'*Bp'*Bp*a + 2*d0;
chi = chis_rnd(1,m + 2*nu);
sige = term1/chi;
% UPDATE: vi (and form inV, b1)
e = z - x*bhat - tvec;
cobs = 0;
for i=1:m;
obs = mobs(i,1);
ee = e(cobs+1:cobs+obs,1)'*e(cobs+1:cobs+obs,1);
chi = chis_rnd(1,rval+obs);
vi(i,1) = (ee + rval)/chi;
inV(cobs+1:cobs+obs,1) = ones(obs,1)/vi(i,1);
b1(i,1) = sum(e0(cobs+1:cobs+obs,1)/vi(i,1));
cobs = cobs + obs;
end;
% UPDATE: rval (if necessary)
if mm ~= 0
rval = gamm_rnd(1,1,mm,kk);
end;
if (metflag == 1) | (inform_flag == 1)
% UPDATE: rho (using metropolis step)
% Construct new candidate rho value: rho2
% Using proposal distribution: N(rho,cc^2)
% truncated to the interval (rmin,rmax)
accept = 0;
while accept == 0
rho2 = rho + cc*randn(1,1);
if (rmin < rho2 & rho2 < rmax)
accept = 1;
end;
cntr = cntr+1; % counts acceptance rate
end;
%Form density ratio
rhox = c_semip(rho,a,sige,W,detval,a1,a2); %log density at rho
rhoy = c_semip(rho2,a,sige,W,detval,a1,a2); %log density at rho2
ratio = exp(rhoy-rhox);
%Make Metropolis comparison
if ratio > 1,
p = 1;
else,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -