|
| 1 | +# Copyright 2025 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 | +import pymc as pm |
| 16 | +import pytensor.tensor as pt |
| 17 | + |
| 18 | +from pymc.distributions.continuous import Continuous |
| 19 | +from pymc.distributions.distribution import SymbolicRandomVariable |
| 20 | +from pymc.distributions.shape_utils import ( |
| 21 | + rv_size_is_none, |
| 22 | +) |
| 23 | +from pymc.pytensorf import normalize_rng_param |
| 24 | +from pytensor.tensor import get_underlying_scalar_constant_value |
| 25 | +from pytensor.tensor.random.utils import ( |
| 26 | + normalize_size_param, |
| 27 | +) |
| 28 | + |
| 29 | +__all__ = ["Spherical"] |
| 30 | + |
| 31 | + |
| 32 | +class SphericalRV(SymbolicRandomVariable): |
| 33 | + name = "spherical" |
| 34 | + extended_signature = "[rng],[size],(n)->[rng],(n)" # TODO: check if this is correct |
| 35 | + _print_name = ("SphericalRV", "\\operatorname{SphericalRV}") |
| 36 | + |
| 37 | + def make_node(self, rng, size, n): |
| 38 | + n = pt.as_tensor_variable(n) |
| 39 | + return super().make_node(rng, size, n) |
| 40 | + |
| 41 | + @classmethod |
| 42 | + def rv_op(cls, n, *, rng=None, size=None): |
| 43 | + rng = normalize_rng_param(rng) |
| 44 | + size = normalize_size_param(size) |
| 45 | + n = pt.as_tensor(n, ndim=0, dtype=int) |
| 46 | + nv = get_underlying_scalar_constant_value(n) |
| 47 | + |
| 48 | + # Perform a direct computation via SVD of a normal matrix |
| 49 | + sz = [] if rv_size_is_none(size) else size |
| 50 | + |
| 51 | + next_rng, z = pt.random.normal(0, 1, size=(*sz, nv), rng=rng).owner.outputs |
| 52 | + samples = z / pt.sqrt(z * z.sum(axis=-1, keepdims=True) + 1e-6) |
| 53 | + # TODO: scale by the .dist given |
| 54 | + |
| 55 | + return cls( |
| 56 | + inputs=[rng, size, n], |
| 57 | + outputs=[next_rng, samples], |
| 58 | + )(rng, size, n) |
| 59 | + |
| 60 | + return samples |
| 61 | + |
| 62 | + |
| 63 | +class Spherical(Continuous): |
| 64 | + rv_type = SphericalRV |
| 65 | + rv_op = SphericalRV.rv_op |
| 66 | + |
| 67 | + @classmethod |
| 68 | + def dist(cls, n, **kwargs): |
| 69 | + n = pt.as_tensor_variable(n).astype(int) |
| 70 | + return super().dist([n], **kwargs) |
| 71 | + |
| 72 | + def support_point(rv, size, n, *args): |
| 73 | + return pt.ones(rv.shape) / pt.sqrt(n) |
| 74 | + |
| 75 | + def logp(value, n): |
| 76 | + # TODO: take dist as a parameter instead of hardcoding |
| 77 | + dist = pm.Gamma.dist(50, 50) |
| 78 | + |
| 79 | + # Get the radius |
| 80 | + r = pt.sqrt(pt.sum(value**2)) |
| 81 | + |
| 82 | + # Get the log prior of the radius |
| 83 | + log_p = pm.logp(dist, r) |
| 84 | + # log_p = pm.logp(pm.TruncatedNormal.dist(1,lower=0),r) |
| 85 | + |
| 86 | + # Add the log det jacobian for radius |
| 87 | + log_p += (value.shape[-1] - 1) * pt.log(r) |
| 88 | + |
| 89 | + return log_p |
0 commit comments