Download the notebook here!

Comparison Plot Demo

[1]:
from collections import namedtuple
from estimagic.visualization.comparison_plot import comparison_plot
import pandas as pd
import numpy as np
from numpy.random import uniform
np.random.seed(89345)
[ ]:
from bokeh.plotting import output_notebook
output_notebook()

Functions for creating artificial estimagic optimization results lists

[ ]:
BLUEPRINT = namedtuple('optimization_result', ['params', 'info'])

def create_comparison_plot_inputs(model_class_dict):
    results_list = []
    counter = 0
    for model_class, specs in model_class_dict.items():
        for m in range(specs['nr_models']):
            df = create_result_df(specs)

            info = {
                'model_name': 'mod_{}'.format(counter),
                'model_class': model_class
            }
            results_list.append(BLUEPRINT(df, info))
            counter += 1
    return results_list


def create_result_df(specs):
    df = pd.DataFrame()
    group_col = []
    final_values = []
    ci_lower = []
    ci_upper = []
    names = []
    for name, nr_params in specs['param_tuples']:
        names += ['{}_{}'.format(name, i) for i in range(nr_params)]
        group_col += [name] * nr_params
        mean = np.random.randint(-5, 10)
        std = np.random.uniform(0, 4)
        vals = np.random.normal(mean, std, nr_params)
        final_values += vals.tolist()
        ci_lower += (vals - uniform(- 0.2, std)).tolist()
        ci_upper += (vals + uniform(+ 0.2, std)).tolist()
    df['group'] = group_col
    df['name'] = names
    df['value'] = final_values
    df['conf_int_lower'] = ci_lower
    df['conf_int_upper'] = ci_upper
    return df

Generate some artifical results

[ ]:
models1 = {
    'small': {
        'nr_models': 15,
        'param_tuples': [('covariance', 2), ('wage reg slope', 1)]
    },
    'large': {
        'nr_models': 14,
        'param_tuples': [('covariance', 2), ('wage reg slope', 1)]
    },
    'other': {
        'nr_models': 10,
        'param_tuples': [('covariance', 2)]
    },
}

reslist1 = create_comparison_plot_inputs(models1)
[ ]:
color_dict={
    'small': 'goldenrod',
    'large': 'navy',
    'other': 'firebrick',
}

Generate the comparison plot

[ ]:
df, grid = comparison_plot(reslist1, color_dict)