From 7f84e8f9e9816281d9852cd8116115a2074910ed Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 29 Aug 2023 12:05:58 +0300 Subject: [PATCH 1/6] add logistic function and generic test cases --- logistic.py | 3 +++ test_logistic.py | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/logistic.py b/logistic.py index e49d1c2..ebaa7d1 100644 --- a/logistic.py +++ b/logistic.py @@ -1 +1,4 @@ # Your code goes here +def f(x,r): + return r*x*(1-x) + \ 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 b4ddad2f603ef3eaa809a3de78476873efc3152a Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 29 Aug 2023 12:33:08 +0300 Subject: [PATCH 2/6] change test with parametrize --- test_logistic.py | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/test_logistic.py b/test_logistic.py index e6b3231..ac5e997 100644 --- a/test_logistic.py +++ b/test_logistic.py @@ -1,27 +1,18 @@ from numpy.testing import assert_allclose - +import pytest from logistic import f # Add here your test for the logistic map +@pytest.mark.parametrize('x,r,expected', [(0, 1.1, 0), + (1, 3.7, 0),]) +def test_f_corner_cases(x,r,expected): + result = f(x, r) + assert_allclose(result, expected) -def test_f_corner_cases(): - # Test cases are (x, r, expected) - cases = [ - (0, 1.1, 0), - (1, 3.7, 0), - ] - 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), +@pytest.mark.parametrize('x,r,expected', [(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 + (0.5, 2, 0.5)]) +def test_f_generic_cases(x,r,expected): + result = f(x, r) + assert_allclose(result, expected) \ No newline at end of file From d925493648fcf1b94e9574a0fa299108a8517a03 Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 29 Aug 2023 14:40:26 +0300 Subject: [PATCH 3/6] add iterate_f --- logistic.py | 8 +++++++- test_logistic.py | 13 ++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/logistic.py b/logistic.py index ebaa7d1..94d9ed2 100644 --- a/logistic.py +++ b/logistic.py @@ -1,4 +1,10 @@ # Your code goes here def f(x,r): return r*x*(1-x) - \ No newline at end of file + +def iterate_f(it,x,r): + results = [x] + for _ in range(it): + x = f(x, r) + results.append(x) + return results diff --git a/test_logistic.py b/test_logistic.py index ac5e997..35c8a8a 100644 --- a/test_logistic.py +++ b/test_logistic.py @@ -1,6 +1,7 @@ from numpy.testing import assert_allclose import pytest from logistic import f +from logistic import iterate_f # Add here your test for the logistic map @@ -15,4 +16,14 @@ def test_f_corner_cases(x,r,expected): (0.5, 2, 0.5)]) def test_f_generic_cases(x,r,expected): result = f(x, r) - assert_allclose(result, expected) \ No newline at end of file + assert_allclose(result, expected) + + +@pytest.mark.parametrize('it, x, r, expected', [ + (1, 0.1, 2.2, [0.1, 0.198]), + (4, 0.2, 3.4, [0.2, 0.544, 0.843418, 0.449019, 0.841163]), + (3, 0.5, 2, [0.5, 0.5, 0.5, 0.5]), +]) +def test_iterate_f(it,x,r,expected): + result = iterate_f(it, x, r) + assert_allclose(result, expected, atol=0.00001) From a69e30d7a820b68f4cd03b7dddcc5cbadefa891e Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 29 Aug 2023 14:48:29 +0300 Subject: [PATCH 4/6] add unit test for fit_r --- test_logistic_fit.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 test_logistic_fit.py diff --git a/test_logistic_fit.py b/test_logistic_fit.py new file mode 100644 index 0000000..789567e --- /dev/null +++ b/test_logistic_fit.py @@ -0,0 +1,17 @@ +import math +import pytest + +from logistic import iterate_f +from logistic_fit import fit_r + + +@pytest.mark.parametrize("x0, r, it", [ + (0.3, 3.421, 23), + (0.1, 3.421, 23), + (0.3, 1.0, 23), + (0.3, 3.421, 5), +]) +def test_logistic_fit_recover_r(x0, r, it): + trajectory = iterate_f(it, x0, r) + fitted_r = fit_r(trajectory) + assert math.isclose(fitted_r, r) From 5b21e8230465bc1fa91a2990184353f99e5c4fb1 Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 29 Aug 2023 15:19:11 +0300 Subject: [PATCH 5/6] test iterate_f for random starting point --- test_logistic.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test_logistic.py b/test_logistic.py index 35c8a8a..0596674 100644 --- a/test_logistic.py +++ b/test_logistic.py @@ -2,6 +2,9 @@ import pytest from logistic import f from logistic import iterate_f +import numpy as np + +SEED = 0 # Add here your test for the logistic map @@ -27,3 +30,13 @@ def test_f_generic_cases(x,r,expected): def test_iterate_f(it,x,r,expected): result = iterate_f(it, x, r) assert_allclose(result, expected, atol=0.00001) + + +def test_iterate_f_random(): + random_state = np.random.RandomState(SEED) + for _ in range(100): + x0 = random_state.uniform(0.0001, 0.9999) + r= 1.5 + xs = iterate_f(it=1000, x=x0, r=r) + expected= 1/3 + assert_allclose(xs[-1], expected, atol=1e-6) \ No newline at end of file From c6fae36bd1708291dd711658815e9f623095c46c Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 29 Aug 2023 15:37:56 +0300 Subject: [PATCH 6/6] add random_state fixture --- test_logistic.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/test_logistic.py b/test_logistic.py index 0596674..be96279 100644 --- a/test_logistic.py +++ b/test_logistic.py @@ -4,7 +4,13 @@ from logistic import iterate_f import numpy as np -SEED = 0 +# set the random seed for once here +SEED = np.random.randint(0, 2**31) +@pytest.fixture +def random_state(): + print(f'Using seed {SEED}') + random_state = np.random.RandomState(SEED) + return random_state # Add here your test for the logistic map @@ -32,8 +38,7 @@ def test_iterate_f(it,x,r,expected): assert_allclose(result, expected, atol=0.00001) -def test_iterate_f_random(): - random_state = np.random.RandomState(SEED) +def test_iterate_f_random(random_state): for _ in range(100): x0 = random_state.uniform(0.0001, 0.9999) r= 1.5