Commit 2a40a142 by Theo Steininger

### Modified parameter handling in pipeline.

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