dit/dit — pull request #25 by chebee7i (2013-09-30 18:45:38+00:00)
Title: Create mixture distributions

here's code from cmpy:

```python
def mixture_distribution(dists, weights):
"""
Create a mixture distribution: $\Sum p_i d_i$

Parameters
----------
dists: [Distribution]
List of distributions to mix.

weights: [float]
List of weights to use in mixing `dists`.

Returns
-------
mix: Distribution
The mixture distribution.

Raises
------
CMPyException:
Raised if there isn't a weight for each distribution, or a distribution for each weight.

Raised if the weights do not sum to unity.
"""
if len(dists) != len(weights):
raise CMPyException("Must supply an equal number of distributions and weights.")
if not close(sum(weights), 1) and all( p >= 0 for p in weights ):
raise CMPyException("Weights must be non-negative and have a unit sum.")

events = set().union(*[ d.events() for d in dists ])
return D({e: sum( w*d[e] for w, d in zip(weights, dists) ) for e in events})
```