예제 #1
0
파일: fit-bias.py 프로젝트: esheldon/espy
def doplot(bf, yrange=None, fitter_type='mcmc'):
    import biggles
    plt=biggles.FramedPlot()

    color1='red'
    color2='blue'
    g1pts=biggles.Points(bf.g1true, bf.g1diff, color=color1,type='filled circle')
    gerr1pts=biggles.SymmetricErrorBarsY(bf.g1true, bf.g1diff, bf.g1err, color=color1)
    g1pts.label=r'$\gamma_1$'

    g2pts=biggles.Points(bf.g2true, bf.g2diff, color=color2,type='filled circle')
    gerr2pts=biggles.SymmetricErrorBarsY(bf.g2true, bf.g2diff, bf.g2err, color=color2)
    g2pts.label=r'$\gamma_2$'

    ply1=bf.get_g1poly()
    ply2=bf.get_g2poly()
    g1c=biggles.Curve(bf.g1true, ply1(bf.g1true),color=color1)
    g2c=biggles.Curve(bf.g2true, ply2(bf.g2true),color=color2)

    res1=bf.g1fit.get_result()
    res2=bf.g2fit.get_result()
    m1_mess=r'$m1: %.4g \pm %.4g$' % (res1['pars'][0],res1['perr'][0])
    c1_mess=r'$c1: %.4g \pm %.4g$' % (res1['pars'][1],res1['perr'][1])
    m2_mess=r'$m2: %.4g \pm %.4g$' % (res2['pars'][0],res2['perr'][0])
    c2_mess=r'$c2: %.4g \pm %.4g$' % (res2['pars'][1],res2['perr'][1])

    y=0.3
    dec=-0.05
    m1lab=biggles.PlotLabel(0.1,y,m1_mess,halign='left')
    y += dec
    c1lab=biggles.PlotLabel(0.1,y,c1_mess,halign='left')
    y += dec
    m2lab=biggles.PlotLabel(0.1,y,m2_mess,halign='left')
    y += dec
    c2lab=biggles.PlotLabel(0.1,y,c2_mess,halign='left')

    key=biggles.PlotKey(0.9,0.15,[g1pts,g2pts],halign='right')

    plt.add(g1pts, gerr1pts, g1c, g2pts, gerr2pts, g2c, key)
    plt.add(m1lab,c1lab,m2lab,c2lab)

    plt.xlabel=r'$\gamma_{true}$'
    plt.ylabel=r'$\Delta \gamma$'
    plt.aspect_ratio=1
    if yrange is not None:
        plt.yrange=yrange
    plt.show()

    if fitter_type=='mcmc':
        import mcmc
        names=['m','c']
        plt1=mcmc.plot_results(bf.g1fit.trials,names=names,show=False)
        plt2=mcmc.plot_results(bf.g2fit.trials,names=names,show=False)

        plt1.title='g1'
        plt2.title='g2'

        plt1.show()
        plt2.show()
예제 #2
0
def fit_input_g(g, show=False, **keys):
    """
    Fit only the g prior
    """
    import esutil as eu
    import biggles
    import mcmc

    # do the fit
    binsize=0.02
    hdict=get_norm_hist(g, min=0, binsize=binsize)
    ivar = numpy.ones(hdict['center'].size)
    w,=numpy.where(hdict['hist_norm_err'] > 0.0)
    if w.size > 0:
        ivar[w] = 1.0/hdict['hist_norm_err'][w]**2

    gpfitter=GPriorFitterErf(hdict['center'],
                             hdict['hist_norm'],
                             ivar,**keys)
    gpfitter.do_fit()
    gpfitter.print_result()
    res=gpfitter.get_result()

    # make a plot
    yrange=[0.0, 1.1*(hdict['hist_norm']+hdict['hist_norm_err']).max()]

    plt=biggles.FramedPlot()
    #plt.aspect_ratio=1
    plt.aspect_ratio=1.0/1.618
    plt.xlabel='e'
    plt.ylabel='p(e)'
    plt.yrange=yrange

    pts=biggles.Points(hdict['center'], hdict['hist_norm'],
                       type='filled circle')
    pts.label='COSMOS'
    perr=biggles.SymmetricErrorBarsY(hdict['center'],
                                     hdict['hist_norm'],
                                     hdict['hist_norm_err'])
    plt.add(pts,perr)


    gvals=numpy.linspace(0.0, 1.0)
    model=gpfitter.get_model_val(res['pars'], g=gvals)
    crv=biggles.Curve(gvals, model, color='red')
    crv.label='model'
    plt.add(crv)

    key=biggles.PlotKey(0.9,0.9,[pts,crv],halign='right')
    plt.add(key)

    trials_plt = mcmc.plot_results(gpfitter.trials,show=False)

    if show:
        plt.show()
        trials_plt.show()

    return gpfitter, plt, trials_plt
