Skip to content

Commit

Permalink
Merge pull request #19 from arm61/variable_mode
Browse files Browse the repository at this point in the history
Variable mode
  • Loading branch information
Andrew McCluskey committed Jun 3, 2020
2 parents b814f10 + d5d0eb0 commit ca757a2
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 9 deletions.
1 change: 1 addition & 0 deletions docs/source/max_likelihood.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@
"metadata": {},
"source": [
"We can see that the variables have changed to be close to the values used in the data synthesis. \n",
"Note, that here `variable_medians` are in fact the variable values that maximise the likelihood.\n",
"\n",
"Let's inspect the model visually. \n",
"This can be achieved easily with the `plotting` module in `uravu`."
Expand Down
40 changes: 33 additions & 7 deletions docs/source/mcmc.ipynb

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions uravu/distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,16 @@ def negative_pdf(self, x):
@property
def dist_max(self):
"""
Get the value that maximises the distribution.
Get the value that maximises the distribution. If no :py:attr:`kde` has been created (for example if the distribution has fewer than 8 values) the median is returned.
Returns
:py:attr:`float`: Most likely value.
"""
return minimize(self.negative_pdf, x0=[self.n]).x
try:
return minimize(self.negative_pdf, x0=[self.n]).x
except AttributeError:
return self.n


@property
def min(self):
Expand Down
13 changes: 13 additions & 0 deletions uravu/relationship.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,19 @@ def variable_medians(self):
medians[i] = var.n
return medians

@property
def variable_modes(self):
"""
The mode values for each of the variables.
Returns:
:py:attr:`array_like`: Variable modes.
"""
modes = np.zeros((len(self.variables)))
for i, var in enumerate(self.variables):
modes[i] = var.dist_max
return modes

@property
def mcmc_done(self):
"""
Expand Down
11 changes: 11 additions & 0 deletions uravu/tests/test_relationship.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@ def test_variable_medians(self):
r.max_likelihood('diff_evo')
assert_equal(np.allclose(r.variable_medians, [1, 0], atol=1.5), True)

def test_variable_modes(self):
r = Relationship(utils.straight_line, TEST_X, TEST_Y, bounds=((0, 10), (-1, 1)))
r.max_likelihood('diff_evo')
r.mcmc(n_burn=10, n_samples=10, progress=False, walkers=5)
assert_equal(np.allclose(r.variable_modes, [1, 0], atol=1.5), True)

def test_variable_modes_no_sampling(self):
r = Relationship(utils.straight_line, TEST_X, TEST_Y, bounds=((0, 10), (-1, 1)))
r.max_likelihood('diff_evo')
assert_equal(np.allclose(r.variable_modes, [1, 0], atol=1.5), True)

def test_mcmc_done(self):
r = Relationship(utils.straight_line, TEST_X, TEST_Y, bounds=((0, 10), (-1, 1)))
r.mcmc(n_burn=10, n_samples=10, progress=False, walkers=5)
Expand Down

0 comments on commit ca757a2

Please sign in to comment.