Ejemplo n.º 1
0
def get_density_plots(estimators_list,
                      simulators_dict,
                      path_to_results,
                      exp_prefix="question1_noise_reg_x",
                      task_ids=None):
    """
  This function allows to compare plots from estimators and simulators (i.e. fitted and true densities). Two modes are currently available:
  1) by specifying estimators and simulator, the function picks one result pair randomly that matches the given simulator/estimator
  selection
  2) by specifying the task_ids as list, it is possible to pick specific plots to compare

  Args:
    estimators: a list containing strings of estimators to be evaluated, e.g. ['KernelMixtureNetwork', 'MixtureDensityNetwork']
    simulators: a dict containing specifications of a simulator under which the estimators shall be compared, e.g.
      {'heteroscedastic': True, 'random_seed': 20, 'std': 1, 'simulator': 'EconDensity'}
    path_to_results: absolute path to where the dumped model files are stored
    exp_prefix: specifies the task question

  Returns:
    A list of figures for fitted and true densities.
  """

    if task_ids is not None:
        assert type(task_ids) == list
        assert len(task_ids) == len(estimators_list)

    RESULTS_FILE = 'results.pkl'
    logger.configure(path_to_results, exp_prefix)

    results_from_pkl_file = dict(logger.load_pkl_log(RESULTS_FILE))
    gof_result = GoodnessOfFitResults(
        single_results_dict=results_from_pkl_file)
    results_df = gof_result.generate_results_dataframe(
        base_experiment.KEYS_OF_INTEREST)
    """ load model's estimators """
    if task_ids is None:

        models_of_interest = {
            k: v
            for k, v in gof_result.single_results_dict.items()
            if v.probabilistic_model_params == simulators_dict and v.ndim_x +
            v.ndim_y == 2
        }

        models = [
            ConfigRunner.load_dumped_estimator(
                take_of_type(1, estimator_str, models_of_interest))
            for estimator_str in estimators_list
        ]
    else:
        models = [
            ConfigRunner.load_dumped_estimators(gof_result, task_id=task_ids)
        ]
    """ load model's simulators """
    # todo: implement when simulator dumps exist

    figs = []

    for model in models:
        graph = model.estimator.sess.graph
        sess = tf.Session(graph=graph)

        with sess:
            sess.run(tf.global_variables_initializer())
            model.estimator.sess = sess
            """ fitted density figures"""
            plt.suptitle(model.estimator.name)
            fig_fitted = model.estimator.plot3d()
            figs.append(fig_fitted)
            """ true density figures """
            # todo: use newly dumped simulators

            sess.close()

    return figs
            'y_noise_std': [0.1],
        },
    }

    simulators_params = {'LinearStudentT': {'ndim_x': [10]}}

    observations = 100 * np.logspace(2, 6, num=8, base=2.0, dtype=np.int32)

    return estimator_params, simulators_params, observations


if __name__ == '__main__':
    estimator_params, simulators_params, observations = question4()
    load = base_experiment.launch_experiment(estimator_params,
                                             simulators_params,
                                             observations,
                                             EXP_PREFIX,
                                             n_mc_samples=N_MC_SAMPLES,
                                             tail_measures=False)

    if load:
        logger.configure(config.DATA_DIR, EXP_PREFIX)

        results_from_pkl_file = dict(logger.load_pkl_log(RESULTS_FILE))
        gof_result = GoodnessOfFitResults(
            single_results_dict=results_from_pkl_file)
        results_df = gof_result.generate_results_dataframe(
            base_experiment.KEYS_OF_INTEREST)

        gof_result = ConfigRunner.load_dumped_estimators(gof_result)
from cde.evaluation.simulation_eval import base_experiment
import cde.model_fitting.ConfigRunner as ConfigRunner
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

EXP_PREFIX = 'question2_entropy_reg'
RESULTS_FILE = 'results.pkl'

logger.configure(
    '/home/jonasrothfuss/Dropbox/Eigene_Dateien/Uni/WS17_18/Density_Estimation/Nonparametric_Density_Estimation/data/cluster',
    EXP_PREFIX)

results_from_pkl_file = dict(logger.load_pkl_log(RESULTS_FILE))
gof_result = GoodnessOfFitResults(single_results_dict=results_from_pkl_file)
results_df = gof_result.generate_results_dataframe(
    base_experiment.KEYS_OF_INTEREST + ['entropy_reg_coef'])

#gof_result = ConfigRunner.load_dumped_estimators(gof_result, task_id=[5])

SMALL_SIZE = 11
MEDIUM_SIZE = 12
LARGE_SIZE = 16
TITLE_SIZE = 20

LINEWIDTH = 6

plt.rc('font', size=SMALL_SIZE)  # controls default text sizes
plt.rc('axes', titlesize=LARGE_SIZE)  # fontsize of the axes title
plt.rc('axes', labelsize=MEDIUM_SIZE)  # fontsize of the x and y labels
plt.rc('xtick', labelsize=SMALL_SIZE)  # fontsize of the tick labels
plt.rc('ytick', labelsize=SMALL_SIZE)  # fontsize of the tick labels
## SKEW Normal

EXP_PREFIX = 'question4_benchmark_skew_NF'
RESULTS_FILE = 'results.pkl'

if not os.path.isdir(EXP_PREFIX):
    os.makedirs(EXP_PREFIX)

logger.configure(
  '/home/simon/Documents/KIT/Informatik/Bachelorarbeit/Conditional_Density_Estimation/data/cluster',
  EXP_PREFIX)

results_from_pkl_file = dict(logger.load_pkl_log(RESULTS_FILE))
gof_result = GoodnessOfFitResults(single_results_dict=results_from_pkl_file)
results_df = gof_result.generate_results_dataframe(base_experiment.KEYS_OF_INTEREST + ['bandwidth'])


plot_dict = dict([(simulator,
                   {"MDN": {"simulator": simulator, "estimator": "MixtureDensityNetwork", "x_noise_std": 0.1},
                     "KMN": {"simulator": simulator, "estimator": "KernelMixtureNetwork", "x_noise_std": 0.1},
                     "LSCDE": {"simulator": simulator, "estimator": "LSConditionalDensityEstimation"},
                     "CKDE": {"simulator": simulator, "estimator": "ConditionalKernelDensityEstimation", "bandwidth": "normal_reference"},
                     "CKDE_CV": {"simulator": simulator, "estimator": "ConditionalKernelDensityEstimation", "bandwidth": "cv_ml"},
                     "NKDE": {"simulator": simulator, "estimator": "NeighborKernelDensityEstimation"},
                    "NF": {"simulator": simulator, "estimator": "NormalizingFlowEstimator",}
                    }) for simulator in ["EconDensity", "ArmaJump", "SkewNormal"]])

fig = gof_result.plot_metric(plot_dict, metric="hellinger_distance", figsize=(15, 5.5))

## ARMA JUMP
Ejemplo n.º 5
0
EXP_PREFIX = "question8_benchmark"
RESULTS_FILE = "results.pkl"
CLUSTER_DIR = "/local/rojonas/cde/data/local"
LOCATION = "{}/{}/{}".format(CLUSTER_DIR, EXP_PREFIX, RESULTS_FILE)
DATA_DIR_LOCAL = "/home/jonasrothfuss/Dropbox/Eigene_Dateien/ETH/02_Projects/02_Noise_Regularization/02_Code_Conditional_Density_Estimation/data/cluster"

logger.configure(
    #"/local/rojonas/cde/data/local",
    DATA_DIR_LOCAL,
    #CLUSTER_DIR,
    EXP_PREFIX,
)

results_from_pkl_file = dict(logger.load_pkl_log(RESULTS_FILE))
gof_result = GoodnessOfFitResults(single_results_dict=results_from_pkl_file)
results_df = gof_result.generate_results_dataframe(base_experiment.KEYS_OF_INTEREST_LOGPROB + ["bandwidth", "param_selection"])
results_df.replace(to_replace=[None], value="None", inplace=True)

# seprarate 2d and 4d GMM
results_df.index = list(range(len(results_df)))

for i, row in results_df[['simulator', 'ndim_y']].iterrows():
    if row['simulator'] == 'GaussianMixture':
        results_df.at[i, 'simulator'] = '%s_%id'%(row['simulator'], row['ndim_y'])

estimators = [
    "MixtureDensityNetwork",
    "KernelMixtureNetwork",
    "NormalizingFlowEstimator",
    "ConditionalKernelDensityEstimation",
    "NeighborKernelDensityEstimation",