def filter_step(G, covY, pred, yt): """Filtering step of Kalman filter. Parameters ---------- G: (dy, dx) numpy array mean of Y_t | X_t is G * X_t covX: (dx, dx) numpy array covariance of Y_t | X_t pred: MeanAndCov object predictive distribution at time t Returns ------- pred: MeanAndCov object filtering distribution at time t logpyt: float log density of Y_t | Y_{0:t-1} """ # data prediction data_pred_mean = np.matmul(pred.mean, G.T) data_pred_cov = dotdot(G, pred.cov, G.T) + covY if covY.shape[0] == 1: logpyt = dists.Normal(loc=data_pred_mean, scale=np.sqrt(data_pred_cov)).logpdf(yt) else: logpyt = dists.MvNormal(loc=data_pred_mean, cov=data_pred_cov).logpdf(yt) # filter residual = yt - data_pred_mean gain = dotdotinv(pred.cov, G.T, data_pred_cov) filt_mean = pred.mean + np.matmul(residual, gain.T) filt_cov = pred.cov - dotdot(gain, G, pred.cov) return MeanAndCov(mean=filt_mean, cov=filt_cov), logpyt
def PY(self, t, xp, x): if self.num_yields == 1: loc = -self.Atau + self.Btau * x scale = self.H if self.emotion_start_t <= t < self.emotion_start_t + self.l: loc *= self.nc return t(df=self.df, loc=loc, scale=scale) else: loc = -self.Atau + self.Btau * x cov = np.eye(self.num_yields) * self.H if self.emotion_start_t <= t < self.emotion_start_t + self.l: loc *= self.nc return dists.MvNormal(loc=loc, cov=cov)
def PY(self, t, xp, x): # print(f"PY is called, shape of x {x}, {x.shape}") if self.num_yields == 1: mu = -self.Atau + self.Btau * x scale = np.sqrt(self.H) return dists.Normal(mu, scale) else: # In particles\core.py\SMC reweight_particles call the fk model's logG and therefore PY here is called by # the Bootstrap filter. In this process, x is inputed as a list, containing all particles in one time step. # So the normal broadcast mechanism doen not work as desire. mu = [-self.Atau + self.Btau * xi for xi in x] cov = np.eye(self.num_yields) * self.H # cov = np.diag(np.asanyarray(self.H)) # cov = np.zeros((self.num_yields, self.num_yields)) # cov[:self.num_yields].flat[0::self.num_yields+1] = self.H return dists.MvNormal(loc=mu, cov=cov)
def filter_step(G, covY, pred_mean, pred_cov, yt): """Filtering step. Note ---- filt_mean may either be of shape (dx,) or (N, dx); in the latter case N predictive steps are performed in parallel. """ # data prediction data_pred_mean = np.matmul(pred_mean, G.T) data_pred_cov = dotdot(G, pred_cov, G.T) + covY if covY.shape[0] == 1: logpyt = dists.Normal(loc=data_pred_mean, scale=np.sqrt(data_pred_cov)).logpdf(yt) else: logpyt = dists.MvNormal(loc=data_pred_mean, cov=data_pred_cov).logpdf(yt) # filter residual = yt - data_pred_mean gain = dotdot(pred_cov, G.T, inv(data_pred_cov)) filt_mean = pred_mean + np.matmul(residual, gain.T) filt_cov = pred_cov - dotdot(gain, G, pred_cov) return filt_mean, filt_cov, logpyt
def proposal(self, t, xp, data): fm, fc, _ = self.kf.posterior_t(xp, data[t]) return dists.MvNormal(loc=fm, cov=fc)
def PY(self, t, xp, x): return dists.MvNormal(loc=np.dot(x, self.G.T), cov=self.covY)
def proposal0(self, data): pred0 = MeanAndCov(mean=self.mu0, cov=self.cov0) f, _ = filter_step(self.G, self.covY, pred0, data[0]) return dists.MvNormal(loc=f.mean, cov=f.cov)
def proposal(self, t, xp, data): pred = MeanAndCov(mean=np.matmul(xp, self.F.T), cov=self.covX) f, _ = filter_step_asarray(self.G, self.covY, pred, data[t]) return dists.MvNormal(loc=f.mean, cov=f.cov)
def PX(self, t, xp): return dists.MvNormal(loc=np.dot(xp, self.F.T) + self.offset(), cov=self.covX)
def PX0(self): # Distribution of X_0 return dists.MvNormal(loc=self.mu, cov=self.sigma, scale=1)
def PX(self, t, xp): return dists.MvNormal(loc=self.predmean(xp), cov=self.SigX)
def proposal(self, t, xp, data): filt, _ = self.approx_post(xp, data[t]) return dists.MvNormal(loc=filt.mean, cov=filt.cov)
def proposal(self, t, xp, data): loc, cov, _ = self.approx_post(xp, data[t]) return dists.MvNormal(loc=loc, cov=cov)
def proposal0(self, data): fm, fc, _ = self.kf.posterior_0(data[0]) return dists.MvNormal(loc=fm, cov=fc)
def PX(self, t, xp): # Distribution of X_t given X_{t-1}=xp (p=past) loc = np.array([xp[0, 0], xp[0, 1], self.a(xp)]).T #return loc return dists.MvNormal(loc=loc, cov=self.sigma, scale=1e-9)
def PX0(self): return dists.MvNormal(loc=self.mu, cov=self.corX)
if dataset_name == 'sonar': N = 10**4 Ms = [10, 20, 30, 40, 50, 60] typM = 50 elif dataset_name == 'pima': N = 10**3 Ms = [1, 3, 5] typM = 3 elif dataset_name == 'eeg': N = 10**3 Ms = [1, 3, 5, 7, 10, 15, 20] typM = 5 # prior & model prior = dists.StructDist({'beta': dists.MvNormal(scale=5., cov=np.eye(p))}) class LogisticRegression(smc_samplers.StaticModel): def logpyt(self, theta, t): # log-likelihood factor t, for given theta lin = np.matmul(theta['beta'], data[t, :]) return -np.logaddexp(0., -lin) # algorithms # N and values of M set above according to dataset ESSrmin = 0.5 nruns = 16 results = []
def PY(self, t, xp, x): return dists.MvNormal(scale=np.exp(0.5 * x), cov=self.corY)
dep_prior_dict['rho'] = dists.Uniform(a=0., b=1.) dep_prior_dict['sigma'] = dists.Cond( lambda theta: dists.Gamma(b=1. / theta['rho'])) dep_prior = dists.StructDist(dep_prior_dict) dep_theta = dep_prior.rvs(size=2000) plt.figure() plt.scatter(dep_theta['rho'], dep_theta['sigma']) plt.axis([0., 1., 0., 8.]) plt.xlabel('rho') plt.ylabel('sigma') reg_prior_dict = OrderedDict() reg_prior_dict['sigma2'] = dists.InvGamma(a=2., b=3.) reg_prior_dict['beta'] = dists.MvNormal(cov=np.eye(20)) reg_prior = dists.StructDist(reg_prior_dict) reg_theta = reg_prior.rvs(size=200) from particles import state_space_models as ssm class StochVol(ssm.StateSpaceModel): default_parameters = {'mu': -1., 'rho': 0.95, 'sigma': 0.2} def PX0(self): # Distribution of X_0 return dists.Normal(loc=self.mu, scale=self.sigma / np.sqrt(1. - self.rho**2)) def PX(self, t, xp): # Distribution of X_t given X_{t-1}=xp (p=past) return dists.Normal(loc=self.mu + self.rho * (xp - self.mu),