Beispiel #1
0
    def poisson(self, n, lam):
        r"""
        The continous approximation, using :math:`n! = \Gamma\left(n+1\right)`,
        to the probability mass function of the Poisson distribution evaluated
        at :code:`n` given the parameter :code:`lam`.

        Example:

            >>> import pyhf
            >>> pyhf.set_backend(pyhf.tensor.mxnet_backend())
            >>> pyhf.tensorlib.poisson(5., 6.)
            <BLANKLINE>
            [0.16062315]
            <NDArray 1 @cpu(0)>

        Args:
            n (Number or Tensor): The value at which to evaluate the approximation to the Poisson distribution p.m.f.
                                  (the observed number of events)
            lam (Number or Tensor): The mean of the Poisson distribution p.d.f.
                                    (the expected number of events)

        Returns:
            MXNet NDArray: Value of the continous approximation to Poisson(n|lam)
        """
        n = self.astensor(n)
        lam = self.astensor(lam)

        # This is currently copied directly from PyTorch's source until a better
        # way can be found to do this in MXNet
        # https://github.com/pytorch/pytorch/blob/39520ffec15ab7e97691fed048de1832e83785e8/torch/distributions/poisson.py#L59-L63
        return nd.exp((nd.log(lam) * n) - lam - nd.gammaln(n + 1.0))
Beispiel #2
0
 def log_prob(self, x: nd.NDArray) -> nd.NDArray:
     mean = self.get_param_maybe_repeated('mean')
     if x.ndim > mean.ndim:
         mean = nd.expand_dims(mean, 0)
     np_x = x.asnumpy().astype(np.int32).astype(np.float32)
     np.testing.assert_almost_equal(x.asnumpy(), np_x)
     return x * nd.log(mean) - mean - nd.gammaln(x + 1.)
Beispiel #3
0
 def poisson_logpdf(self, n, lam):
     n = self.astensor(n)
     lam = self.astensor(lam)
     return n * nd.log(lam) - lam - nd.gammaln(n + 1.0)