示例#1
0
    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
示例#2
0
 def next_steps(self):
     """
     Perform all the iterations until time T == number of observations.
     """
     for t in range(0, self.T):
         #print "time %i" % t
         progressbar(t / (self.T - 1))
         last_tic = time.time()
         TandWresults = self.modelx.transitionAndWeight(self.xparticles[newaxis, ...], \
                 self.observations[t], self.thetaparticles, t + 1)
         self.xparticles[...] = TandWresults["states"][0, ...]
         if self.AP["prediction"]:
             self.prediction(t)
         self.logxweights[:] = TandWresults["weights"][0, :]
         self.logxweights[isnan(self.logxweights)] = -(10**150)
         self.logxweights[isinf(self.logxweights)] = -(10**150)
         self.constants[t] = numpymax(self.logxweights)
         self.logxweights[:] -= self.constants[t]
         self.xweights[:] = exp(self.logxweights)
         self.evidences[t] = exp(self.constants[t]) * mean(self.xweights)
         covmean = self.computeCovarianceAndMean()
         m = (self.shrink) * self.transformedthetaparticles + \
             (1 - self.shrink) * transpose(covmean["mean"][newaxis])
         noise = transpose(random.multivariate_normal(repeat(0, self.modeltheta.parameterdimension), \
             self.hsq * covmean["cov"], size = self.N))
         self.transformedthetaparticles[...] = m + noise
         self.thetaparticles[...] = self.modeltheta.untransform(self.transformedthetaparticles)
         self.resample()
         self.resamplingindices.append(t)
         new_tic = time.time()
         self.computingtimes[t] = new_tic - last_tic
         last_tic = new_tic
         if t in self.savingtimes or t == self.T - 1:
             print "\nsaving particles at time %i" % t
             self.thetahistory[self.alreadystored, ...] = self.thetaparticles.copy()
             #self.weighthistory[self.alreadystored, ...] = self.xweights.copy()
             self.alreadystored += 1
示例#3
0
 def next_steps(self):
     """
     Perform all the iterations until time T == number of observations.
     """
     for t in range(self.T):
         excluded = t in self.excludedobservations
         progressbar(t / (self.T - 1))
         if excluded:
             print "\nobservations", self.observations[
                 t, :], "set to be excluded"
         last_tic = time.time()
         TandWresults = self.modelx.transitionAndWeight(self.xparticles, \
                 self.observations[t], self.thetaparticles, t + 1)
         self.xparticles[...] = TandWresults["states"]
         if not (excluded):
             self.logxweights[...] = TandWresults["weights"]
             # in case the measure function returns nans or infs, set the weigths very low
             self.logxweights[isnan(self.logxweights)] = -(10**150)
             self.logxweights[isinf(self.logxweights)] = -(10**150)
             self.constants[:] = numpymax(self.logxweights, axis=0)
             self.logxweights[...] -= self.constants[:]
         else:
             self.logxweights = zeros((self.Nx, self.Ntheta))
             self.constants[:] = numpymax(self.logxweights, axis=0)
         self.xweights[...] = exp(self.logxweights)
         self.logLike[:] = log(mean(self.xweights,
                                    axis=0)) + self.constants[:]
         # prediction: at this point we have the transitioned x-particles and we didn't update
         # the weights of the theta-particles, and the x-particles are not weighted
         if self.AP["prediction"]:
             self.prediction(t)
         if t > 0:
             self.evidences[t] = self.getEvidence(
                 self.thetalogweights[t - 1, :], self.logLike)
             self.totalLogLike[:] += self.logLike[:]
             self.thetalogweights[
                 t, :] = self.thetalogweights[t - 1, :] + self.logLike[:]
         else:
             self.evidences[t] = self.getEvidence(
                 self.thetalogweights[t, :], self.logLike)
             self.totalLogLike[:] += self.logLike[:]
             self.thetalogweights[
                 t, :] = self.thetalogweights[t, :] + self.logLike[:]
         self.thetalogweights[t, :] -= max(self.thetalogweights[t, :])
         self.xresample()
         self.ESS[t] = ESSfunction(exp(self.thetalogweights[t, :]))
         if self.AP["dynamicNx"]:
             progressbar(t / (self.T - 1),
                         text=" ESS: %.3f, Nx: %i" % (self.ESS[t], self.Nx))
         else:
             progressbar(t / (self.T - 1), text=" ESS: %.3f" % self.ESS[t])
         while self.ESS[t] < (self.AP["ESSthreshold"] * self.Ntheta):
             progressbar(t / (self.T - 1), text =\
                     " ESS: %.3f - resample move step at iteration = %i" % (self.ESS[t], t))
             covdict = self.computeCovarianceAndMean(t)
             if self.AP["proposalkernel"] == "randomwalk":
                 self.proposalcovmatrix = self.AP["rwvariance"] * covdict[
                     "cov"]
                 self.proposalmean = None
             elif self.AP["proposalkernel"] == "independent":
                 self.proposalcovmatrix = covdict["cov"]
                 self.proposalmean = covdict["mean"]
             self.thetaresample(t)
             self.resamplingindices.append(t)
             self.ESS[t] = ESSfunction(exp(self.thetalogweights[t, :]))
             for move in range(self.AP["nbmoves"]):
                 self.PMCMCstep(t)
                 acceptrate = self.acceptratios[-1]
                 progressbar(t / (self.T - 1), text = \
                         " \nresample move step at iteration = %i - acceptance rate: %.3f\n" % (t, acceptrate))
                 if self.acceptratios[-1] < self.AP["dynamicNxThreshold"] \
                         and self.Nx <= (self.AP["NxLimit"] / 2) \
                         and self.AP["dynamicNx"]:
                     self.increaseParticlesNb(t)
                     self.ESS[t] = ESSfunction(
                         exp(self.thetalogweights[t, :]))
         new_tic = time.time()
         self.computingtimes[t] = new_tic - last_tic
         last_tic = new_tic
         """ filtering and smoothing """
         if self.AP["filtering"]:
             self.filtering(t)
         if self.smoothingEnable and t == self.T - 1:
             self.smoothing(t)
         if t in self.savingtimes or t == self.T - 1:
             print "\nsaving particles at time %i" % t
             self.thetahistory[self.alreadystored,
                               ...] = self.thetaparticles.copy()
             self.weighthistory[self.alreadystored,
                                ...] = exp(self.thetalogweights[t, :])
             self.alreadystored += 1
示例#4
0
    def next_steps(self):
        """
        Perform all the iterations until time T == number of observations.
        """
        for t in range(self.T):
            excluded = t in self.excludedobservations
            progressbar(t / (self.T - 1))
            if excluded:
                print "\nobservations", self.observations[t,:], "set to be excluded"
            last_tic = time.time() 
            TandWresults = self.modelx.transitionAndWeight(self.xparticles, \
                    self.observations[t], self.thetaparticles, t) # t + 1)  
            #if states are not time series as in the case of Kangaroo, 
            #then the 0th initial state scorrespond to no observation, so I dummied an initial time 
            self.xparticles[...] = TandWresults["states"] 
            if not(excluded):
                self.logxweights[...] = TandWresults["weights"]
                # in case the measure function returns nans or infs, set the weigths very low
                self.logxweights[isnan(self.logxweights)] = -(10**150)
                self.logxweights[isinf(self.logxweights)] = -(10**150)
                self.constants[:] = numpymax(self.logxweights, axis = 0)
                self.logxweights[...] -= self.constants[:]
            else:
                self.logxweights = zeros((self.Nx, self.Ntheta))
                self.constants[:] = numpymax(self.logxweights, axis = 0)
            self.xweights[...] = exp(self.logxweights)
            if not(self.modelx.HScoreInference): #if use log score for inference
                self.logLike[:] = log(mean(self.xweights, axis = 0)) + self.constants[:] 
            else:
                self.logLike[:] =  - TandWresults["simpleHScore"]
            # prediction: at this point we have the transitioned x-particles and we didn't update
            # the weights of the theta-particles, and the x-particles are not weighted (should be "resampled")
            if self.AP["prediction"]:
                self.prediction(t)
            if t > 0:
                self.evidences[t] = self.getEvidence(self.thetalogweights[t-1, :], self.logLike)
                self.totalLogLike[:] += self.logLike[:]
                self.thetalogweights[t, :] = self.thetalogweights[t-1, :] + self.logLike[:]
            else:
                self.evidences[t] = self.getEvidence(self.thetalogweights[t, :], self.logLike)
                self.totalLogLike[:] += self.logLike[:]
                self.thetalogweights[t, :] = self.thetalogweights[t, :] + self.logLike[:]
            self.lScore[t] = -log(self.evidences[t])

            #store h score
            #self.hScore[t] = self.getHScore(t)  
            if self.modelx.continuousObs: #use latest theta particles 
                hScoreResults = self.modelx.computeHscore(self.xparticles, self.observations[t], self.thetaparticles, \
                exp(self.logxweights), exp(self.thetalogweights[t, :]), t)
            else: #use theta particles from the last step 
                hScoreResults = self.modelx.computeHscore(self.xparticles, self.observations[t], self.thetaparticles_last, \
                exp(self.logxweights_last), exp(self.thetalogweights_last), t)
            self.hScore[t] = hScoreResults["compositeHScore"] 

            self.thetalogweights[t, :] -= max(self.thetalogweights[t, :])
            self.xresample() #resample is to get ancester index, to prepare for the following state transition
            #the above resample step contains the line: self.xparticles[...] = self.xparticles[parentsindices, :]
            #therefore, for the filtering distribution at time t, we should use the xparticles before this step 
            self.ESS[t] = ESSfunction(exp(self.thetalogweights[t, :]))
            if self.AP["dynamicNx"]:
                progressbar(t / (self.T - 1), text = " \n effective sample size: %.3f, Nx: %i" % (self.ESS[t], self.Nx))
            else:
                progressbar(t / (self.T - 1), text = " \n effective sample size: %.3f" % self.ESS[t])
            while self.ESS[t] < (self.AP["ESSthreshold"] * self.Ntheta):
                progressbar(t / (self.T - 1), text =\
                        " \n effective sample size: %.3f" % (self.ESS[t])) #- resample move step at iteration = %i" % (self.ESS[t], t))
                covdict = self.computeCovarianceAndMean(t)
                if self.AP["proposalkernel"] == "randomwalk":
                    self.proposalcovmatrix = self.AP["rwvariance"] * covdict["cov"]
                    self.proposalmean = None
                elif self.AP["proposalkernel"] == "independent":
                    self.proposalcovmatrix = covdict["cov"]
                    self.proposalmean = covdict["mean"]
                self.thetaresample(t)
                self.resamplingindices.append(t)
                self.ESS[t] = ESSfunction(exp(self.thetalogweights[t, :]))
                for move in range(self.AP["nbmoves"]):
                    self.PMCMCstep(t)
                    acceptrate = self.acceptratios[-1]
                    progressbar(t / (self.T - 1), text = \
                            " \nresample move step at iteration = %i - acceptance rate: %.3f\n" % (t, acceptrate))
                    if self.acceptratios[-1] < self.AP["dynamicNxThreshold"] \
                            and self.Nx <= (self.AP["NxLimit"] / 2) \
                            and self.AP["dynamicNx"]:
                        self.increaseParticlesNb(t) 
                        self.ESS[t] = ESSfunction(exp(self.thetalogweights[t, :]))
            #update below the quantities for computing H score of discrete-obs. 
            #put them here after potential PMCMC steps 
            self.thetaparticles_last = self.thetaparticles 
            self.logxweights_last = self.logxweights
            self.thetalogweights_last = self.thetalogweights[t,:]
                        
            new_tic = time.time()
            self.computingtimes[t] = new_tic - last_tic
            last_tic = new_tic
            """ filtering and smoothing """
            if self.AP["filtering"]:
                self.filtering(t)
            if self.smoothingEnable and t == self.T - 1:
                self.smoothing(t)
            if t in self.savingtimes or t == self.T - 1:
                print "\nsaving particles at time %i" % t
                self.thetahistory[self.alreadystored, ...] = self.thetaparticles.copy()
                self.weighthistory[self.alreadystored, ...] = exp(self.thetalogweights[t, :])
                self.alreadystored += 1
