|
| 1 | +import lab as B |
| 2 | +from matrix.shape import broadcast |
| 3 | +from plum import parametric |
| 4 | + |
| 5 | +from .. import _dispatch |
| 6 | +from ..aggregate import Aggregate |
| 7 | +from ..mask import Masked |
| 8 | +from .dist import AbstractDistribution, shape_batch |
| 9 | + |
| 10 | +__all__ = ["Gamma"] |
| 11 | + |
| 12 | + |
| 13 | +@parametric |
| 14 | +class Gamma(AbstractDistribution): |
| 15 | + """Gamma distribution. |
| 16 | +
|
| 17 | + Args: |
| 18 | + k (tensor): Shape parameter. |
| 19 | + scale (tensor): Scale parameter. |
| 20 | + d (int): Dimensionality of the data. |
| 21 | +
|
| 22 | + Attributes: |
| 23 | + k (tensor): Shape parameter. |
| 24 | + scale (tensor): Scale parameter. |
| 25 | + d (int): Dimensionality of the data. |
| 26 | + """ |
| 27 | + |
| 28 | + def __init__(self, k, scale, d): |
| 29 | + self.k = k |
| 30 | + self.scale = scale |
| 31 | + self.d = d |
| 32 | + |
| 33 | + @property |
| 34 | + def mean(self): |
| 35 | + return B.multiply(self.k, self.scale) |
| 36 | + |
| 37 | + @property |
| 38 | + def var(self): |
| 39 | + return B.multiply(B.multiply(self.k, self.scale), self.scale) |
| 40 | + |
| 41 | + @_dispatch |
| 42 | + def sample( |
| 43 | + self: "Gamma[Aggregate, Aggregate, Aggregate]", |
| 44 | + state: B.RandomState, |
| 45 | + dtype: B.DType, |
| 46 | + *shape, |
| 47 | + ): |
| 48 | + samples = [] |
| 49 | + for ki, si, di in zip(self.k, self.scale, self.d): |
| 50 | + state, sample = Gamma(ki, si, di).sample(state, dtype, *shape) |
| 51 | + samples.append(sample) |
| 52 | + return state, Aggregate(*samples) |
| 53 | + |
| 54 | + @_dispatch |
| 55 | + def sample( |
| 56 | + self: "Gamma[B.Numeric, B.Numeric, B.Int]", |
| 57 | + state: B.RandomState, |
| 58 | + dtype: B.DType, |
| 59 | + *shape, |
| 60 | + ): |
| 61 | + return B.randgamma(state, dtype, *shape, alpha=self.k, scale=self.scale) |
| 62 | + |
| 63 | + @_dispatch |
| 64 | + def logpdf(self: "Gamma[Aggregate, Aggregate, Aggregate]", x: Aggregate): |
| 65 | + return sum( |
| 66 | + [ |
| 67 | + Gamma(ki, si, di).logpdf(xi) |
| 68 | + for ki, si, di, xi in zip(self.k, self.scale, self.d, x) |
| 69 | + ], |
| 70 | + 0, |
| 71 | + ) |
| 72 | + |
| 73 | + @_dispatch |
| 74 | + def logpdf(self: "Gamma[B.Numeric, B.Numeric, B.Int]", x: Masked): |
| 75 | + x, mask = x.y, x.mask |
| 76 | + with B.on_device(self.k): |
| 77 | + safe = B.to_active_device(B.one(B.dtype(self))) |
| 78 | + # Make inputs safe. |
| 79 | + x = mask * x + (1 - mask) * safe |
| 80 | + # Run with safe inputs, and filter out the right logpdfs. |
| 81 | + return self.logpdf(x, mask=mask) |
| 82 | + |
| 83 | + @_dispatch |
| 84 | + def logpdf(self: "Gamma[B.Numeric, B.Numeric, B.Int]", x: B.Numeric, *, mask=1): |
| 85 | + logz = B.loggamma(self.k) + self.k * B.log(self.scale) |
| 86 | + logpdf = (self.k - 1) * B.log(x) - x / self.scale - logz |
| 87 | + logpdf = logpdf * mask |
| 88 | + if self.d == 0: |
| 89 | + return logpdf |
| 90 | + else: |
| 91 | + return B.sum(logpdf, axis=tuple(range(B.rank(logpdf)))[-self.d :]) |
| 92 | + |
| 93 | + def __str__(self): |
| 94 | + return f"Gamma({self.k}, {self.scale})" |
| 95 | + |
| 96 | + def __repr__(self): |
| 97 | + return f"Gamma({self.k!r}, {self.scale!r})" |
| 98 | + |
| 99 | + |
| 100 | +@B.dtype.dispatch |
| 101 | +def dtype(dist: Gamma): |
| 102 | + return B.dtype(dist.k, dist.scale) |
| 103 | + |
| 104 | + |
| 105 | +@shape_batch.dispatch |
| 106 | +def shape_batch(dist: "Gamma[B.Numeric, B.Numeric, B.Int]"): |
| 107 | + return B.shape_broadcast(dist.k, dist.scale)[: -dist.d] |
| 108 | + |
| 109 | + |
| 110 | +@shape_batch.dispatch |
| 111 | +def shape_batch(dist: "Gamma[Aggregate, Aggregate, Aggregate]"): |
| 112 | + return broadcast( |
| 113 | + *( |
| 114 | + shape_batch(Gamma(ki, si, di)) |
| 115 | + for ki, si, di in zip(dist.k, dist.scale, dist.d) |
| 116 | + ) |
| 117 | + ) |
0 commit comments