def __init__( self, f, g, y, t=0.0, f_helpers=(), g_helpers=(), control_pars=(), seed=None, additive=False, do_cse=False, ): self.state = y self.n = len(self.state) self.t = t self.parameters = [] self.noises = [] self.noise_index = None self.new_y = None self.new_t = None self.RNG = np.random.RandomState(seed) self.additive = additive from jitcsde import t, y f_subs = list(reversed(f_helpers)) g_subs = list(reversed(g_helpers)) lambda_args = [t] for i in range(self.n): symbol = symengine.Symbol("dummy_argument_%i" % i) lambda_args.append(symbol) f_subs.append((y(i), symbol)) g_subs.append((y(i), symbol)) lambda_args.extend(control_pars) f_wc = list( ordered_subs(entry, f_subs).simplify(ratio=1) for entry in f()) g_wc = list( ordered_subs(entry, g_subs).simplify(ratio=1) for entry in g()) lambdify = symengine.LambdifyCSE if do_cse else symengine.Lambdify core_f = lambdify(lambda_args, f_wc) self.f = lambda t, Y: core_f(np.hstack([t, Y, self.parameters])) if self.additive: core_g = lambdify([t] + list(control_pars), g_wc) self.g = lambda t: core_g(np.hstack([t, self.parameters])) else: core_g = lambdify(lambda_args, g_wc) self.g = lambda t, Y: core_g(np.hstack([t, Y, self.parameters]))
def kmc_test(dt, runner, ks): Y = symengine.Symbol("Y") F = scenario["F"].subs(y(0), Y) G = scenario["G"].subs(y(0), Y) Fd = F.diff(Y) Gd = G.diff(Y) Fdd = Fd.diff(Y) Gdd = Gd.diff(Y) # Theoretical expectation M = [ symengine.Lambdify([Y], F + (F * Fd + G**2 * Fdd / 2) * dt / 2), symengine.Lambdify([Y], G**2 + (2 * F * (F + G * Gd) + G**2 * (2 * Fd + Gd**2 + G * Gdd)) * dt / 2), symengine.Lambdify([Y], 3 * G**2 * (F + G * Gd) * dt), symengine.Lambdify([Y], 3 * G**4 * dt), symengine.Lambdify([Y], 15 * G**4 * (F + 2 * G * Gd) * dt**2), symengine.Lambdify([Y], 15 * G**6 * dt**2) ] # Numerical estimate bins, *kmcs = KMC(runner(), dt, kmax=kmax, nbins=nbins) # Comparing the two i = 0 while i < len(ks): k = ks[i] good = 0 for X, value, error in zip(bins, *kmcs[k]): theory = M[k](X) if value - error < theory < value + error: good += 1 if good < thresholds[k] * len(bins): i += 1 else: ks.pop(i) return ks
Taking everything together, our code is: .. literalinclude:: ../examples/noisy_lorenz.py :dedent: 1 :lines: 75-100 """ if __name__ == "__main__": from jitcsde import y, jitcsde import numpy import symengine ρ = 28 σ = 10 β = symengine.Rational(8, 3) p = 0.1 f = [σ * (y(1) - y(0)), y(0) * (ρ - y(2)) - y(1), y(0) * y(1) - β * y(2)] g = [p * y(i) for i in range(3)] SDE = jitcsde(f, g) initial_state = numpy.random.random(3) SDE.set_initial_value(initial_state, 0.0) data = [] for time in numpy.arange(0.0, 100.0, 0.01): data.append(SDE.integrate(time)) numpy.savetxt("timeseries.dat", data)
def test_check_undefined_variable(self): x = symbols("x") SDE = jitcsde_jump( IJI, amp, [y(0)], [x] ) with self.assertRaises(ValueError): SDE.check()
def test_check_index_too_high(self): SDE = jitcsde_jump( IJI, amp, [y(0)], [y(1)] ) with self.assertRaises(ValueError): SDE.check()
def test_check_index_negative(self): SDE = jitcsde_jump( IJI, amp, [y(0)], [y(-1)] ) with self.assertRaises(ValueError): SDE.check()
import numpy as np from numpy.testing.utils import assert_allclose from jitcsde import jitcsde_jump, y, UnsuccessfulIntegration import platform from symengine import symbols, exp, Rational import unittest if platform.system() == "Windows": compile_args = None else: from jitcxde_common import DEFAULT_COMPILE_ARGS compile_args = DEFAULT_COMPILE_ARGS+["-g","-UNDEBUG"] f = [-y(0)**3 + 4*y(0) + y(0)**2] g = [5*exp(-y(0)**2+y(0)-Rational(3,2)) + 3] λ = 1000 initial_value = np.array([1.0]) # Normal noise result = None class CompareResults(unittest.TestCase): def compare_with_result(self,new_result): global result if result is None: result = new_result else: assert_allclose( result-initial_value,
def test_default(self): self.SDE.set_seed(42) self.SDE.set_initial_value({y(0): initial_value[0]}, 0.0)
def tearDown(self): self.SDE.check() new_result = self.SDE.integrate(0.001) assert self.SDE.y_dict[y(0)] == new_result[0] self.compare_with_result(new_result)
import numpy as np from numpy.testing.utils import assert_allclose from jitcsde import jitcsde, y, UnsuccessfulIntegration, test import platform from symengine import symbols, exp, Rational, Function import unittest if platform.system() == "Windows": compile_args = None else: from jitcxde_common import DEFAULT_COMPILE_ARGS compile_args = DEFAULT_COMPILE_ARGS + ["-g", "-UNDEBUG"] # Ensures that all kinds of formatting the input actually work and produce the same result. The correctness of this result itself is checked in validation_test.py. f = [-y(0)**3 + 4 * y(0) + y(0)**2] g = [5 * exp(-y(0)**2 + y(0) - Rational(3, 2)) + 3] initial_value = np.array([1.0]) # Normal noise result = None class CompareResults(unittest.TestCase): def compare_with_result(self, new_result): global result if result is None: result = new_result else: assert_allclose(result - initial_value,
* An SDE which is like the ODE, just that the speed of the phase-space flow is subject to stochastic fluctuations. Inspired by: https://math.stackexchange.com/q/2712378 """ # The dynamics used for testing # ----------------------------- a = -0.025 b = 0.01 c = 0.02 d = 5 # to make axis scale comparably F = [ y(0) * (a - y(0)) * (y(0) - 1.0) - y(1) / d, d * (b * y(0) - c / d * y(1)), ] initial_state = np.random.random(2) # Integrating the ODE #-------------------- ODE = jitcode(F) ODE.set_integrator("dopri5") ODE.set_initial_value(initial_state, 0.0) times = 500 + np.arange(0, 200, 0.005) ode_data = np.vstack(ODE.integrate(time) for time in times)
Everything else remains unchanged. See `the sources <https://raw.githubusercontent.com/neurophysik/jitcsde/master/examples/noisy_and_jumpy_lorenz.py>`_ for the full example. """ if __name__ == "__main__": from jitcsde import y, jitcsde_jump import numpy import symengine ρ = 28 σ = 10 β = symengine.Rational(8,3) p = 0.1 f = [ σ * (y(1)-y(0)), y(0)*(ρ-y(2)) - y(1), y(0)*y(1) - β*y(2) ] g = [ p*y(i) for i in range(3) ] def IJI(time,state): return numpy.random.exponential(1.0) def jump(time,state): return numpy.array([ 0.0, 0.0, numpy.random.normal(0.0,abs(state[2])) ])
""" import numpy as np from jitcsde._python_core import sde_integrator from jitcsde import jitcsde, y import symengine from kmc import KMC kmax = 6 thresholds = [0.6, 0.6, 0.6, 0.5, 0.5, 0.4] nbins = 100 times = lambda dt, N: np.arange(dt, (N + 1) * dt, dt) scenarios = [{ "F": -y(0)**3 + 4 * y(0) + y(0)**2, "G": 5 * symengine.exp(-y(0)**2 + y(0) - 1.5) + 3.0, "additive": False }, { "F": -y(0)**3 + 4 * y(0) + y(0)**2, "G": symengine.sympify(3), "additive": True }] def test_python_core(scenario, dt=0.0001, N=10000, each_step=lambda SDE: 0): SDE = sde_integrator(lambda: [scenario["F"]], lambda: [scenario["G"]], np.array([0]), additive=scenario["additive"])
else: from jitcxde_common import DEFAULT_COMPILE_ARGS compile_args = DEFAULT_COMPILE_ARGS+["-g","-UNDEBUG","-O0"] def compare(x,y,rtol=1e-4,atol=1e-4): try: assert_allclose(x,y,rtol=rtol,atol=atol) except AssertionError as error: print("\n") print (x,y) raise error number_of_runs = int(argv[1]) F = [ 10*(y(1)-y(0)), y(0)*(28-y(2))-y(1), y(0)*y(1)-8/3.*y(2) ] for additive in [False,True]: if additive: G = [ symengine.sympify(i*0.05) for i in range(len(F)) ] else: G = [ 0.1*y(i) for i in range(len(F)) ] RNG = Random() errors = 0 for realisation in range(number_of_runs):
import numpy as np from numpy.testing.utils import assert_allclose from jitcsde import jitcsde, y, UnsuccessfulIntegration, test import platform from symengine import symbols, exp, Rational import unittest if platform.system() == "Windows": compile_args = None else: from jitcxde_common import DEFAULT_COMPILE_ARGS compile_args = DEFAULT_COMPILE_ARGS + ["-g", "-UNDEBUG"] # Ensures that all kinds of formatting the input actually work and produce the same result. The correctness of this result itself is checked in validation_test.py. f = [-y(0)**3 + 4 * y(0) + y(0)**2] g = [5 * exp(-y(0)**2 + y(0) - Rational(3, 2)) + 3] initial_value = np.array([1.0]) # Normal noise result = None class CompareResults(unittest.TestCase): def compare_with_result(self, new_result): global result if result is None: result = new_result else: assert_allclose(result - initial_value,