How to get 'expr' parameters into the res.flatchain object for use in Corner plots?

21 views
Skip to first unread message

Nick C

unread,
Aug 23, 2016, 3:57:12 PM8/23/16
to lmfit-py
Hi everyone,

I'm using LmFit to find some best-fit parameters. Let's call them A and B. I can easily find the best-fit A and B using lmfit, but things get tricky once emcee comes into play. Because of some prior information and assumptions in my model, it is more statistically robust to explore a posterior probability distribution corner plot of A vs (A+B) rather than A vs B. I notice in the example provided by the documentation, the authors use res.flatchain for the data input to create the corner plot, but unfortunately I read (here)  that flatchain returns values for only the parameters that vary! Here is their code:

>>> mini = lmfit.Minimizer(lnprob, mi.params)
>>> res = mini.emcee(burn=300, steps=600, thin=10, params=mi.params)
>>> import corner
>>> corner.corner(res.flatchain, labels=res.var_names, truths=list(res.params.valuesdict().values()))

The crux of my issue is this: I want to expand the flatchain object to contain values for non-varying, 'expr' parameters so I may create a corner plot that explores posterior probability distributions not for A and B, but for A and (A+B). I tried to dig into the weeds to understand what kind of object flatchain is... but then I started seeing things about pandas, so I decided to turn here.

I'll be very thankful for any help I can get!

Respectfully,




Nick Cemenenkoff

Andrew Nelson

unread,
Aug 23, 2016, 9:18:21 PM8/23/16
to lmfit-py
Ok, so I assume that you have two parameters, A and B. A is freely varying but B is linked to A via an `expr`. The `res.flatchain` attribute is a pandas.DataFrame. The following may help:

import numpy as np
import pandas as pd
import corner
%matplotlib inline

a = np.random.randn(1000)
df = pd.DataFrame(a, columns=['A'])
df['B'] = np.sin(df['A'])
t = corner.corner(df)

All you have to do is add an extra Series to the DataFrame to represent AplusB:

df = res.flatchain
df['ApB'] = df[A] + B
corner.corner(df)

HTH.
Andrew

Nick C

unread,
Aug 23, 2016, 11:03:06 PM8/23/16
to lmfit-py
Thank you! Just the clarification I needed.
Reply all
Reply to author
Forward
0 new messages