def PMCMCstep(self, iteration): #progressbar(iteration / (self.nbiterations - 1)) progressbar(iteration / (self.nbiterations - 1), text = " acceptance rate: %.3f" % \ (sum(self.acceptations[0:iteration]) / iteration)) #print "iteration %i, acceptance rate until now: %.3f" % (iteration, sum(self.acceptations[0:iteration]) / iteration) if (random.uniform(size = 1) < self.proposalmixtureweights[0]): transformedthetastar = self.modeltheta.proposal(self.transformedtheta[:, newaxis], self.fixedproposalcovmatrix)[:, 0] else: transformedthetastar = self.modeltheta.proposal(self.transformedtheta[:, newaxis], self.adaptiveproposalcovmatrix)[:, 0] thetastar = self.modeltheta.untransform(transformedthetastar[:,newaxis])[:,0] proposedSIRs = ParallelSIRs(self.Nx, thetastar[:, newaxis], self.observations, self.modelx) proposedSIRs.first_step() proposedSIRs.next_steps() proposedloglike = proposedSIRs.getTotalLogLike()[0] proposedlogomega = proposedloglike + self.modeltheta.priorlogdensity(transformedthetastar) currentlogomega = self.totalLogLike + self.modeltheta.priorlogdensity(self.transformedtheta) acceptation = (log(random.uniform(size = 1)) < (proposedlogomega - currentlogomega)) # if not(acceptation): # print "\nproposed loglikelihood:", proposedloglike # print "\ncurrent loglikelihood:", self.totalLogLike # print "\naccept:", acceptation if acceptation: self.theta = thetastar.copy() self.transformedtheta = transformedthetastar.copy() self.totalLogLike = proposedloglike.copy() self.chain[:, iteration] = self.theta.copy() self.transformedchain[:, iteration] = self.transformedtheta.copy() if iteration > 10: self.adaptiveproposalcovmatrix = (5.6644 / self.thetadim) * cov(self.transformedchain[:, 1:iteration]) #print "\n", self.adaptiveproposalcovmatrix self.loglikechain[iteration] = self.totalLogLike self.acceptations[iteration] = acceptation
def __init__(self, model, algorithmparameters, autoinit = True): # get the model: self.modelx = model["modelx"] self.modeltheta = model["modeltheta"] self.observations = model["observations"] self.Nx = algorithmparameters["N"] self.nbiterations = algorithmparameters["nbiterations"] # self.theta = self.modeltheta.priorgenerator(1)[:,0] self.thetadim = self.theta.shape[0] self.transformedtheta = self.modeltheta.transform(self.theta[:, newaxis])[:,0] self.chain = zeros((self.thetadim, self.nbiterations)) self.transformedchain = zeros((self.thetadim, self.nbiterations)) self.chain[:, 0] = self.theta.copy() self.transformedchain[:, 0] = self.transformedtheta.copy() # self.acceptations = zeros(self.nbiterations) self.fixedproposalcovmatrix = diag(repeat(0.01 / self.thetadim, self.thetadim)) self.adaptiveproposalcovmatrix = diag(repeat(5.6644 / self.thetadim, self.thetadim)) self.proposalmixtureweights = [0.5, 0.5] currentSIRs = ParallelSIRs(self.Nx, self.theta[:, newaxis], self.observations, self.modelx) currentSIRs.first_step() currentSIRs.next_steps() self.totalLogLike = currentSIRs.getTotalLogLike()[0] self.loglikechain = zeros(self.nbiterations) self.loglikechain[0] = self.totalLogLike print "------------------" print "launching adaptive PMCMC, with algorithm parameters:" for key, element in algorithmparameters.items(): print key, ":", element print "------------------" if autoinit: self.allsteps()
def PMCMCstep(self, t): """ Perform a PMMH move step on each theta-particle. """ transformedthetastar = self.modeltheta.proposal(self.transformedthetaparticles, \ self.proposalcovmatrix, hyperparameters = self.modeltheta.hyperparameters,\ proposalmean = self.proposalmean, proposalkernel = self.AP["proposalkernel"]) thetastar = self.modeltheta.untransform(transformedthetastar) proposedSIRs = ParallelSIRs(self.Nx, thetastar, self.observations[0:(t + 1)], self.modelx) proposedSIRs.first_step() proposedSIRs.next_steps() proposedTotalLogLike = proposedSIRs.getTotalLogLike() acceptations = zeros(self.Ntheta) proposedpriordensityeval = apply_along_axis(func1d = self.modeltheta.priorlogdensity,\ arr = transformedthetastar, axis = 0) proposedlogomega = proposedTotalLogLike + proposedpriordensityeval currentlogomega = self.totalLogLike + self.priordensityeval # if proposal kernel == "randomwalk", then nothing else needs to be computed # since the RW is symmetric. If the proposal kernel is independent, then # the density of the multivariate gaussian used to generate the proposals # needs to be taken into account if self.AP["proposalkernel"] == "randomwalk": pass elif self.AP["proposalkernel"] == "independent": invSigma = linalg.inv(self.proposalcovmatrix) def multinorm_logpdf(x): centeredx = (x - self.proposalmean) return -0.5 * dot(dot(centeredx, invSigma), centeredx) proposaltermstar = apply_along_axis(func1d = multinorm_logpdf, \ arr = transformedthetastar, axis = 0) proposedlogomega -= proposaltermstar proposaltermcurr = apply_along_axis(func1d = multinorm_logpdf, \ arr = self.transformedthetaparticles, axis = 0) currentlogomega -= proposaltermcurr for i in range(self.Ntheta): acceptations[i] = (log(random.uniform(size=1)) < (proposedlogomega[i] - currentlogomega[i])) if acceptations[i]: self.transformedthetaparticles[:, i] = transformedthetastar[:, i].copy( ) self.thetaparticles[:, i] = thetastar[:, i].copy() self.totalLogLike[i] = proposedTotalLogLike[i].copy() self.priordensityeval[i] = proposedpriordensityeval[i].copy() self.xparticles[:, :, i] = proposedSIRs.xparticles[:, :, i].copy() acceptrate = sum(acceptations) / self.Ntheta self.acceptratios.append(acceptrate)
def PMCMCstep(self, t): """ Perform a PMMH move step on each theta-particle. """ #### INGMAR: This is where the proposal is computed transformedthetastar = self.modeltheta.proposal(self.transformedthetaparticles, \ self.proposalcovmatrix, hyperparameters = self.modeltheta.hyperparameters,\ proposalmean = self.proposalmean, proposalkernel = self.AP["proposalkernel"]) thetastar = self.modeltheta.untransform(transformedthetastar) ###### INGMAR: This is where the likelihood is estimated proposedSIRs = ParallelSIRs(self.Nx, thetastar, self.observations[0:(t+1)], self.modelx) proposedSIRs.first_step() proposedSIRs.next_steps() proposedTotalLogLike = proposedSIRs.getTotalLogLike() acceptations = zeros(self.Ntheta) proposedpriordensityeval = apply_along_axis(func1d = self.modeltheta.priorlogdensity,\ arr = transformedthetastar, axis = 0) proposedlogomega = proposedTotalLogLike + proposedpriordensityeval currentlogomega = self.totalLogLike + self.priordensityeval # if proposal kernel == "randomwalk", then nothing else needs to be computed # since the RW is symmetric. If the proposal kernel is independent, then # the density of the multivariate gaussian used to generate the proposals # needs to be taken into account if self.AP["proposalkernel"] == "randomwalk": pass elif self.AP["proposalkernel"] == "independent": invSigma = linalg.inv(self.proposalcovmatrix) def multinorm_logpdf(x): centeredx = (x - self.proposalmean) return -0.5 * dot(dot(centeredx, invSigma), centeredx) proposaltermstar = apply_along_axis(func1d = multinorm_logpdf, \ arr = transformedthetastar, axis = 0) proposedlogomega -= proposaltermstar proposaltermcurr = apply_along_axis(func1d = multinorm_logpdf, \ arr = self.transformedthetaparticles, axis = 0) currentlogomega -= proposaltermcurr for i in range(self.Ntheta): acceptations[i] = (log(random.uniform(size = 1)) < (proposedlogomega[i] - currentlogomega[i])) if acceptations[i]: self.transformedthetaparticles[:, i] = transformedthetastar[:, i].copy() self.thetaparticles[:, i] = thetastar[:, i].copy() self.totalLogLike[i] = proposedTotalLogLike[i].copy() self.priordensityeval[i] = proposedpriordensityeval[i].copy() self.xparticles[:, :, i] = proposedSIRs.xparticles[:, :, i].copy() acceptrate = sum(acceptations) / self.Ntheta self.acceptratios.append(acceptrate)
def increaseParticlesNb(self, t): """ Double the number of x-particles. """ print "increasing Nx: from %i to %i" % (self.Nx, 2 * self.Nx) self.Nx = 2 * self.Nx biggerSIRs = ParallelSIRs(self.Nx, self.thetaparticles, self.observations[0:(t+1),:], self.modelx) biggerSIRs.first_step() biggerSIRs.next_steps() biggerTotalLogLike = biggerSIRs.getTotalLogLike() self.thetalogweights[t, :] = self.thetalogweights[t, :] + biggerTotalLogLike - self.totalLogLike self.totalLogLike = biggerTotalLogLike.copy() self.xparticles = biggerSIRs.xparticles.copy() self.xweights = zeros_like(biggerSIRs.xweights) self.logxweights = zeros_like(biggerSIRs.xweights) self.Nxlist.append(self.Nx) self.increaseindices.append(t)