示例#1
0
def test_integrator(scenario, dt=0.001, N=100000, pin=False):
    SDE = jitcsde([scenario["F"]], [scenario["G"]], verbose=False)
    SDE.set_initial_value(np.array([0.0]))

    if pin:
        size = np.random.exponential(dt)
        number = int(times(dt, N)[-1] / size)
        SDE.pin_noise(number, size)

    SDE.compile_C()

    for t in times(dt, N):
        yield SDE.integrate(t)
示例#2
0
 def test_check_index_negative(self):
     SDE = jitcsde([y(0)], [y(-1)])
     with self.assertRaises(ValueError):
         SDE.check()
示例#3
0
 def setUp(self):
     self.SDE = jitcsde(f, g)
     self.SDE.set_initial_value(initial_value, 0.0)
     self.SDE.compile_C(extra_compile_args=compile_args)
示例#4
0
 def setUp(self):
     self.SDE = jitcsde(f_with_helpers,
                        g_add,
                        helpers=helpers,
                        g_helpers="auto")
     assert (self.SDE.additive)
initial_state = np.random.random(2)

# Integrating the ODE
#--------------------

ODE = jitcode(F, verbose=False)
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])

# Integrating the SDE
# -------------------

SDE = jitcsde([1 * f for f in F], [0.01 * f for f in F], verbose=False)
SDE.set_initial_value(initial_state, 0.0)

times = 500 + np.arange(0, 300, 1)
sde_data = np.vstack([SDE.integrate(time) for time in times])

# The actual test
# ---------------

# Each result of the SDE integration should be near a result of the ODE integration.

# Obtain an accuracy threshold from the maximum distance of consecutive ODE samples. Note that the ODE was sampled with a much finer time step to densely capture the attractor.
self_distances = np.linalg.norm(ode_data[1:, :] - ode_data[:-1, :], axis=1)
threshold = 4 * np.max(self_distances)

# All distances between SDE samples and ODE samples
示例#6
0
    symengine.cos,
]

jitcsde_sympy_provisions = [
    jitcsde.sympy_symbols.t,
    jitcsde.sympy_symbols.y,
    symengine.cos,
]

mixed = [
    jitcsde.sympy_symbols.t,
    jitcsde.y,
    sympy.cos,
]

seed = int(random.getrandbits(32))
results = set()

for t, y, cos in [
        symengine_manually, sympy_manually, jitcsde_provisions,
        jitcsde_sympy_provisions, mixed
]:
    SDE = jitcsde.jitcsde([cos(t) * y(0)], [cos(t) * y(0) / 10], verbose=False)
    SDE.set_seed(seed)
    SDE.set_initial_value([1.0], 0.0)

    result = SDE.integrate(10)[0]
    results.add(result)

assert len(results) == 1
示例#7
0
 def setUp(self):
     self.SDE = jitcsde(f_with_helpers,
                        g_with_helpers,
                        helpers=helpers,
                        g_helpers="same")
示例#8
0
 def setUp(self):
     self.SDE = jitcsde(f, g)
示例#9
0
 def setUp(self):
     self.SDE = jitcsde(f_control_pars,
                        g_control_pars,
                        control_pars=[five, four])
示例#10
0
 def setUp(self):
     self.SDE = jitcsde(f_dict, g_dict)
示例#11
0
 def setUp(self):
     self.SDE = jitcsde(f_generator, g_generator)
示例#12
0
 def setUp(self):
     self.SDE = jitcsde(f_with_helpers,
                        g_with_helpers,
                        helpers=f_helpers,
                        g_helpers=g_helpers)
示例#13
0
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)

# Integrating the SDE
# -------------------

SDE = jitcsde([1 * f for f in F], [0.01 * f for f in F])
SDE.set_initial_value(initial_state, 0.0)

times = 500 + np.arange(0, 300, 1)
sde_data = np.vstack(SDE.integrate(time) for time in times)

# The actual test
# ---------------

# Each result of the SDE integration should be near a result of the ODE integration.

# Obtain an accuracy threshold from the maximum distance of consecutive ODE samples. Note that the ODE was sampled with a much finer time step to densely capture the attractor.
self_distances = np.linalg.norm(ode_data[1:, :] - ode_data[:-1, :], axis=1)
threshold = 4 * np.max(self_distances)

# All distances between SDE samples and ODE samples
示例#14
0
 def test_check_index_too_high(self):
     SDE = jitcsde([y(0)], [y(1)])
     with self.assertRaises(ValueError):
         SDE.check()
示例#15
0
 def setUp(self):
     self.SDE = jitcsde(f_callback,
                        g_callback,
                        callback_functions=callbacks)
示例#16
0
 def test_check_undefined_variable(self):
     x = symbols("x")
     SDE = jitcsde([y(0)], [x])
     with self.assertRaises(ValueError):
         SDE.check()
示例#17
0
 def setUp(self):
     self.SDE = jitcsde(f_strat, g, ito=False)
示例#18
0
 def test_save_and_load(self):
     filename = self.SDE.save_compiled(overwrite=True)
     self.SDE = jitcsde(module_location=filename, n=len(f))
     self.test_default()
示例#19
0
 def setUp(self):
     self.SDE = jitcsde(f_strat_with_helpers,
                        g_with_helpers,
                        helpers=helpers,
                        g_helpers="auto",
                        ito=False)
def Fn(x, xc):
    return np.tanh(kappa * (x - xc))


fig, axes = plt.subplots(2, 3)

# simulate once for a static, once for a changing switch
for i, da in enumerate([0, 0.3]):
    dxdt = [
        1 / epsilon * (f(ys(0), a + da * F(ys(1), Xc)) *
                       (ys(1) - ys(0)) - g(ys(0)) * ys(0)), kX - ys(1) * ys(0)
    ]
    ggg = [sigma, 0]  #noise
    ini = [0.01, 0.01]
    sdesys = jitcsde(dxdt, ggg)
    sdesys.set_initial_value(ini, 0.0)
    sdesys.set_integration_parameters(atol=1e-8,
                                      first_step=0.001,
                                      max_step=0.01,
                                      min_step=1e-13)
    timeseries = []
    tv = np.arange(sdesys.t, sdesys.t + 200, 0.01)
    for time in tv:
        timeseries.append(sdesys.integrate(time))

    timeseries = np.array(timeseries)
    Xv = timeseries[:, 0]
    XTv = timeseries[:, 1]
    if da > 0:  # keep the a values for the changing switch, to use for plotting snapshots of the switch
        av = a + da * Fn(XTv, Xc)
示例#21
0
 def setUp(self):
     self.SDE = jitcsde(f, g_add)
     assert (self.SDE.additive)
示例#22
0
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)
	errors = 0
	
	for realisation in range(number_of_runs):
		print( ".", end="", flush=True )
		
		seed = RNG.randint(0,1000000)
		
		initial_state = np.array([RNG.random() for _ in range(len(F))])
		
		P = py_sde_integrator(
				lambda:F, lambda:G,
				initial_state, 0.0,
				seed=seed, additive=additive
				)
		
		SDE = jitcsde(F,G,additive=additive)
		SDE.compile_C(extra_compile_args=compile_args,chunk_size=1)
		C = SDE.jitced.sde_integrator(0.0,initial_state,seed)
		
		def get_next_step():
			r = RNG.uniform(1e-7,1e-3)
			P.get_next_step(r)
			C.get_next_step(r)
		
		def time():
			compare(P.t, C.t)
		
		def get_state():
			compare(P.get_state(), C.get_state())
		
		def get_p():