File: mixture2.py

package info (click to toggle)
python-bumps 1.0.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,200 kB
  • sloc: python: 24,517; xml: 493; ansic: 373; makefile: 211; javascript: 99; sh: 94
file content (71 lines) | stat: -rwxr-xr-x 2,231 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python

"""
Multimodal demonstration using gaussian mixture model.

The model is a mixture model representing the probability density from a
product of gaussians.

This example show performance of the algorithm on multimodal densities,
with adjustable number of densities and degree of separation.

The peaks are distributed about the x-y plane so that the marginal densities
in x and y are spaced every 2 units using latin hypercube sampling.  For small
peak widths, this means that the densities will not overlap, and the marginal
maximum likelihood for a given x or y value should match the estimated density.
With overlap, the marginal density will over estimate the marginal maximum
likelihood.

Adjust the width of the peaks, *S*, to see the effect of relative diameter of
the modes on sampling.  Adjust the height of the peaks, *I*, to see the
effects of the relative height of the modes.  Adjust the count *n* to see
the effects of the number of modes.

Note that dream.diffev.de_step adds jitter to the parameters at the 1e-6 level,
so *S* < 1e-4 cannot be modeled reliably.

*draws* is set to 1000 samples per mode.  *burn* is set to 100 samples per mode.
Population size *h* is set to 20 per mode.  A good choice for number of
sequences *k* is not yet determined.
"""

from pylab import *
from bumps.dream import *

if 1:  # Fixed layout of 5 minima
    n = 5
    S = [0.1] * 5
    x = [-4, -2, 0, 2, 4]
    y = [2, -2, -4, 0, 4]
    z = [-2, -1, 0, 1, 3]
    I = [5, 2.5, 1, 4, 1]
else:  # Semirandom layout of n minima
    d = 3
    n = 40
    S = [0.1] * n
    x = linspace(-n + 1, n - 1, n)
    y = permutation(x)
    z = permutation(x)
    I = 2 * linspace(-1, 1, n) ** 2 + 1

args = []  # Sequence of density, weight, density, weight, ...
for xi, yi, zi, Si, Ii in zip(x, y, z, S, I):
    args.extend((MVNormal([xi, yi, zi], Si * eye(3)), Ii))
    # args.extend( (MVNormal([xi,yi],Si*eye(2)), Ii) )
model = Mixture(*args)

k = 20 * n
h = int(20 * n / k)
sampler = Dream(
    model=model,
    population=randn(h, k, 2),
    # use_delayed_rejection=False,
    DE_snooker_rate=0.5,
    outlier_test="none",
    draws=4000 * n,
    burn=500 * k,
    thinning=1,
)
mc = sampler.sample()
mc.show()
show()