|
| 1 | +# Copyright 2024 - present 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 | +# |
| 15 | +# MIT License |
| 16 | +# |
| 17 | +# Copyright (c) 2021-2022 aesara-devs |
| 18 | +# |
| 19 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 20 | +# of this software and associated documentation files (the "Software"), to deal |
| 21 | +# in the Software without restriction, including without limitation the rights |
| 22 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 23 | +# copies of the Software, and to permit persons to whom the Software is |
| 24 | +# furnished to do so, subject to the following conditions: |
| 25 | +# |
| 26 | +# The above copyright notice and this permission notice shall be included in all |
| 27 | +# copies or substantial portions of the Software. |
| 28 | +# |
| 29 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 30 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 31 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 32 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 33 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 34 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 35 | +# SOFTWARE. |
| 36 | +"""Measurable rewrites for arithmetic operations.""" |
| 37 | + |
| 38 | +from pytensor import tensor as pt |
| 39 | +from pytensor.graph.basic import Apply |
| 40 | +from pytensor.graph.fg import FunctionGraph |
| 41 | +from pytensor.graph.rewriting.basic import node_rewriter |
| 42 | +from pytensor.tensor.math import Sum |
| 43 | +from pytensor.tensor.random.basic import NormalRV |
| 44 | +from pytensor.tensor.type_other import NoneTypeT |
| 45 | +from pytensor.tensor.variable import TensorVariable |
| 46 | + |
| 47 | +from pymc.logprob.rewriting import measurable_ir_rewrites_db |
| 48 | + |
| 49 | + |
| 50 | +@node_rewriter([Sum]) |
| 51 | +def sum_of_normals(fgraph: FunctionGraph, node: Apply) -> list[TensorVariable] | None: |
| 52 | + [base_var] = node.inputs |
| 53 | + if base_var.owner is None: |
| 54 | + return None |
| 55 | + |
| 56 | + latent_op = base_var.owner.op |
| 57 | + if not isinstance(latent_op, NormalRV): |
| 58 | + return None |
| 59 | + |
| 60 | + rng, size, mu, sigma = base_var.owner.inputs |
| 61 | + |
| 62 | + if isinstance(size.type, NoneTypeT): |
| 63 | + mu_b, sigma_b = pt.broadcast_arrays(mu, sigma) |
| 64 | + else: |
| 65 | + mu_b = pt.broadcast_to(mu, size) # type: ignore[arg-type] |
| 66 | + sigma_b = pt.broadcast_to(sigma, size) # type: ignore[arg-type] |
| 67 | + |
| 68 | + axis = node.op.axis |
| 69 | + mu_sum = pt.sum(mu_b, axis=axis) |
| 70 | + sigma_sum = pt.sqrt(pt.sum(pt.square(sigma_b), axis=axis)) |
| 71 | + |
| 72 | + sum_rv = latent_op(mu_sum, sigma_sum, rng=rng, size=None) |
| 73 | + return [sum_rv] |
| 74 | + |
| 75 | + |
| 76 | +measurable_ir_rewrites_db.register( |
| 77 | + "sum_of_normals", |
| 78 | + sum_of_normals, |
| 79 | + "basic", |
| 80 | + "arithmetic", |
| 81 | +) |
0 commit comments