RandomVariable#

class cuqi.experimental.algebra.RandomVariable(distributions, tree=None, name=None)#

Random variable defined by a distribution with the option to apply algebraic operations on it.

Random variables allow for the definition of Bayesian Problems in a natural way. In the context of code, the random variable can be viewed as a lazily evaluated variable/array. It records operations applied to it and acts as a function that, when called, evaluates the operations and returns the result.

In CUQIpy, random variables can be in two forms: (1) a ‘primal’ random variable that is directly defined by a distribution, e.g. x ~ N(0, 1), or (2) a ‘transformed’ random variable that is defined by applying algebraic operations on one or more random variables, e.g. y = x + 1.

This distinction is purely for the purpose of the implementation in CUQIpy, as mathematically both x ~ N(0, 1) and y = x + 1 ~ N(1, 1) are random variables. The distinction is useful for the code implementation. In the future some operations like the above may allow primal random variables that are transformed if the distribution can be analytically described.

Parameters:
  • distributions (Distribution or list of Distributions) – The distribution from which the random variable originates. If multiple distributions are provided, the random variable is defined by the passed abstract syntax tree representing the algebraic operations applied to one or more random variables.

  • tree (Node, optional) – The tree, represented by the syntax tree nodes, that contain the algebraic operations applied to the random variable. Specifically, the root of the tree should be provided.

  • name (str, optional) – Name of the random variable. If not provided, the name is extracted from either the distribution provided or from the variable name in the code. The name provided must match the parameter name of the distribution.

Example

Basic usage:

from cuqi.distribution import Gaussian

x = RandomVariable(Gaussian(0, 1))

Defining Bayesian problem using random variables:

from cuqi.testproblem import Deconvolution1D
from cuqi.distribution import Gaussian, Gamma, GMRF
from cuqi.experimental.algebra import RandomVariable
from cuqi.problem import BayesianProblem

import numpy as np
A, y_obs, info = Deconvolution1D().get_components()

# Bayesian problem
d = RandomVariable(Gamma(1, 1e-4))
s = RandomVariable(Gamma(1, 1e-4))
x = RandomVariable(GMRF(np.zeros(A.domain_dim), d))
y = RandomVariable(Gaussian(A @ x, 1/s))

BP = BayesianProblem(y, x, s, d)
BP.set_data(y=y_obs)
BP.UQ()

Defining random variable from multiple distributions:

from cuqi.distribution import Gaussian, Gamma
from cuqi.experimental.algebra import RandomVariable, VariableNode

# Define the variables
x = VariableNode('x')
y = VariableNode('y')

# Define the distributions (names must match variables)
dist_x = Gaussian(0, 1, name='x')
dist_y = Gamma(1, 1e-4, name='y')

# Define the tree (this is the algebra that defines the random variable along with the distributions)
tree = x + y

# Define random variable from 2 distributions with relation x+y
rv = RandomVariable([dist_x, dist_y], tree)
__init__(distributions, tree=None, name=None)#

Create random variable from distribution

Methods

__init__(distributions[, tree, name])

Create random variable from distribution

Attributes

dim

distribution

Distribution from which the random variable originates.

distributions

Distributions from which the random variable originates.

expression

Expression (formula) of the random variable.

geometry

is_transformed

Returns True if the random variable is transformed.

name

Name of the random variable.

parameter_names

Name of the parameter that the random variable can be evaluated at.

tree