# Constructing the model¶

In BayesPy, the model is constructed by creating nodes which form a directed network. There are two types of nodes: stochastic and deterministic. A stochastic node corresponds to a random variable (or a set of random variables) from a specific probability distribution. A deterministic node corresponds to a deterministic function of its parents. For a list of built-in nodes, see the User API.

## Creating nodes¶

Creating a node is basically like writing the conditional prior distribution of
the variable in Python. The node is constructed by giving the parent nodes,
that is, the conditioning variables as arguments. The number of parents and
their meaning depend on the node. For instance, a `Gaussian`

node is
created by giving the mean vector and the precision matrix. These parents can be
constant numerical arrays if they are known:

```
>>> from bayespy.nodes import Gaussian
>>> X = Gaussian([2, 5], [[1.0, 0.3], [0.3, 1.0]])
```

or other nodes if they are unknown and given prior distributions:

```
>>> from bayespy.nodes import Gaussian, Wishart
>>> mu = Gaussian([0, 0], [[1e-6, 0],[0, 1e-6]])
>>> Lambda = Wishart(2, [[1, 0], [0, 1]])
>>> X = Gaussian(mu, Lambda)
```

Nodes can also be named by providing `name`

keyword argument:

```
>>> X = Gaussian(mu, Lambda, name='x')
```

The name may be useful when referring to the node using an inference engine.

For the parent nodes, there are two main restrictions: non-constant parent nodes must be conjugate and the parent nodes must be mutually independent in the posterior approximation.

### Conjugacy of the parents¶

In Bayesian framework in general, one can give quite arbitrary probability
distributions for variables. However, one often uses distributions that are easy
to handle in practice. Quite often this means that the parents are given
conjugate priors. This is also one of the limitations in BayesPy: only conjugate
family prior distributions are accepted currently. Thus, although in principle
one could give, for instance, gamma prior for the mean parameter `mu`

, only
Gaussian-family distributions are accepted because of the conjugacy. If the
parent is not of a proper type, an error is raised. This conjugacy is checked
automatically by BayesPy and `NoConverterError`

is raised if a parent cannot
be interpreted as being from a conjugate distribution.

### Independence of the parents¶

Another a bit rarely encountered limitation is that the parents must be mutually independent (in the posterior factorization). Thus, a node cannot have the same stochastic node as several parents without intermediate stochastic nodes. For instance, the following leads to an error:

```
>>> from bayespy.nodes import Dot
>>> Y = Dot(X, X)
Traceback (most recent call last):
...
ValueError: Parent nodes are not independent
```

The error is raised because `X`

is given as two parents for `Y`

, and
obviously `X`

is not independent of `X`

in the posterior approximation. Even
if `X`

is not given several times directly but there are some intermediate
deterministic nodes, an error is raised because the deterministic nodes depend
on their parents and thus the parents of `Y`

would not be independent.
However, it is valid that a node is a parent of another node via several paths
if all the paths or all except one path has intermediate stochastic nodes. This
is valid because the intermediate stochastic nodes have independent posterior
approximations. Thus, for instance, the following construction does not raise
errors:

```
>>> from bayespy.nodes import Dot
>>> Z = Gaussian(X, [[1,0], [0,1]])
>>> Y = Dot(X, Z)
```

This works because there is now an intermediate stochastic node `Z`

on the
other path from `X`

node to `Y`

node.

## Effects of the nodes on inference¶

When constructing the network with nodes, the stochastic nodes actually define three important aspects:

- the prior probability distribution for the variables,
- the factorization of the posterior approximation,
- the functional form of the posterior approximation for the variables.

### Prior probability distribution¶

First, the most intuitive feature of the nodes is that they define the prior
distribution. In the previous example, `mu`

was a stochastic
`GaussianARD`

node corresponding to from the normal
distribution, `tau`

was a stochastic `Gamma`

node corresponding to
from the gamma distribution, and `y`

was a stochastic
`GaussianARD`

node corresponding to from the normal
distribution with mean and precision . If we denote the
set of all stochastic nodes by , and by the set of
parents of a node , the model is defined as

where nodes correspond to the terms .

### Posterior factorization¶

Second, the nodes define the structure of the posterior approximation. The
variational Bayesian approximation factorizes with respect to nodes, that is,
each node corresponds to an independent probability distribution in the
posterior approximation. In the previous example, `mu`

and `tau`

were
separate nodes, thus the posterior approximation factorizes with respect to
them: . Thus, the posterior approximation can be written
as:

where is the set of latent stochastic nodes and
is the set of observed stochastic nodes. Sometimes one may
want to avoid the factorization between some variables. For this purpose, there
are some nodes which model several variables jointly without factorization. For
instance, `GaussianGammaISO`

is a joint node for and
variables from the normal-gamma distribution and the posterior
approximation does not factorize between and , that is,
the posterior approximation is .

### Functional form of the posterior¶

Last, the nodes define the functional form of the posterior approximation.
Usually, the posterior approximation has the same or similar functional form as
the prior. For instance, `Gamma`

