Beispiel #1
0
def exNew(exclude=sc.array([1,2,3,4]),
          plotfilename='exNew.png',nburn=20000,nsamples=200000,
          parsigma=[5,.075,.01,1,.1],dsigma=1.):
    """exMix1: solve the new exercise using MCMC sampling
    Input:
       exclude        - ID numbers to exclude from the analysis (can be None)
       plotfilename   - filename for the output plot
       nburn          - number of burn-in samples
       nsamples       - number of samples to take after burn-in
       parsigma       - proposal distribution width (Gaussian)
       dsigma         - divide uncertainties by this amount
    Output:
       plot
    History:
       2010-04-28 - Written - Bovy (NYU)
    """
    sc.random.seed(1) #In the interest of reproducibility (if that's a word)
    #Read the data
    data= read_data('data_yerr.dat')
    ndata= len(data)
    if not exclude == None:
        nsample= ndata- len(exclude)
    else:
        nsample= ndata
    #First find the chi-squared solution, which we will use as an
    #initial guess
    #Put the data in the appropriate arrays and matrices
    Y= sc.zeros(nsample)
    X= sc.zeros(nsample)
    A= sc.ones((nsample,2))
    C= sc.zeros((nsample,nsample))
    yerr= sc.zeros(nsample)
    jj= 0
    for ii in range(ndata):
        if not exclude == None and sc.any(exclude == data[ii][0]):
            pass
        else:
            Y[jj]= data[ii][1][1]
            X[jj]= data[ii][1][0]
            A[jj,1]= data[ii][1][0]
            C[jj,jj]= data[ii][2]**2./dsigma**2.
            yerr[jj]= data[ii][2]/dsigma
            jj= jj+1
    #Now compute the best fit and the uncertainties
    bestfit= sc.dot(linalg.inv(C),Y.T)
    bestfit= sc.dot(A.T,bestfit)
    bestfitvar= sc.dot(linalg.inv(C),A)
    bestfitvar= sc.dot(A.T,bestfitvar)
    bestfitvar= linalg.inv(bestfitvar)
    bestfit= sc.dot(bestfitvar,bestfit)
    initialguess= sc.array([bestfit[0],bestfit[1],0.,sc.mean(Y),m.log(sc.var(Y))])#(m,b,Pb,Yb,Vb)
    #With this initial guess start off the sampling procedure
    initialX= objective(initialguess,X,Y,yerr)
    currentX= initialX
    bestX= initialX
    bestfit= initialguess
    currentguess= initialguess
    naccept= 0
    samples= []
    samples.append(currentguess)
    for jj in range(nburn+nsamples):
        #Draw a sample from the proposal distribution
        newsample= sc.zeros(5)
        newsample[0]= currentguess[0]+stats.norm.rvs()*parsigma[0]
        newsample[1]= currentguess[1]+stats.norm.rvs()*parsigma[1]
        #newsample[2]= stats.uniform.rvs()
        newsample[2]= currentguess[2]+stats.norm.rvs()*parsigma[2]
        newsample[3]= currentguess[3]+stats.norm.rvs()*parsigma[3]
        newsample[4]= currentguess[4]+stats.norm.rvs()*parsigma[4]
        #Calculate the objective function for the newsample
        newX= objective(newsample,X,Y,yerr)
        #Accept or reject
        #Reject with the appropriate probability
        u= stats.uniform.rvs()
        if u < m.exp(newX-currentX):
            #Accept
            currentX= newX
            currentguess= newsample
            naccept= naccept+1
        if currentX > bestX:
            bestfit= currentguess
            bestX= currentX
        samples.append(currentguess)
    if double(naccept)/(nburn+nsamples) < .2 or double(naccept)/(nburn+nsamples) > .6:
        print "Acceptance ratio was "+str(double(naccept)/(nburn+nsamples))

    samples= sc.array(samples).T[:,nburn:-1]
    print "Best-fit, overall"
    print bestfit, sc.mean(samples[2,:]), sc.median(samples[2,:])

    histmb,edges= sc.histogramdd(samples.T[:,0:2],bins=round(sc.sqrt(nsamples)/5.))
    indxi= sc.argmax(sc.amax(histmb,axis=1))
    indxj= sc.argmax(sc.amax(histmb,axis=0))
    print "Best-fit, marginalized"
    print edges[0][indxi-1], edges[1][indxj-1]
    print edges[0][indxi], edges[1][indxj]
    print edges[0][indxi+1], edges[1][indxj+1]
        
    #2D histogram
    plot.bovy_print()
    levels= special.erf(0.5*sc.arange(1,4))
    #xrange=[edges[0][0],edges[0][-1]]
    #yrange=[edges[1][0],edges[1][-1]]
    xrange=[-120,120]
    yrange=[1.5,3.2]
    histmb,edges= sc.histogramdd(samples.T[:,0:2],
                                 range=[[-120,120],[1.5,3.2]],
                                 bins=(round(sc.sqrt(nsamples)/5.)/(edges[0][-1]-edges[0][0])*(xrange[1]-xrange[0]),
                                       round(sc.sqrt(nsamples)/5.)/(edges[1][-1]-edges[1][0])*(yrange[1]-yrange[0])))
    aspect=(xrange[1]-xrange[0])/(yrange[1]-yrange[0])
    plot.bovy_dens2d(histmb.T,origin='lower',cmap='gist_yarg',
                     contours=True,cntrmass=True,
                     xrange=xrange,yrange=yrange,
                     levels=levels,
                     aspect=aspect,
                     xlabel=r'$b$',ylabel=r'$m$')
    if dsigma == 1.:
        plot.bovy_text(r'$\mathrm{using\ correct\ data\ uncertainties}$',
                       top_right=True)
    else:
        plot.bovy_text(r'$\mathrm{using\ data\ uncertainties\ /\ 2}$',
                       top_right=True)       
    if dsigma == 1.:
        plot.bovy_end_print('exNew1a.png')
    else:
        plot.bovy_end_print('exNew2a.png')

    #Data with MAP line and sampling
    plot.bovy_print()
    bestb= edges[0][indxi]
    bestm= edges[1][indxj]
    xrange=[0,300]
    yrange=[0,700]
    plot.bovy_plot(xrange,bestm*sc.array(xrange)+bestb,'k-',
                   xrange=xrange,yrange=yrange,
                   xlabel=r'$x$',ylabel=r'$y$',zorder=2)
    errorbar(X,Y,yerr,color='k',marker='o',color='k',linestyle='None',zorder=1)
    for ii in range(10):
        #Random sample
        ransample= sc.floor((stats.uniform.rvs()*nsamples))
        ransample= samples.T[ransample,0:2]
        bestb= ransample[0]
        bestm= ransample[1]
        plot.bovy_plot(xrange,bestm*sc.array(xrange)+bestb,
                       overplot=True,xrange=xrange,yrange=yrange,
                       xlabel=r'$x$',ylabel=r'$y$',color='0.75',zorder=1)
    if dsigma == 1.:
        plot.bovy_text(r'$\mathrm{using\ correct\ data\ uncertainties}$',
                       top_right=True)
    else:
        plot.bovy_text(r'$\mathrm{using\ data\ uncertainties\ /\ 2}$',
                       top_right=True)       
    if dsigma == 1.:
        plot.bovy_end_print('exNew1b.png')
    else:
        plot.bovy_end_print('exNew2b.png')
    
    #Pb plot
    plot.bovy_print()
    plot.bovy_hist(samples.T[:,2],color='k',bins=round(sc.sqrt(nsamples)/5.),
                   xlabel=r'$P_\mathrm{b}$',normed=True,histtype='step',
                   range=[0,1])
    if dsigma == 1.:
        plot.bovy_text(r'$\mathrm{using\ correct\ data\ uncertainties}$',
                       top_right=True)
    else:
        plot.bovy_text(r'$\mathrm{using\ data\ uncertainties\ /\ 2}$',
                       top_right=True)       
    if dsigma == 1.:
        plot.bovy_end_print('exNew1c.png')
    else:
        plot.bovy_end_print('exNew2c.png')
    
    return
Beispiel #2
0
def exMix1(
    exclude=None,
    plotfilenameA="exMix1a.png",
    plotfilenameB="exMix1b.png",
    plotfilenameC="exMix1c.png",
    nburn=20000,
    nsamples=1000000,
    parsigma=[5, 0.075, 0.2, 1, 0.1],
    dsigma=1.0,
    bovyprintargs={},
    sampledata=None,
):
    """exMix1: solve exercise 5 (mixture model) using MCMC sampling
    Input:
       exclude        - ID numbers to exclude from the analysis (can be None)
       plotfilename*  - filenames for the output plot
       nburn          - number of burn-in samples
       nsamples       - number of samples to take after burn-in
       parsigma       - proposal distribution width (Gaussian)
       dsigma         - divide uncertainties by this amount
    Output:
       plot
    History:
       2010-04-28 - Written - Bovy (NYU)
    """
    sc.random.seed(-1)  # In the interest of reproducibility (if that's a word)
    # Read the data
    data = read_data("data_yerr.dat")
    ndata = len(data)
    if not exclude == None:
        nsample = ndata - len(exclude)
    else:
        nsample = ndata
    # First find the chi-squared solution, which we will use as an
    # initial guess
    # Put the data in the appropriate arrays and matrices
    Y = sc.zeros(nsample)
    X = sc.zeros(nsample)
    A = sc.ones((nsample, 2))
    C = sc.zeros((nsample, nsample))
    yerr = sc.zeros(nsample)
    jj = 0
    for ii in range(ndata):
        if not exclude == None and sc.any(exclude == data[ii][0]):
            pass
        else:
            Y[jj] = data[ii][1][1]
            X[jj] = data[ii][1][0]
            A[jj, 1] = data[ii][1][0]
            C[jj, jj] = data[ii][2] ** 2.0 / dsigma ** 2.0
            yerr[jj] = data[ii][2] / dsigma
            jj = jj + 1

    brange = [-120, 120]
    mrange = [1.5, 3.2]

    # This matches the order of the parameters in the "samples" vector
    mbrange = [brange, mrange]

    if sampledata is None:
        sampledata = runSampler(X, Y, A, C, yerr, nburn, nsamples, parsigma, mbrange)

    (histmb, edges, mbsamples, pbhist, pbedges) = sampledata

    # Hack -- produce fake Pbad samples from Pbad histogram.
    pbsamples = hstack([array([x] * N) for x, N in zip((pbedges[:-1] + pbedges[1:]) / 2, pbhist)])

    indxi = sc.argmax(sc.amax(histmb, axis=1))
    indxj = sc.argmax(sc.amax(histmb, axis=0))
    print "Best-fit, marginalized"
    print edges[0][indxi - 1], edges[1][indxj - 1]
    print edges[0][indxi], edges[1][indxj]
    print edges[0][indxi + 1], edges[1][indxj + 1]

    # 2D histogram
    plot.bovy_print(**bovyprintargs)
    levels = special.erf(0.5 * sc.arange(1, 4))
    xe = [edges[0][0], edges[0][-1]]
    ye = [edges[1][0], edges[1][-1]]
    aspect = (xe[1] - xe[0]) / (ye[1] - ye[0])
    plot.bovy_dens2d(
        histmb.T,
        origin="lower",
        cmap=cm.gist_yarg,
        interpolation="nearest",
        contours=True,
        cntrmass=True,
        extent=xe + ye,
        levels=levels,
        aspect=aspect,
        xlabel=r"$b$",
        ylabel=r"$m$",
    )
    xlim(brange)
    ylim(mrange)

    plot.bovy_end_print(plotfilenameA)

    # Data with MAP line and sampling
    plot.bovy_print(**bovyprintargs)
    bestb = edges[0][indxi]
    bestm = edges[1][indxj]
    xrange = [0, 300]
    yrange = [0, 700]
    plot.bovy_plot(
        xrange,
        bestm * sc.array(xrange) + bestb,
        "k-",
        xrange=xrange,
        yrange=yrange,
        xlabel=r"$x$",
        ylabel=r"$y$",
        zorder=2,
    )
    errorbar(X, Y, yerr, marker="o", color="k", linestyle="None", zorder=1)

    for m, b in mbsamples:
        plot.bovy_plot(
            xrange,
            m * sc.array(xrange) + b,
            overplot=True,
            xrange=xrange,
            yrange=yrange,
            xlabel=r"$x$",
            ylabel=r"$y$",
            color="0.75",
            zorder=1,
        )

    plot.bovy_end_print(plotfilenameB)

    # Pb plot
    if not "text_fontsize" in bovyprintargs:
        bovyprintargs["text_fontsize"] = 11
    plot.bovy_print(**bovyprintargs)
    plot.bovy_hist(
        pbsamples,
        bins=round(sc.sqrt(nsamples) / 5.0),
        xlabel=r"$P_\mathrm{b}$",
        normed=True,
        histtype="step",
        range=[0, 1],
        edgecolor="k",
    )
    ylim(0, 4.0)
    if dsigma == 1.0:
        plot.bovy_text(r"$\mathrm{using\ correct\ data\ uncertainties}$", top_right=True)
    else:
        plot.bovy_text(r"$\mathrm{using\ data\ uncertainties\ /\ 2}$", top_left=True)

    plot.bovy_end_print(plotfilenameC)

    return sampledata
def ex17(exclude=sc.array([3]),plotfilename='ex17.png',
         nburn=5000,nsamples=200000,
         parsigma=[1,m.pi/200.,.1],
		 bovyprintargs={}):
    """ex17: solve exercise 17 by MCMC
    Input:
       exclude        - ID numbers to exclude from the analysis
       plotfilename   - filename for the output plot
       nburn          - number of burn-in samples
       nsamples       - number of samples to take after burn-in
       parsigma       - proposal distribution width (Gaussian)
    Output:
       plot
    History:
       2010-05-07 - Written - Bovy (NYU)
    """
    #Read the data
    data= read_data('data_allerr.dat',allerr=True)
    ndata= len(data)
    nsample= ndata- len(exclude)
    #First find the chi-squared solution, which we will use as an
    #initial gues
    #Put the dat in the appropriate arrays and matrices
    Y= sc.zeros(nsample)
    X= sc.zeros(nsample)
    A= sc.ones((nsample,2))
    C= sc.zeros((nsample,nsample))
    Z= sc.zeros((nsample,2))
    yerr= sc.zeros(nsample)
    ycovar= sc.zeros((2,nsample,2))#Makes the sc.dot easier
    jj= 0
    for ii in range(ndata):
        if sc.any(exclude == data[ii][0]):
            pass
        else:
            Y[jj]= data[ii][1][1]
            X[jj]= data[ii][1][0]
            Z[jj,0]= X[jj]
            Z[jj,1]= Y[jj]
            A[jj,1]= data[ii][1][0]
            C[jj,jj]= data[ii][2]**2.
            yerr[jj]= data[ii][2]
            ycovar[0,jj,0]= data[ii][3]**2.
            ycovar[1,jj,1]= data[ii][2]**2.
            ycovar[0,jj,1]= data[ii][4]*m.sqrt(ycovar[0,jj,0]*ycovar[1,jj,1])
            ycovar[1,jj,0]= ycovar[0,jj,1]
            jj= jj+1
    #Now compute the best fit and the uncertainties
    bestfit= sc.dot(linalg.inv(C),Y.T)
    bestfit= sc.dot(A.T,bestfit)
    bestfitvar= sc.dot(linalg.inv(C),A)
    bestfitvar= sc.dot(A.T,bestfitvar)
    bestfitvar= linalg.inv(bestfitvar)
    bestfit= sc.dot(bestfitvar,bestfit)
    #Now sample
    inittheta= m.acos(1./m.sqrt(1.+bestfit[1]**2.))
    if bestfit[1] < 0.:
        inittheta= m.pi- inittheta
    initialguess= sc.array([bestfit[0]*m.cos(inittheta),inittheta,sc.log(1.)])#(m,b,logV)
    #With this initial guess start off the sampling procedure
    initialX= objective(initialguess,Z,ycovar)
    currentX= initialX
    bestX= initialX
    bestfit= initialguess
    currentguess= initialguess
    naccept= 0
    samples= []
    samples.append(currentguess)
    for jj in range(nburn+nsamples):
        #Draw a sample from the proposal distribution
        newsample= sc.zeros(3)
        newsample[0]= currentguess[0]+stats.norm.rvs()*parsigma[0]
        newsample[1]= currentguess[1]+stats.norm.rvs()*parsigma[1]
        newsample[2]= currentguess[2]+stats.norm.rvs()*parsigma[2]
        #Calculate the objective function for the newsample
        newX= objective(newsample,Z,ycovar)
        #Accept or reject
        #Reject with the appropriate probability
        u= stats.uniform.rvs()
        try:
            test= m.exp(newX-currentX)
        except OverflowError:
            test= 2.
        if u < test:
            #Accept
            currentX= newX
            currentguess= newsample
            naccept= naccept+1
        if currentX > bestX:
            bestfit= currentguess
            bestX= currentX
        samples.append(currentguess)
    if double(naccept)/(nburn+nsamples) < .5 or double(naccept)/(nburn+nsamples) > .8:
        print "Acceptance ratio was "+str(double(naccept)/(nburn+nsamples))

    samples= sc.array(samples).T[:,nburn:-1]
    print "Best-fit, overall"
    print bestfit, sc.mean(samples[2,:]), sc.median(samples[2,:])

    histmb,edges= sc.histogramdd(samples.T[:,0:2],bins=round(sc.sqrt(nsamples)/2.))
    indxi= sc.argmax(sc.amax(histmb,axis=1))
    indxj= sc.argmax(sc.amax(histmb,axis=0))
    print "Best-fit, marginalized"
    print edges[0][indxi-1], edges[1][indxj-1]
    print edges[0][indxi], edges[1][indxj]
    print edges[0][indxi+1], edges[1][indxj+1]

    t= edges[1][indxj]
    bcost= edges[0][indxi]
    mf= m.sqrt(1./m.cos(t)**2.-1.)
    b= bcost/m.cos(t)
    print b, mf

    #Plot
    plot.bovy_print(**bovyprintargs)
    hist, bins, patchess= plot.bovy_hist(sc.exp(samples.T[:,2]/2.),edgecolor='k',
                                      bins=round(sc.sqrt(nsamples)/2.),
                                      xlabel=r'$\sqrt{V}$',normed=True,
                                      histtype='step')
    cumhist= sc.cumsum(hist)/sc.sum(hist)/(bins[1]-bins[0])
    ninefive= 0.
    ninenine= 0.
    foundfive= False
    foundnine= False
    for ii in range(len(cumhist)):
        if cumhist[ii]*(bins[1]-bins[0]) > 0.95 and not foundfive:
            ninefive= bins[ii]
            foundfive= True
        if cumhist[ii]*(bins[1]-bins[0]) > 0.99 and not foundnine:
            ninenine= bins[ii]
            foundnine= True
    print ninefive, ninenine
    axvline(ninefive,color='0.5',lw=2.)
    axvline(ninenine,color='0.5',lw=2.)
    plot.bovy_end_print(plotfilename)


    return

    #Plot result
    plot.bovy_print()
    xrange=[0,300]
    yrange=[0,700]
    plot.bovy_plot(sc.array(xrange),mf*sc.array(xrange)+b,
                   'k--',xrange=xrange,yrange=yrange,
                   xlabel=r'$x$',ylabel=r'$y$',zorder=2)
    for ii in range(10):
        #Random sample
        ransample= sc.floor((stats.uniform.rvs()*nsamples))
        ransample= samples.T[ransample,0:2]
        mf= m.sqrt(1./m.cos(ransample[1])**2.-1.)
        b= ransample[0]/m.cos(ransample[1])
        bestb= b
        bestm= mf
        plot.bovy_plot(sc.array(xrange),bestm*sc.array(xrange)+bestb,
                       overplot=True,color='0.75',zorder=0)

    #Add labels
    nsamples= samples.shape[1]
    for ii in range(nsample):
        Pb= 0.
        for jj in range(nsamples):
            Pb+= Pbad(samples[:,jj],Z[ii,:],ycovar[:,ii,:])
        Pb/= nsamples
        text(Z[ii,0]+5,Z[ii,1]+5,'%.1f'%Pb,color='0.5',zorder=3)


    #Plot the data OMG straight from plot_data.py
    data= read_data('data_allerr.dat',True)
    ndata= len(data)
    #Create the ellipses and the data points
    id= sc.zeros(nsample)
    x= sc.zeros(nsample)
    y= sc.zeros(nsample)
    ellipses=[]
    ymin, ymax= 0, 0
    xmin, xmax= 0,0
    jj= 0
    for ii in range(ndata):
        if sc.any(exclude == data[ii][0]):
            continue
        id[jj]= data[ii][0]
        x[jj]= data[ii][1][0]
        y[jj]= data[ii][1][1]
        #Calculate the eigenvalues and the rotation angle
        ycovar= sc.zeros((2,2))
        ycovar[0,0]= data[ii][3]**2.
        ycovar[1,1]= data[ii][2]**2.
        ycovar[0,1]= data[ii][4]*m.sqrt(ycovar[0,0]*ycovar[1,1])
        ycovar[1,0]= ycovar[0,1]
        eigs= linalg.eig(ycovar)
        angle= m.atan(-eigs[1][0,1]/eigs[1][1,1])/m.pi*180.
        thisellipse= Ellipse(sc.array([x[jj],y[jj]]),2*m.sqrt(eigs[0][0]),
                             2*m.sqrt(eigs[0][1]),angle)
        ellipses.append(thisellipse)
        if (x[jj]+m.sqrt(ycovar[0,0])) > xmax:
            xmax= (x[jj]+m.sqrt(ycovar[0,0]))
        if (x[jj]-m.sqrt(ycovar[0,0])) < xmin:
            xmin= (x[jj]-m.sqrt(ycovar[0,0]))
        if (y[jj]+m.sqrt(ycovar[1,1])) > ymax:
            ymax= (y[jj]+m.sqrt(ycovar[1,1]))
        if (y[jj]-m.sqrt(ycovar[1,1])) < ymin:
            ymin= (y[jj]-m.sqrt(ycovar[1,1]))
        jj= jj+1
        
    #Add the error ellipses
    ax=gca()
    for e in ellipses:
        ax.add_artist(e)
        e.set_facecolor('none')
    ax.plot(x,y,color='k',marker='o',linestyle='None')


    plot.bovy_end_print(plotfilename)
Beispiel #4
0
def ex17(exclude=sc.array([3]),
         plotfilename='ex17.png',
         nburn=5000,
         nsamples=200000,
         parsigma=[1, m.pi / 200., .1],
         bovyprintargs={}):
    """ex17: solve exercise 17 by MCMC
    Input:
       exclude        - ID numbers to exclude from the analysis
       plotfilename   - filename for the output plot
       nburn          - number of burn-in samples
       nsamples       - number of samples to take after burn-in
       parsigma       - proposal distribution width (Gaussian)
    Output:
       plot
    History:
       2010-05-07 - Written - Bovy (NYU)
    """
    #Read the data
    data = read_data('data_allerr.dat', allerr=True)
    ndata = len(data)
    nsample = ndata - len(exclude)
    #First find the chi-squared solution, which we will use as an
    #initial gues
    #Put the dat in the appropriate arrays and matrices
    Y = sc.zeros(nsample)
    X = sc.zeros(nsample)
    A = sc.ones((nsample, 2))
    C = sc.zeros((nsample, nsample))
    Z = sc.zeros((nsample, 2))
    yerr = sc.zeros(nsample)
    ycovar = sc.zeros((2, nsample, 2))  #Makes the sc.dot easier
    jj = 0
    for ii in range(ndata):
        if sc.any(exclude == data[ii][0]):
            pass
        else:
            Y[jj] = data[ii][1][1]
            X[jj] = data[ii][1][0]
            Z[jj, 0] = X[jj]
            Z[jj, 1] = Y[jj]
            A[jj, 1] = data[ii][1][0]
            C[jj, jj] = data[ii][2]**2.
            yerr[jj] = data[ii][2]
            ycovar[0, jj, 0] = data[ii][3]**2.
            ycovar[1, jj, 1] = data[ii][2]**2.
            ycovar[0, jj, 1] = data[ii][4] * m.sqrt(
                ycovar[0, jj, 0] * ycovar[1, jj, 1])
            ycovar[1, jj, 0] = ycovar[0, jj, 1]
            jj = jj + 1
    #Now compute the best fit and the uncertainties
    bestfit = sc.dot(linalg.inv(C), Y.T)
    bestfit = sc.dot(A.T, bestfit)
    bestfitvar = sc.dot(linalg.inv(C), A)
    bestfitvar = sc.dot(A.T, bestfitvar)
    bestfitvar = linalg.inv(bestfitvar)
    bestfit = sc.dot(bestfitvar, bestfit)
    #Now sample
    inittheta = m.acos(1. / m.sqrt(1. + bestfit[1]**2.))
    if bestfit[1] < 0.:
        inittheta = m.pi - inittheta
    initialguess = sc.array(
        [bestfit[0] * m.cos(inittheta), inittheta,
         sc.log(1.)])  #(m,b,logV)
    #With this initial guess start off the sampling procedure
    initialX = objective(initialguess, Z, ycovar)
    currentX = initialX
    bestX = initialX
    bestfit = initialguess
    currentguess = initialguess
    naccept = 0
    samples = []
    samples.append(currentguess)
    for jj in range(nburn + nsamples):
        #Draw a sample from the proposal distribution
        newsample = sc.zeros(3)
        newsample[0] = currentguess[0] + stats.norm.rvs() * parsigma[0]
        newsample[1] = currentguess[1] + stats.norm.rvs() * parsigma[1]
        newsample[2] = currentguess[2] + stats.norm.rvs() * parsigma[2]
        #Calculate the objective function for the newsample
        newX = objective(newsample, Z, ycovar)
        #Accept or reject
        #Reject with the appropriate probability
        u = stats.uniform.rvs()
        try:
            test = m.exp(newX - currentX)
        except OverflowError:
            test = 2.
        if u < test:
            #Accept
            currentX = newX
            currentguess = newsample
            naccept = naccept + 1
        if currentX > bestX:
            bestfit = currentguess
            bestX = currentX
        samples.append(currentguess)
    if double(naccept) / (nburn + nsamples) < .5 or double(naccept) / (
            nburn + nsamples) > .8:
        print "Acceptance ratio was " + str(
            double(naccept) / (nburn + nsamples))

    samples = sc.array(samples).T[:, nburn:-1]
    print "Best-fit, overall"
    print bestfit, sc.mean(samples[2, :]), sc.median(samples[2, :])

    histmb, edges = sc.histogramdd(samples.T[:, 0:2],
                                   bins=round(sc.sqrt(nsamples) / 2.))
    indxi = sc.argmax(sc.amax(histmb, axis=1))
    indxj = sc.argmax(sc.amax(histmb, axis=0))
    print "Best-fit, marginalized"
    print edges[0][indxi - 1], edges[1][indxj - 1]
    print edges[0][indxi], edges[1][indxj]
    print edges[0][indxi + 1], edges[1][indxj + 1]

    t = edges[1][indxj]
    bcost = edges[0][indxi]
    mf = m.sqrt(1. / m.cos(t)**2. - 1.)
    b = bcost / m.cos(t)
    print b, mf

    #Plot
    plot.bovy_print(**bovyprintargs)
    hist, bins, patchess = plot.bovy_hist(sc.exp(samples.T[:, 2] / 2.),
                                          edgecolor='k',
                                          bins=round(sc.sqrt(nsamples) / 2.),
                                          xlabel=r'$\sqrt{V}$',
                                          normed=True,
                                          histtype='step')
    cumhist = sc.cumsum(hist) / sc.sum(hist) / (bins[1] - bins[0])
    ninefive = 0.
    ninenine = 0.
    foundfive = False
    foundnine = False
    for ii in range(len(cumhist)):
        if cumhist[ii] * (bins[1] - bins[0]) > 0.95 and not foundfive:
            ninefive = bins[ii]
            foundfive = True
        if cumhist[ii] * (bins[1] - bins[0]) > 0.99 and not foundnine:
            ninenine = bins[ii]
            foundnine = True
    print ninefive, ninenine
    axvline(ninefive, color='0.5', lw=2.)
    axvline(ninenine, color='0.5', lw=2.)
    plot.bovy_end_print(plotfilename)

    return

    #Plot result
    plot.bovy_print()
    xrange = [0, 300]
    yrange = [0, 700]
    plot.bovy_plot(sc.array(xrange),
                   mf * sc.array(xrange) + b,
                   'k--',
                   xrange=xrange,
                   yrange=yrange,
                   xlabel=r'$x$',
                   ylabel=r'$y$',
                   zorder=2)
    for ii in range(10):
        #Random sample
        ransample = sc.floor((stats.uniform.rvs() * nsamples))
        ransample = samples.T[ransample, 0:2]
        mf = m.sqrt(1. / m.cos(ransample[1])**2. - 1.)
        b = ransample[0] / m.cos(ransample[1])
        bestb = b
        bestm = mf
        plot.bovy_plot(sc.array(xrange),
                       bestm * sc.array(xrange) + bestb,
                       overplot=True,
                       color='0.75',
                       zorder=0)

    #Add labels
    nsamples = samples.shape[1]
    for ii in range(nsample):
        Pb = 0.
        for jj in range(nsamples):
            Pb += Pbad(samples[:, jj], Z[ii, :], ycovar[:, ii, :])
        Pb /= nsamples
        text(Z[ii, 0] + 5, Z[ii, 1] + 5, '%.1f' % Pb, color='0.5', zorder=3)

    #Plot the data OMG straight from plot_data.py
    data = read_data('data_allerr.dat', True)
    ndata = len(data)
    #Create the ellipses and the data points
    id = sc.zeros(nsample)
    x = sc.zeros(nsample)
    y = sc.zeros(nsample)
    ellipses = []
    ymin, ymax = 0, 0
    xmin, xmax = 0, 0
    jj = 0
    for ii in range(ndata):
        if sc.any(exclude == data[ii][0]):
            continue
        id[jj] = data[ii][0]
        x[jj] = data[ii][1][0]
        y[jj] = data[ii][1][1]
        #Calculate the eigenvalues and the rotation angle
        ycovar = sc.zeros((2, 2))
        ycovar[0, 0] = data[ii][3]**2.
        ycovar[1, 1] = data[ii][2]**2.
        ycovar[0, 1] = data[ii][4] * m.sqrt(ycovar[0, 0] * ycovar[1, 1])
        ycovar[1, 0] = ycovar[0, 1]
        eigs = linalg.eig(ycovar)
        angle = m.atan(-eigs[1][0, 1] / eigs[1][1, 1]) / m.pi * 180.
        thisellipse = Ellipse(sc.array([x[jj], y[jj]]), 2 * m.sqrt(eigs[0][0]),
                              2 * m.sqrt(eigs[0][1]), angle)
        ellipses.append(thisellipse)
        if (x[jj] + m.sqrt(ycovar[0, 0])) > xmax:
            xmax = (x[jj] + m.sqrt(ycovar[0, 0]))
        if (x[jj] - m.sqrt(ycovar[0, 0])) < xmin:
            xmin = (x[jj] - m.sqrt(ycovar[0, 0]))
        if (y[jj] + m.sqrt(ycovar[1, 1])) > ymax:
            ymax = (y[jj] + m.sqrt(ycovar[1, 1]))
        if (y[jj] - m.sqrt(ycovar[1, 1])) < ymin:
            ymin = (y[jj] - m.sqrt(ycovar[1, 1]))
        jj = jj + 1

    #Add the error ellipses
    ax = gca()
    for e in ellipses:
        ax.add_artist(e)
        e.set_facecolor('none')
    ax.plot(x, y, color='k', marker='o', linestyle='None')

    plot.bovy_end_print(plotfilename)