示例#5
0
 def next_steps(self):
     """
     Perform all the iterations until time T == number of observations.
     """
     for t in range(self.T):
         excluded = t in self.excludedobservations
         progressbar(t / (self.T - 1))
         if excluded:
             print "\nobservations", self.observations[t,:], "set to be excluded"
         last_tic = time.time()
         TandWresults = self.modelx.transitionAndWeight(self.xparticles, \
                 self.observations[t], self.thetaparticles, t + 1)
         self.xparticles[...] = TandWresults["states"]
         if not(excluded):
             self.logxweights[...] = TandWresults["weights"]
             # in case the measure function returns nans or infs, set the weigths very low
             self.logxweights[isnan(self.logxweights)] = -(10**150)
             self.logxweights[isinf(self.logxweights)] = -(10**150)
             self.constants[:] = numpymax(self.logxweights, axis = 0)
             self.logxweights[...] -= self.constants[:]
         else:
             self.logxweights = zeros((self.Nx, self.Ntheta))
             self.constants[:] = numpymax(self.logxweights, axis = 0)
         self.xweights[...] = exp(self.logxweights)
         self.logLike[:] = log(mean(self.xweights, axis = 0)) + self.constants[:]
         # prediction: at this point we have the transitioned x-particles and we didn't update
         # the weights of the theta-particles, and the x-particles are not weighted
         if self.AP["prediction"]:
             self.prediction(t)
         if t > 0:
             self.evidences[t] = self.getEvidence(self.thetalogweights[t-1, :], self.logLike)
             self.totalLogLike[:] += self.logLike[:]
             self.thetalogweights[t, :] = self.thetalogweights[t-1, :] + self.logLike[:]
         else:
             self.evidences[t] = self.getEvidence(self.thetalogweights[t, :], self.logLike)
             self.totalLogLike[:] += self.logLike[:]
             self.thetalogweights[t, :] = self.thetalogweights[t, :] + self.logLike[:]
         self.thetalogweights[t, :] -= max(self.thetalogweights[t, :])
         self.xresample()
         self.ESS[t] = ESSfunction(exp(self.thetalogweights[t, :]))
         if self.AP["dynamicNx"]:
             progressbar(t / (self.T - 1), text = " ESS: %.3f, Nx: %i" % (self.ESS[t], self.Nx))
         else:
             progressbar(t / (self.T - 1), text = " ESS: %.3f" % self.ESS[t])
         while self.ESS[t] < (self.AP["ESSthreshold"] * self.Ntheta):
             progressbar(t / (self.T - 1), text =\
                     " ESS: %.3f - resample move step at iteration = %i" % (self.ESS[t], t))
             covdict = self.computeCovarianceAndMean(t)
             if self.AP["proposalkernel"] == "randomwalk":
                 self.proposalcovmatrix = self.AP["rwvariance"] * covdict["cov"]
                 self.proposalmean = None
             elif self.AP["proposalkernel"] == "independent":
                 self.proposalcovmatrix = covdict["cov"]
                 self.proposalmean = covdict["mean"]
             self.thetaresample(t)
             self.resamplingindices.append(t)
             self.ESS[t] = ESSfunction(exp(self.thetalogweights[t, :]))
             for move in range(self.AP["nbmoves"]):
                 self.PMCMCstep(t)
                 acceptrate = self.acceptratios[-1]
                 progressbar(t / (self.T - 1), text = \
                         " \nresample move step at iteration = %i - acceptance rate: %.3f\n" % (t, acceptrate))
                 if self.acceptratios[-1] < self.AP["dynamicNxThreshold"] \
                         and self.Nx <= (self.AP["NxLimit"] / 2) \
                         and self.AP["dynamicNx"]:
                     self.increaseParticlesNb(t)
                     self.ESS[t] = ESSfunction(exp(self.thetalogweights[t, :]))
         new_tic = time.time()
         self.computingtimes[t] = new_tic - last_tic
         last_tic = new_tic
         """ filtering and smoothing """
         if self.AP["filtering"]:
             self.filtering(t)
         if self.smoothingEnable and t == self.T - 1:
             self.smoothing(t)
         if t in self.savingtimes or t == self.T - 1:
             print "\nsaving particles at time %i" % t
             self.thetahistory[self.alreadystored, ...] = self.thetaparticles.copy()
             self.weighthistory[self.alreadystored, ...] = exp(self.thetalogweights[t, :])
             self.alreadystored += 1