예제 #1
0
def true_nll_gaussian(x, mu, sigma):
    x = convert_to_container(x, container=tuple)
    mu = convert_to_container(mu, container=tuple)
    sigma = convert_to_container(sigma, container=tuple)
    constraint = z.constant(0.0)
    if not len(x) == len(mu) == len(sigma):
        raise ValueError("params, mu and sigma have to have the same length.")
    for x_, mean, sig in zip(x, mu, sigma):
        constraint += z.reduce_sum(z.square(x_ - mean) / (2.0 * z.square(sig)))

    return constraint
예제 #2
0
def true_nll_gaussian(params, mu, sigma):
    params = convert_to_container(params, container=tuple)
    mu = convert_to_container(mu, container=tuple)
    sigma = convert_to_container(sigma, container=tuple)
    constraint = ztf.constant(0.)
    if not len(params) == len(mu) == len(sigma):
        raise ValueError("params, mu and sigma have to have the same length.")
    for param, mean, sig in zip(params, mu, sigma):
        constraint += ztf.reduce_sum(
            ztf.square(param - mean) / (2. * ztf.square(sig)))

    return constraint
예제 #3
0
파일: parameter.py 프로젝트: mozgit/zfit
def set_values(params: Union[Parameter, Iterable[Parameter]],
               values: Union[ztyping.NumericalScalarType,
                             Iterable[ztyping.NumericalScalarType],
                             ZfitResult]):
    """Set the values (using a context manager or not) of multiple parameters.

    Args:
        params: Parameters to set the values
        values: list-like object that supports indexing

    Returns:

    """
    params = convert_to_container(params)
    if isinstance(values, ZfitResult):
        result = values
        values = []
        for param in params:
            if not param in result.params:
                raise ValueError(
                    f"Cannot set {param} with {repr(result)} as it is not contained."
                )
            values.append(result.params[param]['value'])
    elif len(params) > 1:
        if not tf.is_tensor(values) or isinstance(values, np.ndarray):
            values = convert_to_container(values)
            if not len(params) == len(values):
                raise ValueError(
                    f"Incompatible length of parameters and values: {params}, {values}"
                )
    if not all(param.independent for param in params):
        raise ParameterNotIndependentError(
            f'trying to set value of parameters that are not independent '
            f'{[param for param in params if not param.independent]}')

    def setter(values):
        for i, param in enumerate(params):
            param.set_value(values[i])

    def getter():
        return [param.read_value() for param in params]

    return TemporarilySet(values, setter=setter, getter=getter)
예제 #4
0
    def sort_by_axes(self, axes, allow_superset: bool = False):
        axes = convert_to_container(axes)
        new_reorder_list = [self._reorder_indices_list[self.space.axes.index(ax)] for ax in axes]
        value = self.space.with_axes(axes=axes), new_reorder_list

        getter = lambda: (self.space, self._reorder_indices_list)

        def setter(value):
            self._space, self._reorder_indices_list = value

        return TemporarilySet(value=value, getter=getter, setter=setter)
예제 #5
0
    def sort_by_obs(self, obs, allow_superset: bool = False):
        obs = convert_to_container(obs)
        new_reorder_list = [self._reorder_indices_list[self.space.obs.index(ob)] for ob in obs]

        value = self.space.with_obs(obs=obs), new_reorder_list

        getter = lambda: (self.space, self._reorder_indices_list)

        def setter(value):
            self._space, self._reorder_indices_list = value

        return TemporarilySet(value=value, getter=getter, setter=setter)
예제 #6
0
파일: dependents.py 프로젝트: mozgit/zfit
def _extract_dependencies(zfit_objects: Iterable[ZfitObject]) -> ztyping.DependentsType:
    """Calls the :py:meth:`~BaseDependentsMixin.get_dependents` method on every object and returns a combined set.

    Args:
        zfit_objects ():

    Returns:
        set(zfit.Parameter): A set of independent Parameters
    """
    zfit_objects = convert_to_container(zfit_objects)
    dependents = (obj.get_cache_deps(only_floating=False) for obj in zfit_objects)
    dependents_set = OrderedSet(itertools.chain.from_iterable(dependents))  # flatten
    return dependents_set
예제 #7
0
 def __init__(self,
              name,
              value_fn,
              dependents,
              dtype=ztypes.float,
              **kwargs):  # TODO: automatize dependents
     dependents = convert_to_container(dependents)
     if dependents is None:
         params = OrderedSet()
     else:
         params = self._extract_dependents(dependents)
     params = {p.name: p for p in params}
     super().__init__(params=params,
                      value_fn=value_fn,
                      name=name,
                      dtype=dtype,
                      **kwargs)
예제 #8
0
    def __init__(self,
                 name: str,
                 value_fn: Callable,
                 params: Union[Dict[str,
                                    ZfitParameter], Iterable[ZfitParameter],
                               ZfitParameter] = NotSpecified,
                 dtype: tf.dtypes.DType = ztypes.float,
                 dependents: Union[Dict[str, ZfitParameter],
                                   Iterable[ZfitParameter],
                                   ZfitParameter] = NotSpecified):
        """Arbitrary composition of parameters.

        A `ComposedParameter` allows for arbitrary combinations of parameters and correlations

        Args:
            name: Unique name of the Parameter
            value_fn: Function that returns the value of the composed parameter and takes as arguments `params` as
                arguments.
            params: If it is a `dict`, this will direclty be used as the `params` attribute, otherwise the
                parameters will be automatically named with f"param_{i}". The values act as arguments to `value_fn`.
            dtype: Output of `value_fn` dtype
            dependents:
                .. deprecated:: unknown
                    use `params` instead.
        """
        if dependents is not NotSpecified:
            params = dependents
            warnings.warn("`dependents` is deprecated, use `params` instead.")
        elif params is NotSpecified:
            raise ValueError
        if isinstance(params, dict):
            params_dict = params
        else:
            params = convert_to_container(params)
            if params is None:
                params_dict = {}
            else:
                params_dict = {f'param_{i}': p for i, p in enumerate(params)}
        super().__init__(params=params_dict,
                         value_fn=value_fn,
                         name=name,
                         dtype=dtype)
예제 #9
0
파일: cache.py 프로젝트: vincecr0ft/zfit
    def add_cache_dependents(self, cache_dependents: ztyping.CacherOrCachersType,
                             allow_non_cachable: bool = True):
        """Add dependents that render the cache invalid if they change.

        Args:
            cache_dependents (ZfitCachable):
            allow_non_cachable (bool): If `True`, allow `cache_dependents` to be non-cachables.
                If `False`, any `cache_dependents` that is not a `ZfitCachable` will raise an error.

        Raises:
            TypeError: if one of the `cache_dependents` is not a `ZfitCachable` _and_ `allow_non_cachable`
                if `False`.
        """
        cache_dependents = convert_to_container(cache_dependents)
        for cache_dependent in cache_dependents:
            if isinstance(cache_dependent, ZfitCachable):
                cache_dependent.register_cacher(self)
            elif not allow_non_cachable:
                raise TypeError("cache_dependent {} is not a `ZfitCachable` but {}".format(cache_dependent,
                                                                                           type(cache_dependent)))
예제 #10
0
파일: values.py 프로젝트: zfit/zfit
    def __init__(
        self,
        args,
        variables: Mapping,
        norm: ValueHolder = None,
        target=None,
        holders=None,
    ):
        args = convert_to_container(args)
        variables = self._check_input_variables(variables)
        varmap = {name: var.name for name, var in variables.items()}
        self.target = target
        self._varmap = varmap
        self._vararg = self._create_vararg_map(args, varmap)

        # needed to create auto composite
        self.norm = norm
        self.holders = holders
        self.variables = variables
        self.args = args
예제 #11
0
    def __init__(self,
                 data: tf.Tensor,
                 bandwidth: ztyping.ParamTypeInput,
                 obs: ztyping.ObsTypeInput,
                 name: str = "GaussianKDE"):
        """Gaussian Kernel Density Estimation using Silverman's rule of thumb

        Args:
            data: Data points to build a kernel around
            bandwidth: sigmas for the covariance matrix of the multivariate gaussian
            obs:
            name: Name of the PDF
        """
        dtype = zfit.settings.ztypes.float
        if isinstance(data, zfit.core.interfaces.ZfitData):

            raise WorkInProgressError("Currently, no dataset supported yet")
            # size = data.nevents
            # dims = data.n_obs
            # with data.
            # data = data.value()
            # if data.weights is not None:

        else:
            if not isinstance(data, tf.Tensor):
                data = ztf.convert_to_tensor(value=data)
            data = ztf.to_real(data)

            shape_data = tf.shape(data)
            size = tf.cast(shape_data[0], dtype=dtype)
            dims = tf.cast(shape_data[-1], dtype=dtype)
        bandwidth = convert_to_container(bandwidth)

        # Bandwidth definition, use silverman's rule of thumb for nd
        def reshaped_kerner_factory():
            cov = tf.linalg.diag([
                tf.square((4. / (dims + 2.))**(1 / (dims + 4)) *
                          size**(-1 / (dims + 4)) * s) for s in bandwidth
            ])
            # kernel prob output shape: (n,)
            kernel = tfd.MultivariateNormalFullCovariance(
                loc=data, covariance_matrix=cov)
            return tfd.Independent(kernel)

        # reshaped_kernel = kernel

        probs = tf.broadcast_to(1 / size, shape=(tf.cast(size, tf.int32), ))
        categorical = tfd.Categorical(
            probs=probs)  # no grad -> no need to recreate
        dist_kwargs = lambda: dict(mixture_distribution=categorical,
                                   components_distribution=
                                   reshaped_kerner_factory())
        distribution = tfd.MixtureSameFamily
        # TODO lambda for params
        params = OrderedDict(
            (f"bandwidth_{i}", h) for i, h in enumerate(bandwidth))
        super().__init__(distribution=distribution,
                         dist_params={},
                         dist_kwargs=dist_kwargs,
                         params=params,
                         obs=obs,
                         name=name)