def setUp(self): """ Set up linearised ODESolver and Res2Bod as a non-linearised ODE. """ prior = stsp.IBM(q=1, dim=4) self.solver = linsolver.LinearisedODESolver(prior, filtertype="kalman") self.ode = standard_ode.Res2Bod(t0=0.0, tmax=1.2)
def check_custom_filter_not_implemented(self): """ Custom filter should be an option, yet raise an error. """ with self.assertRaises(NotImplementedError): linsolver.LinearisedODESolver(self.working_2d_prior, filtertype="custom")
def setUp(self): """ Set up linear ODE (i.e. one-dim, one parameter) and one evalpt. """ # Set Model Parameters odeparam = 1. y0, y0_unc = 1.0, 0 t0, tmax = 0.0, 1.25 # Set Method Parameters q = 1 h = 0.1 # Set up and solve ODE ibm = statespace.IBM(q=q, dim=1) solver = linsolve.LinearisedODESolver(ibm) ivp = linode.LinearODE(t0, tmax, odeparam, y0, y0_unc) tsteps, means, __, rhs_parts, uncerts = solver.solve(ivp, stepsize=h) self.mean = odesolver.get_trajectory(means, 0, 0) # Set up BM and IBM covariance matrices evalpt = np.array([tsteps[-1]]) derdat = (tsteps, rhs_parts, 0.) const, jacob = linearisation.compute_linearisation( ssm=ibm, initial_value=y0, derivative_data=derdat, prdct_tsteps=evalpt) # Compute GP Estimation of filter mean at t=tmax self.postmean = const + np.dot(jacob[:, 0], odeparam)
def test_uncert_not_scalar(self): """ We test whether the uncertainty (third element of derivative_data) is only accepted as a scalar. """ # Set Model Parameters odeparam = 1. y0, y0_unc = 1.0, 0 t0, tmax = 0.0, 1.25 # Set Method Parameters q = 1 h = 0.1 # Set up and solve ODE ibm = statespace.IBM(q=q, dim=1) solver = linsolve.LinearisedODESolver(ibm) ivp = linode.LinearODE(t0, tmax, odeparam, y0, y0_unc) tsteps, means, __, rhs_parts, should_not_work = solver.solve(ivp, stepsize=h) self.mean = odesolver.get_trajectory(means, 0, 0) # Set up BM and IBM covariance matrices evalpt = np.array(tsteps[[-1]]) with self.assertRaises(TypeError): derdat = (tsteps, rhs_parts, should_not_work) linearisation.compute_linearisation(ssm=ibm, initial_value=y0, derivative_data=derdat, prdct_tsteps=evalpt)
def setUp(self): """ Set up Lotka-Volterra ODE (i.e. two-dim, four parameter) and multiple (two) evalpts. """ # Set Model Parameters odeparam = np.array([0, 1, 1, 2]) y0, y0_unc = np.ones(2), 0 * np.ones(2) t0, tmax = 0.0, 1.25 # Set Method Parameters q = 1 h = 0.1 # Set up and solve ODE ibm = statespace.IBM(q=q, dim=len(y0)) solver = linsolve.LinearisedODESolver(ibm) ivp = linode.LotkaVolterra(t0, tmax, odeparam, y0, y0_unc) tsteps, means, __, rhs_parts, uncerts = solver.solve(ivp, stepsize=h) self.mean = odesolver.get_trajectory_multidim(means, [0, 1], 0) # Set up BM and IBM covariance matrices evalpt = np.array(tsteps[[-1, -10]]) derdat = (tsteps, rhs_parts, 0.) const, jacob = linearisation.compute_linearisation( ssm=ibm, initial_value=y0, derivative_data=derdat, prdct_tsteps=evalpt) # Compute GP Estimation of filter mean at t=tmax postmean = const + np.dot(jacob, odeparam) self.postmean = postmean.reshape((2, 2))
def check_nonexistant_filterkey(self): """ Entering any filterkey other than 'kalman', 'particle' or 'custom' raises AssertionError. """ with self.assertRaises(NameError): linsolver.LinearisedODESolver(self.working_2d_prior, filtertype="rubbish")
def setUp(self): """ Set up linear ode and solve with stepsize h = 0.1. """ prior = stsp.IBM(q=1, dim=1) self.solver = linsolver.LinearisedODESolver(prior, filtertype="kalman") self.ode = linode.LinearODE(t0=0.0, tmax=1.2, params=2.0, initval=4.1) self.h = 0.1 output = self.solver.solve(self.ode, self.h) self.tsteps, self.means, self.stdevs, __, __ = output
def setUp(self): """ Set up linear ODE and linearised ODESolver. """ prior = stsp.IBM(q=1, dim=1) solver = linsolver.LinearisedODESolver(prior, filtertype="kalman") ode = linode.LinearODE(t0=0.0, tmax=1.2, params=2.0, initval=4.1) h = 0.1 output = solver.solve(ode, h) __, __, __, self.rhs_parts, self.uncert = output
def test_inconsistent_prior_and_ssm(self): """ Prior uses dim=2, so a scalar ODE should raise an AssertionError. """ solver = linsolver.LinearisedODESolver(self.working_2d_prior, self.working_filtertype) wrong_dimensional_ode = linode.LinearODE(t0=0., tmax=1., params=1.123, initval=1.) with self.assertRaises(AssertionError): solver.solve(wrong_dimensional_ode, stepsize=0.1)
def setUp(self): """ Set up LotkaVolterra ODE and LinearisedODESolver. """ prior = stsp.IBM(q=1, dim=2) solver = linsolver.LinearisedODESolver(prior, filtertype="kalman") params = [0.1, 0.2, 0.3, 0.4] ode = linode.LotkaVolterra(t0=0.0, tmax=1.2, params=params, initval=np.ones(2)) h = 0.1 output = solver.solve(ode, h) __, __, __, self.rhs_parts, self.uncert = output
np.random.seed(1) # Set Model Parameters initial_value = np.array([20, 20]) initial_time, end_time = 0.0, 5.0 ivpvar = 1e-2 thetatrue = np.array([1.0, 0.1, 0.1, 1.0]) ivp = linode.LotkaVolterra(initial_time, end_time, params=thetatrue, initval=initial_value) # Set Method Parameters h_for_data = (end_time - initial_time) / 10000 h = (end_time - initial_time) / 200 solver = linsolver.LinearisedODESolver(statespace.IBM(q=1, dim=2)) ipdata = create_data(solver, ivp, thetatrue, h_for_data, ivpvar) iplklhd = ip.InvProblemLklhd(ipdata, ivp, solver, h, with_jacob=True) # Sample from posteriors niter = 50 init_theta = np.array([0.8, 0.2, 0.05, 1.1]) samples_ham, probs_ham = hamiltonian(niter, iplklhd, init_theta, stepsize=0.2, nsteps=6) samples_lang, probs_lang = langevin(niter, iplklhd, init_theta, stepsize=1.2)
np.random.seed(1) # Set Model Parameters initial_value = 2.0 initial_time = 0.1 end_time = 1.1 ivpnoise = 0.01 thetatrue = 0.25 # Set Method Parameters q = 1 h = 0.2 nsamps = 25 init_theta = 0.99 * np.ones(1) ibm = statespace.IBM(q=q, dim=1) solver = linsolver.LinearisedODESolver(ibm) pwidth = 0.004 # Create Data and Jacobian ivp = linode.LinearODE(initial_time, end_time, params=thetatrue, initval=initial_value, initval_unc=0.0) evalpt, data = create_data(solver, ivp, thetatrue, 1e-04, ivpnoise) tsteps, __, __, __, __ = solver.solve(ivp, stepsize=h) evalpt = np.array(tsteps[[-1]]) kernel_prefactor = linearisation.compute_kernel_prefactor( ibm, 0.0, tsteps, evalpt) # Sample states from Markov chain