From 49092acead35199860f8af0ed53337d9b2e2fdd6 Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 29 Aug 2023 12:06:57 +0300 Subject: [PATCH 1/3] created test for generic cases --- logistic.py | 6 ++++++ test_logistic.py | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/logistic.py b/logistic.py index e49d1c2..464dce3 100644 --- a/logistic.py +++ b/logistic.py @@ -1 +1,7 @@ # Your code goes here + +def f(x,r): + return r*x*(1-x) + + +#print(f(0, 1.1)) \ No newline at end of file diff --git a/test_logistic.py b/test_logistic.py index 9391bee..e6b3231 100644 --- a/test_logistic.py +++ b/test_logistic.py @@ -14,3 +14,14 @@ def test_f_corner_cases(): for x, r, expected in cases: result = f(x, r) assert_allclose(result, expected) + +def test_f_generic_cases(): + # Test cases are (x, r, expected) + cases = [ + (0.1, 2.2, 0.198), + (0.2, 3.4, 0.544), + (0.5, 2, 0.5) + ] + for x, r, expected in cases: + result = f(x, r) + assert_allclose(result, expected) \ No newline at end of file From 4fb0943b62e96dd4cc37d4e7e86025676b63d3d6 Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 29 Aug 2023 12:30:57 +0300 Subject: [PATCH 2/3] Parametrize corner and generic cases --- test_logistic.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/test_logistic.py b/test_logistic.py index e6b3231..ad9cfe3 100644 --- a/test_logistic.py +++ b/test_logistic.py @@ -1,10 +1,12 @@ from numpy.testing import assert_allclose from logistic import f +import pytest # Add here your test for the logistic map + def test_f_corner_cases(): # Test cases are (x, r, expected) cases = [ @@ -24,4 +26,26 @@ def test_f_generic_cases(): ] for x, r, expected in cases: result = f(x, r) - assert_allclose(result, expected) \ No newline at end of file + assert_allclose(result, expected) + +@pytest.mark.parametrize('x,r,expected',[ + (0.1, 2.2, 0.198), + (0.2, 3.4, 0.544), + (0.5, 2, 0.5) + ]) + +def test_f_generic_cases_param(x,r,expected): + # Test cases are (x, r, expected) + + result = f(x,r) + assert_allclose(result, expected) + + +@pytest.mark.parametrize('x,r,expected',[ + (0, 1.1, 0), + (1, 3.7, 0), + ]) +def test_f_generic_corner_param(x,r,expected): + # Test cases are (x, r, expected) + result = f(x,r) + assert_allclose(result, expected) \ No newline at end of file From 485d7217b8388eb2024f4ca527c5e1528c81470d Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 29 Aug 2023 15:26:04 +0300 Subject: [PATCH 3/3] generate different tests for logistic fitting --- logistic.py | 17 +++++++++++++++- test_logistic.py | 52 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/logistic.py b/logistic.py index 464dce3..ca8bf26 100644 --- a/logistic.py +++ b/logistic.py @@ -1,7 +1,22 @@ + # Your code goes here def f(x,r): return r*x*(1-x) -#print(f(0, 1.1)) \ No newline at end of file +#print(f(0, 1.1)) + +def iterate_f(it,x,r): + time_series=[] + time_series.append(x) + for i in range(it): + x=r*x*(1-x) + time_series.append(x) + return time_series + +if __name__ == '__main__': + import matplotlib.pyplot as plt + from plot_logistic import plot_trajectory + plot_trajectory(100,4,0) + plt.show() diff --git a/test_logistic.py b/test_logistic.py index ad9cfe3..e5315ee 100644 --- a/test_logistic.py +++ b/test_logistic.py @@ -1,8 +1,15 @@ from numpy.testing import assert_allclose +from math import isclose +import numpy as np + from logistic import f +from logistic import iterate_f +from logistic_fit import fit_r import pytest +SEED=np.random.seed(42) + # Add here your test for the logistic map @@ -48,4 +55,47 @@ def test_f_generic_cases_param(x,r,expected): def test_f_generic_corner_param(x,r,expected): # Test cases are (x, r, expected) result = f(x,r) - assert_allclose(result, expected) \ No newline at end of file + assert_allclose(result, expected) + +@pytest.mark.parametrize('x,r,it,expected',[ + (0.1,2.2, 1, [0.1,0.198]), + (0.2,3.4, 4, [0.2,0.544,0.843418,0.449019,0.841163]), + (0.5,2,3,[0.5,0.5,0.5,0.5]) + ]) + +def test_iterative_f(x,r,it,expected): + result=iterate_f(it,x,r) + assert_allclose(result,expected,rtol=5e-07) + +def test_fit_r(): + expected=2 + xs=iterate_f(it=23,x=0.3, r=expected) + result=fit_r(xs) + assert isclose(result, expected)==True + +@pytest.mark.parametrize('x,it,r,expected',[ + (0.3,23,5.3,5.3), + (0.5,12,2,2), + (0.9,50,3.4,3.4) + ]) +def test_fit_r_param(x,it,r,expected): + + xs=iterate_f(it,x,r) + result=fit_r(xs) + assert isclose(result, expected)==True + +def test_fit_r_random(): + x0=np.random.uniform(0,1,100) + + for x in x0: + xs=iterate_f(100,x,1.5) + assert isclose(xs[-1],1/3) + +@pytest.mark.parametrize('x0',[(x)for x in np.random.uniform(0,2,100)] ) + + +def test_fit_r_random_param(x0): + + + xs=iterate_f(100,x0,1.5) + assert isclose(xs[-1],1/3) and isclose(xs[-2],1/3) and isclose(xs[-3],1/3) \ No newline at end of file