# 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: