|
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
|
10 | 11 | from pytensor import Variable
|
11 | 12 | from pytensor.compile.builders import OpFromGraph
|
12 | 13 | from pytensor.compile.mode import Mode
|
13 |
| -from pytensor.graph import Op, vectorize_graph |
| 14 | +from pytensor.graph import FunctionGraph, Op, vectorize_graph |
14 | 15 | from pytensor.graph.replace import clone_replace, graph_replace
|
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,56 @@ 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 | + |
| 58 | + inner_rv = op.inner_outputs[outputs.index(rv)] |
| 59 | + marginalized_inner_rv, *other_dependent_inner_rvs = ( |
| 60 | + out |
| 61 | + for out in op.inner_outputs |
| 62 | + if out is not inner_rv and not isinstance(out.type, RandomType) |
| 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 | + marginalized_inner_rv_dummy = marginalized_inner_rv.clone() |
| 68 | + other_dependent_inner_rv_to_dummies = { |
| 69 | + inner_rv: inner_rv.clone() for inner_rv in other_dependent_inner_rvs |
| 70 | + } |
| 71 | + inner_rv = clone_replace( |
| 72 | + inner_rv, |
| 73 | + replace={marginalized_inner_rv: marginalized_inner_rv_dummy} |
| 74 | + | other_dependent_inner_rv_to_dummies, |
| 75 | + ) |
| 76 | + |
| 77 | + # Get support point of inner RV and marginalized RV |
| 78 | + inner_rv_support_point = support_point(inner_rv) |
| 79 | + marginalized_inner_rv_support_point = support_point(marginalized_inner_rv) |
| 80 | + |
| 81 | + replacements = [ |
| 82 | + # Replace the marginalized RV dummy by its support point |
| 83 | + (marginalized_inner_rv_dummy, marginalized_inner_rv_support_point), |
| 84 | + # Replace other dependent RVs dummies by the respective outer outputs. |
| 85 | + # PyMC will replace them by their support points later |
| 86 | + *( |
| 87 | + (v, outputs[op.inner_outputs.index(k)]) |
| 88 | + for k, v in other_dependent_inner_rv_to_dummies.items() |
| 89 | + ), |
| 90 | + # Replace outer input RVs |
| 91 | + *zip(op.inner_inputs, inputs), |
| 92 | + ] |
| 93 | + fgraph = FunctionGraph(outputs=[inner_rv_support_point], clone=False) |
| 94 | + fgraph.replace_all(replacements, import_missing=True) |
| 95 | + [rv_support_point] = fgraph.outputs |
| 96 | + return rv_support_point |
| 97 | + |
| 98 | + |
47 | 99 | class MarginalFiniteDiscreteRV(MarginalRV):
|
48 | 100 | """Base class for Marginalized Finite Discrete RVs"""
|
49 | 101 |
|
|
0 commit comments