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)
Exemplo n.º 2
0
    X=fixeff,
    Z_u=raneff,
    y=LMEdata.logprmt.values,
    p_intercept=prior_intercept,
    p_sd=prior_sd,
    p_fmu=priors_mu,
    p_fsigma=priors_sigma,
    p_r=priors_raneff,
    logT=1)

PMT_fit = LME.sampling(data=PMT_LME_data,
                       iter=2000,
                       chains=6,
                       n_jobs=6,
                       warmup=1000,
                       control=dict(adapt_delta=0.99))

PMT_fit = az.from_pystan(posterior=PMT_fit,
                         posterior_predictive='y_hat',
                         observed_data="y",
                         log_likelihood='log_lik',
                         coords={'b': fixeff_form.split('+')[1:]},
                         dims={'raw_beta': ['b']})

PMT_fit.to_netcdf("FittedModels/PMT_fit.nc")

print(PMT_fit)
stan_utility.check_treedepth(PMT_fit)
stan_utility.check_energy(PMT_fit)
stan_utility.check_div(PMT_fit)
Exemplo n.º 3
0
            weights_ext = bayesian_weights[:-1]
            simulated_ext = full_simulated[:, 0:-1]

        combine_curve(
            experimental,
            simulated_ext,
            weights_ext,
            options.iteration,
            experimental_file.split(".dat")[0] + ".fit",
        )

        # TODO: Write uncertainities here probably to a separate file
        for index, fname in enumerate(file_names):
            fout.write(str(bayesian_weights[index]) + " ")
        fout.write(str(jsd) + " ")
        fout.write(str(crysol_chi2) + "\n")
        # priors = bayesian_weights
        # And soem stan utilitry stats
        stan_utility.check_treedepth(fit)
        stan_utility.check_energy(fit)
        stan_utility.check_div(fit)
    fout.close()
    calculatePostChi()
    # plot_intermediates(options.output)
    # plot_chi2(options.output)
    # #Simple svd for the comparison
    # for experimental_file in open(options.experimental_files).readlines():
    #     experimental = read_file_safe(experimental_file.strip("\n"))
    #     svd_U, svd_s, svd_Vt = simple_svd(experimental)
    #     print(svd_U, svd_s, svd_Vt)
Exemplo n.º 4
0
    P=fixeff.shape[-1],  #number of pop level effects
    J=len(GLMEdata.participant.unique()),
    n_u=raneff.shape[-1],
    subj=GLMEdata.participant,
    X=fixeff,
    Z_u=raneff,
    y=np.asarray(GLMEdata.response.values),
    p_intercept=prior_intercept,
    p_fmu=priors_mu,
    p_fsigma=priors_sigma,
    p_r=priors_raneff)

Precision_fit = GLME.sampling(data=Precision_GLME_data,
                              iter=2000,
                              chains=6,
                              n_jobs=6,
                              warmup=1000,
                              control=dict(adapt_delta=0.99))
stan_utility.check_treedepth(Precision_fit)
stan_utility.check_energy(Precision_fit)
stan_utility.check_div(Precision_fit)

Precision_fit = az.from_pystan(posterior=Precision_fit,
                               posterior_predictive='y_hat',
                               observed_data="y",
                               log_likelihood='log_lik',
                               coords={'b': fixeff_form.split('+')[1:]},
                               dims={'raw_beta': ['b']})

Precision_fit.to_netcdf("FittedModels/Precision_fit.nc")