File: modelconstruct.rst

package info (click to toggle)
python-bayespy 0.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,132 kB
  • sloc: python: 22,402; makefile: 156
file content (522 lines) | stat: -rw-r--r-- 20,918 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
..
   Copyright (C) 2014 Jaakko Luttinen

   This file is licensed under the MIT License. See LICENSE for a text of the
   license.


.. currentmodule:: bayespy.nodes
                
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
:ref:`sec-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 :class:`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:

1. the prior probability distribution for the variables,

2. the factorization of the posterior approximation,

3. 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
:class:`GaussianARD` node corresponding to :math:`\mu` from the normal
distribution, ``tau`` was a stochastic :class:`Gamma` node corresponding to
:math:`\tau` from the gamma distribution, and ``y`` was a stochastic
:class:`GaussianARD` node corresponding to :math:`y` from the normal
distribution with mean :math:`\mu` and precision :math:`\tau`.  If we denote the
set of all stochastic nodes by :math:`\Omega`, and by :math:`\pi_X` the set of
parents of a node :math:`X`, the model is defined as
                
.. math::


   p(\Omega) = \prod_{X \in \Omega} p(X|\pi_X),

where nodes correspond to the terms :math:`p(X|\pi_X)`\ .

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: :math:`q(\mu)q(\tau)`\ . Thus, the posterior approximation can be written
as:

.. math::


   p(\tilde{\Omega}|\hat{\Omega}) \approx \prod_{X \in \tilde{\Omega}} q(X),

                
where :math:`\tilde{\Omega}` is the set of latent stochastic nodes and
:math:`\hat{\Omega}` 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, :class:`GaussianGammaISO` is a joint node for :math:`\mu` and
:math:`\tau` variables from the normal-gamma distribution and the posterior
approximation does not factorize between :math:`\mu` and :math:`\tau`, that is,
the posterior approximation is :math:`q(\mu,\tau)`.
                
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, :class:`Gamma` uses gamma distribution to also
approximate the posterior distribution.  Similarly, :class:`GaussianARD` uses
Gaussian distribution for the posterior.  However, the posterior approximation
of :class:`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
:math:`\mathbf{y}_{mn}` could be defined as

.. math::

      
   \mathbf{y}_{mn} \sim \mathcal{N}(\boldsymbol{\mu}, \mathbf{\Lambda}),\qquad m=0,\ldots,9, \quad n=0,\ldots,29.

This can also be visualized as a graphical model:

                
.. bayesnet::

    \node[latent] (y) {$\mathbf{y}_{mn}$} ;
    \node[latent, above left=1.8 and 0.4 of y] (mu) {$\boldsymbol{\mu}$} ;
    \node[latent, above right=1.8 and 0.4 of y] (Lambda) {$\mathbf{\Lambda}$} ;
    \factor[above=of y] {y-f} {left:$\mathcal{N}$} {mu,Lambda}     {y};
    \plate {m-plate} {(y)(y-f)(y-f-caption)} {$m=0,\ldots,9$} ;
    \plate {n-plate} {(m-plate)(m-plate-caption)} {$n=0,\ldots,29$} ;
                
The variable has two plates: one for the index :math:`m` and one for the
index :math:`n`\ . 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
:math:`\mathbf{y}_{mn}`\ , it is also possible to share plates with parents. For
instance, the mean could be different for each index :math:`m` and the precision
for each index :math:`n`\ :

.. math::


   \mathbf{y}_{mn} \sim \mathcal{N}(\boldsymbol{\mu}_m,
   \mathbf{\Lambda}_n),\qquad m=0,\ldots,9, \quad n=0,\ldots,29.

which has the following graphical representation:

                
.. bayesnet::

    \node[latent] (y) {$\mathbf{y}_{mn}$} ;
    \node[latent, above left=1 and 2 of y] (mu) {$\boldsymbol{\mu}_m$} ;
    \node[latent, above right=1 and 1 of y] (Lambda) {$\mathbf{\Lambda}_n$} ;
    \factor[above=of y] {y-f} {above:$\mathcal{N}$} {mu,Lambda}     {y};
    \plate {m-plate} {(mu)(y)(y-f)(y-f-caption)} {$m=0,\ldots,9$} ;
    \plate {n-plate} {(Lambda)(y)(y-f)(y-f-caption)(m-plate-caption)(m-plate.north east)} {$n=0,\ldots,29$} ;
                
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:

1. The two plates have equal size
2. 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.

.. _sec-irregular-plates:

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, :class:`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 :math:`4 \times 3`
-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):

