`RandomVariable`

- Class
`ed.RandomVariable`

- Class
`ed.models.RandomVariable`

Defined in `edward/models/random_variable.py`

.

Base class for random variables.

A random variable is an object parameterized by tensors. It is equipped with methods such as the log-density, mean, and sample.

It also wraps a tensor, where the tensor corresponds to a sample from the random variable. This enables operations on the TensorFlow graph, allowing random variables to be used in conjunction with other TensorFlow ops.

The random variable's shape is given by

`sample_shape + batch_shape + event_shape`

,

where `sample_shape`

is an optional argument representing the dimensions of samples drawn from the distribution (default is a scalar); `batch_shape`

is the number of independent random variables (determined by the shape of its parameters); and `event_shape`

is the shape of one draw from the distribution (e.g., `Normal`

has a scalar `event_shape`

; `Dirichlet`

has a vector `event_shape`

).

`RandomVariable`

assumes use in a multiple inheritance setting. The child class must first inherit `RandomVariable`

, then second inherit a class in `tf.contrib.distributions`

. With Python's method resolution order, this implies the following during initialization (using `distributions.Bernoulli`

as an example):

- Start the
`__init__()`

of the child class, which passes all`*args, **kwargs`

to`RandomVariable`

. - This in turn passes all
`*args, **kwargs`

to`distributions.Bernoulli`

, completing the`__init__()`

of`distributions.Bernoulli`

. - Complete the
`__init__()`

of`RandomVariable`

, which calls`self.sample()`

, relying on the method from`distributions.Bernoulli`

. - Complete the
`__init__()`

of the child class.

Methods from both `RandomVariable`

and `distributions.Bernoulli`

populate the namespace of the child class. Methods from `RandomVariable`

will take higher priority if there are conflicts.

```
p = tf.constant(0.5)
x = Bernoulli(p)
z1 = tf.constant([[1.0, -0.8], [0.3, -1.0]])
z2 = tf.constant([[0.9, 0.2], [2.0, -0.1]])
x = Bernoulli(logits=tf.matmul(z1, z2))
mu = Normal(tf.constant(0.0), tf.constant(1.0))
x = Normal(mu, tf.constant(1.0))
```

`sample_shape`

Sample shape of random variable.

`shape`

Shape of random variable.

`unique_name`

Name of random variable with its unique scoping name. Use `name`

to just get the name of the random variable.

**init**

```
__init__(
*args,
**kwargs
)
```

Create a new random variable.

: tf.TensorShape, optional. Shape of samples to draw from the random variable.`sample_shape`

: tf.Tensor, optional. Fixed tensor to associate with random variable. Must have shape`value`

`sample_shape + batch_shape + event_shape`

.: list, optional. Optional list of graph collections keys. The random variable is added to these collections. Defaults to ["random_variables"].`collections`

**abs**

`__abs__()`

**add**

`__add__(other)`

**and**

`__and__(other)`

**bool**

`__bool__()`

**div**

`__div__(other)`

**eq**

`__eq__(other)`

**floordiv**

`__floordiv__(other)`

**ge**

`__ge__(other)`

**getitem**

`__getitem__(key)`

Subset the tensor associated to the random variable, not the random variable itself.

**gt**

`__gt__(other)`

**invert**

`__invert__()`

**iter**

`__iter__()`

**le**

`__le__(other)`

**lt**

`__lt__(other)`

**mod**

`__mod__(other)`

**mul**

`__mul__(other)`

**neg**

`__neg__()`

**nonzero**

`__nonzero__()`

**or**

`__or__(other)`

**pow**

`__pow__(other)`

**radd**

`__radd__(other)`

**rand**

`__rand__(other)`

**rdiv**

`__rdiv__(other)`

**rfloordiv**

`__rfloordiv__(other)`

**rmod**

`__rmod__(other)`

**rmul**

`__rmul__(other)`

**ror**

`__ror__(other)`

**rpow**

`__rpow__(other)`

**rsub**

`__rsub__(other)`

**rtruediv**

`__rtruediv__(other)`

**rxor**

`__rxor__(other)`

**sub**

`__sub__(other)`

**truediv**

`__truediv__(other)`

**xor**

`__xor__(other)`

`eval`

```
eval(
session=None,
feed_dict=None
)
```

In a session, computes and returns the value of this random variable.

This is not a graph construction method, it does not add ops to the graph.

This convenience method requires a session where the graph containing this variable has been launched. If no session is passed, the default session is used.

: tf.BaseSession, optional. The`session`

`tf.Session`

to use to evaluate this random variable. If none, the default session is used.: dict, optional. A dictionary that maps`feed_dict`

`tf.Tensor`

objects to feed values. See`tf.Session.run()`

for a description of the valid feed values.

```
x = Normal(0.0, 1.0)
with tf.Session() as sess:
# Usage passing the session explicitly.
print(x.eval(sess))
# Usage with the default session. The 'with' block
# above makes 'sess' the default session.
print(x.eval())
```

`get_ancestors`

`get_ancestors(collection=None)`

Get ancestor random variables.

`get_blanket`

`get_blanket(collection=None)`

Get the random variable's Markov blanket.

`get_children`

`get_children(collection=None)`

Get child random variables.

`get_descendants`

`get_descendants(collection=None)`

Get descendant random variables.

`get_parents`

`get_parents(collection=None)`

Get parent random variables.

`get_shape`

`get_shape()`

Get shape of random variable.

`get_siblings`

`get_siblings(collection=None)`

Get sibling random variables.

`get_variables`

`get_variables(collection=None)`

Get TensorFlow variables that the random variable depends on.

`value`

`value()`

Get tensor that the random variable corresponds to.