def setUp(self):
     ivp = ode.Res2Bod(t0=0., tmax=0.011)
     ibm_prior = stsp.IBM(q=2, dim=4)
     solver = oso.ODESolver(ibm_prior)
     self.t, mean, stdev = solver.solve(ivp, stepsize=0.0001)
     self.m1 = oso.get_trajectory(mean, 0, 0)
     self.m2 = oso.get_trajectory(mean, 1, 0)
예제 #2
0
    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):
     """
     Create a working prior,
     working filtertype and working evalparam.
     """
     self.working_2d_prior = stsp.IBM(q=1, dim=2)
     self.working_filtertype = "kalman"
 def test_instantiation_impossible(self):
     """
     Test passes if Filter object cannot be instantiated.
     """
     ssm = statespace.IBM(q=2, dim=1)
     with self.assertRaises(TypeError):
         filters.Filter(ssm)
예제 #5
0
    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 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)
예제 #7
0
    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))
예제 #8
0
 def _setup_ibm_q1(self):
     """
     Set up IBM(1) state space model, i.e. q=1.
     """
     q = 1
     bm_amp = 1.0 + np.random.rand()
     dim = 1
     self.ssm = stsp.IBM(q, dim, bm_amp)
 def setUp(self):
     """Setup odesolver and solve a scalar ode"""
     prior = stsp.IBM(q=1, dim=1)
     self.solver = oso.ODESolver(prior, filtertype="kalman")
     self.ode = ode.LinearODE(t0=0.0, tmax=1.2, params=2.0, initval=4.1)
     self.h = 0.1
     self.tsteps, self.means, self.stdevs = self.solver.solve(
         self.ode, stepsize=self.h)
예제 #10
0
 def _setup_ibm_q2(self):
     """
     Set up IBM(2) state space model.
     """
     q = 2
     bm_amp = 1.0 + np.random.rand()
     dim = 1
     self.ssm = stsp.IBM(q, dim, bm_amp)
 def setUp(self):
     pars = [0., 0.2, 0.2, 3.0]
     y0 = np.array([-1, 1])
     ivp = ode.FitzHughNagumo(t0=0., tmax=5., params=pars, initval=y0)
     ibm_prior = stsp.IBM(q=1, dim=2)
     solver = oso.ODESolver(ibm_prior)
     self.t, mean, stdev = solver.solve(ivp, stepsize=0.05)
     self.m1 = oso.get_trajectory(mean, 0, 0)
     self.m2 = oso.get_trajectory(mean, 1, 0)
 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 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 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
 def test_solution_controlvar(self):
     """
     Controlvariate to the test_solution() test. For q < 2 or
     stepsize > 0.0001 the precision should not be achieved anymore. 
     """
     ivp = ode.Res2Bod(t0=0., tmax=0.011)
     ibm_prior = stsp.IBM(q=1, dim=4)
     solver = oso.ODESolver(ibm_prior)
     self.t, mean, stdev = solver.solve(ivp, stepsize=0.0001)
     self.m1 = oso.get_trajectory(mean, 0, 0)
     self.m2 = oso.get_trajectory(mean, 1, 0)
     x_benchmark = 0.986122388809302
     y_benchmark = -0.014199005665214861
     thresh = 1e-10
     self.assertEqual(traj_passes_pt(self.m1, x_benchmark, thresh), False)
     self.assertEqual(traj_passes_pt(self.m2, y_benchmark, thresh), False)
예제 #16
0
Checks whether odesolver works up to high accuracy
via solving the restricted two-body problem.
This might take 20-30 seconds.
If the resulting orbit is periodic, the solution is correct.
"""

import numpy as np
import matplotlib.pyplot as plt

from difflikelihoods import ode
from difflikelihoods import odesolver
from difflikelihoods import statespace

# Solve ODE
stsp = statespace.IBM(q=2, dim=4)
solver = odesolver.ODESolver(ssm=stsp, filtertype="kalman")
r2b = ode.Res2Bod(t0=0, tmax=32)
t, m, s = solver.solve(r2b, stepsize=0.0001)

# Extract relevant trajectories
mtraj1 = odesolver.get_trajectory(m, 0, 0)
mtraj2 = odesolver.get_trajectory(m, 1, 0)

# Plot solution
plt.title("Restricted Two-Body Problem")
plt.plot(mtraj1, mtraj2, label="Trajectory")
plt.plot(mtraj1[0], mtraj2[0], "o", label="Starting point at t=t0")
plt.plot(mtraj1[-1], mtraj2[-1], "o", label="Endpoint at t=tmax")
plt.legend()
plt.show()
예제 #17
0
    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)
"""
solve_fhn.py

Checking the compliance of ode.py, statespace.py and odesolver.py
by solving the FitzHugh-Nagumo model.
If the output looks 'proper' and a little uncertainty is visible, the test passes.
"""

import numpy as np
from difflikelihoods import ode
from difflikelihoods import odesolver
from difflikelihoods import statespace
import matplotlib.pyplot as plt

# Solve ODE
stsp = statespace.IBM(q=1, dim=2)
solver = odesolver.ODESolver(ssm=stsp, filtertype="kalman")
fhn = ode.FitzHughNagumo(
    t0=0.0,
    tmax=15.0,
    params=[0.0, 0.08, 0.07, 1.25],
    initval=np.array([1.0, 0.0]),
    initval_unc=1e-24,
)
t, m, s = solver.solve(fhn, stepsize=0.01)

# Extract relevant trajectories
mtraj1 = odesolver.get_trajectory(m, 0, 0)
munc1 = odesolver.get_trajectory(s, 0, 0)
mtraj2 = odesolver.get_trajectory(m, 1, 0)
munc2 = odesolver.get_trajectory(s, 1, 0)
예제 #19
0
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)