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

📄 mess_g.m

📁 计量工具箱
💻 M
📖 第 1 页 / 共 2 页
字号:
                 alpha = alpha2;              end;          rtmp(iter,1) = alpha;           % evaulate the likelihood using current draws     if lflag == 0                like = -(n/2)*log(2*pi*sige) - (e'*e)/(2*sige);     end;                            % update rval     if mm ~= 0                rval = gamm_rnd(1,1,mm,kk);       end;                  if iter > nomit % if we are past burn-in, save the draws    bsave(iter-nomit,:) = bhat';    ssave(iter-nomit,1) = sige;    asave(iter-nomit,1) = alpha;      if lflag == 0       lsave = lsave + like;     else       lsave = lsave + 0;     end;    end;                        if iter == nomit % update cc based on initial draws         tst = 2*std(rtmp(1:nomit,1));         if tst > 0.1         cc = tst;         end;    end;iter = iter + 1; waitbar(iter/ndraw);         end; % end of sampling loopclose(hwait);stime = etime(clock,t0);% compute posterior meansif lflag == 0lmean = lsave/(ndraw-nomit);else    lmean = 0;end;amean = mean(asave);bmean = mean(bsave);astd = std(asave);bstd = std(bsave);smean = mean(ssave);% find acceptance rateresults.accept = 1 - cnta/(iter+cnta);% NOTE: this could be interpreted as the% probability that alpha is in the mesh grid% do the expensive calculation here% rather than lookuptmp = rho.^(0:neigh-1);tmp = tmp/sum(tmp);wy = y;Y = y(:,ones(1,q));for i=2:q;wy = wy(nnlist)*tmp';Y(:,i) = wy;end;[junk nq] = size(Y);nq1 = nq-1;v = ones(nq,1);for i=2:nq;v(i,1) = amean.^(i-1);end;W = (1./[1 cumprod(1:nq1)]);sy = Y*diag(W)*v;e = sy - x*bmean';yhat = y - e;sigu = e'*e;ym = y - mean(y);rsqr1 = sigu;rsqr2 = ym'*ym;rsqr = 1.0 - rsqr1/rsqr2; % r-squaredrsqr1 = rsqr1/(n-k);rsqr2 = rsqr2/(n-1.0);rbar = 1 - (rsqr1/rsqr2); % rbar-squaredtime = etime(clock,timet);results.meth  = 'mess_g';results.bdraw = bsave;results.adraw = asave;results.bmean = bmean';results.bstd  = bstd';results.amean = amean;results.astd  = astd;results.smean = smean;results.sdraw = ssave;results.rho   = rho;results.lmean = lmean;results.bprior = c;results.bpstd  = sqrt(diag(T));results.nobs  = n;results.nvar  = k;results.ndraw = ndraw;results.nomit = nomit;results.time  = time;results.stime = stime;results.ntime = gtime;results.nu = nu;results.d0 = d0;results.tflag = 'plevel';results.aflag = pflag;results.palpha = palpha;results.acov = S;results.y = y;results.yhat = yhat;results.resid = e;results.rsqr = rsqr;results.rbar = rbar;results.neigh = neigh;results.q     = q;results.nobs = n;results.nvar = k;results.xflag = xflag;results.nflag = nflag;case{1} % case of x-variables transformed      xone = x(:,1);   if all(xone == 1)      xsub = x(:,2:k);   else      xsub = x;   end;t1 = clock;   % time this operationtmp = rho.^(0:neigh-1);tmp = tmp/sum(tmp);% we have to construct the weight matrix using neighbors% find index into nearest neighborsif nflag == 0nnlist = find_nn(latt,long,neigh);elseif nflag == 1nnlist = find_nn2(latt,long,neigh);elseerror('mess_g: bad nflag option');end;% check for empty nnlist columnschk = find(nnlist == 0);if length(chk) > 0;  if nflag == 1 % no saving the user here error('mess_g: trying too many neighbors, some do not exist'); else % we save the user here nnlist = find_nn2(latt,long,neigh); end;end;% construct and save Sywy = y;Y = y(:,ones(1,q));for i=2:q;wy = wy(nnlist)*tmp';Y(:,i) = wy;end;% create and save Sx[junk nk] = size(xsub);xout = x;for i=1:nk;xi = xsub(:,i);tmpp = xi(nnlist)*tmp';xout = [xout tmpp];end;xmat = xout;% end of up front stuff with Sy saved gtime = etime(clock,t1);% ====== initializations% compute this stuff once to save time[junk kk] = size([x xsub]); % need to add diffuse priors                           % to the spatial lags of x-variablesTnew = eye(kk)*1e+12;Tnew(1:k,1:k) = T;TI = inv(Tnew);ctmp = zeros(kk,1);ctmp(1:k,1) = c;c = ctmp;TIc = TI*c;cc=0.2; % initial metropolis valuecntr = 0; iter = 1;alpha = astart;rho = 1;in = ones(n,1);sige = sig0;% storage for draws          bsave = zeros(ndraw-nomit,kk);          asave = zeros(ndraw-nomit,1);          ssave = zeros(ndraw-nomit,1);           lsave = 0;          rtmp = zeros(nomit,1);hwait = waitbar(0,'MCMC sampling ...');t0 = clock;                  iter = 1;          while (iter <= ndraw); % start sampling;          [junk nq] = size(Y);          nq1 = nq-1;          v = ones(nq,1);          for i=2:nq;          v(i,1) = alpha.^(i-1);          end;          W = (1./[1 cumprod(1:nq1)]);          Sy = Y*diag(W)*v;          % update beta             AI = inv(xmat'*xmat + sige*TI);                    b = xmat'*Sy + sige*TIc;          b0 = AI*b;          bhat = norm_rnd(sige*AI) + b0;                     % update sige          nu1 = n + 2*nu;           e = (Sy - xmat*bhat);          d1 = 2*d0 + e'*e;          chi = chis_rnd(1,nu1);          sige = d1/chi;          % metropolis step to get alpha update          if pflag == 0          alphax = c_mess(alpha,y,xmat,Y,bhat,sige);           elseif pflag == 1          alphax = c_mess(alpha,y,xmat,Y,bhat,sige,palpha,S);           end;                    accept = 0;          alpha2 = alpha + cc*randn(1,1);          while accept == 0           if alpha2 <=0            accept = 1;             else           alpha2 = alpha + cc*randn(1,1);           cntr = cntr+1; % counts accept rate           end;           end;            if pflag == 0           alphay = c_mess(alpha2,y,xmat,Y,bhat,sige);           elseif pflag == 1           alphay = c_mess(alpha2,y,xmat,Y,bhat,sige,palpha,S);           end;                    ru = unif_rnd(1,0,1);          if ((alphay - alphax) > exp(1)),          p = 1;          else,                    ratio = exp(alphay-alphax);          p = min(1,ratio);          end;              if (ru < p)                 alpha = alpha2;              end;          rtmp(iter,1) = alpha;           % evaulate the likelihood using current draws     if lflag == 0                like = -(n/2)*log(2*pi*sige) - (e'*e)/(2*sige);     end;                            % update rval     if mm ~= 0                rval = gamm_rnd(1,1,mm,kk);       end;                  if iter > nomit % if we are past burn-in, save the draws    bsave(iter-nomit,:) = bhat';    ssave(iter-nomit,1) = sige;    asave(iter-nomit,1) = alpha;     if lflag == 0       lsave = lsave + like;    else       lsave = lsave + 0;    end;            end;       if iter == nomit % update cc based on initial draws         tst = 2*std(rtmp(1:nomit,1));         if tst > 0.05         cc = tst;         end;    end;                 iter = iter + 1; waitbar(iter/ndraw);         end; % end of sampling loopclose(hwait);stime = etime(clock,t0);% compute posterior meansif lflag == 0lmean = lsave/(ndraw-nomit);else    lmean = 0;end;amean = mean(asave);bmean = mean(bsave);astd = std(asave);bstd = std(bsave);smean = mean(ssave);% find acceptance rateresults.accept = 1 - cntr/(iter+cntr);tmp = rho.^(0:neigh-1);tmp = tmp/sum(tmp);% construct Sy, Sx based on posterior meanswy = y;Y = y(:,ones(1,q));for i=2:q;wy = wy(nnlist)*tmp';Y(:,i) = wy;end;[junk nq] = size(Y);nq1 = nq-1;v = ones(nq,1);for i=2:nq;v(i,1) = amean.^(i-1);end;W = (1./[1 cumprod(1:nq1)]);sy = Y*diag(W)*v;% create  Sx based on posterior mean of rhoxmat = x;for i=1:nk;xi = xsub(:,i);tmpp = xi(nnlist)*tmp';xmat = [xmat tmpp];end;e = Sy - xmat*bmean';yhat = y - e;sigu = e'*e;ym = y - mean(y);rsqr1 = sigu;rsqr2 = ym'*ym;rsqr = 1.0 - rsqr1/rsqr2; % r-squaredrsqr1 = rsqr1/(n-k);rsqr2 = rsqr2/(n-1.0);rbar = 1 - (rsqr1/rsqr2); % rbar-squaredtime = etime(clock,timet);results.meth  = 'mess_g';results.bdraw = bsave;results.adraw = asave;results.bmean = bmean';results.bstd  = bstd';results.amean = amean;results.astd  = astd;results.smean = smean;results.sdraw = ssave;results.lmean = lmean;results.bprior = c;results.bpstd  = sqrt(diag(Tnew));results.nobs  = n;results.nvar  = k;results.ndraw = ndraw;results.nomit = nomit;results.time  = time;results.stime = stime;results.nu = nu;results.d0 = d0;results.tflag = 'plevel';results.aflag = aflag;results.palpha = palpha;results.acov = S;results.y = y;results.yhat = yhat;results.resid = e;results.rsqr = rsqr;results.rbar = rbar;results.rho   = rho;results.neigh = neigh;results.q     = q;results.ntime = gtime;results.nobs = n;results.nvar = k;results.xflag = xflag;otherwise   end; % end of switch   function cout = c_mess(alpha,ys,xs,Symat,beta,sige,a,B);% PURPOSE: evaluate the conditional distribution of alpha %          for the Bayesian mess_g model% ---------------------------------------------------%  USAGE: cout = c_mess2(alpha,y,x,Symat,beta,alpha,rho)%  where:  alpha  = matrix exponential alpha spatial parameter%            y    = dependent variable vector%            x    = explanatory variables matrix%          Symat  = matrix from mess_g %            beta = kx1 current bhat vector%            sige = current value of sige%               a = prior mean for alpha     (optional input)%               B = prior variance for alpha (optional input)% ---------------------------------------------------%  RETURNS: a conditional used in Metropolis-Hastings sampling%  NOTE: called only by mess_g%  --------------------------------------------------%  SEE ALSO: mess_g, mess_gd% ---------------------------------------------------% written by: James P. LeSage 1/2000% University of Toledo% Department of Economics% Toledo, OH 43606% jlesage@spatial-econometrics.comn = length(ys);[junk nq] = size(Symat);nq1 = nq-1;v = ones(nq,1);for i=2:nq;v(i,1) = alpha.^(i-1);end;W = (1./[1 cumprod(1:nq1)]);Sy = Symat*diag(W)*v;e = Sy - xs*beta;epe = e'*e;if nargin == 6cout =  -epe/(2*sige);elseif nargin == 8B = B*sige;cout = -epe/(2*sige) - 0.5*(((alpha-a)^2)/B);elseerror('c_mess: Wrong # of inputs arguments');end;

⌨️ 快捷键说明

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