Beispiel #1
0
def calc_evidence():

    # some setups for calculating the evidence

    split = True
    autothin = False
    setthinning = 0.5
    thinning = setthinning
    burnfraction = 0.

    if (split):
        nchains = 2
    else:
        nchains = 1

    chainfile_emu = 'samples/emulator_*'

    evi_emu = MCEvidence(chainfile_emu,
                         split=split,
                         kmax=0,
                         verbose=0,
                         priorvolume=1.,
                         thinlen=thinning,
                         burnlen=int(burnfraction * nsamples_emu),
                         debug=False).evidence()

    return evi_emu
Beispiel #2
0
def gaussian_emcee(nwalkers=300,thin=5,nmcmc=5000):
    #Evidence calculation based on emcee sampling
    mNd=gaussian_eg()
    mecNd=make_emcee_chain(mNd,nwalkers=nwalkers)
    samples,lnp=mecNd.mcmc(nmcmc=nmcmc,thin=thin)


    #estimate evidence
    chain={'samples':samples,'loglikes':lnp}
                          
    mce=MCEvidence(chain, verbose=2,ischain=True,                       
                       brange=[3,4.2]).evidence(rand=True)

    return mce
Beispiel #3
0
    def Run(self):
        if self.state:
            self.printInfo("Calculatong MCEvidence...")

            mce = MCEvidence([self.chain],
                             ischain=True,
                             thinlen=self.Thinlen,
                             burnlen=self.Burnlen).evidence(pos_lnp=True)
            avgL = np.mean(self.lnlike)
            Gresult = self.myResult(avgL, mce)

            self.printInfo("Results are :")
            self.printRes(Gresult)
            return Gresult
        else:
            self.printErr("path error!")
            return 0
            mce_info['PriorVol'].append(prior_volume)
            mce_info['ndim'].append(ndim)
            #
            logger.debug('***model: {},  ndim:{}, volume:{}, name={}'.format(
                mm, ndim, prior_volume, parMC['name']))
            #
            nc_read = ''
            nc_use = ''

            for icc, cext in enumerate(chains_extension_list):
                fchain = fname + cext
                e, info = MCEvidence(fchain,
                                     ndim=ndim,
                                     isfunc=h0_gauss_lnp,
                                     priorvolume=prior_volume,
                                     kmax=kmax,
                                     verbose=verbose,
                                     burnlen=burnfrac,
                                     thinlen=thinfrac).evidence(info=True,
                                                                pos_lnp=False)
                mce[imm, icc] = e[0]
                icc += 1
                nc_read = nc_read + '%s,' % info['Nsamples_read']
                nc_use = nc_use + '%s,' % info['Nsamples']

            mce_info['N_read'].append(nc_read)
            mce_info['N_used'].append(nc_use)
        else:
            print('*** not available: ', fname)
            mce[imm, :] = np.nan
            mce_info['N_read'].append('')
        mce = np.zeros(len(Models['model'])) - np.inf

        for i, mm in enumerate(Models['model']):  #model loop

            #print('m,d,m+d+s',mm,dd,ss,type(mm),type(dd),type(ss))
            if ss:
                ff = '{}_{}_{}'.format(mm, dd, ss)
            else:
                ff = '{}_{}'.format(mm, dd)

            method = 'PLA/{0}/{1}/{2}'.format(mm, dd, ff)

            try:
                print('------')
                mce[i] = MCEvidence(method, verbose=verbose).evidence()[0]
                print()
            except:
                print('model+data not available: ', method)
                mce[i] = -np.inf

            df[ss] = mce

        #get relative evidence
        print('df.data.max=', df[ss].max())
        df[ss] = df[ss] - df[ss].max()

    print('--------------- data={}---------'.format(dd))
    print(tabulate(df, headers='keys', tablefmt='psql'))

    #append all tables to file
