|
| 1 | +from collections.abc import Sequence |
| 2 | +from functools import wraps |
| 3 | +from typing import Literal |
| 4 | + |
| 5 | +import pytensor.tensor.random.basic as ptr |
| 6 | +from pytensor.graph.basic import Variable |
| 7 | +from pytensor.tensor.random.op import RandomVariable |
| 8 | +from pytensor.xtensor import as_xtensor |
| 9 | +from pytensor.xtensor.math import sqrt |
| 10 | +from pytensor.xtensor.vectorization import XRV |
| 11 | + |
| 12 | + |
| 13 | +def _as_xrv( |
| 14 | + core_op: RandomVariable, |
| 15 | + core_inps_dims_map: Sequence[Sequence[int]] | None = None, |
| 16 | + core_out_dims_map: Sequence[int] | None = None, |
| 17 | +): |
| 18 | + """Helper function to define an XRV constructor. |
| 19 | +
|
| 20 | + Parameters |
| 21 | + ---------- |
| 22 | + core_op : RandomVariable |
| 23 | + The core random variable operation to wrap. |
| 24 | + core_inps_dims_map : Sequence[Sequence[int]] | None, optional |
| 25 | + A sequence of sequences mapping the core dimensions (specified by the user) |
| 26 | + for each input parameter. This is used when lowering to a RandomVariable operation, |
| 27 | + to decide the ordering of the core dimensions for each input. |
| 28 | + If None, it assumes the core dimensions are positional from left to right. |
| 29 | + core_out_dims_map : Sequence[int] | None, optional |
| 30 | + A sequence mapping the core dimensions (specified by the user) for the output variable. |
| 31 | + This is used when lowering to a RandomVariable operation, |
| 32 | + to decide the ordering of the core dimensions for the output. |
| 33 | + If None, it assumes the core dimensions are positional from left to right. |
| 34 | +
|
| 35 | + """ |
| 36 | + if core_inps_dims_map is None: |
| 37 | + # Assume core_dims map positionally from left to right |
| 38 | + core_inps_dims_map = [tuple(range(ndim)) for ndim in core_op.ndims_params] |
| 39 | + if core_out_dims_map is None: |
| 40 | + # Assume core_dims map positionally from left to right |
| 41 | + core_out_dims_map = tuple(range(core_op.ndim_supp)) |
| 42 | + |
| 43 | + core_dims_needed = max( |
| 44 | + (*(len(i) for i in core_inps_dims_map), len(core_out_dims_map)), default=0 |
| 45 | + ) |
| 46 | + |
| 47 | + @wraps(core_op) |
| 48 | + def xrv_constructor( |
| 49 | + *params, |
| 50 | + core_dims: Sequence[str] | str | None = None, |
| 51 | + extra_dims: dict[str, Variable] | None = None, |
| 52 | + rng: Variable | None = None, |
| 53 | + ): |
| 54 | + if core_dims is None: |
| 55 | + core_dims = () |
| 56 | + if core_dims_needed: |
| 57 | + raise ValueError( |
| 58 | + f"{core_op.name} needs {core_dims_needed} core_dims to be specified" |
| 59 | + ) |
| 60 | + elif isinstance(core_dims, str): |
| 61 | + core_dims = (core_dims,) |
| 62 | + |
| 63 | + if len(core_dims) != core_dims_needed: |
| 64 | + raise ValueError( |
| 65 | + f"{core_op.name} needs {core_dims_needed} core_dims, but got {len(core_dims)}" |
| 66 | + ) |
| 67 | + |
| 68 | + full_input_core_dims = tuple( |
| 69 | + tuple(core_dims[i] for i in inp_dims_map) |
| 70 | + for inp_dims_map in core_inps_dims_map |
| 71 | + ) |
| 72 | + full_output_core_dims = tuple(core_dims[i] for i in core_out_dims_map) |
| 73 | + full_core_dims = (full_input_core_dims, full_output_core_dims) |
| 74 | + |
| 75 | + if extra_dims is None: |
| 76 | + extra_dims = {} |
| 77 | + |
| 78 | + return XRV( |
| 79 | + core_op, core_dims=full_core_dims, extra_dims=tuple(extra_dims.keys()) |
| 80 | + )(rng, *extra_dims.values(), *params) |
| 81 | + |
| 82 | + return xrv_constructor |
| 83 | + |
| 84 | + |
| 85 | +bernoulli = _as_xrv(ptr.bernoulli) |
| 86 | +beta = _as_xrv(ptr.beta) |
| 87 | +betabinom = _as_xrv(ptr.betabinom) |
| 88 | +binomial = _as_xrv(ptr.binomial) |
| 89 | +categorical = _as_xrv(ptr.categorical) |
| 90 | +cauchy = _as_xrv(ptr.cauchy) |
| 91 | +dirichlet = _as_xrv(ptr.dirichlet) |
| 92 | +exponential = _as_xrv(ptr.exponential) |
| 93 | +gamma = _as_xrv(ptr._gamma) |
| 94 | +gengamma = _as_xrv(ptr.gengamma) |
| 95 | +geometric = _as_xrv(ptr.geometric) |
| 96 | +gumbel = _as_xrv(ptr.gumbel) |
| 97 | +halfcauchy = _as_xrv(ptr.halfcauchy) |
| 98 | +halfnormal = _as_xrv(ptr.halfnormal) |
| 99 | +hypergeometric = _as_xrv(ptr.hypergeometric) |
| 100 | +integers = _as_xrv(ptr.integers) |
| 101 | +invgamma = _as_xrv(ptr.invgamma) |
| 102 | +laplace = _as_xrv(ptr.laplace) |
| 103 | +logistic = _as_xrv(ptr.logistic) |
| 104 | +lognormal = _as_xrv(ptr.lognormal) |
| 105 | +multinomial = _as_xrv(ptr.multinomial) |
| 106 | +nbinom = negative_binomial = _as_xrv(ptr.negative_binomial) |
| 107 | +normal = _as_xrv(ptr.normal) |
| 108 | +pareto = _as_xrv(ptr.pareto) |
| 109 | +poisson = _as_xrv(ptr.poisson) |
| 110 | +t = _as_xrv(ptr.t) |
| 111 | +triangular = _as_xrv(ptr.triangular) |
| 112 | +truncexpon = _as_xrv(ptr.truncexpon) |
| 113 | +uniform = _as_xrv(ptr.uniform) |
| 114 | +vonmises = _as_xrv(ptr.vonmises) |
| 115 | +wald = _as_xrv(ptr.wald) |
| 116 | +weibull = _as_xrv(ptr.weibull) |
| 117 | + |
| 118 | + |
| 119 | +def multivariate_normal( |
| 120 | + mean, |
| 121 | + cov, |
| 122 | + *, |
| 123 | + core_dims: Sequence[str], |
| 124 | + extra_dims=None, |
| 125 | + rng=None, |
| 126 | + method: Literal["cholesky", "svd", "eigh"] = "cholesky", |
| 127 | +): |
| 128 | + mean = as_xtensor(mean) |
| 129 | + if len(core_dims) != 2: |
| 130 | + raise ValueError( |
| 131 | + f"multivariate_normal requires 2 core_dims, got {len(core_dims)}" |
| 132 | + ) |
| 133 | + |
| 134 | + # Align core_dims, so that the dim that exists in mean comes before the one that only exists in cov |
| 135 | + # This will be the core dimension of the output |
| 136 | + if core_dims[0] not in mean.type.dims: |
| 137 | + core_dims = core_dims[::-1] |
| 138 | + |
| 139 | + xop = _as_xrv(ptr.MvNormalRV(method=method)) |
| 140 | + return xop(mean, cov, core_dims=core_dims, extra_dims=extra_dims, rng=rng) |
| 141 | + |
| 142 | + |
| 143 | +def standard_normal( |
| 144 | + extra_dims: dict[str, Variable] | None = None, |
| 145 | + rng: Variable | None = None, |
| 146 | +): |
| 147 | + """Standard normal random variable.""" |
| 148 | + return normal(0, 1, extra_dims=extra_dims, rng=rng) |
| 149 | + |
| 150 | + |
| 151 | +def chisquare( |
| 152 | + df, |
| 153 | + extra_dims: dict[str, Variable] | None = None, |
| 154 | + rng: Variable | None = None, |
| 155 | +): |
| 156 | + """Chi-square random variable.""" |
| 157 | + return gamma(df / 2.0, 2.0, extra_dims=extra_dims, rng=rng) |
| 158 | + |
| 159 | + |
| 160 | +def rayleigh( |
| 161 | + scale, |
| 162 | + extra_dims: dict[str, Variable] | None = None, |
| 163 | + rng: Variable | None = None, |
| 164 | +): |
| 165 | + """Rayleigh random variable.""" |
| 166 | + |
| 167 | + df = scale * 0 + 2 # Poor man's broadcasting, to pass dimensions of scale to the RV |
| 168 | + return sqrt(chisquare(df, extra_dims=extra_dims, rng=rng)) * scale |
0 commit comments