.. math::

    \mathbf{Y} = \mathbf{C}\mathbf{X}^T + \mathrm{noise}

where :math:`\mathbf{Y}` is :math:`M\times N` data matrix, :math:`\mathbf{C}` is
:math:`M\times D` loading matrix, :math:`\mathbf{X}` is :math:`N\times D` state
matrix, and noise is isotropic Gaussian.  The dimensionality :math:`D` is
usually assumed to be much smaller than :math:`M` and :math:`N`.


A probabilistic formulation can be written as:

.. math::

   p(\mathbf{Y}) &= \prod^{M-1}_{m=0} \prod^{N-1}_{n=0} \mathcal{N}(y_{mn} |
   \mathbf{c}_m^T \mathbf{x}_n, \tau)
   \\
   p(\mathbf{X}) &= \prod^{N-1}_{n=0} \prod^{D-1}_{d=0} \mathcal{N}(x_{nd} |
   0, 1)
   \\
   p(\mathbf{C}) &= \prod^{M-1}_{m=0} \prod^{D-1}_{d=0} \mathcal{N}(c_{md} |
   0, \alpha_d)
   \\
   p(\boldsymbol{\alpha}) &= \prod^{D-1}_{d=0} \mathcal{G} (\alpha_d | 10^{-3},
   10^{-3})
   \\
   p(\tau) &= \mathcal{G} (\tau | 10^{-3}, 10^{-3})

where we have given automatic relevance determination (ARD) prior for
:math:`\mathbf{C}`.  This can be visualized as a graphical model:

.. bayesnet::

    \node[latent] (y) {$\mathbf{y}_{mn}$} ;
    \node[det, above=of y] (dot) {dot} ;
    \node[latent, right=2 of dot] (tau) {$\tau$} ;
    \node[latent, above left=1 and 2 of dot] (C) {$c_{md}$} ;
    \node[latent, above=of C] (alpha) {$\alpha_d$} ;
    \node[latent, above right=1 and 1 of dot] (X) {$x_{nd}$} ;

    \factor[above=of y] {y-f} {left:$\mathcal{N}$} {dot,tau} {y};
    \factor[above=of C] {C-f} {left:$\mathcal{N}$} {alpha} {C};
    \factor[above=of X] {X-f} {above:$\mathcal{N}$} {} {X};
    \factor[above=of alpha] {alpha-f} {above:$\mathcal{G}$} {} {alpha};
    \factor[above=of tau] {tau-f} {above:$\mathcal{G}$} {} {tau};
    \edge {C,X} {dot};

    \tikzstyle{plate caption} += [below left=0pt and 0pt of #1.north east] ;
    \plate {d-plate} {(X)(X-f)(X-f-caption)(C)(C-f)(C-f-caption)(alpha)(alpha-f)(alpha-f-caption)} {$d=0,\ldots,2$} ;
    \tikzstyle{plate caption} += [below left=5pt and 0pt of #1.south east] ;
    \plate {m-plate} {(y)(y-f)(y-f-caption)(C)(C-f)(C-f-caption)(d-plate.south west)} {$m=0,\ldots,9$} ;
    \plate {n-plate} {(y)(y-f)(y-f-caption)(X)(X-f)(X-f-caption)(m-plate-caption)(m-plate.north east)(d-plate.south east)} {$n=0,\ldots,99$} ;

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 :math:`\mathbf{X}`:

>>> 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 :math:`\boldsymbol{\alpha}` of the loading
matrix:

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

The prior for the loading matrix :math:`\mathbf{C}`:

>>> 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
:math:`D`-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 :math:`\tau`:

>>> 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.