def reweight_particles(self): wgts = self.wgts logG = self.fk.logG(self.t, self.X) new_wgts = wgts.add(logG) loglt = rs.log_mean_exp(new_wgts.lw) - rs.log_mean_exp(wgts.lw) self.loglt += [loglt] self.logLt += loglt self.wgts = new_wgts
def reset_weights(self): """Reset weights after a resampling step. """ if self.fk.isAPF: lw = (rs.log_mean_exp(self.logetat, W=self.W) - self.logetat[self.A]) self.wgts = rs.Weights(lw=lw) else: self.wgts = rs.Weights()
def compute_summaries(self): if self.t > 0: prec_log_mean_w = self.log_mean_w self.log_mean_w = rs.log_mean_exp(self.wgts.lw) if self.t==0 or self.rs_flag: self.loglt = self.log_mean_w else: self.loglt = self.log_mean_w - prec_log_mean_w self.logLt += self.loglt if self.verbose: print(self) if self.summaries: self.summaries.collect(self) if self.hist: self.hist.save(X=self.X, w=self.wgts, A=self.A)
def compute_summaries(self): if self.t > 0: prec_log_mean_w = self.log_mean_w self.log_mean_w = rs.log_mean_exp(self.wgts.lw) if self.t == 0 or self.rs_flag: self.loglt = self.log_mean_w else: self.loglt = self.log_mean_w - prec_log_mean_w self.logLt += self.loglt if self.verbose: print(self) if self.hist: self.hist.save(self) # must collect summaries *after* history, because a collector (e.g. # FixedLagSmoother) may needs to access history if self.summaries: self.summaries.collect(self)
def run(self, N=100): """ Parameter --------- N: int number of particles Returns ------- wgts: Weights object The importance weights (with attributes lw, W, and ESS) X: ThetaParticles object The N particles (with attributes theta, logpost) norm_cst: float Estimate of the normalising constant of the target """ th = self.proposal.rvs(size=N) self.X = ThetaParticles(theta=th, lpost=None) self.X.lpost = self.model.logpost(th) lw = self.X.lpost - self.proposal.logpdf(th) self.wgts = rs.Weights(lw=lw) self.norm_cst = rs.log_mean_exp(lw)
def logLT(self) -> float: """ returns the local logLT (for debugging) """ # noinspection PyTypeChecker return rs.log_mean_exp(self.logLTs_of_M1)
def logLT(self) -> float: """ Calculate the log of the normalizing constant of the model. Can be useful for debugging (i.e., by comparing its with the estimates returned by the SMCNew executor). """ return sum([rs.log_mean_exp(s) for s in self._logG])