#Metropolis-Hastings on the space of step functions f.log.lq <- function(t1,t2,t3,g,h1,h2,y,tobs) { ## Purpose: Computation of log likelihood ratio in the model ## y[i]=mean(tobs[i]) + standard normal errors where mean is ## one of two step functions which are equal outside (t1,t3]. ## Either the mean is =g on (t1,t3] or =h1 on (t1,t2] and =h2 on (t2,t3]. ## ------------------------------------------------------------------------- ## Arguments: as described above ## ------------------------------------------------------------------------- ## Author: Hans-Ruedi Kuensch, Date: 31 Jan 2002, 10:25 n <- length(tobs) erg <- 0.0 ind <- (t1 1) plot(stepfun(tt[2:k],g),xlim=c(0,1),add=T) else abline(h=g) #add a jump j <- sample((1:k),1,prob=(tt[-1]-tt[-(k+1)])) #select interval r <- tt[j] + runif(1)*(tt[j+1]-tt[j]) #propose new jump time h1 <- tau*rnorm(1) #propose new heights h2 <- tau*rnorm(1) tr <- c(tt[1:j],r,tt[(j+1):(k+1)]) #add new jump to the function if (j==k) h <- c(g,h2) else h <- c(g[1:j],h2,g[(j+1):k]) h[j] <- h1 plot(stepfun(tr[2:(k+1)],h),xlim=c(0,1),add=T) #plot proposed function #compute acceptance probability #lambda*k/(k+1) #if no data acc <- lambda*k*exp(f.log.lq(tt[j],r,tt[j+1],g[j],h1,h2,y,tobs))/(k+1) #with data acc if (runif(1)2 #abline(h=h[1]) #plot proposed function if k=2 #compute acceptance probability #k/(lambda*(k-1)) #if no data acc <- exp(-f.log.lq(tt[j],tt[j+1],tt[j+2],h1,g[j],g[j+1],y,tobs))* k/(lambda*(k-1)) if (runif(1) < acc) { tt <- tr g <- h k <- k-1 } # Many steps m.est <- matrix(0,nrow=1000,ncol=n) #rows = estimated function at tobs, #for every 10th replicate. k.est <- rep(0,10000) #number of jumps of estimated function for (i in (1:10000)) { erg <- f.jump.metr(erg$tt,erg$g,y,tobs,lambda,tau) k.est[i] <- length(erg$g)-1 if ((i %% 10) == 0){ anz <- hist(tobs,breaks=erg$tt,plot=F)$counts m.est[i/10,] = rep(erg$g,anz) } } plot(1:10000,k.est,type="l") sum(abs(k.est[-1]-k.est[-10000])>0.2) #number of accepted proposals plot(tobs,y) #plotting the estimated functions and their quantiles lines(tobs,rep(y.m,y.t)) for (i in (1:100)) lines(tobs,m.est[10*i,],col=2) lines(tobs,apply(m.est,2,mean),col=3) lines(tobs,apply(m.est,2,quantile,probs=0.1),col=2) lines(tobs,apply(m.est,2,quantile,probs=0.5),col=3) lines(tobs,apply(m.est,2,quantile,probs=0.9),col=4) #Fuer Zuekost-Vortrag ps.latex("zuekost-5-1.eps",height=8) plot(tobs,y,xlab="x") ps.end() ps.latex("zuekost-5-2.eps",height=8) plot(1:10000,k.est,type="l",xlab="iteration",ylab="k") ps.end() ps.latex("zuekost-5-3.eps",height=8) plot(tobs,y,xlab="x") lines(tobs,rep(y.m,y.t)) for (i in (1:100)) lines(tobs,m.est[10*i,],col=2) ps.end() ps.latex("zuekost-5-4.eps",height=8) plot(tobs,y,xlab="x") lines(tobs,rep(y.m,y.t)) lines(tobs,apply(m.est,2,mean),col=3) lines(tobs,apply(m.est,2,quantile,probs=0.1),col=2) lines(tobs,apply(m.est,2,quantile,probs=0.5),col=3) lines(tobs,apply(m.est,2,quantile,probs=0.9),col=4) lines(tobs,rep(y.m,y.t)) ps.end()