def test_model_fit(fit, parameters, max_depth=10):
    if type(parameters[0])==tuple:
        fit_params = []
        for data_param, fit_param in parameters:
            print(fit_param)
            if hasattr(data_param, '__len__') and len(data_param)!=fit[fit_param].shape[1]:
                inds = len(data_param)
                within_95 = 0.0
                for i in range(inds):
                    within_95 += parameter_within_95(fit, data_param, fit_param, ind=i)
                within_95 /= inds
            else:
                within_95 = parameter_within_95(fit, data_param, fit_param)
            if within_95.mean()>.9:
                c = '32'
            else:
                c = '31'
            print("\x1b[%sm\"%.0f%% of values recovered\"\x1b[0m"%(c, within_95.mean()*100))

            Rhats = _summary(fit, pars=fit_param)['summary'][:,-1]
            if all(abs(Rhats-1)<.1):
                c = '32'
            else:
                c = '31'
            print("\x1b[%sm\"Maximum Rhat of %.2f\"\x1b[0m"%(c,max(Rhats)))
            fit_params.append(fit_param)
    stan_utility.check_treedepth(fit,max_depth=max_depth)
    stan_utility.check_energy(fit)
    check_div(fit, fit_param)
Beispiel #2
0
def fit2latex(fit, model_name, params, proj_dir):
    s = _summary(fit, params, [0.025, 0.975])
    body = _array_to_table(s['summary'], s['summary_rownames'],
                           s['summary_colnames'], 2)

    # Convert string to dataframe
    df = pd.read_fwf(StringIO(body), dtype=str, index_col=0)
    # Drop columns we don't care about
    df = df.drop(["se_mean", "sd", "Rhat"], axis=1)
    df = format_cols(df)

    # Round the ESS to the nearest 10
    df["n_eff"] = df["n_eff"].astype(int) // 100 * 100

    # Tidy up column and row names for tex
    df = rename_cols(df)
    df = rename_rows(df)

    df = df.rename_axis('{Parameter}', axis=1)

    # Column formatting. The S column type is provided by siunitx
    col_format = "@{}cS[table-format=2.2]S[table-format=2.2]S[table-format=2.2]r@{}"
    # Print dataframe to latex
    tex = df.to_latex(escape=False, column_format=col_format)

    save_path = path.join(proj_dir, 'out', '{}'.format(model_name))

    with open(save_path + '.tab', "w") as file:
        file.write(tex)
Beispiel #3
0
def get_rhat(fit) -> float:
    """Get `rhat` for the log-probability of a fit.
    
    This is a measure of the convergence across sampling chains.
    Good convergence is indicated by a value near 1.0.
    """
    x = _summary(fit, ['lp__'], [])
    summary = pd.DataFrame(x['summary'], columns=x['summary_colnames'], index=x['summary_rownames'])
    return summary.loc['lp__', 'Rhat']