Beispiel #1
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))
 def test_init_wrong_initval_3(self):
     """
     Test passes if LotkaVolterra initialisation complains about
     wrong number of parameters; 3 instead of 2.
     """
     t0, tmax = 0., 1.
     params = [0.2, 3.0, 1.0, 2.0]
     initval, initval_unc = [1., 2., 3.], 0.1
     with self.assertRaises(TypeError):
         linode.LotkaVolterra(t0, tmax, params, initval, initval_unc)
 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
Beispiel #4
0
    ])
    ipdata = ip.InvProblemData(evalpts, data, ivpvar)
    return ipdata


if __name__ == "__main__":

    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,
"""

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

# Solve ODE
stsp = statespace.IBM(q=2, dim=2)
solver = odesolver.ODESolver(ssm=stsp, filtertype="kalman")
t0, tmax = 0.0, 15.0
lotka = linode.LotkaVolterra(
    t0=t0,
    tmax=tmax,
    params=[1.0, 0.01, 0.02, 1.0],
    initval=np.array([20.0, 20.0]),
    initval_unc=1,
)
h = 0.01
t, m, s = solver.solve(lotka, stepsize=h)

# 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)

# Plot solution
plt.title("Lotka-Volterra Phase Plane Plot (h=%r)" % h)
plt.plot(mtraj1, mtraj2, color="darkslategray", linewidth=1.5, label="Trajectory")
 def setUp(self):
     t0, tmax = 0., 1.
     params = [2., 1., 2., 1.]
     initval, initval_unc = [1., 1.], 0.1
     self.lotka = linode.LotkaVolterra(t0, tmax, params, initval,
                                       initval_unc)