uses gamma distribution to also
approximate the posterior distribution. Similarly, `GaussianARD`

uses
Gaussian distribution for the posterior. However, the posterior approximation
of `GaussianARD`

uses a full covariance matrix although the prior assumes
a diagonal covariance matrix. Thus, there can be slight differences in the
exact functional form of the posterior approximation but the rule of thumb is
that the functional form of the posterior approximation is the same as or more
general than the functional form of the prior.

## Using plate notation¶

### Defining plates¶

Stochastic nodes take the optional parameter `plates`

, which can be used to
define plates of the variable. A plate defines the number of repetitions of a
set of variables. For instance, a set of random variables
could be defined as

This can also be visualized as a graphical model:

The variable has two plates: one for the index and one for the index . In BayesPy, this random variable can be constructed as:

```
>>> y = Gaussian(mu, Lambda, plates=(10,30))
```

Note

The plates are always given as a tuple of positive integers.

Plates also define indexing for the nodes, thus you can use simple NumPy-style slice indexing to obtain a subset of the plates:

```
>>> y_0 = y[0]
>>> y_0.plates
(30,)
>>> y_even = y[:,::2]
>>> y_even.plates
(10, 15)
>>> y_complex = y[:5, 10:20:5]
>>> y_complex.plates
(5, 2)
```

Note that this indexing is for the plates only, not for the random variable dimensions.

### Sharing and broadcasting plates¶

Instead of having a common mean and precision matrix for all , it is also possible to share plates with parents. For instance, the mean could be different for each index and the precision for each index :

which has the following graphical representation:

This can be constructed in BayesPy, for instance, as:

```
>>> from bayespy.nodes import Gaussian, Wishart
>>> mu = Gaussian([0, 0], [[1e-6, 0],[0, 1e-6]], plates=(10,1))
>>> Lambda = Wishart(2, [[1, 0], [0, 1]], plates=(1,30))
>>> X = Gaussian(mu, Lambda)
```

There are a few things to notice here. First, the plates are defined similarly
as shapes in NumPy, that is, they use similar broadcasting rules. For instance,
the plates `(10,1)`

and `(1,30)`

broadcast to `(10,30)`

. In fact, one
could use plates `(10,1)`

and `(30,)`

to get the broadcasted plates
`(10,30)`

because broadcasting compares the plates from right to left starting
from the last axis. Second, `X`

is not given `plates`

keyword argument
because the default plates are the plates broadcasted from the parents and that
was what we wanted so it was not necessary to provide the keyword argument. If
we wanted, for instance, plates `(20,10,30)`

for `X`

, then we would have
needed to provide `plates=(20,10,30)`

.

The validity of the plates between a child and its parents is checked as follows. The plates are compared plate-wise starting from the last axis and working the way forward. A plate of the child is compatible with a plate of the parent if either of the following conditions is met:

- The two plates have equal size
- The parent has size 1 (or no plate)

Table below shows an example of compatible plates for a child node and its two parent nodes:

node | plates | ||||||
---|---|---|---|---|---|---|---|

parent1 | 3 | 1 | 1 | 1 | 8 | 10 | |

parent2 | 1 | 1 | 5 | 1 | 10 | ||

child | 5 | 3 | 1 | 7 | 5 | 8 | 10 |

### Plates in deterministic nodes¶

Note that plates can be defined explicitly only for stochastic nodes. For
deterministic nodes, the plates are defined implicitly by the plate broadcasting
rules from the parents. Deterministic nodes do not need more plates than this
because there is no randomness. The deterministic node would just have the same
value over the extra plates, but it is not necessary to do this explicitly
because the child nodes of the deterministic node can utilize broadcasting
anyway. Thus, there is no point in having extra plates in deterministic nodes,
and for this reason, deterministic nodes do not use `plates`

keyword argument.

### Plates in constants¶

It is useful to understand how the plates and the shape of a random variable are
connected. The shape of an array which contains all the plates of a random
variable is the concatenation of the plates and the shape of the variable. For
instance, consider a 2-dimensional Gaussian variable with plates `(3,)`

. If
you want the value of the constant mean vector and constant precision matrix to
vary between plates, they are given as `(3,2)`

-shape and `(3,2,2)`

-shape
arrays, respectively:

```
>>> import numpy as np
>>> mu = [ [0,0], [1,1], [2,2] ]
>>> Lambda = [ [[1.0, 0.0],
... [0.0, 1.0]],
... [[1.0, 0.9],
... [0.9, 1.0]],
... [[1.0, -0.3],
... [-0.3, 1.0]] ]
>>> X = Gaussian(mu, Lambda)
>>> np.shape(mu)
(3, 2)
>>> np.shape(Lambda)
(3, 2, 2)
>>> X.plates
(3,)
```

Thus, the leading axes of an array are the plate axes and the trailing axes are
the random variable axes. In the example above, the mean vector has plates
`(3,)`

and shape `(2,)`

