Ejemplo n.º 1
0
def test_itoEuler_1D_additive():
    tspan = np.arange(0.0, 2000.0, 0.002)
    y0 = 0.0
    f = lambda y, t: -1.0 * y
    G = lambda y, t: 0.2
    y = sdeint.itoEuler(f, G, y0, tspan)
    assert(np.isclose(np.mean(y), 0.0, rtol=0, atol=1e-02))
    assert(np.isclose(np.var(y), 0.2*0.2/2, rtol=1e-01, atol=0))
Ejemplo n.º 2
0
def test_itoEuler_1D_additive():
    tspan = np.arange(0.0, 2000.0, 0.002)
    y0 = 0.0
    f = lambda y, t: -1.0 * y
    G = lambda y, t: 0.2
    y = sdeint.itoEuler(f, G, y0, tspan)
    assert (np.isclose(np.mean(y), 0.0, rtol=0, atol=1e-02))
    assert (np.isclose(np.var(y), 0.2 * 0.2 / 2, rtol=1e-01, atol=0))
Ejemplo n.º 3
0
    def solve(self, **kwargs):
        """ Solves initial value problem """

        if (len(self.sol) == 0):
            # Solve only if not already solved
            if(self.method == 'EuMa'):
                self.sol_cartesian = sdeint.itoEuler(self.f, self.G, self.y0, self.ts, **kwargs)

            elif (self.method == 'Ito'):
                self.sol_cartesian = sdeint.itoint(self.f, self.G, self.y0, self.ts, **kwargs)

            elif (self.method == 'Strato'):
                self.sol_cartesian = sdeint.stratint(self.f, self.G, self.y0, self.ts, **kwargs)

            else:
                raise ValueError('Only supported methods are EuMa, Ito and Strato')
        else:
            # Do nothing
            pass

        if self.topology == 'cartesian':
            self.sol = self.sol_cartesian
        elif self.topology == 'torus':
            self.sol = np.mod(self.sol_cartesian, 2*np.pi)
Ejemplo n.º 4
0
 def test_itoEuler_R74(self, exact_solution_R74):
     (dW, I, J, f, f_strat, G, G_separate, y0, tspan,
      y) = exact_solution_R74
     yEuler = sdeint.itoEuler(f, G, y0, tspan, dW=dW)
     _assert_close(yEuler, y, 1e-1, 1e-1)
     return yEuler
Ejemplo n.º 5
0
 def test_itoEuler_KPS445(self, exact_solution_KPS445):
     (dW, I, J, f, f_strat, G, y0, tspan, y) = exact_solution_KPS445
     yEuler = sdeint.itoEuler(f, G, y0, tspan, dW=dW)[:, 0]
     _assert_close(yEuler, y, 1e-1, 1e-1)
     return yEuler
Ejemplo n.º 6
0
x_initial = np.ones(N)
local_state = np.random.RandomState(1)
noise = local_state.normal(0, np.sqrt(dt), (np.size(t) - 1, N)) * strength
index_lattice = np.where(A != 0)
A_interaction_lattice = A[index_lattice].reshape(N, degree)
t1 = time.time()
dyn = odeint(mutual, x, t, args=(N, index, neighbor, A_interaction))
t2 = time.time()
dyn_lattice = odeint(mutual_lattice,
                     x,
                     t,
                     args=(N, index_lattice, degree, A_interaction_lattice))
t3 = time.time()
dyn_all = sdeint.itoEuler(close(mutual, *(N, index, neighbor, A_interaction)),
                          close(eta_diag, *(N, )),
                          x_initial,
                          t,
                          dW=noise)
t4 = time.time()
dyn_all_lattice = sdeint.itoEuler(close(
    mutual_lattice, *(N, index_lattice, degree, A_interaction_lattice)),
                                  close(eta_diag, *(N, )),
                                  x_initial,
                                  t,
                                  dW=noise)
t5 = time.time()
dyn_sde = sdesolver(close(mutual, *(N, index, neighbor, A_interaction)),
                    x_initial,
                    t,
                    dW=noise)
t6 = time.time()
Ejemplo n.º 7
0
 def test_itoEuler_R74(self, exact_solution_R74):
     (dW, I, J, f, f_strat, G, G_separate, y0, tspan,y) = exact_solution_R74
     yEuler = sdeint.itoEuler(f, G, y0, tspan, dW=dW)
     _assert_close(yEuler, y, 1e-1, 1e-1)
     return yEuler
Ejemplo n.º 8
0
 def test_itoEuler_KPS445(self, exact_solution_KPS445):
     (dW, I, J, f, f_strat, G, y0, tspan, y) = exact_solution_KPS445
     yEuler = sdeint.itoEuler(f, G, y0, tspan, dW=dW)[:,0]
     _assert_close(yEuler, y, 1e-1, 1e-1)
     return yEuler
Ejemplo n.º 9
0
	def derive_functions(self, drift_order, diff_order, t_inc=None, T=None, m0=None, Dt='default', dt=1):
		"""
		Simulated to optimize the best fitting functional forms for the derrived drift and diffusion 
		coefficients.

		Args
		----
		drift_order : int
			order of the drift coefficient observed
		diff_order : int
			order of the diffusion coefficient observed
		t_inc : float (or None)
			time increment for the SDE integration, if None
			time_increment will be taken as 1/autocorrelation time or
			the observed (input) timeseries
		T : int (or None)
			total time units of simulation, if None,
			T will be taken as the fraction of  total time units of 
			observed (input) data and its autocorrealtion time.
		m0 : float (or None)
			initial state of the SDE, if None, then m0 will be the first 
			value of the input data
		Dt : list or 'default', 'all'
			list of drift timescales for to simulate reconstructed SDE and compare
			it with the observed data.

			If 'default', Dt = range(1, autocorrealtion_time+1, 10)

			if 'all', Dt will include all timescales for which the drift and 
			diffusion coefficients are derrived.
		dt : int, (default = 1)
			diffusion time scale 

		Returns
		-------
		opt_F : callable
			drift polynomila function of the given order, and the timescale for which the simulated
			SDE's PDF was in close match with that of the observed.
		opt_G : callable
			diffusion polynomial function of the given order and timescale `dt`.

		Note
		----
		If the Dt set contains, timescale values for which the drift and diffusion 
		coefficents have not been derrived, then such timescale values will be ignored.

		In other words, Dt set must be a subset of the set containing the slider timescales.

		"""
		if self.vector:
			print("Feature not implemented for vector data")
			return None

		if Dt == 'default':
			start, stop, n_steps = 1, self._ddsde.autocorrelation_time, 10
			Dt = set(np.linspace(1, self._ddsde.autocorrelation_time, 10, dtype=np.int))
			Dt = Dt.intersection(set(self._ddsde._avaiable_timescales))
		elif Dt == 'all':
			Dt = list(self._ddsde._avaiable_timescales)
		else:
			Dt = set(Dt)
			Dt = Dt.intersection(set(self._ddsde._avaiable_timescales))
		Dt = sorted(list(Dt))

		if t_inc is None:
			t_inc = 1/self._ddsde.autocorrelation_time
		if T is None:
			T = int(np.ceil(len(self._data_X)/self._ddsde.autocorrelation_time))
		t_span = np.arange(0, T, t_inc)

		pbar = tqdm.tqdm(total=len(Dt))
		_g = self.fit("G", order=diff_order, diff_time_scale=dt)
		G = lambda x, t: _g(x)
		m0 = self._data_X[0]
		M = []
		for i in Dt:
			_f = self.fit("F", order=drift_order, drift_time_scale=i)
			F = lambda x, t: _f(x)
			n = sdeint.itoEuler(F, G, m0, t_span)
			M.append(n.flatten())
			pbar.update(1)
		pbar.close()

		M = np.array(M)
		divergence_list = []
		p = self._data_X.copy()
		for q in M:
			divergence_list.append(self._divergence(p, q))
		divergence_list = np.array(divergence_list)
		opt_Dt = divergence_list.argmin() + 1

		fig = plt.figure(dpi=300)
		plt.plot(list(Dt), divergence_list)
		plt.xlabel('Dt')
		plt.ylabel('KL Divergence')
		plt.show()
		print("optimium time scale Dt : {}".format(opt_Dt))
		opt_F = self.fit('F', order=drift_order, drift_time_scale=opt_Dt)
		opt_G = self.fit('G', order=diff_order, diff_time_scale=dt)
		print(opt_F)
		print(opt_G)
		return opt_F, opt_G, M,d