def test_euler_Lambda(self, Lambda): """Test the range of valid lambda values for the logistic growth model. Arguments: Lambda {float} -- growth factor. """ # integration paramters h = 0.001 # time steps t_0 = 0.0 # initial time t_final = 10.0 # final time (limit time interval just to avoid computing time issues) y_0 = 0.1 # initial condition times = np.arange(start=t_0, stop=t_final, step=h) analytic = logistic_growth_dimensionless(times, Lambda=Lambda) # logistic growth ODE model = lambda t, x: Lambda * x * (1 - x) logistic_model = euler.euler(model) numeric = logistic_model.integrate(h=h, t_0=t_0, t_final=t_final, y_0=y_0)[1, :] squared_distance = (analytic - numeric)**2 tolerance = 0.01 assert np.all(squared_distance <= tolerance**2)
def test_euler_initial_y(self, y_0): """testing the valid realm of y_0. Here the logistic function can only assume values in (0, 1). Arguments: y_0 {float} -- initial value of the state variable. """ # model parameters Lambda = 1.0 # integration paramters h = 0.001 # time steps t_0 = 0.0 # initial time t_final = 10.0 # final time (limit time interval just to avoid computing time issues) y_0 = 0.1 # initial condition times = np.arange(start=t_0, stop=t_final, step=h) analytic = logistic_growth_dimensionless(times, Lambda=Lambda) # logistic growth ODE model = lambda t, x: Lambda * x * (1 - x) logistic_model = euler.euler(model) numeric = logistic_model.integrate(h=h, t_0=t_0, t_final=t_final, y_0=y_0)[1, :] squared_distance = (analytic - numeric)**2 tolerance = 0.01 assert np.all(squared_distance <= tolerance**2)
def test_euler_intial_time(self, t_0): """Testing valid values for t_0. Within the physical limits of the computer any number t_0 shoud work. We just choose a rather small range [-100, 100] for efficiency of the test. Arguments: t_0 {float} -- initial time of integration. """ # model parameters Lambda = 1.0 # integration paramters h = 0.001 # time steps t_final = 10.0 # final time (limit time interval just to avoid computing time issues) y_0 = 0.1 # initial condition times = np.arange(start=t_0, stop=t_final, step=h) analytic = logistic_growth_dimensionless(times, t_0=t_0, Lambda=Lambda) # logistic growth ODE model = lambda t, x: Lambda * x * (1 - x) logistic_model = euler.euler(model) numeric = logistic_model.integrate(h=h, t_0=t_0, t_final=t_final, y_0=y_0)[1, :] squared_distance = (analytic - numeric)**2 tolerance = 0.01 assert np.all(squared_distance <= tolerance**2)
def test_infer_parameters(): """test model: logistic growth """ ### exact parameters exact_parameters = [1.0, 0.1] Lambda, x_0 = exact_parameters ### generate data scale = 0.01 data_time = np.linspace(0, 10, 100) exact_solution = logistic_growth_dimensionless(times=data_time, x_0=x_0, Lambda=Lambda) gen = DataGenerator() data_y = gen.generate_data(exact_solution, scale=scale) data = np.vstack(tup=(data_time, data_y)) ODEmodel = lambda t, x, Lambda: Lambda * x * (1 - x) problem = InferenceProblem(ODEmodel, data) initial_parameters = [3.0] # initial lambda initial_y0 = 0.3 step_size = 0.1 estimated_parameters = problem.infer_parameters( y_0=initial_y0, initial_parameters=initial_parameters, step_size=step_size) assert np.allclose(a=estimated_parameters, b=exact_parameters, rtol=5.0e-2)
import numpy as np from PAID.EulerMethod.euler import euler from PAID.data.generator import DataGenerator from PAID.InferenceMethod.inference import InferenceProblem from tests.exact_models.models import logistic_growth_dimensionless ### exact parameters exact_parameters = [1.0, 0.1] Lambda, x_0 = exact_parameters ### generate data scale = 0.05 data_time = np.linspace(0, 10, 100) exact_solution = logistic_growth_dimensionless(times=data_time, x_0=x_0, Lambda=Lambda) gen = DataGenerator() data_y = gen.generate_data(exact_solution, scale=scale) data = np.vstack(tup=(data_time, data_y)) ODEmodel = lambda t, x, Lambda: Lambda * x * (1 - x) problem = InferenceProblem(ODEmodel, data) initial_parameters = [3.0] # initial lambda initial_y0 = 0.3 step_size = 0.1 estimated_parameters = problem.infer_parameters( y_0=initial_y0, initial_parameters=initial_parameters, step_size=step_size)