```python
import torch
from torch.distributions.uniform import Uniform
import numpy as np
from matplotlib import pyplot as plt
from torch.distributions.utils import broadcast_all
```


```python
from exponential import RightTruncatedExponential
```

# Mixture of Discrete and Continuous over [0,1)

This distribution is a mixture of a point masses at $0$ and a continuous distribution over the support $(0, 1)$.

The distribution function looks like this:

\begin{equation}
f(x|\alpha, \beta) = \alpha \delta(x) + (1-\alpha) g(x|\beta)
\end{equation}

where $0 < \alpha < 1$ and $g(x|\beta)$ is a (properly normalised) density over $(0,1)$.


```python
from mixture import MixtureD0C01 as Mixture
from utils import mc_entropy, mc_kl
```


```python
logits = torch.from_numpy(np.array([0.])).float()
logits
```




    tensor([0.])




```python
p = Mixture(logits0=logits, 
            cont=RightTruncatedExponential(rate=torch.ones(1), upper=1.))
print(p.p0, p.pc)
```

    tensor([0.5000]) tensor([0.5000])



```python
x = p.rsample(torch.Size([10]))
x
```




    tensor([[0.0160],
            [0.6370],
            [0.0000],
            [0.0000],
            [0.7453],
            [0.0000],
            [0.0000],
            [0.0000],
            [0.0133],
            [0.0000]])




```python
_= plt.hist(p.rsample(torch.Size([1000])).numpy(), bins=50)
```


```python
p2 = Mixture(logits0=torch.ones(1), 
            cont=RightTruncatedExponential(rate=torch.ones(1) * 1., upper=1.))
print(p2.p0, p2.pc)
```

    tensor([0.7311]) tensor([0.2689])



```python
kl_mc(p, p2, 1000000)
```




    tensor([0.1205])




```python
torch.distributions.kl.kl_divergence(p, p2)
```




    tensor([0.1201])




```python

```

eded4dde6f4369834d94ae09428fca296b4c6aa8
ipynb
Jupyter Notebook
Mixture.ipynb
akashrajkn/sparse-distributions
961447fef7307905cf3d8a32ac31d5473c6c1f20
MIT
Mixture.ipynb
akashrajkn/sparse-distributions
961447fef7307905cf3d8a32ac31d5473c6c1f20
MIT
Mixture.ipynb
akashrajkn/sparse-distributions
961447fef7307905cf3d8a32ac31d5473c6c1f20
MIT
Qwen/Qwen-72B
1. YES
2. YES