### Modified parameter handling in pipeline.

`Added likelihood and prior base- and sum-classes.`
parent b1d911f5
 # -*- coding: utf-8 -*- import numpy as np def carrier_mapper(x, a=-np.inf, m=0, b=np.inf): """ Maps x from [-inf, inf] into the interval [a, b], where x=0 -> m """ if a == -np.inf and b == np.inf and m == 0: return x x = np.float(x) a = np.float(a) b = np.float(b) if m is None: if a == -np.inf and b == np.inf: m = 0 else: m = a + (b-a)/2. else: m = np.float(m) # map x from [-inf, inf] to [0, 1] y = np.arctan(x)/np.pi + 0.5 # compute where m would lie in [0, 1] n = (m - a)/(b - a) # strech y, such that x=0 -> n y = y**np.emath.logn(0.5, n) # strech y to the interval [a,b] y = y*(b-a) + a return y
 # -*- coding: utf-8 -*- from likelihood import Likelihood
 # -*- coding: utf-8 -*- import abc from keepers import Loggable class Likelihood(Loggable, object): @abc.abstractmethod def __call__(self, observables): raise NotImplementedError
 # -*- coding: utf-8 -*- from likelihood import Likelihood class SumLikelihood(Likelihood): def __init__(self, likelihoods): self.likelihoods = [] for likelihood in likelihoods: assert isinstance(likelihood, Likelihood) self.likelihoods += [likelihood] def __call__(self, observables): likelihood = 0 for current_likelihood in self.likelihoods: likelihood += current_likelihood(observables) return likelihood
 # -*- coding: utf-8 -*- from observer import Observer
 # -*- coding: utf-8 -*- class Observer(object): pass
 ... ... @@ -8,15 +8,18 @@ from likelihoods import Likelihood from observers import Observer from priors import Prior from carrier_mapper import carrier_mapper class Pipeline(Loggable, object): def __init__(self, observer, likelihood, prior, parameters=[], ensemble_size=1): def __init__(self, observer, likelihood, prior, ensemble_size=1, active_parameters=[], parameter_mapping={}): self.logger.debug("Setting up pipeline.") self.observer = observer self.likelihood = likelihood self.prior = prior self.parameters = parameters self.active_parameters = active_parameters self.parameter_mapping = parameter_mapping self.ensemble_size = ensemble_size @property ... ... @@ -51,23 +54,24 @@ class Pipeline(Loggable, object): "prior must be an instance of prior-class.") @property def parameters(self): return self._parameters def parameter_mapping(self): return self._parameter_mapping @parameters.setter def parameters(self, parameters): @parameter_mapping.setter def parameter_mapping(self, parameters): """ parameters is either a list of the parameter-names, or a list of lists containing [parameter-name, min, max, mean] The parameter-mapping must be a dictionary with key: parameter-name value: [min, mean, max] """ new_parameters = [] new_mapping = {} for p in parameters: if isinstance(p, list): new_parameters += [[str(p), p, p, p]] else: new_parameters += [[str(p), None, None, None]] self.logger.debug("Setting parameters to %s." % str(new_parameters)) self._parameters = new_parameters new_key = str(p) new_value = [p, p, p] new_mapping[new_key] = new_value self.logger.debug("Setting parameter_mapping %s to %s." % (new_key, new_mapping[new_key])) self._parameter_mapping = new_mapping @property def ensemble_size(self): ... ... @@ -82,52 +86,19 @@ class Pipeline(Loggable, object): self.logger.debug("Setting ensemble size to %i." % ensemble_size) self._ensemble_size = ensemble_size @staticmethod def carrier_mapper(x, a=-np.inf, b=np.inf, m=0): """ Maps x from [-inf, inf] into the interval [a, b], where x=0 -> m """ if a == -np.inf and b == np.inf and m == 0: return x x = np.float(x) a = np.float(a) b = np.float(b) if m is None: if a == -np.inf and b == np.inf: m = 0 def _map_parameters(self, parameter_list): parameter_dict = {} for i, name in enumerate(self.active_parameters): if name in self.parameter_mapping: mapping = self.parameter_mapping[name] mapped_parameter = carrier_mapper(parameter_list[i], a=mapping, m=mapping, b=mapping) else: m = a + (b-a)/2. else: m = np.float(m) # map x from [-inf, inf] to [0, 1] y = np.arctan(x)/np.pi + 0.5 # compute where m would lie in [0, 1] n = (m - a)/(b - a) # strech y, such that x=0 -> n y = y**np.emath.logn(0.5, n) # strech y to the interval [a,b] y = y*(b-a) + a return y mapped_parameter = np.float(parameter_list[i]) parameter_dict[name] = mapped_parameter return parameter_dict def __call__(self, parameter_list): parameter_dict = {} for (i, p) in enumerate(self.parameters): parameter_dict[p] = self.carrier_mapper(parameter_list[i], a=p, b=p, m=p) mapped_parameters = self._map_parameters(parameter_list)
 # -*- coding: utf-8 -*- from prior import Prior
 # -*- coding: utf-8 -*- import abc from keepers import Loggable class Prior(Loggable, object): @abc.abstractmethod def __call__(self, parameters): raise NotImplemented
 # -*- coding: utf-8 -*- from prior import Prior class SumPrior(Prior): def __init__(self, priors): self.priors = [] for prior in priors: assert isinstance(prior, Prior) self.priors += [prior] def __call__(self, parameters): prior = 0 for current_prior in self.priors: prior += current_prior(parameters) return prior
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!