Skip to content

Commit

Permalink
#262 increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
DavAug committed Sep 21, 2022
1 parent cdd8174 commit 01a5cf5
Showing 1 changed file with 135 additions and 0 deletions.
135 changes: 135 additions & 0 deletions chi/tests/test_population_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,34 @@ def test_compute_sensitivities(self):
self.assertAlmostEqual(dtheta[0], ref_dmu)
self.assertAlmostEqual(dtheta[1], ref_dsigma)

# Matrix input
# Compute ref scores
parameters = [[mu], [sigma]]
ref_ll = self.pop_model.compute_log_likelihood(parameters, psis)
ref_dpsi = 0
ref_dmu = 0
ref_dsigma = -n_ids

# Compute log-likelihood and sensitivities
score, dpsi, dtheta = self.pop_model.compute_sensitivities(
parameters, psis)

self.assertAlmostEqual(score, ref_ll)
self.assertEqual(dpsi.shape, (n_ids, 1))
self.assertEqual(dtheta.shape, (2,))
self.assertAlmostEqual(dpsi[0, 0], ref_dpsi)
self.assertAlmostEqual(dpsi[1, 0], ref_dpsi)
self.assertAlmostEqual(dpsi[2, 0], ref_dpsi)
self.assertAlmostEqual(dpsi[3, 0], ref_dpsi)
self.assertAlmostEqual(dpsi[4, 0], ref_dpsi)
self.assertAlmostEqual(dpsi[5, 0], ref_dpsi)
self.assertAlmostEqual(dpsi[6, 0], ref_dpsi)
self.assertAlmostEqual(dpsi[7, 0], ref_dpsi)
self.assertAlmostEqual(dpsi[8, 0], ref_dpsi)
self.assertAlmostEqual(dpsi[9, 0], ref_dpsi)
self.assertAlmostEqual(dtheta[0], ref_dmu)
self.assertAlmostEqual(dtheta[1], ref_dsigma)

# Test case I.2: centered, with dlop_dpsi
psis = np.ones((n_ids, 1))
mu = 1
Expand Down Expand Up @@ -1852,6 +1880,14 @@ def test_compute_sensitivities(self):
self.assertEqual(dpsi.shape, (n_ids, n_dim))
self.assertEqual(dtheta.shape, (10,))

# Same, but unflattened
parameters = np.ones(n_ids * n_dim)
score, dpsi, dtheta = pop_model.compute_sensitivities(
parameters, observations, flattened=False)
self.assertTrue(np.isinf(score))
self.assertEqual(dpsi.shape, (n_ids, n_dim))
self.assertEqual(dtheta.shape, (n_ids, n_ids, n_dim))

# Test IV: n_ids=5, dim=2 3 dimensional input
n_ids = 5
n_dim = 2
Expand All @@ -1868,6 +1904,15 @@ def test_compute_sensitivities(self):
self.assertTrue(np.all(dpsi == 0))
self.assertTrue(np.all(dtheta == 0))

# Same, but unflattened
score, dpsi, dtheta = pop_model.compute_sensitivities(
parameters, observations, flattened=False)
self.assertEqual(score, 0)
self.assertEqual(dpsi.shape, (n_ids, n_dim))
self.assertEqual(dtheta.shape, (5, 5, 2))
self.assertTrue(np.all(dpsi == 0))
self.assertTrue(np.all(dtheta == 0))

# Test V: add dlogp_dpsi
n_ids = 5
n_dim = 2
Expand Down Expand Up @@ -2023,6 +2068,25 @@ def test_compute_individual_parameters(self):
self.assertEqual(ref_psis[8], psis[8, 0])
self.assertEqual(ref_psis[9], psis[9, 0])

# Same, but matrix input
etas = np.arange(10) * 0.1
theta = np.array([[0.3, 2]])
psis = self.non_centered.compute_individual_parameters(theta, etas)

ref_psis = np.exp(theta[0][0] + theta[0][1] * etas)

self.assertEqual(psis.shape, (10, 1))
self.assertEqual(ref_psis[0], psis[0, 0])
self.assertEqual(ref_psis[1], psis[1, 0])
self.assertEqual(ref_psis[2], psis[2, 0])
self.assertEqual(ref_psis[3], psis[3, 0])
self.assertEqual(ref_psis[4], psis[4, 0])
self.assertEqual(ref_psis[5], psis[5, 0])
self.assertEqual(ref_psis[6], psis[6, 0])
self.assertEqual(ref_psis[7], psis[7, 0])
self.assertEqual(ref_psis[8], psis[8, 0])
self.assertEqual(ref_psis[9], psis[9, 0])

def test_compute_log_likelihood(self):
# Hard to test exactly, but at least test some edge cases where
# loglikelihood is straightforward to compute analytically
Expand Down Expand Up @@ -2365,6 +2429,27 @@ def test_compute_sensitivities(self):
self.assertAlmostEqual(dtheta[0], ref_dmu)
self.assertAlmostEqual(dtheta[1], ref_dsigma)

# Same, but matrix input
parameters = [[mu_log, sigma_log]]
score, dpsi, dtheta = self.pop_model.compute_sensitivities(
parameters, psis)

self.assertAlmostEqual(score, ref_ll)
self.assertEqual(dpsi.shape, (n_ids, 1))
self.assertEqual(dtheta.shape, (2,))
self.assertAlmostEqual(dpsi[0, 0], ref_dpsi)
self.assertAlmostEqual(dpsi[1, 0], ref_dpsi)
self.assertAlmostEqual(dpsi[2, 0], ref_dpsi)
self.assertAlmostEqual(dpsi[3, 0], ref_dpsi)
self.assertAlmostEqual(dpsi[4, 0], ref_dpsi)
self.assertAlmostEqual(dpsi[5, 0], ref_dpsi)
self.assertAlmostEqual(dpsi[6, 0], ref_dpsi)
self.assertAlmostEqual(dpsi[7, 0], ref_dpsi)
self.assertAlmostEqual(dpsi[8, 0], ref_dpsi)
self.assertAlmostEqual(dpsi[9, 0], ref_dpsi)
self.assertAlmostEqual(dtheta[0], ref_dmu)
self.assertAlmostEqual(dtheta[1], ref_dsigma)

n_ids = 10

# Test case I.2: centered, with dlop_dpsi
Expand Down Expand Up @@ -2909,6 +2994,13 @@ def test_compute_sensitivities(self):
self.assertEqual(dpsi.shape, (4, 1))
self.assertEqual(dtheta.shape, (1,))

# Same, but unflattened
score, dpsi, dtheta = self.pop_model.compute_sensitivities(
parameters, observations, flattened=False)
self.assertEqual(score, -np.inf)
self.assertEqual(dpsi.shape, (4, 1))
self.assertEqual(dtheta.shape, (4, 1, 1))

# Test case II: all values agree with parameter
parameters = [1]
observations = [1, 1, 1, 1]
Expand Down Expand Up @@ -3394,6 +3486,11 @@ def test_fix_parameters_bad_input(self):
with self.assertRaisesRegex(ValueError, 'The name-value dictionary'):
self.pop_model.fix_parameters(name_value_dict)

def test_get_dim_names(self):
names = self.pop_model.get_dim_names()
self.assertEqual(len(names), 1)
self.assertEqual(names[0], 'Dim. 1')

def test_get_population_model(self):
pop_model = self.pop_model.get_population_model()
self.assertIsInstance(pop_model, chi.PopulationModel)
Expand Down Expand Up @@ -3510,6 +3607,22 @@ def test_set_get_covariate_names(self):

self.cpop_model.set_covariate_names(None)

def test_set_dim_names(self):
self.pop_model.set_dim_names(['Test'])
names = self.pop_model.get_dim_names()
self.assertEqual(len(names), 1)
self.assertEqual(names[0], 'Test')

self.pop_model.set_dim_names(None)
names = self.pop_model.get_dim_names()
self.assertEqual(len(names), 1)
self.assertEqual(names[0], 'Dim. 1')

def test_set_n_ids(self):
self.pop_model.set_n_ids(11)
nids = self.pop_model.n_ids()
self.assertEqual(nids, 0) # Lognormal model does not maintain n_ids

def test_set_get_parameter_names(self):
# Set some parameter names
names = ['Test 1', 'Test 2']
Expand Down Expand Up @@ -3825,6 +3938,28 @@ def test_compute_sensitivities(self):
self.assertAlmostEqual(dtheta[0], ref_dmu)
self.assertAlmostEqual(dtheta[1], ref_dsigma)

# Same, but matrix input
# Compute log-likelihood and sensitivities
parameters = [[mu], [sigma]]
score, dpsi, dtheta = self.pop_model.compute_sensitivities(
parameters, psis)

self.assertAlmostEqual(score, ref_ll)
self.assertEqual(dpsi.shape, (n_ids, 1))
self.assertEqual(dtheta.shape, (2,))
self.assertAlmostEqual(dpsi[0, 0], ref_dpsi)
self.assertAlmostEqual(dpsi[1, 0], ref_dpsi)
self.assertAlmostEqual(dpsi[2, 0], ref_dpsi)
self.assertAlmostEqual(dpsi[3, 0], ref_dpsi)
self.assertAlmostEqual(dpsi[4, 0], ref_dpsi)
self.assertAlmostEqual(dpsi[5, 0], ref_dpsi)
self.assertAlmostEqual(dpsi[6, 0], ref_dpsi)
self.assertAlmostEqual(dpsi[7, 0], ref_dpsi)
self.assertAlmostEqual(dpsi[8, 0], ref_dpsi)
self.assertAlmostEqual(dpsi[9, 0], ref_dpsi)
self.assertAlmostEqual(dtheta[0], ref_dmu)
self.assertAlmostEqual(dtheta[1], ref_dsigma)

score, dpsi, dtheta = self.pop_model.compute_sensitivities(
parameters, psis, flattened=False)
self.assertAlmostEqual(score, ref_ll)
Expand Down

0 comments on commit 01a5cf5

Please sign in to comment.