Random functions¶
Distributions over function-valued random variables (RandomFunction,
GaussianRandomFunction) and over measure-valued random variables
(RandomMeasure).
RandomFunction(*, name=None)
¶
Bases: Distribution[Callable[[X], Y]]
A distribution over functions f: X → Y.
The primary interface is __call__. Calling the random
function on a set of inputs returns a distribution representing
the (joint) distribution over the corresponding function outputs.
In other words, calling returns the finite-dimensional distributions
of the stochastic process. Log-densities are typically not available
as random functions do not have densities in the standard sense.
Sampling a random function means sampling an entire functional
trajectory. For infinite-dimensional models (e.g., Gaussian processes),
drawing an entire function realization may be impossible or require
approximation. Finite-dimensional subclasses that support sampling
should inherit SupportsSampling and implement
_sample(key, sample_shape).
This class is generic in X (input type) and Y (output type).
Source code in probpipe/core/_random_functions.py
ArrayRandomFunction(input_shape, output_shape=(), *, name=None)
¶
Bases: RandomFunction[Array, Array]
A random function mapping arrays to arrays.
Given prediction input X with shape
(*extra_batch, n, *input_shape) (n = number of input
points), predict returns a
DistributionArray whose outer batch_shape
covers axes that are independent across cells and whose
per-cell event_shape covers axes that are jointly modeled:
+----------------+-----------------+----------------+------------------------------+----------------------+ | joint_inputs | joint_outputs | Cell type | Outer batch_shape | Cell event_shape | +================+=================+================+==============================+======================+ | False | False | Normal | (*extra_batch, n, *out) | () | +----------------+-----------------+----------------+------------------------------+----------------------+ | True | False | MVN | (*extra_batch, *out) | (n,) | +----------------+-----------------+----------------+------------------------------+----------------------+ | False | True | MVN | (*extra_batch, n) | output_shape | +----------------+-----------------+----------------+------------------------------+----------------------+ | True | True | MVN | (*extra_batch,) | (n, *out) | +----------------+-----------------+----------------+------------------------------+----------------------+
out is shorthand for output_shape. In all modes a
sample has the same total shape:
(*sample_shape, *extra_batch, n, *output_shape). The flags
only change which axes are jointly modeled (event) vs
independent (batch).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_shape
|
tuple of int
|
Shape of a single input point, e.g. |
required |
output_shape
|
tuple of int
|
Shape of a single output, e.g. |
()
|
Source code in probpipe/core/_random_functions.py
input_shape
property
¶
Shape of a single input point.
output_shape
property
¶
Shape of a single output point.
__call__(X, *, joint_inputs=False, joint_outputs=False)
¶
Return a predictive distribution over outputs at input points X.
Validates inputs, parses shapes, and delegates to predict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
array - like
|
Input points with shape |
required |
joint_inputs
|
bool
|
If True, the |
False
|
joint_outputs
|
bool
|
If True, |
False
|
Returns:
| Type | Description |
|---|---|
DistributionArray
|
A |
Source code in probpipe/core/_random_functions.py
predict(X, *, joint_inputs=False, joint_outputs=False)
abstractmethod
¶
Subclass implementation of prediction.
When this method is called, X has already been validated and
converted to a JAX array. Subclasses should return a
DistributionArray whose outer
batch_shape and per-cell event_shape conform to the
shape table in the ArrayRandomFunction class
docstring.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Array
|
Validated input points, shape |
required |
joint_inputs
|
bool
|
Whether predictions should be joint across input points. |
False
|
joint_outputs
|
bool
|
Whether predictions should be joint across outputs. |
False
|
Source code in probpipe/core/_random_functions.py
GaussianRandomFunction(input_shape, output_shape=(), *, name=None)
¶
Bases: ArrayRandomFunction
Abstract random function with Gaussian predictive distributions.
Subclasses implement predict_mean and predict_variance
at minimum. If the model supports joint modes it must also implement
predict_covariance.
The base predict method assembles these into the appropriate
Normal or MultivariateNormal
distribution with the correct batch/event shape partition.
This class is not restricted to GPs — any model that produces Gaussian (or Gaussian-approximated) predictions can inherit from it.
Source code in probpipe/core/_random_functions.py
predict_mean(X)
abstractmethod
¶
Predictive mean at input points X.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Array
|
Shape |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Shape |
Source code in probpipe/distributions/gaussian_random_function.py
predict_variance(X)
abstractmethod
¶
Marginal predictive variance at input points X.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Array
|
Shape |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Shape |
Source code in probpipe/distributions/gaussian_random_function.py
predict_covariance(X, *, joint_inputs=False, joint_outputs=False)
¶
Predictive covariance matrix.
Required only if the model supports joint modes. Returns the covariance over whichever axes are flagged as joint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Array
|
Shape |
required |
joint_inputs
|
bool
|
Include cross-input covariance. |
False
|
joint_outputs
|
bool
|
Include cross-output covariance. |
False
|
Returns:
| Type | Description |
|---|---|
Array
|
The shape depends on the joint flags:
|
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If the subclass does not support the requested joint mode. |
Source code in probpipe/distributions/gaussian_random_function.py
__rmatmul__(other)
¶
A @ grf — linear map of outputs.
other must be a 2-D array of shape (d_out, d_w) and
self.output_shape must be 1-D (d_w,).
Source code in probpipe/distributions/gaussian_random_function.py
predict(X, *, joint_inputs=False, joint_outputs=False)
¶
Assemble a Gaussian-valued DistributionArray from
mean / variance / covariance.
Returns a DistributionArray whose cells
are Normal (fully marginal) or
MultivariateNormal (any joint axis); the
outer batch_shape covers axes that are independent
across cells, the per-cell event_shape covers axes that
are jointly modeled.
============== ============== ================ ============================== ===================
joint_inputs joint_outputs Cell type Outer batch_shape Cell event_shape
============== ============== ================ ============================== ===================
False False Normal (*extra_batch, n, *out) ()
True False MVN (*extra_batch, *out) (n,)
False True MVN (*extra_batch, n) output_shape
True True MVN (*extra_batch,) (n, *out)
============== ============== ================ ============================== ===================
out is shorthand for output_shape. In all modes a
sample has total shape
(*sample_shape, *extra_batch, n, *output_shape); the
flags only change which axes are jointly modeled (event)
vs independent (batch).
Subclasses may override this if they need non-standard assembly (e.g. structured covariance representations).
Source code in probpipe/distributions/gaussian_random_function.py
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | |
LinearBasisFunction(feature_map, weights, input_shape, output_shape=(), bias=None)
¶
Bases: GaussianRandomFunction, SupportsSampling
Linear model with fixed Gaussian weights.
Implements the model:
.. math::
f(x) = a + \Phi(x)\, w
where :math:w \sim \mathcal{N}(m, C) is a fixed Gaussian distribution
over weights, :math:\Phi(x) is a user-supplied feature map, and
:math:a is an optional bias.
The feature map maps each input to a vector (scalar output) or matrix
(multi-output) of basis-function evaluations. The weight distribution
is supplied as a MultivariateNormal.
This model always supports joint_inputs=True since the cross-input
covariance :math:\Phi(x_i)\, C\, \Phi(x_j)^T is available
analytically.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feature_map
|
callable
|
Maps input
where |
required |
weights
|
MultivariateNormal
|
Fixed Gaussian distribution over weight vector, with
|
required |
input_shape
|
tuple of int
|
Shape of a single input point. |
required |
output_shape
|
tuple of int
|
Shape of a single output. Default |
()
|
bias
|
array - like
|
Additive bias of shape |
None
|
Source code in probpipe/distributions/gaussian_random_function.py
predict_mean(X)
¶
Predictive mean: a + Phi(X) @ m.
Returns shape (*extra_batch, n, *output_shape).
Source code in probpipe/distributions/gaussian_random_function.py
predict_variance(X)
¶
Marginal predictive variance.
For scalar output: diag(Phi(X) C Phi(X)^T) element-wise.
Returns shape (*extra_batch, n, *output_shape).
Source code in probpipe/distributions/gaussian_random_function.py
RandomMeasure(*, name=None)
¶
Bases: Distribution[Distribution[T]]
A distribution over probability distributions on T.
A draw D ~ M is itself a Distribution[T]. Capabilities
(sampling, expected distribution, random log-density) are declared
via the protocols in probpipe.core.protocols — subclasses opt
in by implementing the corresponding _method and inheriting the
matching Supports* protocol.
The base class does not expose outer support / event_shape /
batch_shape: those concepts apply to tensor-valued distributions
and have no useful content for a distribution-valued one. Inner
metadata (inner_support, inner_event_shape) lives on
NumericRandomMeasure when T is array-like.
Batches of random measures should use
DistributionArray, which
treats RandomMeasure instances as scalar Distribution
components like any other.
Source code in probpipe/core/_random_measures.py
NumericRandomMeasure(*, name=None)
¶
Bases: RandomMeasure[Array]
A random measure whose inner draws are array-valued distributions.
Adds two pieces of metadata that are only meaningful when the inner sample type is array-like:
inner_support— theConstraintthat every innerDistribution[Array]'s samples satisfy.inner_event_shape— theevent_shapeshared by the inner distributions; the shape of one sample drawn from anyD ~ M.
These mirror NumericRecordDistribution's
support / event_shape for the random-measure layer.
Subclasses must override both.