Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create logistic function and tests for generic cases #2

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion logistic.py
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
# Your code goes here
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
72 changes: 64 additions & 8 deletions test_logistic.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,72 @@
import pytest
import numpy as np
from numpy.testing import assert_allclose

from logistic import f
from logistic import f, iterate_f
from logistic_fit import fit_r

# Add here your test for the logistic map
cases = [
(0.1, 2.2, 0.198),
(0.2, 3.4, 0.544),
(0.5, 2.0, 0.5)
]
@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)


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)


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)

SEED = np.random.randint(0, 2**31)

@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
expected = 1/3

for _ in range(100):
x0 = random_state.uniform(0.0001, 0.99999)

result = iterate_f(it, x0, r)

assert np.isclose(result[-1], expected, atol=0.0001)