|
| 1 | +# Copyright 2023 The PyMC Developers |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +from typing import List, Optional |
| 15 | + |
| 16 | +import numpy as np |
| 17 | +import pytensor.tensor as pt |
| 18 | + |
| 19 | +from pytensor.graph.basic import Node |
| 20 | +from pytensor.graph.fg import FunctionGraph |
| 21 | +from pytensor.graph.rewriting.basic import node_rewriter |
| 22 | +from pytensor.scalar.basic import GT, LT |
| 23 | +from pytensor.tensor.math import gt, lt |
| 24 | + |
| 25 | +from pymc.logprob.abstract import ( |
| 26 | + MeasurableElemwise, |
| 27 | + MeasurableVariable, |
| 28 | + _logcdf_helper, |
| 29 | + _logprob, |
| 30 | + _logprob_helper, |
| 31 | +) |
| 32 | +from pymc.logprob.rewriting import measurable_ir_rewrites_db |
| 33 | +from pymc.logprob.utils import check_potential_measurability, ignore_logprob |
| 34 | + |
| 35 | + |
| 36 | +class MeasurableComparison(MeasurableElemwise): |
| 37 | + """A placeholder used to specify a log-likelihood for a binary comparison RV sub-graph.""" |
| 38 | + |
| 39 | + valid_scalar_types = (GT, LT) |
| 40 | + |
| 41 | + |
| 42 | +@node_rewriter(tracks=[gt, lt]) |
| 43 | +def find_measurable_comparisons( |
| 44 | + fgraph: FunctionGraph, node: Node |
| 45 | +) -> Optional[List[MeasurableComparison]]: |
| 46 | + rv_map_feature = getattr(fgraph, "preserve_rv_mappings", None) |
| 47 | + if rv_map_feature is None: |
| 48 | + return None # pragma: no cover |
| 49 | + |
| 50 | + if isinstance(node.op, MeasurableComparison): |
| 51 | + return None # pragma: no cover |
| 52 | + |
| 53 | + (compared_var,) = node.outputs |
| 54 | + base_var, const = node.inputs |
| 55 | + |
| 56 | + if not ( |
| 57 | + base_var.owner |
| 58 | + and isinstance(base_var.owner.op, MeasurableVariable) |
| 59 | + and base_var not in rv_map_feature.rv_values |
| 60 | + ): |
| 61 | + return None |
| 62 | + |
| 63 | + # check for potential measurability of const |
| 64 | + if not check_potential_measurability((const,), rv_map_feature): |
| 65 | + return None |
| 66 | + |
| 67 | + # Make base_var unmeasurable |
| 68 | + unmeasurable_base_var = ignore_logprob(base_var) |
| 69 | + |
| 70 | + compared_op = MeasurableComparison(node.op.scalar_op) |
| 71 | + compared_rv = compared_op.make_node(unmeasurable_base_var, const).default_output() |
| 72 | + compared_rv.name = compared_var.name |
| 73 | + return [compared_rv] |
| 74 | + |
| 75 | + |
| 76 | +measurable_ir_rewrites_db.register( |
| 77 | + "find_measurable_comparisons", |
| 78 | + find_measurable_comparisons, |
| 79 | + "basic", |
| 80 | + "comparison", |
| 81 | +) |
| 82 | + |
| 83 | + |
| 84 | +@_logprob.register(MeasurableComparison) |
| 85 | +def comparison_logprob(op, values, base_rv, operand, **kwargs): |
| 86 | + (value,) = values |
| 87 | + |
| 88 | + base_rv_op = base_rv.owner.op |
| 89 | + |
| 90 | + logcdf = _logcdf_helper(base_rv, operand, **kwargs) |
| 91 | + logccdf = pt.log1mexp(logcdf) |
| 92 | + |
| 93 | + condn_exp = pt.eq(value, np.array(True)) |
| 94 | + |
| 95 | + if isinstance(op.scalar_op, GT): |
| 96 | + logprob = pt.switch(condn_exp, logccdf, logcdf) |
| 97 | + elif isinstance(op.scalar_op, LT): |
| 98 | + if base_rv.dtype.startswith("int"): |
| 99 | + logpmf = _logprob_helper(base_rv, operand, **kwargs) |
| 100 | + logcdf_lt_true = _logcdf_helper(base_rv, operand - 1, **kwargs) |
| 101 | + logprob = pt.switch(condn_exp, logcdf_lt_true, pt.logaddexp(logccdf, logpmf)) |
| 102 | + else: |
| 103 | + logprob = pt.switch(condn_exp, logcdf, logccdf) |
| 104 | + else: |
| 105 | + raise TypeError(f"Unsupported scalar_op {op.scalar_op}") |
| 106 | + |
| 107 | + if base_rv_op.name: |
| 108 | + logprob.name = f"{base_rv_op}_logprob" |
| 109 | + logcdf.name = f"{base_rv_op}_logcdf" |
| 110 | + |
| 111 | + return logprob |
0 commit comments