Skip to content

Commit

Permalink
tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew McCluskey committed Feb 25, 2020
1 parent 32993fc commit af35f32
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 47 deletions.
2 changes: 1 addition & 1 deletion uravu/sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def nested_sampling(
relationship.ordinate,
relationship.unaccounted_uncertainty,
],
ptform_args=[priors,],
ptform_args=[priors],
)
sampler.run_nested(print_progress=progress, **kwargs)
results = sampler.results
Expand Down
6 changes: 3 additions & 3 deletions uravu/tests/test_optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_ln_likelihood(self):
test_rel = relationship.Relationship(
utils.straight_line, test_x, test_y, test_y_e
)
expected_lnl = -1724236.163534402
expected_lnl = -1724226.97414907
actual_lnl = optimize.ln_likelihood(
test_rel.variables, test_rel.function, test_rel.x, test_rel.y
)
Expand All @@ -43,7 +43,7 @@ def test_negative_lnl_a(self):
test_rel = relationship.Relationship(
utils.straight_line, test_x, test_y, test_y_e
)
expected_negtive_lnl = 1724236.163534402
expected_negtive_lnl = 1724226.97414907
actual_negative_lnl = optimize.negative_lnl(
test_rel.variables, test_rel.function, test_rel.x, test_rel.y
)
Expand All @@ -63,7 +63,7 @@ def test_negative_lnl_b(self):
test_y_e,
unaccounted_uncertainty=True,
)
expected_negtive_lnl = 48.08862931921932
expected_negtive_lnl = 45.21123241122563
actual_negative_lnl = optimize.negative_lnl(
test_rel.variables,
test_rel.function,
Expand Down
72 changes: 39 additions & 33 deletions uravu/tests/test_relationship.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import unittest
import numpy as np
import uncertainties
import scipy.stats
from uncertainties import unumpy as unp
from numpy.testing import assert_almost_equal, assert_equal
from uravu import UREG, utils
Expand Down Expand Up @@ -39,14 +40,14 @@
)

STRING_C = (
"Function Name: straight_line \n"
"Abscissa: [ 1.00e+00 2.00e+00 ... 9.00e+00 1.00e+01 ] \n"
"Ordinate: [ 1.62e+00 -6.12e-01 ... 3.19e-01 -2.49e-01 ] \n"
"Ordinate uncertainty: [ 1.00e-01 1.00e-01 ... 1.00e-01 1.00e-01 ]\n"
"Abscissa Name: x \nOrdinate Name: y \nAbscissa Unit: dimensionless \n"
"Ordinate Unit: dimensionless \nVariables: [ 1.00e+00 1.00e+00 ] \n"
"ln(evidence): (-7.89+/-nan)e+02 \nUnaccounted uncertainty: False \n"
"MCMC performed: False \nNested sampling performed: True \n"
"Function Name: straight_line \nAbscissa: [ 1.00e+00 2.00e+00 ... "
"9.00e+00 1.00e+01 ] \nOrdinate: [ 1.62e+00 -6.12e-01 ... 3.19e-01 "
"-2.49e-01 ] \nOrdinate uncertainty: [ 1.00e-01 1.00e-01 ... 1.00e-01 "
"1.00e-01 ]\nAbscissa Name: x \nOrdinate Name: y \nAbscissa Unit: "
"dimensionless \nOrdinate Unit: dimensionless \nVariables: [ 1.00e+00 "
"1.00e+00 ] \nln(evidence): (-7.84+/-0.01)e+02 \nUnaccounted "
"uncertainty: False \nMCMC performed: False \nNested sampling performed: "
"True \n"
)

STRING_D = (
Expand Down Expand Up @@ -74,27 +75,24 @@
)

STRING_F = (
"Function Name: straight_line \n"
"Abscissa: [ 1.00e+00 2.00e+00 ... 9.00e+00 1.00e+01 ] \n"
"Ordinate: [ 1.62e+00 -6.12e-01 ... 3.19e-01 -2.49e-01 ] \n"
"Ordinate uncertainty: [ 1.00e-01 1.00e-01 ... 1.00e-01 1.00e-01 ]\n"
"Abscissa Name: x \nOrdinate Name: y \nAbscissa Unit: dimensionless \n"
"Ordinate Unit: dimensionless \n"
"Variables: [ 1.13e-02+3.17e+00-9.35e-01 3.41e-01+4.90e+00-3.18e+00 ] \n"
"Unaccounted uncertainty: False \nMCMC performed: True \n"
"Nested sampling performed: False \n"
"Function Name: straight_line \nAbscissa: [ 1.00e+00 2.00e+00 ... "
"9.00e+00 1.00e+01 ] \nOrdinate: [ 1.62e+00 -6.12e-01 ... 3.19e-01 "
"-2.49e-01 ] \nOrdinate uncertainty: [ 1.00e-01 1.00e-01 ... 1.00e-01 "
"1.00e-01 ]\nAbscissa Name: x \nOrdinate Name: y \nAbscissa Unit: "
"dimensionless \nOrdinate Unit: dimensionless \nVariables: [ "
"9.99e-01+2.00e+00-1.23e-02 1.00e+00+2.01e+00-1.45e-02 ] \nUnaccounted "
"uncertainty: False \nMCMC performed: True \nNested sampling performed: "
"False \n"
)