예제 #3
0
파일: toysim.py 프로젝트: esheldon/nsim
    def do_make_plots(self, trials, glike):
        import mcmc
        tab=mcmc.plot_results(trials, names=['g1','g2'])

        if self.sampler_type != 'true':
            import biggles
            import esutil as eu
            # add some points drawn from true distribution
            sh=trials.shape
            true_trials=numpy.zeros( sh )
            true_trials[:,0],true_trials[:,1]=glike.sample(sh[0])

            

        key=raw_input('hit a key (q to quit): ')
        if key=='q':
            stop
예제 #4
0
파일: fit_gprior.py 프로젝트: esheldon/espy
def fit_gprior_m_style(cat_type, version=None,
                       a=0.25, g0=0.1, gmax=0.87, gmax_min=None, Awidth=1.0,
                       binsize=0.02, doplot=False):
    """
    cat_type should be "galfit" or "ngmix-exp" "ngmix-dev" "ngmix-bdf"

    If cat_type=="galfit" then fit to the shapes from the sersic fits.
    
    If cat=="ngmix-exp" use my fits, same for dev.  Must send version= as well

    This works much better than an lm fitter

    for all cosmos galaxies I get
        [840.0, 1.05, 0.087, 0.810]
    """
    import mcmc
    import emcee
    import esutil as eu
    from esutil.random import srandu

    g=get_shapes(cat_type, version=version)

    bs=eu.stat.Binner(g)
    bs.dohist(binsize=binsize)
    bs.calc_stats()
    xdata=bs['center']
    ydata=bs['hist']

    nwalkers=200
    burnin=500
    nstep=100

    print 'fitting exp'

    A=ydata.sum()*(xdata[1]-xdata[0])

    pcen=[A,a,g0,gmax]
    npars=4
    guess=numpy.zeros( (nwalkers,npars) )
    guess[:,0] = pcen[0]*(1.+0.1*srandu(nwalkers))
    guess[:,1] = pcen[1]*(1.+0.1*srandu(nwalkers))
    guess[:,2] = pcen[2]*(1.+0.1*srandu(nwalkers))
    guess[:,3] = pcen[3]*(1.+0.1*srandu(nwalkers))

    ivar = numpy.ones(xdata.size)
    w,=numpy.where(ydata > 0)
    ivar[w] = 1./ydata[w]
    gfitter=GPriorMFitter(xdata, ydata, ivar, Aprior=A, Awidth=Awidth, gmax_min=gmax_min)

    print 'pcen:',pcen

    sampler = emcee.EnsembleSampler(nwalkers, 
                                    npars,
                                    gfitter.get_lnprob,
                                    a=2)

    pos, prob, state = sampler.run_mcmc(guess, burnin)
    sampler.reset()
    pos, prob, state = sampler.run_mcmc(pos, nstep)

    trials  = sampler.flatchain

    pars,pcov=mcmc.extract_stats(trials)

    d=numpy.diag(pcov)
    perr = numpy.sqrt(d)

    res={'A':pars[0],
         'A_err':perr[0],
         'a':pars[1],
         'a_err':perr[1],
         'g0':pars[2],
         'g0_err':perr[2],
         'gmax': pars[3],
         'gmax_err':perr[3],
         'pars':pars,
         'pcov':pcov,
         'perr':perr}


    fmt="""
A:    %(A).6g +/- %(A_err).6g
a:    %(a).6g +/- %(a_err).6g
g0:   %(g0).6g +/- %(g0_err).6g
gmax: %(gmax).6g +/- %(gmax_err).6g
    """.strip()

    print fmt % res

    if doplot:
        import mcmc
        import ngmix
        mcmc.plot_results(trials,names=['A','a','g0','gmax'],
                          title=cat_type)
        p=ngmix.priors.GPriorM(pars)
        gsamp=p.sample1d(g.size)
        plt=eu.plotting.bhist(g, binsize=binsize, show=False)
        eu.plotting.bhist(gsamp, binsize=binsize,
                          plt=plt, color='blue',
                          xlabel='|g|',
                          xrange=[0.,1.],
                          title=cat_type)

    return res
