|
4 | 4 | import pytensor.tensor as pt
|
5 | 5 |
|
6 | 6 | from pymc.distributions import Bernoulli, Categorical, DiscreteUniform
|
| 7 | +from pymc.distributions.distribution import _support_point, support_point |
7 | 8 | from pymc.logprob.abstract import MeasurableOp, _logprob
|
8 | 9 | from pymc.logprob.basic import conditional_logp, logp
|
9 | 10 | from pymc.pytensorf import constant_fold
|
|
15 | 16 | from pytensor.scan import map as scan_map
|
16 | 17 | from pytensor.scan import scan
|
17 | 18 | from pytensor.tensor import TensorVariable
|
| 19 | +from pytensor.tensor.random.type import RandomType |
18 | 20 |
|
19 | 21 | from pymc_experimental.distributions import DiscreteMarkovChain
|
20 | 22 |
|
@@ -44,6 +46,60 @@ def support_axes(self) -> tuple[tuple[int]]:
|
44 | 46 | return tuple(support_axes_vars)
|
45 | 47 |
|
46 | 48 |
|
| 49 | +@_support_point.register |
| 50 | +def support_point_marginal_rv(op: MarginalRV, rv, *inputs): |
| 51 | + """Support point for a marginalized RV. |
| 52 | +
|
| 53 | + The support point of a marginalized RV is the support point of the inner RV, |
| 54 | + conditioned on the marginalized RV taking its support point. |
| 55 | + """ |
| 56 | + outputs = rv.owner.outputs |
| 57 | + rv_idx = outputs.index(rv) |
| 58 | + inner_rv = op.inner_outputs[rv_idx] |
| 59 | + other_inner_rvs = [ |
| 60 | + out |
| 61 | + for out in op.inner_outputs |
| 62 | + if not isinstance(out.type, RandomType) and out is not inner_rv |
| 63 | + ] |
| 64 | + |
| 65 | + # Replace references to inner rvs by the dummy variables (including the marginalized RV) |
| 66 | + # This is necessary because the inner RVs may depend on each other |
| 67 | + other_inner_rv_to_dummies = { |
| 68 | + other_inner_rv: other_inner_rv.clone() for other_inner_rv in other_inner_rvs |
| 69 | + } |
| 70 | + inner_rv = clone_replace(inner_rv, other_inner_rv_to_dummies) |
| 71 | + inner_rv_support_point = support_point(inner_rv) |
| 72 | + |
| 73 | + # Replace the dummy marginalized RV by the support point of the marginalized RV |
| 74 | + marginalized_rv = other_inner_rvs[0] |
| 75 | + marginalized_rv_support_point = support_point(marginalized_rv) |
| 76 | + dummy_marginalized_rv = other_inner_rv_to_dummies[marginalized_rv] |
| 77 | + inner_rv_support_point = clone_replace( |
| 78 | + inner_rv_support_point, |
| 79 | + {dummy_marginalized_rv: marginalized_rv_support_point}, |
| 80 | + ) |
| 81 | + |
| 82 | + # Replace the remaining dummy variables by outer RVs |
| 83 | + rv_support_point = graph_replace( |
| 84 | + inner_rv_support_point, |
| 85 | + replace={ |
| 86 | + v: outputs[op.inner_outputs.index(k)] |
| 87 | + for k, v in other_inner_rv_to_dummies.items() |
| 88 | + if k is not marginalized_rv |
| 89 | + }, |
| 90 | + strict=False, |
| 91 | + ) |
| 92 | + |
| 93 | + # Make it a function of any remaining outer inputs |
| 94 | + rv_support_point = graph_replace( |
| 95 | + rv_support_point, |
| 96 | + replace=tuple(zip(op.inner_inputs, inputs)), |
| 97 | + strict=False, |
| 98 | + ) |
| 99 | + |
| 100 | + return rv_support_point |
| 101 | + |
| 102 | + |
47 | 103 | class MarginalFiniteDiscreteRV(MarginalRV):
|
48 | 104 | """Base class for Marginalized Finite Discrete RVs"""
|
49 | 105 |
|
|
0 commit comments