def test_ParameterInDistribution(self): shape = 10, 100 a = 1e-2 * torch.ones((shape[0], 1)) dt = 1e-2 dist = Normal(loc=0., scale=Parameter(Exponential(10.))) init = Normal(a, 1.) sde = AffineEulerMaruyama((f_sde, g_sde), (a, 0.15), init, dist, dt=dt, num_steps=10) sde.sample_params(shape) # ===== Initialize ===== # x = sde.i_sample(shape) # ===== Propagate ===== # num = 1000 samps = [x] for t in range(num): samps.append(sde.propagate(samps[-1])) samps = torch.stack(samps) self.assertEqual(samps.size(), torch.Size([num + 1, *shape])) # ===== Sample path ===== # path = sde.sample_path(num + 1, shape) self.assertEqual(samps.shape, path.shape)
def test_SDE(self): shape = 1000, 100 a = 1e-2 * torch.ones((shape[0], 1)) dt = 0.1 norm = Normal(0., math.sqrt(dt)) init = Normal(a, 1.) sde = AffineEulerMaruyama((f_sde, g_sde), (a, 0.15), init, norm, dt=dt, num_steps=10) # ===== Initialize ===== # x = sde.i_sample(shape) # ===== Propagate ===== # num = 100 samps = [x] for t in range(num): samps.append(sde.propagate(samps[-1])) samps = torch.stack(samps) self.assertEqual(samps.size(), torch.Size([num + 1, *shape])) # ===== Sample path ===== # path = sde.sample_path(num + 1, shape) self.assertEqual(samps.shape, path.shape)
def test_SDE(self): shape = 1000, 100 a = 1e-2 * torch.ones((shape[0], 1)) dt = 0.1 init = norm = DistributionWrapper(Normal, loc=a, scale=math.sqrt(dt)) sde = AffineEulerMaruyama((f_sde, g_sde), (a, 0.15), init, norm, dt=dt, num_steps=10) # ===== Initialize ===== # x = sde.i_sample(shape) self.assert_timeseries_sampling(100, sde, x, shape)
def test_ParameterInDistribution(self): shape = 10, 100 a = 1e-2 * torch.ones((shape[0], 1)) dt = 1e-2 dist = DistributionWrapper(Normal, loc=0.0, scale=Prior(Exponential, rate=10.0)) init = DistributionWrapper(Normal, loc=a, scale=1.0) sde = AffineEulerMaruyama((f_sde, g_sde), (a, 0.15), init, dist, dt=dt, num_steps=10) sde.sample_params(shape) # ===== Initialize ===== # x = sde.i_sample(shape) self.assert_timeseries_sampling(100, sde, x, shape)
def test_SDE(self): def f(x, a, s): return -a * x def g(x, a, s): return s em = AffineEulerMaruyama((f, g), (0.02, 0.15), Normal(0., 1.), Normal(0., 1.), dt=1e-2, num_steps=10) model = LinearGaussianObservations(em, scale=1e-3) x, y = model.sample_path(500) with self.assertRaises(NotImplementedError): SISR(model, 200) for filt in [SISR(model, 500, proposal=Bootstrap()), UKF(model)]: filt = filt.initialize().longfilter(y) means = filt.filtermeans if isinstance(filt, UKF): means = means[:, 0] self.assertLess(torch.std(x - means), 5e-2)
def test_SDE(self): def f(x, a, s): return -a * x def g(x, a, s): return s dt = 1e-2 norm = DistributionWrapper(Normal, loc=0.0, scale=sqrt(dt)) em = AffineEulerMaruyama((f, g), (0.02, 0.15), norm, norm, dt=1e-2, num_steps=10) model = LinearGaussianObservations(em, scale=1e-3) x, y = model.sample_path(500) for filt in [SISR(model, 500, proposal=prop.Bootstrap()), UKF(model)]: result = filt.longfilter(y) means = result.filter_means if isinstance(filt, UKF): means = means[:, 0] self.assertLess(torch.std(x - means), 5e-2)