Posterior Summaries

Hi,

I’m trying to calculate the posterior summaries, such as median, mean, and MAP, and 95%-CIs for each model parameter. Do we already have a function in the source code that does this?

In general, I’m looking to generate something similar to table 1 in OutbreakFlow: Model-based Bayesian inference of disease outbreak dynamics with invertible neural networks and its application to the COVID-19 pandemics in Germany

Best,
Ali

Hi Ali, unfortunately, there is currently no such function, but once you have the posterior samples, you can do something along the lines of:

def estimate_map(samples):
    
    bw = 1.06 * samples.std() * samples.size ** (-1 / 5.)
    scores = KernelDensity(bandwidth=bw).fit(samples.reshape(-1, 1)).score_samples(samples.reshape(-1, 1))
    max_i = scores.argmax()
    map_i = samples[max_i]
    return map_i

# Set parameter names as desired
param_names = ...

# Compute quantiles and medians
qs_95 = np.quantile(samples, q=[0.025, 0.975], axis=0)
qs_95_str = ['[{0:.3f} - {1:.3f}]'.format(qs_95[0, i], qs_95[1, i]) for i in range(len(param_names))]
meds = np.array(['{0:.3f}'.format(m) for m in np.median(samples, axis=0)])
means = np.array(['{0:.3f}'.format(m) for m in np.mean(samples, axis=0)])
maps = np.array(['{0:.3f}'.format(estimate_map(samples[:, i])) for i in range(len(param_names))])
  
# Prepare table
table = pd.DataFrame(index=param_names, data={'Median': meds, 'Mean': means, 'MAP': maps, '95-CI': qs_95_str})
2 Likes

Thank you, Stefan! This is very helpful.
Best,
Ali