CDF for Beta Distribution missing, any implementation tips?

It seems that the cdf for most distributions works, e.g.,

import torch
from torch.distributions.laplace import Laplace

m = Laplace(torch.tensor([0.0]), torch.tensor([1.0]))
m.cdf(-0.0886)

However, for Beta distributions, it does not seem to be implemented yet:

from torch.distributions.beta import Beta

m = Beta(torch.tensor([0.5]), torch.tensor([0.5]))
m.cdf(0.0029)

---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-10-b5e9a04733d4> in <module>
      2 
      3 m = Beta(torch.tensor([0.5]), torch.tensor([0.5]))
----> 4 m.cdf(0.0029)

~/miniconda3/lib/python3.7/site-packages/torch/distributions/distribution.py in cdf(self, value)
    152             value (Tensor):
    153         """
--> 154         raise NotImplementedError
    155 
    156     def icdf(self, value):

NotImplementedError: 

I was searching for GitHub PRs/Issues but couldn’t find any. Does anyone have any tips regarding an efficient implementation in PyTorch for this? I was wondering, maybe someone implemented this already in a numerically stable way or so. Any pointers would be appreciated!

1 Like

Something like this with numerical integration using torch.trapz?

import torch
from torch.distributions import Beta

conc1, conc0 = 2.0, 3.0
bt = Beta(conc1, conc0)
def beta_cdf(b, npts=100):
    x = torch.linspace(0, b, npts)
    return torch.trapz(bt.log_prob(x).exp(), x)
beta_cdf(0.65)