STRING_G = (
"Function Name: straight_line \n"
"Abscissa: [ 1.00e+00 2.00e+00 ... 9.00e+00 1.00e+01 ] \n"
"Ordinate: [ 1.62e+00 -6.12e-01 ... 3.19e-01 -2.49e-01 ] \n"
"Ordinate uncertainty: [ 1.00e-01 1.00e-01 ... 1.00e-01 1.00e-01 ]\n"
"Abscissa Name: x \nOrdinate Name: y \nAbscissa Unit: dimensionless \n"
"Ordinate Unit: dimensionless \n"
"Variables: [ 1.13e-02+/-9.35e-01 3.41e-01+/-3.18e+00 ] \n"
"Unaccounted uncertainty: False \nMCMC performed: True \n"
"Nested sampling performed: False \n"
"Function Name: straight_line \nAbscissa: [ 1.00e+00 2.00e+00 ... "
"9.00e+00 1.00e+01 ] \nOrdinate: [ 1.62e+00 -6.12e-01 ... 3.19e-01 "
"-2.49e-01 ] \nOrdinate uncertainty: [ 1.00e-01 1.00e-01 ... 1.00e-01 "
"1.00e-01 ]\nAbscissa Name: x \nOrdinate Name: y \nAbscissa Unit: "
"dimensionless \nOrdinate Unit: dimensionless \nVariables: [ "
"9.99e-01+/-1.23e-02 1.00e+00+/-1.45e-02 ] \nUnaccounted uncertainty: "
"False \nMCMC performed: True \nNested sampling performed: False \n"
)

CITATION_A = (
Expand Down Expand Up @@ -608,12 +606,20 @@ def test_prior(self):
test_y_e = test_y * 0.1
test_rel = Relationship(utils.straight_line, test_x, test_y, test_y_e)
test_rel.max_likelihood()
result_priors = test_rel.prior(np.random.random((2, 100)))
assert_equal(result_priors.shape, (2, 100))
assert_equal(result_priors[0].min() > -18, True)
assert_equal(result_priors[0].max() < 22, True)
assert_equal(result_priors[1].min() > -9, True)
assert_equal(result_priors[1].max() < 11, True)
result_priors = test_rel.prior()
assert_equal(len(result_priors), 2)
assert_equal(
isinstance(
result_priors[0], scipy.stats._distn_infrastructure.rv_frozen
),
True,
)
assert_equal(
isinstance(
result_priors[1], scipy.stats._distn_infrastructure.rv_frozen
),
True,
)

def test_mcmc(self):
"""
Expand Down Expand Up @@ -765,7 +771,7 @@ def test_bic(self):
test_y = np.ones(10)
test_y_e = np.ones(10) * 0.1
test_rel = Relationship(utils.straight_line, test_x, test_y, test_y_e)
expected_bic = -23.06776100915812
expected_bic = -41.44653167325224
actual_bic = test_rel.bayesian_information_criteria()
assert_almost_equal(actual_bic, expected_bic)

Expand Down
20 changes: 10 additions & 10 deletions uravu/tests/test_sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ def test_mcmc_b(self):
test_rel = Relationship(utils.straight_line, test_x, test_y, test_y_e,)
test_rel.max_likelihood()

def other_prior(array):
def other_prior():
"""
Another potential prior.
"""
broad = np.copy(array)
priors = []
for i, variable in enumerate(test_rel.variables):
loc = variable - variable * 5
scale = (variable + variable * 5) - loc
broad[i] = norm.ppf(broad[i], loc=loc, scale=scale)
return broad
loc = variable
scale = 1
priors.append(norm(loc=loc, scale=scale))
return priors

actual_variables = sampling.mcmc(
test_rel, prior_function=other_prior, n_burn=10, n_samples=10
Expand Down Expand Up @@ -94,16 +94,16 @@ def test_nested_sampling_b(self):
test_rel = Relationship(utils.straight_line, test_x, test_y, test_y_e)
test_rel.max_likelihood()

def other_prior(array):
def other_prior():
"""
Another potential prior.
"""
broad = np.copy(array)
priors = []
for i, variable in enumerate(test_rel.variables):
loc = variable
scale = 1
broad[i] = norm.ppf(broad[i], loc=loc, scale=scale)
return broad
priors.append(norm(loc=loc, scale=scale))
return priors

actual_ln_evidence = sampling.nested_sampling(
test_rel, prior_function=other_prior, maxiter=10
Expand Down

0 comments on commit af35f32

Please sign in to comment.