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

Test for logistic fit #15

Open
wants to merge 6 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
16 changes: 16 additions & 0 deletions logistic.py
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
# Your code goes here
def f(x,r):
return r*x*(1-x)

def iterate_f(x0,r,it):
iteration_timecourse = []

iteration_timecourse.append(x0)

x = f(x0,r)
iteration_timecourse.append(x)

for i in range(it-1):
x = f(x,r)
iteration_timecourse.append(x)

return iteration_timecourse
2 changes: 1 addition & 1 deletion logistic_fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def fit_r(xs):
it = len(xs) - 1

def error(r):
return np.linalg.norm(xs - iterate_f(it, x0, r))
return np.linalg.norm(xs - iterate_f(x0, r, it))

errors = []
for r in np.linspace(0, 4, 4001):
Expand Down
17 changes: 14 additions & 3 deletions plot_logistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from logistic import iterate_f


def plot_trajectory(n, r, x0, fname="single_trajectory.png"):
def plot_trajectory(x0,r,n, fname="single_trajectory.png"):
"""
Saves a plot of a single trajectory of the logistic function

Expand All @@ -23,9 +23,9 @@ def plot_trajectory(n, r, x0, fname="single_trajectory.png"):
returns
fig, ax (matplotlib objects)
"""
xs = iterate_f(n, x0, r)
xs = iterate_f(x0, r, n)
fig, ax = plt.subplots(figsize=(10, 5))
ax.plot(list(range(n)), xs)
ax.plot(list(range(n+1)), xs)
fig.suptitle('Logistic Function')

fig.savefig(fname)
Expand Down Expand Up @@ -68,3 +68,14 @@ def plot_bifurcation(start, end, step, fname="bifurcation.png", it=100000,
ax.set_xlabel("r")
fig.savefig(fname)
return fig, ax





x0=0.1
r=1.5
n=20

plot_trajectory(x0, r, n)

Binary file added single_trajectory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
66 changes: 55 additions & 11 deletions test_logistic.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,60 @@
from numpy.testing import assert_allclose
from logistic import f, iterate_f
import pytest
from math import isclose
import numpy as np

from logistic import f

SEED = np.random.randint(0, 2**31)
# Add here your test for the logistic map

# def test_f_general():
# 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)


# 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)


# @pytest.mark.parametrize('x,r,expected',[(0.1,2.2,0.198),(0.2,3.4,0.544),(0.5,2,0.5),(0,1.1,0),(1,3.7,0)])
# def test_f(x,r,expected):
# result = f(x,r)
# assert isclose(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_iterate_f(x,r,it,expected):
# result = iterate_f(x,r,it)
# assert_allclose(result,expected,atol=0.001)

@pytest.fixture
def random_state():
print(f'\nUsing Seed: {SEED}')
random_state = np.random.RandomState(SEED)
return random_state

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_random_starting_point_convergence(random_state):
r = 1.5
n = 100
it = 30
n_convergence_datapoints = 3
for _ in range(n):
x0 = random_state.uniform(0.0001, 0.9999)
xs = iterate_f(x0,r,it)
for i in range(1, n_convergence_datapoints+1):
assert np.isclose(xs[-i], 1/3, atol=0.001)