Commit 54abacb0 authored by Moritz Huebner's avatar Moritz Huebner

Merge branch 'allow_interpolated_prior_to_change' into 'master'

min and max as properties for interpolated prior

See merge request Monash/tupak!47
parents 5e12fbc1 8db9b393
Pipeline #20515 passed with stages
in 15 minutes and 14 seconds
......@@ -301,29 +301,15 @@ class TruncatedGaussian(Prior):
class Interped(Prior):
def __init__(self, xx, yy, minimum=None, maximum=None, name=None, latex_label=None):
def __init__(self, xx, yy, minimum=np.nan, maximum=np.nan, name=None, latex_label=None):
"""Initialise object from arrays of x and y=p(x)"""
Prior.__init__(self, name, latex_label)
all_interpolated = interp1d(x=xx, y=yy, bounds_error=False, fill_value=0)
if minimum is None or minimum < min(xx):
self.minimum = min(xx)
else:
self.minimum = minimum
if maximum is None or maximum > max(xx):
self.maximum = max(xx)
else:
self.maximum = maximum
self.xx = np.linspace(self.minimum, self.maximum, len(xx))
self.yy = all_interpolated(self.xx)
if np.trapz(self.yy, self.xx) != 1:
logging.info('Supplied PDF for {} is not normalised, normalising.'.format(self.name))
self.yy /= np.trapz(self.yy, self.xx)
self.YY = cumtrapz(self.yy, self.xx, initial=0)
# Need last element of cumulative distribution to be exactly one.
self.YY[-1] = 1
self.probability_density = interp1d(x=self.xx, y=self.yy, bounds_error=False, fill_value=0)
self.cumulative_distribution = interp1d(x=self.xx, y=self.YY, bounds_error=False, fill_value=0)
self.inverse_cumulative_distribution = interp1d(x=self.YY, y=self.xx, bounds_error=True)
self.xx = xx
self.yy = yy
self.all_interpolated = interp1d(x=xx, y=yy, bounds_error=False, fill_value=0)
Prior.__init__(self, name, latex_label,
minimum=np.nanmax(np.array((min(xx), minimum))),
maximum=np.nanmin(np.array((max(xx), maximum))))
self.__initialize_attributes()
def prob(self, val):
"""Return the prior probability of val"""
......@@ -344,9 +330,46 @@ class Interped(Prior):
def __repr__(self):
prior_name = self.__class__.__name__
prior_args = ', '.join(
['{}={}'.format(key, self.__dict__[key]) for key in ['xx', 'yy', '_Prior__latex_label']])
['{}={}'.format(name, self.__dict__[key]) for key, name in zip(['xx', 'yy', 'name', '_Prior__latex_label'],
['xx', 'yy', 'name', 'latex_label'])])
return "{}({})".format(prior_name, prior_args)
@property
def minimum(self):
return self.__minimum
@minimum.setter
def minimum(self, minimum):
self.__minimum = minimum
if '_Interped__maximum' in self.__dict__ and self.__maximum < np.inf:
self.__update_instance()
@property
def maximum(self):
return self.__maximum
@maximum.setter
def maximum(self, maximum):
self.__maximum = maximum
if '_Interped__minimum' in self.__dict__ and self.__minimum < np.inf:
self.__update_instance()
def __update_instance(self):
self.xx = np.linspace(self.minimum, self.maximum, len(self.xx))
self.yy = self.all_interpolated(self.xx)
self.__initialize_attributes()
def __initialize_attributes(self):
if np.trapz(self.yy, self.xx) != 1:
logging.info('Supplied PDF for {} is not normalised, normalising.'.format(self.name))
self.yy /= np.trapz(self.yy, self.xx)
self.YY = cumtrapz(self.yy, self.xx, initial=0)
# Need last element of cumulative distribution to be exactly one.
self.YY[-1] = 1
self.probability_density = interp1d(x=self.xx, y=self.yy, bounds_error=False, fill_value=0)
self.cumulative_distribution = interp1d(x=self.xx, y=self.YY, bounds_error=False, fill_value=0)
self.inverse_cumulative_distribution = interp1d(x=self.YY, y=self.xx, bounds_error=True)
class FromFile(Interped):
......@@ -365,7 +388,9 @@ class FromFile(Interped):
def __repr__(self):
prior_name = self.__class__.__name__
prior_args = ', '.join(
['{}={}'.format(key, self.__dict__[key]) for key in ['id', 'minimum', 'maximum', '_Prior__latex_label']])
['{}={}'.format(name, self.__dict__[key])
for key, name in zip(['id', '_Interped__minimum', '_Interped__maximum', 'name', '_Prior__latex_label'],
['id', 'minimum', 'maximum', 'name', 'latex_label'])])
return "{}({})".format(prior_name, prior_args)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment