Skip to content

Commit

Permalink
improve test coverage and fix no uncertainty in y
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew McCluskey committed Mar 6, 2020
1 parent 043f10c commit 6be912a
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 62 deletions.
16 changes: 7 additions & 9 deletions uravu/optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,12 @@ def ln_likelihood(
var = variables
log_f = -np.inf
model = function(abscissa.m, *var)
if isinstance(ordinate.m.any(), uncertainties.core.AffineScalarFunc):
y_data = unp.nominal_values(ordinate.m)
dy_data = unp.std_devs(ordinate.m)

sigma2 = dy_data ** 2 + model ** 2 * np.exp(2 * log_f)
return -0.5 * np.sum((model - y_data) ** 2 / sigma2 + np.log(sigma2))
else:
y_data = ordinate.m
y_data = unp.nominal_values(ordinate.m)
dy_data = unp.std_devs(ordinate.m)

if np.isclose(dy_data.all(), 0.0):
sigma2 = model ** 2 * np.exp(2 * log_f)
return -0.5 * np.sum((model - y_data) ** 2 / sigma2 + np.log(sigma2))
else:
sigma2 = dy_data ** 2 + model ** 2 * np.exp(2 * log_f)
return -0.5 * np.sum((model - y_data) ** 2 / sigma2 + np.log(sigma2))

36 changes: 20 additions & 16 deletions uravu/relationship.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ class Relationship:
shape `(N,)`.
ordinate_uncertainty (array_like, optional): The uncertainty in each
of the ordinate data points. This should have a shape `(N,)`.
Default to no uncertainties on the ordiante
Default to no uncertainties on the ordiante. If there is no
ordinate uncertainty, an unaccounted uncertainty is automatically
added.
abscissa_uncertainty (array_like, optional): The uncertainty in each
of the absiccsa data points. This should have a shape `(N, d)`.
Default is no uncertainties on absicca.
Expand Down Expand Up @@ -109,32 +111,34 @@ def __init__(
if abscissa_uncertainty is not None:
abscissa_uncertainty = np.array(abscissa_uncertainty)
if abscissa.shape[0] == ordinate.shape[0]:
if ordinate.shape[0] == ordinate_uncertainty.shape[0]:
if (abscissa_uncertainty is not None) and (
abscissa.shape[0] == abscissa_uncertainty.shape[0]
):
self.abscissa = unp.uarray(abscissa, abscissa_uncertainty)
else:
self.abscissa = abscissa
self.abscissa *= abscissa_unit
if ordinate_uncertainty is not None:
if (abscissa_uncertainty is not None) and (
abscissa.shape[0] == abscissa_uncertainty.shape[0]
):
self.abscissa = unp.uarray(abscissa, abscissa_uncertainty)
else:
self.abscissa = abscissa
self.abscissa *= abscissa_unit
if ordinate_uncertainty is not None:
if ordinate.shape[0] == ordinate_uncertainty.shape[0]:
self.ordinate = (
unp.uarray(ordinate, ordinate_uncertainty)
* ordinate_unit
)
else:
self.ordinate = ordinate * ordinate_unit
raise ValueError(
"The number of data points in the ordinate does not "
"match that in the ordinate uncertainty."
)
else:
raise ValueError(
"The number of data points in the ordinate does not "
"match that in the ordinate uncertainty."
)
self.ordinate = ordinate * ordinate_unit
else:
raise ValueError(
"The number of data points in the abscissa does "
"not match that for the ordinate."
)
if unaccounted_uncertainty:
if (unp.std_devs(self.ordinate.m) == 0.0).any():
self.unaccounted_uncertainty = True
if self.unaccounted_uncertainty:
self.variables = np.ones((self.len_parameters() + 1))
else:
self.variables = np.ones((self.len_parameters()))
Expand Down
17 changes: 16 additions & 1 deletion uravu/tests/test_optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class TestOptimize(unittest.TestCase):
Unit tests for optimize module.
"""

def test_ln_likelihood(self):
def test_ln_likelihood_a(self):
"""
Test ln_likelihood function.
"""
Expand All @@ -33,6 +33,21 @@ def test_ln_likelihood(self):
)
assert_almost_equal(actual_lnl, expected_lnl)

def test_ln_likelihood_b(self):
"""
Test ln_likelihood function with no uncertainty.
"""
test_x = np.linspace(0, 99, 10)
test_y = np.ones(10)
test_rel = relationship.Relationship(
utils.straight_line, test_x, test_y
)
expected_lnl = -45.21054955719477
actual_lnl = optimize.ln_likelihood(
test_rel.variables, test_rel.function, test_rel.x, test_rel.y, unaccounted_uncertainty=test_rel.unaccounted_uncertainty
)
assert_almost_equal(actual_lnl, expected_lnl)

def test_negative_lnl_a(self):
"""
Test negative_lnl function.
Expand Down
Loading

0 comments on commit 6be912a

Please sign in to comment.