📄 mess_g1.m
字号:
elseif nflag == 1nnlistall = find_nn(latt,long,mmax,3);elseerror('mess_g1: bad nflag option');end;% check for empty nnlist columnschk = find(nnlistall == 0);if length(chk) > 0; if nflag == 1 % no saving the user here error('mess_g3: trying too many neighbors, some do not exist'); else % we save the user here nnlistall = find_nn(latt,long,mmax,3); end;end;for jj=1:nneigh;neigh = mgrid(jj);nnlist = nnlistall(:,1:neigh);tmp = rho.^(0:neigh-1);tmp = tmp/sum(tmp);% construct and save Sywy = y;Y = y(:,ones(1,q));for i=2:q;wy = wy(nnlist)*tmp';Y(:,i) = wy;end;% save YSymat(:,:,jj) = Y;% save alphammat(jj,1) = neigh;end; % end of loop over neighbor values % end of up front stuff with Sy saved in Symatgtime = etime(clock,t1);% initializations and starting values for the samplerrho = 1;alpha = astart;cc=0.2; % initial metropolis valuecnta = 0; % counter for acceptance rate for alphacntr = 0; % counter for acceptance rate for rhoiter = 1;in = ones(n,1);sige = sig0;% storage for draws bsave = zeros(ndraw-nomit,k); asave = zeros(ndraw-nomit,1); ssave = zeros(ndraw-nomit,1); msave = zeros(ndraw-nomit,1); lsave = 0; rtmp = zeros(nomit,1);hwait = waitbar(0,'MCMC sampling ...');t0 = clock; iter = 1; while (iter <= ndraw); % start sampling; % update beta AI = inv(x'*x + sige*TI); % lookup Sy based on neigh values gsize = mmat(2,1) - mmat(1,1); i1 = find(mmat <= rho + gsize); i2 = find(mmat <= rho - gsize); i1 = max(i1); i2 = max(i2); indexm = round((i1+i2)/2); if isempty(indexm) indexm = 1; end; Ycap = squeeze(Symat(:,:,indexm)); % create Sy based on Y [junk nq] = size(Ycap); 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 = Ycap*diag(W)*v; b = x'*Sy + sige*TIc; b0 = AI*b; bhat = norm_rnd(sige*AI) + b0; % update sige nu1 = n + 2*nu; e = (Sy - x*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_mess1(alpha,y,x,Symat,bhat,sige,neigh,mmat); elseif pflag == 1 alphax = c_mess1(alpha,y,x,Symat,bhat,sige,neigh,mmat,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); cnta = cnta+1; % counts accept rate for alpha end; end; if pflag == 0 alphay = c_mess1(alpha2,y,x,Symat,bhat,sige,neigh,mmat); elseif pflag == 1 alphay = c_mess1(alpha2,y,x,Symat,bhat,sige,neigh,mmat,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;% update neigh using metroplis-hastings step neighx = m_mess1(neigh,y,x,Symat,bhat,alpha,mmat); accept = 0; neigh2 = round(unif_rnd(1,mmin,mmax)); neighy = m_mess1(neigh2,y,x,Symat,bhat,alpha,mmat); ru = unif_rnd(1,0,1); if ((neighy - neighx) > exp(1)), p = 1; else, ratio = exp(neighy-neighx); p = min(1,ratio); end; if (ru < p) neigh = neigh2; end; % 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; msave(iter-nomit,1) = neigh; 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);mmean = mean(msave);mstd = std(msave);% 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 lookupmround = round(mmean);tmp = rho.^(0:mround-1);tmp = tmp/sum(tmp);nnlist = nnlistall(:,1:mround);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_g1';results.bdraw = bsave;results.mdraw = msave;results.adraw = asave;results.bmean = bmean';results.bstd = bstd';results.amean = amean;results.astd = astd;results.smean = smean;results.sdraw = ssave;results.mmean = mmean;results.mstd = mstd;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.rho = rho;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;% find index into nearest neighborsif nflag == 0nnlistall = find_nn(latt,long,mmax);elseif nflag == 1nnlistall = find_nn(latt,long,mmax,3);elseerror('mess_g1: bad nflag option');end;% check for empty nnlist columnschk = find(nnlistall == 0);if length(chk) > 0; if nflag == 1 % no saving the user here error('mess_g3: trying too many neighbors, some do not exist'); else % we save the user here nnlistall = find_nn(latt,long,mmax,4); end;end;% ========= do up front grid over rho, alpha valuesresults.mmin = mmin;results.mmax = mmax;mgrid = mmin:1:mmax;nneigh = length(mgrid);t1 = clock; % time this operation% storage for Sy over the gridSymat = zeros(n,q,nneigh); % vectors of Sy for various alpha,rho valuesSxmat = zeros(n,2*k-1,nneigh); % matrices of Sx for various alpha,rho valuesmmat = zeros(nneigh,1); % save rho valuesfor jj=1:nneigh;neigh = mgrid(jj);nnlist = nnlistall(:,1:neigh);tmp = rho.^(0:neigh-1);tmp = tmp/sum(tmp);% construct and save Sywy = y;Y = y(:,ones(1,q));for i=2:q;wy = wy(nnlist)*tmp';Y(:,i) = wy;end;% save SySymat(:,:,jj) = Y;% 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;Sxmat(:,:,jj) = xout;% save alpha and neighmmat(jj,1) = neigh;end; % end of loop over neigh values % end of up front stuff with Sy saved in Symatgtime = 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);tmp = zeros(kk,1);tmp(1:k,1) = c;c = tmp;TIc = TI*c;cc=0.2; % initial metropolis valuecntr = 0; iter = 1;alpha = astart;rho = 1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -