Esempio n. 1
0
def test_lognormal():
    import biggles
    import esutil as eu
    from esutil.random import LogNormal, srandu
    from esutil.stat import histogram

    n=1000
    nwalkers=100
    burnin=100
    nstep=100

    mean=8
    sigma=3
    ln=LogNormal(mean,sigma)
    vals=ln.sample(n)

    binsize=0.5

    plt=eu.plotting.bhist(vals, binsize=binsize,show=False)

    h=histogram(vals, binsize=binsize,more=True)
    herr=sqrt(h['hist'])
    herr=herr.clip(1.0, herr.max())

    guess=[n*(1. + .1*srandu()),
           mean*(1. + .1*srandu()),
           sigma*(1. + .1*srandu())]
    guess=[n*binsize,mean,sigma]

    print 'guess:',guess
    nlf=LogNormalFitter(h['center'], h['hist'], guess, nwalkers, burnin, nstep,
                        yerr=herr)

    print nlf

    res=nlf.get_result()
    
    model=nlf.get_model()

    yvals=model.scaled(h['center'])
    plt.add(biggles.Curve(h['center'], yvals, color='blue'))
    plt.show()
Esempio n. 2
0
def test_combined(show=False,
                  nwalkers=20,
                  burnin=25,
                  nrand=500,
                  ntry=1,
                  gerr=0.1):
    import esutil as eu
    from esutil.random import LogNormal, Normal, NormalND, srandu
    import time
    npars=6

    cen_prior = CenPrior([15.0, 16.0], [0.1, 0.2])

    g_prior = GPriorBA(0.3)
    print 'pre-generating g values from prior (for seed to sampler)'
    ng_pre = 10000
    g1vals_pre,g2vals_pre = g_prior.sample2d(ng_pre)
    print 'done'

    g_dist_gauss = NormalND( [0.2,0.1], [gerr,gerr] )

    gg_prior = GPriorTimesGauss(g_prior, g_dist_gauss)

    T_prior = LogNormal(16.0, 3.0)
    counts_prior = LogNormal(100.0, 10.0)


    comb=CombinedPriorSimple(cen_prior,
                             gg_prior,
                             T_prior,
                             counts_prior)


    nwalkers=20
    print 'nstep per:',nrand/nwalkers
    start=numpy.zeros( (nwalkers,npars) )

    minerr2=0.01**2
    tm=time.time()
    for i in xrange(ntry):
        start[:,0:2] = cen_prior.sample(nwalkers)

        # g start
        g_err2 = g_dist_gauss.sigma[0]**2 + g_dist_gauss.sigma[1]**2

        if g_err2 < minerr2:
            print 'error small enough from max like, skipping resample'
            continue

        nsig_g = 3
        g1_range = numpy.array([g_dist_gauss.mean[0]-nsig_g*g_dist_gauss.sigma[0],
                                g_dist_gauss.mean[0]+nsig_g*g_dist_gauss.sigma[0]])
        g2_range = numpy.array([g_dist_gauss.mean[1]-nsig_g*g_dist_gauss.sigma[1],
                                g_dist_gauss.mean[1]+nsig_g*g_dist_gauss.sigma[1]])

        g1_range.clip(-1.0,1.0,g1_range)
        g2_range.clip(-1.0,1.0,g2_range)
        g1_width = g1_range.max() - g1_range.min()
        g2_width = g2_range.max() - g2_range.min()
        if g1_width==0 or g2_width==0:
            # crazy, probably near e==0.  Just draw from prior
            randi = eu.numpy_util.randind(ng_pre, nwalkers)
            g1rand=g1vals_pre[randi]
            g2rand=g2vals_pre[randi]
        else:

            w,=numpy.where(  (g1vals_pre > g1_range[0])
                           & (g1vals_pre < g1_range[1])
                           & (g2vals_pre > g1_range[0])
                           & (g2vals_pre < g1_range[1]) )
            if w.size < nwalkers:
                raise ValueError("too few")

            randi = eu.numpy_util.randind(w.size, nwalkers)
            randi=w[randi]
            g1rand=g1vals_pre[randi]
            g2rand=g2vals_pre[randi]


        """
        sig2=g_dist_gauss.sigma[0]**2 + g_dist_gauss.sigma[1]**2
        if sig2 < 0.3**2:
            g1rand = g_dist_gauss.mean[0]*(1.0 + 0.01*srandu(nwalkers))
            g2rand = g_dist_gauss.mean[1]*(1.0 + 0.01*srandu(nwalkers))
        else:
            g1rand,g2rand=g_prior_ba.sample2d(nwalkers)
        """

        start[:,2] = g1rand
        start[:,3] = g2rand

        start[:,4] = T_prior.sample(nwalkers)
        start[:,5] = counts_prior.sample(nwalkers)

        prand = comb.sample(start,
                            nrand, 
                            burnin=burnin,
                            nwalkers=nwalkers)

    print 'time per:',(time.time()-tm)/ntry

    print 'g1: %g +/- %g' % (prand[:,2].mean(), prand[:,2].std())
    print 'g2: %g +/- %g' % (prand[:,3].mean(), prand[:,3].std())
    if show:
        plot_many_hist(prand)
Esempio n. 3
0
    def _sample_dist_with_gauss(self):
        from esutil.random import NormalND, LogNormal

        nwalkers=20
        burnin=25

        pars=self._lm_result['pars']
        cov=self._lm_result['pcov']*4

        cen_prior = priors.CenPrior(pars[0:2], [cov[0,0], cov[1,1]])

        g1_sigma = numpy.sqrt(cov[2,2])
        g2_sigma = numpy.sqrt(cov[3,3])

        if g1_sigma < 0.001:
            g1_sigma=0.001
        if g2_sigma < 0.001:
            g2_sigma=0.001

        g_dist_gauss = NormalND( pars[2:4], [g1_sigma,g2_sigma])
        gg_prior = priors.GPriorTimesGauss(self.gprior, g_dist_gauss)

        Tg=pars[4]
        T_sigma = numpy.sqrt(cov[4,4])
        if Tg < 0.01:
            Tg=0.01
        if T_sigma < 0.01:
            T_sigma=0.01

        counts_g=pars[5]
        counts_sigma = numpy.sqrt(cov[5,5])
        if counts_g < 0.01:
            counts_g=0.01
        if counts_sigma < 0.001:
            counts_sigma=0.001

        T_prior = LogNormal(Tg, T_sigma)
        counts_prior = LogNormal(counts_g, counts_sigma)
 
        comb=priors.CombinedPriorSimple(cen_prior,
                                        gg_prior,
                                        T_prior,
                                        counts_prior)

        g1rand,g2rand=self._get_gstart(g_dist_gauss,nwalkers)
        if g1rand is None:
            # we will just use the lm fit
            return None,None

        start=numpy.zeros( (nwalkers,self.npars) )

        start[:,0:2] = cen_prior.sample(nwalkers)


        start[:,2] = g1rand
        start[:,3] = g2rand

        start[:,4] = T_prior.sample(nwalkers)
        start[:,5] = counts_prior.sample(nwalkers)

        sampler = comb.sample(start,
                              self.nsample, 
                              burnin=burnin,
                              nwalkers=nwalkers,
                              get_sampler=True)

        prand = sampler.flatchain
        lnp = sampler.lnprobability
        lnp = lnp.reshape(lnp.shape[0]*lnp.shape[1])
        probs = numpy.exp(lnp)
        return prand,probs