, and the precision matrix has plates `(3,)`

and
shape `(2,2)`

.

### Factorization of plates¶

It is important to undestand the independency structure the plates induce for the model. First, the repetitions defined by a plate are independent a priori given the parents. Second, the repetitions are independent in the posterior approximation, that is, the posterior approximation factorizes with respect to plates. Thus, the plates also have an effect on the independence structure of the posterior approximation, not only prior. If dependencies between a set of variables need to be handled, that set must be handled as a some kind of multi-dimensional variable.

### Irregular plates¶

The handling of plates is not always as simple as described above. There are cases in which the plates of the parents do not map directly to the plates of the child node. The user API should mention such irregularities.

For instance, the parents of a mixture distribution have a plate which contains the different parameters for each cluster, but the variable from the mixture distribution does not have that plate:

```
>>> from bayespy.nodes import Gaussian, Wishart, Categorical, Mixture
>>> mu = Gaussian([[0], [0], [0]], [ [[1]], [[1]], [[1]] ])
>>> Lambda = Wishart(1, [ [[1]], [[1]], [[1]]])
>>> Z = Categorical([1/3, 1/3, 1/3], plates=(100,))
>>> X = Mixture(Z, Gaussian, mu, Lambda)
>>> mu.plates
(3,)
>>> Lambda.plates
(3,)
>>> Z.plates
(100,)
>>> X.plates
(100,)
```

The plates `(3,)`

and `(100,)`

should not broadcast according to the rules
mentioned above. However, when validating the plates, `Mixture`

removes
the plate which corresponds to the clusters in `mu`

and `Lambda`

. Thus,
`X`

has plates which are the result of broadcasting plates `()`

and
`(100,)`

which equals `(100,)`

.

Also, sometimes the plates of the parents may be mapped to the variable axes. For instance, an automatic relevance determination (ARD) prior for a Gaussian variable is constructed by giving the diagonal elements of the precision matrix (or tensor). The Gaussian variable itself can be a scalar, a vector, a matrix or a tensor. A set of five -dimensional Gaussian matrices with ARD prior is constructed as:

```
>>> from bayespy.nodes import GaussianARD, Gamma
>>> tau = Gamma(1, 1, plates=(5,4,3))
>>> X = GaussianARD(0, tau, shape=(4,3))
>>> tau.plates
(5, 4, 3)
>>> X.plates
(5,)
```

Note how the last two plate axes of `tau`

are mapped to the variable axes of
`X`

with shape `(4,3)`

and the plates of `X`

are obtained by taking the
remaining leading plate axes of `tau`

.

## Example model: Principal component analysis¶

Now, we’ll construct a bit more complex model which will be used in the following sections. The model is a probabilistic version of principal component analysis (PCA):

where is data matrix, is loading matrix, is state matrix, and noise is isotropic Gaussian. The dimensionality is usually assumed to be much smaller than and .

A probabilistic formulation can be written as:

where we have given automatic relevance determination (ARD) prior for . This can be visualized as a graphical model:

Now, let us construct this model in BayesPy. First, we’ll define the dimensionality of the latent space in our model:

```
>>> D = 3
```

Then the prior for the latent states :

```
>>> X = GaussianARD(0, 1,
... shape=(D,),
... plates=(1,100),
... name='X')
```

Note that the shape of `X`

is `(D,)`

, although the latent dimensions are
marked with a plate in the graphical model and they are conditionally
independent in the prior. However, we want to (and need to) model the posterior
dependency of the latent dimensions, thus we cannot factorize them, which would
happen if we used `plates=(1,100,D)`

and `shape=()`

. The first plate axis
with size 1 is given just for clarity.

The prior for the ARD parameters of the loading matrix:

```
>>> alpha = Gamma(1e-3, 1e-3,
... plates=(D,),
... name='alpha')
```

The prior for the loading matrix :

```
>>> C = GaussianARD(0, alpha,
... shape=(D,),
... plates=(10,1),
... name='C')
```

Again, note that the shape is the same as for `X`

for the same reason. Also,
the plates of `alpha`

, `(D,)`

, are mapped to the full shape of the node
`C`

, `(10,1,D)`

, using standard broadcasting rules.

The dot product is just a deterministic node:

```
>>> F = Dot(C, X)
```

However, note that `Dot`

requires that the input Gaussian nodes have the same
shape and that this shape has exactly one axis, that is, the variables are
vectors. This the reason why we used shape `(D,)`

for `X`

and `C`

but
from a bit different perspective. The node computes the inner product of
-dimensional vectors resulting in plates `(10,100)`

broadcasted from
the plates `(1,100)`

and `(10,1)`

:

```
>>> F.plates
(10, 100)
```

The prior for the observation noise :

```
>>> tau = Gamma(1e-3, 1e-3, name='tau')
```

Finally, the observations are conditionally independent Gaussian scalars:

```
>>> Y = GaussianARD(F, tau, name='Y')
```

Now we have defined our model and the next step is to observe some data and to perform inference.