|
19 | 19 | from pytensor.xtensor import random as pxr
|
20 | 20 |
|
21 | 21 | from pymc.dims.distributions.core import VectorDimDistribution
|
22 |
| -from pymc.dims.distributions.transforms import ZeroSumTransform |
| 22 | +from pymc.dims.distributions.transforms import SimplexTransform, ZeroSumTransform |
23 | 23 | from pymc.distributions.multivariate import ZeroSumNormalRV
|
24 | 24 | from pymc.util import UNSET
|
25 | 25 |
|
@@ -63,6 +63,61 @@ def dist(cls, p=None, *, logit_p=None, core_dims=None, **kwargs):
|
63 | 63 | return super().dist([p], core_dims=core_dims, **kwargs)
|
64 | 64 |
|
65 | 65 |
|
| 66 | +class Dirichlet(VectorDimDistribution): |
| 67 | + """Dirichlet distribution. |
| 68 | +
|
| 69 | + Parameters |
| 70 | + ---------- |
| 71 | + a : xtensor_like, optional |
| 72 | + Probabilities of each category. Must sum to 1 along the core dimension. |
| 73 | + core_dims : str |
| 74 | + The core dimension of the distribution, which represents the categories. |
| 75 | + The dimension must be present in `p` or `logit_p`. |
| 76 | + **kwargs |
| 77 | + Other keyword arguments used to define the distribution. |
| 78 | +
|
| 79 | + Returns |
| 80 | + ------- |
| 81 | + XTensorVariable |
| 82 | + An xtensor variable representing the categorical distribution. |
| 83 | + The output does not contain the core dimension, as it is absorbed into the distribution. |
| 84 | +
|
| 85 | +
|
| 86 | + """ |
| 87 | + |
| 88 | + xrv_op = ptxr.dirichlet |
| 89 | + |
| 90 | + @classmethod |
| 91 | + def __new__( |
| 92 | + cls, *args, core_dims=None, dims=None, default_transform=UNSET, observed=None, **kwargs |
| 93 | + ): |
| 94 | + if core_dims is not None: |
| 95 | + if isinstance(core_dims, tuple | list): |
| 96 | + [core_dims] = core_dims |
| 97 | + |
| 98 | + # Create default_transform |
| 99 | + if observed is None and default_transform is UNSET: |
| 100 | + default_transform = SimplexTransform(dim=core_dims) |
| 101 | + |
| 102 | + # If the user didn't specify dims, take it from core_dims |
| 103 | + # We need them to be forwarded to dist in the `dim_lenghts` argument |
| 104 | + # if dims is None and core_dims is not None: |
| 105 | + # dims = (..., *core_dims) |
| 106 | + |
| 107 | + return super().__new__( |
| 108 | + *args, |
| 109 | + core_dims=core_dims, |
| 110 | + dims=dims, |
| 111 | + default_transform=default_transform, |
| 112 | + observed=observed, |
| 113 | + **kwargs, |
| 114 | + ) |
| 115 | + |
| 116 | + @classmethod |
| 117 | + def dist(cls, a, *, core_dims=None, **kwargs): |
| 118 | + return super().dist([a], core_dims=core_dims, **kwargs) |
| 119 | + |
| 120 | + |
66 | 121 | class MvNormal(VectorDimDistribution):
|
67 | 122 | """Multivariate Normal distribution.
|
68 | 123 |
|
|
0 commit comments