From 9e2f7375728b785b1fd41a073e68f2d0097aea6b Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 29 Aug 2023 12:01:29 +0300 Subject: [PATCH 1/8] Write logistic function --- logistic.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/logistic.py b/logistic.py index e49d1c2..eb45a52 100644 --- a/logistic.py +++ b/logistic.py @@ -1 +1,3 @@ -# Your code goes here +def f(x, r): + return r * x * (1-x) + From f8e475bfa9404c373b01db14a4ef223370edb3bf Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 29 Aug 2023 12:01:55 +0300 Subject: [PATCH 2/8] Add generic cases test --- test_logistic.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/test_logistic.py b/test_logistic.py index 9391bee..3061761 100644 --- a/test_logistic.py +++ b/test_logistic.py @@ -2,7 +2,15 @@ from logistic import f -# Add here your test for the logistic map +def test_generic_cases(): + cases = [ + (0.1, 2.2, 0.198), + (0.2, 3.4, 0.544), + (0.5, 2.0, 0.5) + ] + for x, r, expected in cases: + result = f(x, r) + assert_allclose(result, expected) def test_f_corner_cases(): From 56d6f4f44eb85cee3a5ab8d6e75ea01ae2604b1b Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 29 Aug 2023 12:26:31 +0300 Subject: [PATCH 3/8] Use parametrize for tests --- test_logistic.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/test_logistic.py b/test_logistic.py index 3061761..8aa5376 100644 --- a/test_logistic.py +++ b/test_logistic.py @@ -1,24 +1,24 @@ +import pytest from numpy.testing import assert_allclose from logistic import f -def test_generic_cases(): - cases = [ +cases = [ (0.1, 2.2, 0.198), (0.2, 3.4, 0.544), (0.5, 2.0, 0.5) ] - for x, r, expected in cases: - result = f(x, r) - assert_allclose(result, expected) +@pytest.mark.parametrize('x, r, expected', cases) +def test_generic_cases(x, r, expected): + result = f(x, r) + assert_allclose(result, expected) -def test_f_corner_cases(): - # Test cases are (x, r, expected) - cases = [ +cases = [ (0, 1.1, 0), (1, 3.7, 0), ] - for x, r, expected in cases: - result = f(x, r) - assert_allclose(result, expected) +@pytest.mark.parametrize('x, r, expected', cases) +def test_f_corner_cases(x, r, expected): + result = f(x, r) + assert_allclose(result, expected) From 69016b4fd8f41d17559ea3a897c61534f918cb5f Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 29 Aug 2023 14:40:53 +0300 Subject: [PATCH 4/8] Add test for iteraterate_f --- test_logistic.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/test_logistic.py b/test_logistic.py index 8aa5376..f763941 100644 --- a/test_logistic.py +++ b/test_logistic.py @@ -1,7 +1,7 @@ import pytest from numpy.testing import assert_allclose -from logistic import f +from logistic import f, iterate_f cases = [ (0.1, 2.2, 0.198), @@ -22,3 +22,14 @@ def test_generic_cases(x, r, expected): def test_f_corner_cases(x, r, expected): result = f(x, r) assert_allclose(result, expected) + + +cases = [ + (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]) + ] +@pytest.mark.parametrize('it, x, r, expected', cases) +def test_iterate_f(it, x, r, expected): + result = iterate_f(it, x, r) + assert_allclose(result, expected, atol=0.000001) \ No newline at end of file From 3e30009e6d81c4c84222f757b9ef807755c30425 Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 29 Aug 2023 14:41:16 +0300 Subject: [PATCH 5/8] Implement iterate_f --- logistic.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/logistic.py b/logistic.py index eb45a52..3232680 100644 --- a/logistic.py +++ b/logistic.py @@ -1,3 +1,9 @@ def f(x, r): return r * x * (1-x) +def iterate_f(it, x, r): + l = [x] + for i in range(it): + x = f(x, r) + l.append(x) + return l From 9594c44f7ebfe8f9d93ae3331105ac793cc95cab Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 29 Aug 2023 15:05:56 +0300 Subject: [PATCH 6/8] Add test for fit_r --- test_logistic.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/test_logistic.py b/test_logistic.py index f763941..bda1795 100644 --- a/test_logistic.py +++ b/test_logistic.py @@ -1,7 +1,9 @@ import pytest +import numpy as np from numpy.testing import assert_allclose from logistic import f, iterate_f +from logistic_fit import fit_r cases = [ (0.1, 2.2, 0.198), @@ -32,4 +34,17 @@ def test_f_corner_cases(x, r, expected): @pytest.mark.parametrize('it, x, r, expected', cases) def test_iterate_f(it, x, r, expected): result = iterate_f(it, x, r) - assert_allclose(result, expected, atol=0.000001) \ No newline at end of file + assert_allclose(result, expected, atol=0.000001) + + +cases = [ + (23, 0.3, 3.421), + (50, 0.6, 2.57), + (40, 0.8, 0), + (37, 0.1, 2.56) + ] +@pytest.mark.parametrize('it, x0, r', cases) +def test_fit_r(it, x0, r): + xs = iterate_f(it, x0, r) + result = fit_r(xs) + assert np.isclose(result, r, atol=0.0001) \ No newline at end of file From b4addbcb3c12d2d80b9ea077b9ad329262738d9e Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 29 Aug 2023 15:17:35 +0300 Subject: [PATCH 7/8] Add test for convergence in iteration --- test_logistic.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/test_logistic.py b/test_logistic.py index bda1795..87347f1 100644 --- a/test_logistic.py +++ b/test_logistic.py @@ -47,4 +47,20 @@ def test_iterate_f(it, x, r, expected): def test_fit_r(it, x0, r): xs = iterate_f(it, x0, r) result = fit_r(xs) - assert np.isclose(result, r, atol=0.0001) \ No newline at end of file + assert np.isclose(result, r, atol=0.0001) + + +def test_iterate_f_convergence(): + r=1.5 + it=35 + + random_state=np.random.RandomState(seed=42) + + for _ in range(100): + x0 = random_state.uniform(0.0001, 0.99999) + + result = iterate_f(it, x0, r) + expected = 1/3 + + assert np.isclose(result[-1], expected, atol=0.0001) + From 76dd048db22c22a40cc55b6dccf1f5771bf7136d Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 29 Aug 2023 15:38:55 +0300 Subject: [PATCH 8/8] Add random state fixture --- test_logistic.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/test_logistic.py b/test_logistic.py index 87347f1..8a7da52 100644 --- a/test_logistic.py +++ b/test_logistic.py @@ -49,18 +49,24 @@ def test_fit_r(it, x0, r): result = fit_r(xs) assert np.isclose(result, r, atol=0.0001) +SEED = np.random.randint(0, 2**31) -def test_iterate_f_convergence(): +@pytest.fixture +def random_state(): + print(f"Seed: ", SEED) + random_state = np.random.RandomState(SEED) + return random_state + + +def test_iterate_f_convergence(random_state): r=1.5 it=35 - - random_state=np.random.RandomState(seed=42) + expected = 1/3 for _ in range(100): x0 = random_state.uniform(0.0001, 0.99999) result = iterate_f(it, x0, r) - expected = 1/3 assert np.isclose(result[-1], expected, atol=0.0001)