Skip to content
Snippets Groups Projects
Commit 0489e6d4 authored by Gregory Ashton's avatar Gregory Ashton
Browse files

Simplify reference and update example

parent ff191b79
No related branches found
No related tags found
No related merge requests found
Pipeline #
......@@ -167,48 +167,10 @@ In the last example, we considered only cases with known noise (e.g., a
prespecified standard deviation. We now present a general function which can
handle unknown noise (in which case you need to specify a prior for
:math:`\sigma`, or known noise (in which case you pass the known noise in when
instatiating the likelihood::
instatiating the likelihood
class GaussianLikelihood(tupak.Likelihood):
def __init__(self, x, y, function, sigma=None):
"""
A general Gaussian likelihood for known or unknown noise - the model
parameters are inferred from the arguments of function
Parameters
----------
x, y: array_like
The data to analyse
function:
The python function to fit to the data. Note, this must take the
dependent variable as its first argument. The other arguments are
will require a prior and will be sampled over (unless a fixed
value is given).
sigma: None, float, array_like
If None, the standard deviation of the noise is unknown and will be
estimated (note: this requires a prior to be given for sigma). If
not None, this defined the standard-deviation of the data points.
This can either be a single float, or an array with length equal
to that for `x` and `y`.
"""
self.x = x
self.y = y
self.N = len(x)
self.function = function
# These lines of code infer the parameters from the provided function
parameters = inspect.getargspec(function).args
parameters.pop(0)
self.parameters = dict.fromkeys(parameters)
self.function_keys = self.parameters.keys()
self.parameters['sigma'] = None
def log_likelihood(self):
model_parameters = {k: self.parameters[k] for k in self.function_keys}
res = self.y - self.function(self.x, **model_parameters)
sigma = self.parameters['sigma']
return -0.5 * (np.sum((res / sigma)**2)
+ self.N*np.log(2*np.pi*sigma**2))
.. literalinclude:: ../examples/other_examples/linear_regression_unknown_noise.py
:lines: 52-94
An example using this likelihood can be found `on this page <https://git.ligo.org/Monash/tupak/blob/master/examples/other_examples/linear_regression_unknown_noise.py>`_.
......
......@@ -74,6 +74,7 @@ class GaussianLikelihood(tupak.Likelihood):
self.x = x
self.y = y
self.N = len(x)
self.sigma = sigma
self.function = function
# These lines of code infer the parameters from the provided function
......@@ -81,18 +82,15 @@ class GaussianLikelihood(tupak.Likelihood):
parameters.pop(0)
self.parameters = dict.fromkeys(parameters)
self.function_keys = self.parameters.keys()
if sigma is None:
if self.sigma is None:
self.parameters['sigma'] = None
self.sigma = self.parameters['sigma']
else:
self.sigma = sigma
def log_likelihood(self):
self.sigma = self.parameters['sigma']
sigma = self.parameters.get('sigma', self.sigma)
model_parameters = {k: self.parameters[k] for k in self.function_keys}
res = self.y - self.function(self.x, **model_parameters)
return -0.5 * (np.sum((res / self.sigma)**2)
+ self.N*np.log(2*np.pi*self.sigma**2))
return -0.5 * (np.sum((res / sigma)**2)
+ self.N*np.log(2*np.pi*sigma**2))
# Now lets instantiate a version of our GaussianLikelihood, giving it
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment