Beispiel #1
0
def get_result_point(stage, config, point_llk='max'):
    """
    Return point of a given stage result.

    Parameters
    ----------
    stage : :class:`models.Stage`
    config : :class:`config.BEATConfig`
    point_llk : str
        with specified llk(max, mean, min).

    Returns
    -------
    dict
    """
    if config.sampler_config.name == 'Metropolis':
        if stage.step is None:
            raise AttributeError('Loading Metropolis results requires'
                                 ' sampler parameters to be loaded!')

        sc = config.sampler_config.parameters
        from beat.sampler.metropolis import get_trace_stats
        pdict, _ = get_trace_stats(stage.mtrace, stage.step, sc.burn, sc.thin)
        point = pdict[point_llk]

    elif config.sampler_config.name == 'SMC':
        llk = stage.mtrace.get_values(varname='like', combine=True)

        posterior_idxs = utility.get_fit_indexes(llk)

        point = stage.mtrace.point(idx=posterior_idxs[point_llk])

    elif config.sampler_config.name == 'PT':
        params = config.sampler_config.parameters
        llk = stage.mtrace.get_values(varname='like',
                                      burn=int(params.n_samples * params.burn),
                                      thin=params.thin)

        posterior_idxs = utility.get_fit_indexes(llk)

        point = stage.mtrace.point(idx=posterior_idxs[point_llk])

    else:
        raise NotImplementedError('Sampler "%s" is not supported!' %
                                  config.sampler_config.name)

    return point
Beispiel #2
0
def get_trace_stats(mtrace, step, burn=0.5, thin=2):
    """
    Get mean value of trace variables and return point.

    Parameters
    ----------
    mtrace : :class:`pymc3.backends.base.MultiTrace`
        Multitrace sampling result
    step : initialised :class:`smc.SMC` sampler object
    burn : float
        Burn-in parameter to throw out samples from the beginning of the trace
    thin : int
        Thinning of samples in the trace

    Returns
    -------
    dict with points, covariance matrix
    """

    n_steps = len(mtrace)

    array_population = num.zeros((step.n_jobs * int(
                                    num.ceil(n_steps * (1 - burn) / thin)),
                                    step.ordering.dimensions))

    # collect end points of each chain and put into array
    for var, slc, shp, _ in step.ordering.vmap:
        if len(shp) == 0:
            array_population[:, slc] = num.atleast_2d(
                                mtrace.get_values(varname=var,
                                            burn=int(burn * n_steps),
                                            thin=thin,
                                            combine=True)).T
        else:
            array_population[:, slc] = mtrace.get_values(
                                                varname=var,
                                                burn=int(burn * n_steps),
                                                thin=thin,
                                                combine=True)

    llks = mtrace.get_values(
        varname=step.likelihood_name,
        burn=int(burn * n_steps),
        thin=thin,
        combine=True)

    posterior_idxs = utility.get_fit_indexes(llks)
    d = {}
    for k, v in posterior_idxs.iteritems():
        d[k] = step.bij.rmap(array_population[v, :])

    d['dist_mean'] = step.bij.rmap(array_population.mean(axis=0))
    avar = array_population.var(axis=0)
    if avar.sum() == 0.:
        logger.warn('Trace std not valid not enough samples! Use 1.')
        avar = 1.
    cov = num.eye(step.ordering.dimensions) * avar
    return d, cov