def _summary_sim(sim, pars, probs):
    """Summarize chains together and separately

    REF: rstan/rstan/R/misc.R

    Parameters are unraveled in *column-major order*.

    Parameters
    ----------
    sim : dict
        dict from from a stanfit fit object, i.e., fit['sim']
    pars : Iterable of str
        parameter names
    probs : Iterable of probs
        desired quantiles

    Returns
    -------
    summaries : OrderedDict of array
        This dictionary contains the following arrays indexed by the keys
        given below:
        - 'msd' : array of shape (num_params, 2) with mean and sd
        - 'sem' : array of length num_params with standard error for the mean
        - 'c_msd' : array of shape (num_params, 2, num_chains)
        - 'quan' : array of shape (num_params, num_quan)
        - 'c_quan' : array of shape (num_params, num_quan, num_chains)
        - 'ess' : array of shape (num_params, 1)
        - 'rhat' : array of shape (num_params, 1)

    Note
    ----
    `_summary_sim` has the parameters in *column-major* order whereas `_summary`
    gives them in *row-major* order. (This follows RStan.)
    """
    # NOTE: this follows RStan rather closely. Some of the calculations here
    probs_len = len(probs)
    n_chains = len(sim['samples'])
    # tidx is a dict with keys that are parameters and values that are their
    # indices using column-major ordering
    tidx = _pars_total_indexes(sim['pars_oi'], sim['dims_oi'], sim['fnames_oi'], pars)
    tidx_colm = [tidx[par] for par in pars]
    tidx_colm = list(itertools.chain(*tidx_colm))  # like R's unlist()
    tidx_rowm = [tidx[par+'_rowmajor'] for par in pars]
    tidx_rowm = list(itertools.chain(*tidx_rowm))
    tidx_len = len(tidx_colm)
    lmsdq = [_get_par_summary(sim, i, probs) for i in tidx_colm]
    msd = np.row_stack([x['msd'] for x in lmsdq])
    quan = np.row_stack([x['quan'] for x in lmsdq])
    probs_str = tuple(["{:g}%".format(100*p) for p in probs])
    msd.shape = (tidx_len, 2)
    quan.shape = (tidx_len, probs_len)

    c_msd = np.row_stack([x['c_msd'] for x in lmsdq])
    c_quan = np.row_stack([x['c_quan'] for x in lmsdq])
    c_msd.shape = (tidx_len, 2, n_chains)
    c_quan.shape = (tidx_len, probs_len, n_chains)
    sim_attr_args = sim.get('args', None)
    if sim_attr_args is None:
        cids = list(range(n_chains))
    else:
        cids = [x['chain_id'] for x in sim_attr_args]

    c_msd_names = dict(parameters=np.asarray(sim['fnames_oi'])[tidx_colm],
                       stats=("mean", "sd"),
                       chains=tuple("chain:{}".format(cid) for cid in cids))
    c_quan_names = dict(parameters=np.asarray(sim['fnames_oi'])[tidx_colm],
                        stats=probs_str,
                        chains=tuple("chain:{}".format(cid) for cid in cids))
    ess_and_rhat = np.array([pystan.chains.ess_and_splitrhat(sim, n) for n in tidx_colm])
    ess, rhat = [arr.ravel() for arr in np.hsplit(ess_and_rhat, 2)]
    return dict(msd=msd, c_msd=c_msd, c_msd_names=c_msd_names, quan=quan,
                c_quan=c_quan, c_quan_names=c_quan_names,
                sem=msd[:, 1] / np.sqrt(ess), ess=ess, rhat=rhat,
                row_major_idx=tidx_rowm, col_major_idx=tidx_colm)
def _summary_sim ( sim , pars , probs ) : probs_len = len ( probs ) n_chains = len ( sim [ 'samples' ] ) tidx = _pars_total_indexes ( sim [ 'pars_oi' ] , sim [ 'dims_oi' ] , sim [ 'fnames_oi' ] , pars ) tidx_colm = [ tidx [ par ] for par in pars ] tidx_colm = list ( itertools . chain ( * tidx_colm ) ) tidx_rowm = [ tidx [ par + '_rowmajor' ] for par in pars ] tidx_rowm = list ( itertools . chain ( * tidx_rowm ) ) tidx_len = len ( tidx_colm ) lmsdq = [ _get_par_summary ( sim , i , probs ) for i in tidx_colm ] msd = np . row_stack ( [ x [ 'msd' ] for x in lmsdq ] ) quan = np . row_stack ( [ x [ 'quan' ] for x in lmsdq ] ) probs_str = tuple ( [ "{:g}%" . format ( 100 * p ) for p in probs ] ) msd . shape = ( tidx_len , 2 ) quan . shape = ( tidx_len , probs_len ) c_msd = np . row_stack ( [ x [ 'c_msd' ] for x in lmsdq ] ) c_quan = np . row_stack ( [ x [ 'c_quan' ] for x in lmsdq ] ) c_msd . shape = ( tidx_len , 2 , n_chains ) c_quan . shape = ( tidx_len , probs_len , n_chains ) sim_attr_args = sim . get ( 'args' , None ) if sim_attr_args is None : cids = list ( range ( n_chains ) ) else : cids = [ x [ 'chain_id' ] for x in sim_attr_args ] c_msd_names = dict ( parameters = np . asarray ( sim [ 'fnames_oi' ] ) [ tidx_colm ] , stats = ( "mean" , "sd" ) , chains = tuple ( "chain:{}" . format ( cid ) for cid in cids ) ) c_quan_names = dict ( parameters = np . asarray ( sim [ 'fnames_oi' ] ) [ tidx_colm ] , stats = probs_str , chains = tuple ( "chain:{}" . format ( cid ) for cid in cids ) ) ess_and_rhat = np . array ( [ pystan . chains . ess_and_splitrhat ( sim , n ) for n in tidx_colm ] ) ess , rhat = [ arr . ravel ( ) for arr in np . hsplit ( ess_and_rhat , 2 ) ] return dict ( msd = msd , c_msd = c_msd , c_msd_names = c_msd_names , quan = quan , c_quan = c_quan , c_quan_names = c_quan_names , sem = msd [ : , 1 ] / np . sqrt ( ess ) , ess = ess , rhat = rhat , row_major_idx = tidx_rowm , col_major_idx = tidx_colm )
Summarize chains together and separately

    REF: rstan/rstan/R/misc.R

    Parameters are unraveled in *column-major order*.

    Parameters
    ----------
    sim : dict
        dict from from a stanfit fit object, i.e., fit['sim']
    pars : Iterable of str
        parameter names
    probs : Iterable of probs
        desired quantiles

    Returns
    -------
    summaries : OrderedDict of array
        This dictionary contains the following arrays indexed by the keys
        given below:
        - 'msd' : array of shape (num_params, 2) with mean and sd
        - 'sem' : array of length num_params with standard error for the mean
        - 'c_msd' : array of shape (num_params, 2, num_chains)
        - 'quan' : array of shape (num_params, num_quan)
        - 'c_quan' : array of shape (num_params, num_quan, num_chains)
        - 'ess' : array of shape (num_params, 1)
        - 'rhat' : array of shape (num_params, 1)

    Note
    ----
    `_summary_sim` has the parameters in *column-major* order whereas `_summary`
    gives them in *row-major* order. (This follows RStan.)