Beispiel #6
0
    def mcmcRunner(self, iniFile=None, **kwargs):
        """
            This method calls MCMCAnalyzer.
            Returns [MCMCAnalyzer object, time, evidence via MCEvidence (if it is possible)]

        """
        if iniFile:
            nsamp = self.config.getint('mcmc', 'nsamp', fallback=50000)
            skip = self.config.getint('mcmc', 'skip', fallback=300)
            ## temperature at which to sample, weights get readjusted on the fly
            temp = self.config.getfloat('mcmc', 'temp', fallback=2)
            chainno = self.config.getint('mcmc', 'chainno', fallback=1)
            GRstop = self.config.getfloat('mcmc', 'GRstop', fallback=0.01)
            evidence = self.config.getboolean('mcmc',
                                              'evidence',
                                              fallback=False)
        else:
            nsamp = kwargs.pop('nsamp', 50000)
            skip = kwargs.pop('skip', 300)
            temp = kwargs.pop('temp', 2)
            chainno = kwargs.pop('chainno', 1)
            GRstop = kwargs.pop('GRstop', 0.01)
            evidence = kwargs.pop('evidence', False)
            if kwargs:
                logger.critical(
                    'Unexpected **kwargs for MCMC: {}'.format(kwargs))
                logger.info(
                    'You can skip writing any option and SimpleMC will use default values.\n'
                    'MCMC executer kwargs are:\n\tnsamp (int) Default: 50000\n\t'
                    'skip (int) Default 300\n\ttemp (float) Default: 2.0'
                    '\n\tchainno (int) Default: 1\n\t'
                    'evidence (boolean) Default: False')
                sys.exit(1)
                #raise TypeError('Unexpected **kwargs: {}'.format(kwargs))
        logger.info("\n\tnsamp: {}\n\tskip: {}\n\t"
                    "temp: {}\n\tchain num: {}\n\tevidence: {}".format(
                        nsamp, skip, temp, chainno, evidence))
        if self.analyzername is None: self.analyzername = 'mcmc'
        self.outputpath = "{}_{}".format(self.outputpath, self.analyzername)
        #Check whether the file already exists
        self.outputChecker()
        ti = time.time()

        #Main process
        M = MCMCAnalyzer(self.L,
                         self.outputpath,
                         skip=skip,
                         nsamp=nsamp,
                         temp=temp,
                         chain_num=chainno,
                         addDerived=self.addDerived,
                         GRstop=GRstop)

        self.ttime = time.time() - ti

        #Compute Bayesian Evidence
        if evidence:
            try:
                from MCEvidence import MCEvidence
                logger.info(
                    "Aproximating bayesian evidence with MCEvidence (arXiv:1704.03472)\n"
                )
                MLE = MCEvidence(self.outputpath + ".txt").evidence()
                self.result = [
                    'mcmc', M, "Evidence with MCEvidence : {}\n".format(MLE),
                    strresult
                ]
            except:
                #writeSummary(self.chainsdir, outputname, ttime)
                # print("Warning!")
                # print("MCEvidence could not calculate the Bayesian evidence [very small weights]\n")
                logger.error(
                    "MCEvidence could not calculate the Bayesian evidence [very small weights]"
                )
        else:
            self.result = ['mcmc', M, "Maxlike: {}".format(M.maxloglike)]

        return True
Beispiel #7
0
def glm_stan(iterations=10000,outdir='chains'):
    import pystan
    stanmodel='''
     data {
             int<lower=1> K;
             int<lower=0> N;
             real y[N];
             matrix[N,K] x;
     }
     parameters {
             vector[K] beta;
             real sigma;
     }
     model {         
             real mu[N];
             vector[N] eta   ;
             eta <- x*beta;
             for (i in 1:N) {
                mu[i] <- (eta[i]);
             };
             increment_log_prob(normal_log(y,mu,sigma));

     }
     '''   
    glmq=glm_eg()
    df=pd.DataFrame()
    df['x1']=glmq.x
    df['x2']=glmq.x**2
    df['y']=glmq.y_sample

    data={'N':glmq.ndata,
               'K':glmq.ndim,
                'x':df[['x1','x2']],
               'y':glmq.y_sample}

   
    if os.path.exists(outdir):
        os.makedirs(outdir)
    cache_fname='{}/glm2d_pystan_chain.pkl'.format(outdir)
    #read chain from cache if possible 
    try:
        raise
        print('reading chain from: '+cache_fname)
        stan_chain = pickle.load(open(cache_fname, 'rb'))
    except:
        # Intialize pystan -- this will convert our pystan code into C++
        # and run MCMC
        fit = pystan.stan(model_code=stanmodel, data=data,
                          iter=1000, chains=4)

        # Extract PyStan chain for GLM example
        stan_chain=fit.extract(permuted=True)

        # Check input parameter recovery and estimate evidence
        if 'beta' in stan_chain.keys(): stan_chain['samples']=stan_chain.pop('beta')
        if 'lp__' in stan_chain.keys(): stan_chain['loglikes']=stan_chain.pop('lp__')
        
        
        print('writing chain in: '+cache_fname)
        with open(cache_fname, 'wb') as f:
                pickle.dump(stan_chain, f)


    theta_means = stan_chain['beta'].mean(axis=0)
    print('GLM example input parameter values: ',harry.theta)
    print('GLM example estimated parameter values: ',theta_means)


    # Here given pystan samples and log probability, we compute evidence ratio 
    mce=MCEvidence(stan_chain,verbose=2,ischain=True,brange=[3,4.2]).evidence()

    return mce
Beispiel #8
0
if __name__ == '__main__':
    if len(sys.argv) > 1:
        method=sys.argv[1]
    else:
        method='gaussian_eg'

    if len(sys.argv) > 2:
        nsamples=sys.argv[2]
    else:
        nsamples=10000

    if method in ['gaussian_eg','glm_eg']:
        print('Using example: ',method)

        #get class instance
        XClass = getattr(sys.modules[__name__], method)
        
        # Now Generate samples.
        print('Calling sampler to get MCMC chain: nsamples=',nsamples)
        samples,logl=XClass(verbose=2).Sampler(nsamples=nsamples)

        print('samples and loglikes shape: ',samples.shape,logl.shape)
        
        chain={'samples':samples,'loglikes':logl}       
        mce=MCEvidence(chain,thinlen=2,burnlen=0.1,verbose=2,ischain=True).evidence()
        
    else:
        mce=eval(method+'()')