Skip to content

Commit 676f2c6

Browse files
committed
Merge branch 'feature/likelihood_check' into feature/keep_the_noise_down
2 parents b7c0ea2 + 1757da6 commit 676f2c6

File tree

1 file changed

+30
-21
lines changed

1 file changed

+30
-21
lines changed

autofit/non_linear/fitness.py

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import numpy as np
22
import os
3+
from typing import Optional
34

45
from autoconf import conf
56

67
from autofit import exc
78

9+
from autofit.mapper.prior_model.abstract import AbstractPriorModel
10+
from autofit.non_linear.paths.abstract import AbstractPaths
11+
from autofit.non_linear.analysis import Analysis
12+
813
from timeout_decorator import timeout
914

1015
from autofit import jax_wrapper
@@ -24,42 +29,42 @@ def get_timeout_seconds():
2429
class Fitness:
2530
def __init__(
2631
self,
27-
model,
28-
analysis,
29-
paths = None,
32+
model : AbstractPriorModel,
33+
analysis : Analysis,
34+
paths : Optional[AbstractPaths] = None,
3035
fom_is_log_likelihood: bool = True,
3136
resample_figure_of_merit: float = -np.inf,
3237
convert_to_chi_squared: bool = False,
3338
):
3439
"""
35-
Interfaces with any non-linear in order to fit a model to the data and return a log likelihood via
36-
an `Analysis` class.
40+
Interfaces with any non-linear search to fit the model to the data and return a log likelihood via
41+
the analysis.
3742
38-
The interface of a non-linear search and a fitness function can be summarized as follows:
43+
The interface of a non-linear search and fitness function is summarized as follows:
3944
40-
1) The non-linear search chooses a new set of parameters for the model, which are passed to the fitness
45+
1) The non-linear search samples a new set of model parameters, which are passed to the fitness
4146
function's `__call__` method.
4247
43-
2) The parameter values (typically a list) are mapped to an instance of the model (via its priors if
44-
appropriate for the non-linear search).
48+
2) The list of parameter values are mapped to an instance of the model.
4549
4650
3) The instance is passed to the analysis class's log likelihood function, which fits the model to the
4751
data and returns the log likelihood.
4852
4953
4) A final figure-of-merit is computed and returned to the non-linear search, which is either the log
50-
likelihood or log posterior depending on the type of non-linear search.
54+
likelihood or log posterior (e.g. adding the log prior to the log likelihood).
5155
52-
It is common for nested sampling algorithms to require that the figure of merit returned is a log likelihood
53-
as priors are often built into the mapping of values from a unit hyper-cube. Optimizers and MCMC methods
54-
typically require that the figure of merit returned is a log posterior, with the prior terms added via this
55-
fitness function. This is not a strict rule, but is a good default.
56+
Certain searches (commonly nested samplers) require the parameters to be mapped from unit values to physical
57+
values, which is performed internally by the fitness object in step 2.
5658
57-
Some methods also require a chi-squared value to be computed (which is minimized), which is the log likelihood
58-
multiplied by -2.0. The `Fitness` class can also compute this value, if the `convert_to_chi_squared` bool is
59-
`True`.
59+
Certain searches require the returned figure of merit to be a log posterior (often MCMC methods) whereas
60+
others require it to be a log likelihood (often nested samples which account for priors internally) in step 4.
61+
Which values is returned by the `fom_is_log_likelihood` bool.
6062
61-
If a model-fit raises an exception of returns a `np.nan` a `resample_figure_of_merit` value is returned. The
62-
appropriate value depends on the non-linear search, but is typically either `None`, `-np.inf` or `1.0e99`.
63+
Some searches require a chi-squared value (which they minimized), given by the log likelihood multiplied
64+
by -2.0. This is returned by the fitness if the `convert_to_chi_squared` bool is `True`.
65+
66+
If a model-fit raises an exception or returns a `np.nan`, a `resample_figure_of_merit` value is returned
67+
instead. The appropriate value depends on the search, but is typically either `None`, `-np.inf` or `1.0e99`.
6368
All values indicate to the non-linear search that the model-fit should be resampled or ignored.
6469
6570
Parameters
@@ -70,6 +75,9 @@ def __init__(
7075
model
7176
The model that is fitted to the data, which is used by the non-linear search to create instances of
7277
the model that are fitted to the data via the log likelihood function.
78+
paths
79+
The paths of the search, which if the search is being resumed from an old run is used to check that
80+
the likelihood function has not changed from the previous run.
7381
fom_is_log_likelihood
7482
If `True`, the figure of merit returned by the fitness function is the log likelihood. If `False`, the
7583
figure of merit is the log posterior.
@@ -88,7 +96,8 @@ def __init__(
8896
self.convert_to_chi_squared = convert_to_chi_squared
8997
self._log_likelihood_function = None
9098

91-
self.check_log_likelihood(fitness=self)
99+
if self.paths is not None:
100+
self.check_log_likelihood(fitness=self)
92101

93102
def __getstate__(self):
94103
state = self.__dict__.copy()
@@ -169,7 +178,7 @@ def check_log_likelihood(self, fitness):
169178
Parameters
170179
----------
171180
paths
172-
The PyAutoFit paths object which manages all paths, e.g. where the non-linear search outputs are stored,
181+
certain searches the non-linear search outputs are stored,
173182
visualization, and pickled objects used by the database and aggregator.
174183
result
175184
The result containing the maximum log likelihood fit of the model.

0 commit comments

Comments
 (0)