pymc-devs/pymc — pull request #305 by juliohm (2013-07-04 13:55:37+00:00)
Title: Added stochastic_from_data for non-parametric distributions

It's sometimes very useful to represent nonparametric distributions in Bayesian inference, specially when we try to update our prior knowledge using multimodal posteriors. Following is an example to solving an inverse problem with PyMC that requires nonparametric distributions fitted with arbitrary histograms. The solution is either 2 or -2 as represented by the posterior.

```python
from pymc import *
import matplotlib.pyplot as plt

xtrue = 2 # unknown in the real application
prior = rnormal(0,1,10000) # initial guess is inaccurate
for i in range(5):
x = stochastic_from_data('x', prior)
y = x*x
obs = Normal('obs', y, 0.1, xtrue*xtrue + rnormal(0,1), observed=True)

model = Model([x,y,obs])
mcmc = MCMC(model)
mcmc.sample(10000)

Matplot.plot(mcmc.trace('x'))
plt.show()

prior = mcmc.trace('x')[:]
```

This code uses [Kernel Density Estimation](http://en.wikipedia.org/wiki/Kernel_density_estimation) for fitting the histogram following a discussion on [Stackoverflow](http://stackoverflow.com/questions/17409324/solving-inverse-problems-with-pymc). I plan to add the multidimensional case after I update my SciPy installation.