Skip to content

Commit

Permalink
Merge 2741406 into ed20d29
Browse files Browse the repository at this point in the history
  • Loading branch information
antonykamp committed Jun 19, 2020
2 parents ed20d29 + 2741406 commit e6f1f12
Show file tree
Hide file tree
Showing 3 changed files with 282 additions and 276 deletions.
17 changes: 17 additions & 0 deletions tests/auto_variables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import symfit as sf
import pytest

x, y, z = sf.variables('x, y, z')
a, b, c = sf.parameters('a, b, c')


@pytest.fixture(autouse=True)
def initVariales():
global x, y, z
x, y, z = sf.variables('x, y, z')


@pytest.fixture(autouse=True)
def initParameters():
global a, b, c
a, b, c = sf.parameters('a, b, c')
113 changes: 34 additions & 79 deletions tests/test_finite_difference.py
Original file line number Diff line number Diff line change
@@ -1,102 +1,57 @@
import symfit as sf
import numpy as np
import pytest
from tests.auto_variables import *


def setup_method():
np.random.seed(0)


def test_1_1_model():
'''Tests the case with 1 component and 1 parameter'''
x, y = sf.variables('x, y')
a = sf.Parameter(name='a')
model = sf.Model({y: 3 * a * x**2})
x_data = np.arange(10)

exact = model.eval_jacobian(x=x_data, a=3.5)
approx = model.finite_difference(x=x_data, a=3.5)
_assert_equal(exact, approx)

exact = model.eval_jacobian(x=3, a=3.5)
approx = model.finite_difference(x=3, a=3.5)
_assert_equal(exact, approx)


def test_1_multi_model():
'''Tests the case with 1 component and multiple parameters'''
x, y = sf.variables('x, y')
a, b = sf.parameters('a, b')
model = sf.Model({y: 3 * a * x**2 - sf.exp(b) * x})
x_data = np.arange(10)

exact = model.eval_jacobian(x=x_data, a=3.5, b=2)
approx = model.finite_difference(x=x_data, a=3.5, b=2)
_assert_equal(exact, approx)

exact = model.eval_jacobian(x=3, a=3.5, b=2)
approx = model.finite_difference(x=3, a=3.5, b=2)
_assert_equal(exact, approx)


def test_multi_1_model():
'''Tests the case with multiple components and one parameter'''
x, y, z = sf.variables('x, y, z')
a, = sf.parameters('a')
model = sf.Model({y: 3 * a * x**2,
z: sf.exp(a*x)})
x_data = np.arange(10)

exact = model.eval_jacobian(x=x_data, a=3.5)
approx = model.finite_difference(x=x_data, a=3.5)
_assert_equal(exact, approx)

exact = model.eval_jacobian(x=3, a=3.5)
approx = model.finite_difference(x=3, a=3.5)
_assert_equal(exact, approx)


def test_multi_multi_model():
'''Tests the case with multiple components and multiple parameters'''
x, y, z = sf.variables('x, y, z')
a, b, c = sf.parameters('a, b, c')
model = sf.Model({y: 3 * a * x**2 + b * x - c,
z: sf.exp(a*x - b) * c})
x_data = np.arange(10)

exact = model.eval_jacobian(x=x_data, a=3.5, b=2, c=5)
approx = model.finite_difference(x=x_data, a=3.5, b=2, c=5)
_assert_equal(exact, approx, rel=1e-3)

exact = model.eval_jacobian(x=3, a=3.5, b=2, c=5)
approx = model.finite_difference(x=3, a=3.5, b=2, c=5)
_assert_equal(exact, approx, rel=1e-3)


def test_multi_indep():
@pytest.mark.parametrize('x_data', [np.arange(10), 3])
@pytest.mark.parametrize('init_model,init_par',
[
({y: 3 * a * x**2}, {'a': 3.5}),
({y: 3 * a * x**2 - sf.exp(b) * x},
{'a': 3.5, 'b': 2}),
({y: 3 * a * x**2, z: sf.exp(a*x)}, {'a': 3.5}),
({y: 3 * a * x**2 + b * x - c, z: sf.exp(a*x - b) * c},
{'a': 3.5, 'b': 2, 'c': 5})
]
)
def test_model(x_data, init_model, init_par):
'''
Tests cases with: 1 component and 1 parameter,
1 component and multiple parameters,
multiple components and one parameter,
multiple components and multiple parameters
'''
model = sf.Model(init_model)
exact = model.eval_jacobian(x=x_data, **init_par)
approx = model.finite_difference(x=x_data, **init_par)
_assert_equal(exact, approx, rel=1e-5)


@pytest.mark.parametrize('x_data, w_data',
[
(np.arange(10)/10, np.arange(10)),
(0.3, np.arange(10)),
(0.3, 5)
]
)
def test_multi_indep(x_data, w_data):
'''
Tests the case with multiple components, multiple parameters and
multiple independent variables
'''
w, x, y, z = sf.variables('w, x, y, z')
a, b, c = sf.parameters('a, b, c')
w = sf.Variable('w')
model = sf.Model({y: 3 * a * x**2 + b * x * w - c,
z: sf.exp(a*x - b) + c*w})
x_data = np.arange(10)/10
w_data = np.arange(10)

exact = model.eval_jacobian(x=x_data, w=w_data, a=3.5, b=2, c=5)
approx = model.finite_difference(x=x_data, w=w_data, a=3.5, b=2, c=5)
_assert_equal(exact, approx)

exact = model.eval_jacobian(x=0.3, w=w_data, a=3.5, b=2, c=5)
approx = model.finite_difference(x=0.3, w=w_data, a=3.5, b=2, c=5)
_assert_equal(exact, approx)

exact = model.eval_jacobian(x=0.3, w=5, a=3.5, b=2, c=5)
approx = model.finite_difference(x=0.3, w=5, a=3.5, b=2, c=5)
_assert_equal(exact, approx)


def test_ODE_stdev():
"""
Expand Down
Loading

0 comments on commit e6f1f12

Please sign in to comment.