Matplot.func_envelopes examples?

300 views
Skip to first unread message

Roban

unread,
Nov 4, 2010, 7:01:55 AM11/4/10
to PyMC
Hello,

I'm intrigued by the capabilities hinted at in some of the
pymc.Matplot docstrings, but for the life of me I can't figure out how
to use func_envelopes and func_sd_envelopes. Does anyone have examples
of the use of this function and class? Ideally I'd like to be plotting
2-D confidence regions for my parameters. I'm using the latest commit
from github.

Thanks for your help with this, and thanks to the developers for all
of your hard work on pymc.

-Roban

Anand Patil

unread,
Nov 4, 2010, 7:42:23 AM11/4/10
to py...@googlegroups.com
Hi Roban, try this:


-----------


import pymc as pm
import numpy as np
import matplotlib.pyplot as pl

a = pm.Uninformative('a',0)
b = pm.Uninformative('b',0)
v = pm.OneOverX('v',1)

x = np.linspace(-1,1,5)
data = x*2-1+np.random.normal(size=len(x))*.5

y = pm.Normal('y',a+b*x,1./v,value=data,observed=True)
xplot = np.linspace(-2,2,51)
ypred = pm.Normal('ypred',a+b*xplot,1./v)
M = pm.MCMC([a,b,v,y,ypred])

M.isample(10000,4000,10)

env = pm.Matplot.func_envelopes(ypred)
for e in env:
    e.display(xplot,.5,new=False)
    
pl.plot(x,data,'r.',markersize=4)


---------

Cheers
Anand


--
You received this message because you are subscribed to the Google Groups "PyMC" group.
To post to this group, send email to py...@googlegroups.com.
To unsubscribe from this group, send email to pymc+uns...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pymc?hl=en.


Roban

unread,
Nov 4, 2010, 8:35:47 AM11/4/10
to PyMC
Thanks so much, that's very helpful. I can totally understand the use
case now.

I guess what I actually want to do is somewhat different. In the
example below, it would be to plot nice contours for the joint
distribution of a.trace() and b.trace(), or a.trace() and v.trace(),
etc. Should be straightforward, but am I right that there's nothing to
do that currently in pymc.Matplot? If not, I'll try to contribute
whatever I come up with.

-Roban

On Nov 4, 12:42 pm, Anand Patil <anand.prabhakar.pa...@gmail.com>
wrote:
> Hi Roban, try this:
>
> -----------
>
> import pymc as pm
> import numpy as np
> import matplotlib.pyplot as pl
>
> a = pm.Uninformative('a',0)
> b = pm.Uninformative('b',0)
> v = pm.OneOverX('v',1)
>
> x = np.linspace(-1,1,5)
> data = x*2-1+np.random.normal(size=len(x))*.5
>
> y = pm.Normal('y',a+b*x,1./v,value=data,observed=True)
> xplot = np.linspace(-2,2,51)
> ypred = pm.Normal('ypred',a+b*xplot,1./v)
> M = pm.MCMC([a,b,v,y,ypred])
>
> M.isample(10000,4000,10)
>
> env = pm.Matplot.func_envelopes(ypred)
> for e in env:
>     e.display(xplot,.5,new=False)
>
> pl.plot(x,data,'r.',markersize=4)
>
> ---------
>
> Cheers
> Anand
>
> On Thu, Nov 4, 2010 at 11:01 AM, Roban <roba...@gmail.com> wrote:
> > Hello,
>
> > I'm intrigued by the capabilities hinted at in some of the
> > pymc.Matplot docstrings, but for the life of me I can't figure out how
> > to use func_envelopes and func_sd_envelopes. Does anyone have examples
> > of the use of this function and class? Ideally I'd like to be plotting
> > 2-D confidence regions for my parameters. I'm using the latest commit
> > from github.
>
> > Thanks for your help with this, and thanks to the developers for all
> > of your hard work on pymc.
>
> > -Roban
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "PyMC" group.
> > To post to this group, send email to py...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > pymc+uns...@googlegroups.com <pymc%2Bunsu...@googlegroups.com>.

Anand Patil

unread,
Nov 4, 2010, 8:58:12 AM11/4/10
to py...@googlegroups.com
Hi Roban, no, there's nothing that does contours right now, but see http://healthyalgorithms.wordpress.com/2010/10/19/mcmc-in-python-how-to-stick-a-statistical-model-on-a-system-dynamics-model-in-pymc/ . Please do send along whatever you come up with.

Anand

To unsubscribe from this group, send email to pymc+uns...@googlegroups.com.

Roban Hultman Kramer

unread,
Nov 6, 2010, 3:16:36 PM11/6/10
to py...@googlegroups.com
Anand,

Thanks to your help, I've come up with something quite serviceable.

https://gist.github.com/665605

The plot2Ddist function plots the joint distribution of two variables,
with estimated density contours and marginal histograms. It includes
code allowing the contours to be specified by the fraction of points
contained inside them, which is useful for plotting
credible/confidence regions.

There's an example script based on the func_envelope example that you sent me.

The slowness comes from the Gaussian Kernel Density estimate (from
scipy.stats.gaussian_kde), which scales with the number of samples.
The KDE is used then used to find the contours (using matplotlib). I'm
sure there are more efficient ways to find the contours.

-Roban

--
Roban Hultman Kramer | Zwicky Fellow | Institute for Astronomy
ETH Zürich (Swiss Federal Institute of Technology)

Reply all
Reply to author
Forward
0 new messages