예제 #5
0
def fit_g_prior(run, model, prior_name, type='m-erf', **keys):
    """
    Fit only the g prior
    """
    import esutil as eu
    import biggles
    import mcmc

    fl=read_field_list(run, model, prior_name, **keys)
    comb=make_combined_pars_subtract_mean_shape(fl)

    g=comb[:,0]

    binsize=0.01
    #binsize=0.005

    hdict=get_norm_hist(g, min=0, binsize=binsize)

    yrange=[0.0, 1.1*(hdict['hist_norm']+hdict['hist_norm_err']).max()]
    plt=eu.plotting.bscatter(hdict['center'],
                             hdict['hist_norm'],
                             yerr=hdict['hist_norm_err'],
                             yrange=yrange,
                             show=False)

    ivar = ones(hdict['center'].size)
    w,=where(hdict['hist_norm_err'] > 0.0)
    if w.size > 0:
        ivar[w] = 1.0/hdict['hist_norm_err'][w]**2

    if type=='great-des':
        prior=ngmix.priors.GPriorGreatDES()
        prior.dofit(hdict['center'],
                    hdict['hist_norm'],
                    show=True)
        return

    elif type=='m-erf':
        prior=ngmix.priors.GPriorMErf()
        prior.dofit(hdict['center'],
                    hdict['hist_norm'],
                    show=True)
        return

    elif type=='m-erf2':
        prior=ngmix.priors.GPriorMErf2()
        prior.dofit(hdict['center'],
                    hdict['hist_norm'],
                    show=True)
        return


    elif type=='ba':
        prior=ngmix.priors.GPriorBA()
        prior.dofit(hdict['center'],
                    hdict['hist_norm'],
                    show=True)
        return


    elif type=='bdf':
        print("bdf")
        gpfitter=GPriorFitterAlt(hdict['center'],
                                 hdict['hist_norm'],
                                 ivar)
    elif type=='exp':
        print("exp")
        gpfitter=GPriorFitterExp(hdict['center'],
                                 hdict['hist_norm'],
                                 ivar)

    gpfitter.do_fit()
    gpfitter.print_result()
    res=gpfitter.get_result()

    gvals=numpy.linspace(0.0, 1.0)
    model=gpfitter.get_model_val(res['pars'], g=gvals)
    crv=biggles.Curve(gvals, model, color='red')
    plt.add(crv)

    mcmc.plot_results(gpfitter.trials)
    plt.show()
예제 #6
0
파일: prior.py 프로젝트: esheldon/espy
def fit_gprior_2gauss_cut(xdata, ydata, ivar):
    """
    This works much better than the lm fitter
    Input is the histogram data.
    """
    import mcmc
    import emcee

    nwalkers=800
    burnin=1000
    nstep=100

    A=ydata.sum()#*(xdata[1]-xdata[0])

    A1 = 0.6*A
    A2 = 0.4*A

    sigma1 = 0.02
    sigma2 = 0.3

    pcen = numpy.array([A1,sigma1,A2,sigma2])

    npars=pcen .size
    guess=zeros( (nwalkers,npars) )
    guess[:,0] = pcen[0]*(1.+0.2*srandu(nwalkers))
    guess[:,1] = pcen[1]*(1.+0.2*srandu(nwalkers))
    guess[:,2] = pcen[2]*(1.+0.2*srandu(nwalkers))
    guess[:,3] = pcen[3]*(1.+0.2*srandu(nwalkers))

    gfitter=GPrior2GaussCutFitter(xdata, ydata, ivar)

    print 'pcen:',pcen

    sampler = emcee.EnsembleSampler(nwalkers, 
                                    npars,
                                    gfitter.get_lnprob,
                                    a=2)

    pos, prob, state = sampler.run_mcmc(guess, burnin)
    sampler.reset()
    pos, prob, state = sampler.run_mcmc(pos, nstep)

    arate = sampler.acceptance_fraction.mean()
    print 'arate:',arate
    trials  = sampler.flatchain
    mcmc.plot_results(trials, ptypes=['log','linear','log','linear'])
    

    pars,pcov=mcmc.extract_stats(trials)

    d=diag(pcov)
    perr = sqrt(d)

    gprior=GPrior2GaussCut(pars)

    res={'A1':pars[0],
         'A1_err':perr[0],
         'sigma1':pars[1],
         'sigma1_err':perr[1],
         'A2':pars[2],
         'A2_err':perr[2],
         'sigma2':pars[3],
         'sigma2_err':perr[3],

         'pars':pars,
         'pcov':pcov,
         'perr':perr}


    fmt="""
A1:        %(A1).6g +/- %(A1_err).6g
sigma1:    %(sigma1).6g +/- %(sigma1_err).6g
A2:        %(A2).6g +/- %(A2_err).6g
sigma2:    %(sigma2).6g +/- %(sigma2_err).6g
    """.strip()

    print fmt % res

    return gprior,res