def exMix1(exclude=None,
           plotfilenameA='exMix1a.png',
           plotfilenameB='exMix1b.png',
           plotfilenameC='exMix1c.png',
           nburn=20000,
           nsamples=1000000,
           parsigma=[5, .075, .2, 1, .1],
           dsigma=1.,
           bovyprintargs={},
           sampledata=None):
    """exMix1: solve exercise 5 (mixture model) using MCMC sampling
    Input:
       exclude        - ID numbers to exclude from the analysis (can be None)
       plotfilename*  - filenames for the output plot
       nburn          - number of burn-in samples
       nsamples       - number of samples to take after burn-in
       parsigma       - proposal distribution width (Gaussian)
       dsigma         - divide uncertainties by this amount
    Output:
       plot
    History:
       2010-04-28 - Written - Bovy (NYU)
    """
    sc.random.seed(-1)  #In the interest of reproducibility (if that's a word)
    #Read the data
    data = read_data('data_yerr.dat')
    ndata = len(data)
    if not exclude == None:
        nsample = ndata - len(exclude)
    else:
        nsample = ndata
    #First find the chi-squared solution, which we will use as an
    #initial guess
    #Put the data in the appropriate arrays and matrices
    Y = sc.zeros(nsample)
    X = sc.zeros(nsample)
    A = sc.ones((nsample, 2))
    C = sc.zeros((nsample, nsample))
    yerr = sc.zeros(nsample)
    jj = 0
    for ii in range(ndata):
        if not exclude == None and sc.any(exclude == data[ii][0]):
            pass
        else:
            Y[jj] = data[ii][1][1]
            X[jj] = data[ii][1][0]
            A[jj, 1] = data[ii][1][0]
            C[jj, jj] = data[ii][2]**2. / dsigma**2.
            yerr[jj] = data[ii][2] / dsigma
            jj = jj + 1

    brange = [-120, 120]
    mrange = [1.5, 3.2]

    # This matches the order of the parameters in the "samples" vector
    mbrange = [brange, mrange]

    if sampledata is None:
        sampledata = runSampler(X, Y, A, C, yerr, nburn, nsamples, parsigma,
                                mbrange)

    (histmb, edges, mbsamples, pbhist, pbedges) = sampledata

    # Hack -- produce fake Pbad samples from Pbad histogram.
    pbsamples = hstack([
        array([x] * N)
        for x, N in zip((pbedges[:-1] + pbedges[1:]) / 2, pbhist)
    ])

    indxi = sc.argmax(sc.amax(histmb, axis=1))
    indxj = sc.argmax(sc.amax(histmb, axis=0))
    print "Best-fit, marginalized"
    print edges[0][indxi - 1], edges[1][indxj - 1]
    print edges[0][indxi], edges[1][indxj]
    print edges[0][indxi + 1], edges[1][indxj + 1]

    #2D histogram
    plot.bovy_print(**bovyprintargs)
    levels = special.erf(0.5 * sc.arange(1, 4))
    xe = [edges[0][0], edges[0][-1]]
    ye = [edges[1][0], edges[1][-1]]
    aspect = (xe[1] - xe[0]) / (ye[1] - ye[0])
    plot.bovy_dens2d(histmb.T,
                     origin='lower',
                     cmap=cm.gist_yarg,
                     interpolation='nearest',
                     contours=True,
                     cntrmass=True,
                     extent=xe + ye,
                     levels=levels,
                     aspect=aspect,
                     xlabel=r'$b$',
                     ylabel=r'$m$')
    xlim(brange)
    ylim(mrange)

    plot.bovy_end_print(plotfilenameA)

    #Data with MAP line and sampling
    plot.bovy_print(**bovyprintargs)
    bestb = edges[0][indxi]
    bestm = edges[1][indxj]
    xrange = [0, 300]
    yrange = [0, 700]
    plot.bovy_plot(xrange,
                   bestm * sc.array(xrange) + bestb,
                   'k-',
                   xrange=xrange,
                   yrange=yrange,
                   xlabel=r'$x$',
                   ylabel=r'$y$',
                   zorder=2)
    errorbar(X, Y, yerr, marker='o', color='k', linestyle='None', zorder=1)

    for m, b in mbsamples:
        plot.bovy_plot(xrange,
                       m * sc.array(xrange) + b,
                       overplot=True,
                       xrange=xrange,
                       yrange=yrange,
                       xlabel=r'$x$',
                       ylabel=r'$y$',
                       color='0.75',
                       zorder=1)

    plot.bovy_end_print(plotfilenameB)

    #Pb plot
    if not 'text_fontsize' in bovyprintargs:
        bovyprintargs['text_fontsize'] = 11
    plot.bovy_print(**bovyprintargs)
    plot.bovy_hist(pbsamples,
                   bins=round(sc.sqrt(nsamples) / 5.),
                   xlabel=r'$P_\mathrm{b}$',
                   normed=True,
                   histtype='step',
                   range=[0, 1],
                   edgecolor='k')
    ylim(0, 4.)
    if dsigma == 1.:
        plot.bovy_text(r'$\mathrm{using\ correct\ data\ uncertainties}$',
                       top_right=True)
    else:
        plot.bovy_text(r'$\mathrm{using\ data\ uncertainties\ /\ 2}$',
                       top_left=True)

    plot.bovy_end_print(plotfilenameC)

    return sampledata