def compute_diag(SNe):
    lens = 0.055
    #c = 3e5 #km/s
    sigma_lens = 0.055 * SNe['zcmb']
    sigma_pecvel = 5*8.33e-4/(np.log(10) *SNe['zcmb']) # See eq. 13 in B14
    sigma_coh = np.array([coh_dict[JLA.survey(sn)] for sn in SNe])
    return np.column_stack((sigma_coh, sigma_lens, sigma_pecvel))
Example #2
0
def compute_diag(SNe):
    lens = 0.055
    #c = 3e5 #km/s
    sigma_lens = 0.055 * SNe['zcmb']
    sigma_pecvel = 5*5e-4/(np.log(10) *SNe['zcmb'])
    sigma_coh = np.array([coh_dict[JLA.survey(sn)] for sn in SNe])
    return np.column_stack((sigma_coh, sigma_lens, sigma_pecvel))
    def apply(self, SNe, to_write=False):
        """ correction to SNe"""
        z_value, z_err = [], []

        for sn in SNe:
            c = SkyCoord(sn['ra']*u.degree, sn['dec']*u.degree)
            gc = c.galactic
            vpec = self.lookup_velocity(sn['zhel'], gc.l.degree, gc.b.degree)
            z_c = self.correct_redshift(sn['zhel'], vpec, gc.l.degree, gc.b.degree)
            z_plus = self.correct_redshift(sn['zhel'], self.r_plus*vpec,
                                           gc.l.degree, gc.b.degree)
            z_minus = self.correct_redshift(sn['zhel'], self.r_minus*vpec,
                                            gc.l.degree, gc.b.degree)
            if JLA.survey(sn) == 'nearby':
                z_value.append(z_c)
                z_err.append(np.mean([z_plus - z_c, z_c - z_minus]))
            else:
                z_value.append(sn['zcmb'])
                z_err.append(0)
                
        if to_write:
            np.savetxt('z_CMB_corrected.txt', np.array(z_value))
        return z_value, z_err
Example #4
0
    def apply(self, SNe, to_write=False):
        """ correction to SNe"""
        z_value, z_err = [], []

        for sn in SNe:
            c = SkyCoord(sn['ra'] * u.degree, sn['dec'] * u.degree)
            gc = c.galactic
            vpec = self.lookup_velocity(sn['zhel'], gc.l.degree, gc.b.degree)
            z_c = self.correct_redshift(sn['zhel'], vpec, gc.l.degree,
                                        gc.b.degree)
            z_plus = self.correct_redshift(sn['zhel'], self.r_plus * vpec,
                                           gc.l.degree, gc.b.degree)
            z_minus = self.correct_redshift(sn['zhel'], self.r_minus * vpec,
                                            gc.l.degree, gc.b.degree)
            if JLA.survey(sn) == 'nearby':
                z_value.append(z_c)
                z_err.append(np.mean([z_plus - z_c, z_c - z_minus]))
            else:
                z_value.append(sn['zcmb'])
                z_err.append(0)

        if to_write:
            np.savetxt('z_CMB_corrected.txt', np.array(z_value))
        return z_value, z_err
Example #5
0
def compute_bias(options):

    import numpy
    import astropy.io.fits as fits
    import JLA_library as JLA
    from astropy.table import Table
    from astropy.cosmology import FlatwCDM
    from  scipy.optimize import leastsq
    import matplotlib.pyplot as plt
    from scipy.stats import t


    # -----------  Read in the configuration file ------------
    params=JLA.build_dictionary(options.config)

    # -----------  Read in the SN ordering ------------------------
    SNeList = Table(numpy.genfromtxt(options.SNlist,
                               usecols=(0, 2),
                               dtype='S30,S200',
                               names=['id', 'lc']))
    nSNe = len(SNeList)

    for i, SN in enumerate(SNeList):
        SNeList['id'][i] = SNeList['id'][i].replace('lc-', '').replace('.list', '').replace('_smp','')

    lcfile = JLA.get_full_path(params[options.lcfits])
    SNe = Table.read(lcfile, format='fits')
    print 'There are %d SNe' % (nSNe)

    indices = JLA.reindex_SNe(SNeList['id'], SNe)
    SNe=SNe[indices]

    # Add a column that records the error in the bias correction
    SNe['e_bias'] = numpy.zeros(nSNe,'f8')

    # Read in the bias correction (see, for example, Fig.5 in B14)
    # Fit a polynomial to the data
    # Determine the uncertainties

    bias = numpy.genfromtxt(JLA.get_full_path(params['biasPolynomial']),
                                  skip_header=4,
                                  usecols=(0, 1, 2, 3),
                                  dtype='S10,f8,f8,f8',
                                  names=['sample', 'redshift', 'bias', 'e_bias'])

    if options.plot:
        fig=plt.figure()
        ax=fig.add_subplot(111)
        colour={'nearby':'b','SNLS':'r','SDSS':'g','DES':'k'}

    for sample in numpy.unique(bias['sample']):
        selection=(bias['sample']==sample)
        guess=[0,0,0]

        print bias[selection]
        plsq=leastsq(residuals, guess, args=(bias[selection]['bias'],
                                             bias[selection]['redshift'],
                                             bias[selection]['e_bias'],
                                             'poly'), full_output=1)

        if plsq[4] in [1,2,3,4]:
            print 'Solution for %s found' % (sample)

        if options.plot:
            ax.errorbar(bias[selection]['redshift'],
                    bias[selection]['bias'],
                    yerr=bias[selection]['e_bias'],
                    ecolor='k',
                    color=colour[sample],
                    fmt='o',
                    label=sample)
            z=numpy.arange(numpy.min(bias[selection]['redshift']),numpy.max(bias[selection]['redshift']),0.001)
            ax.plot(z,poly(z,plsq[0]),color=colour[sample])

        # For each SNe, determine the uncerainty in the correction. We use the approach descibed in
        # https://www.astro.rug.nl/software/kapteyn/kmpfittutorial.html
        
        # Compute the chi-sq.
        chisq=(((bias[selection]['bias']-poly(bias[selection]['redshift'],plsq[0]))/bias[selection]['e_bias'])**2.).sum()
        dof=selection.sum()-len(guess)
        print "Reduced chi-square value for sample %s is %5.2e" % (sample, chisq / dof)

        alpha=0.315 # Confidence interval is 100 * (1-alpha)
        # Compute the upper alpha/2 value for the student t distribution with dof
        thresh=t.ppf((1-alpha/2.0), dof)
        
        if options.plot and sample!='nearby':
            # The following is only valid for polynomial fitting functions, and we do not compute it for the nearby sample
            upper_curve=[]
            lower_curve=[]
            for x in z:
                vect=numpy.matrix([1,x,x**2.])
                offset=thresh * numpy.sqrt(chisq / dof * (vect*numpy.matrix(plsq[1])*vect.T)[0,0])
                upper_curve.append(poly(x,plsq[0])+offset)
                lower_curve.append(poly(x,plsq[0])-offset)

            ax.plot(z,lower_curve,'--',color=colour[sample])
            ax.plot(z,upper_curve,'--',color=colour[sample])

        # Compute the error in the bias
        # We increase the absolute value
        # In other words, if the bias is negative, we subtract the error to make it even more negative
        # This is to get the correct sign in the off diagonal elements
        # We assume 100% correlation between SNe
        for i,SN in enumerate(SNe):
            if SN['zcmb'] > 0:
                redshift = SN['zcmb']
            else:
                redshift = SN['zhel']
            if JLA.survey(SN) == sample:
                # For the nearby SNe, the uncertainty in the bias correction is the bias correction itself
                if sample=='nearby':
                    SNe['e_bias'][i]=poly(redshift,plsq[0])
                    #print SN['name'],redshift, SNe['e_bias'][i]
                else:
                    vect = numpy.matrix([1,redshift,redshift**2.])
                    if poly(redshift,plsq[0]) > 0:
                        sign = 1
                    else:
                        sign = -1

                    SNe['e_bias'][i] = sign * thresh * numpy.sqrt(chisq / dof * (vect*numpy.matrix(plsq[1])*vect.T)[0,0])

                # We are getting some unrealistcally large values

    date = JLA.get_date()

    if options.plot:
        ax.legend()
        plt.savefig('C_bias_%s.png' % (date))
        plt.close()

    # Compute the bias matrix
    # 

    Zero=numpy.zeros(nSNe)
    H=numpy.concatenate((SNe['e_bias'],Zero,Zero)).reshape(3,nSNe).ravel(order='F')
    C_bias = numpy.matrix(H).T * numpy.matrix(H)

    fits.writeto('C_bias_%s.fits' % (date),C_bias,clobber=True) 

    return None
Example #6
0
def compute_bias(options):

    import numpy
    import astropy.io.fits as fits
    import JLA_library as JLA
    from astropy.table import Table
    from astropy.cosmology import FlatwCDM
    from  scipy.optimize import leastsq
    import matplotlib.pyplot as plt
    from scipy.stats import t


    # -----------  Read in the configuration file ------------

    params=JLA.build_dictionary(options.config)

    # -----------  Read in the SN ordering ------------------------
    SNeList = Table(numpy.genfromtxt(options.SNlist,
                               usecols=(0, 2),
                               dtype='S30,S200',
                               names=['id', 'lc']))
    nSNe = len(SNeList)

    for i, SN in enumerate(SNeList):
        SNeList['id'][i] = SNeList['id'][i].replace('lc-', '').replace('.list', '')
        
    lcfile = JLA.get_full_path(params[options.lcfits])
    SNe = Table.read(lcfile, format='fits')

    print 'There are %d SNe' % (nSNe)

    indices = JLA.reindex_SNe(SNeList['id'], SNe)
    SNe=SNe[indices]
    # Add a column that records the error in the bias
    SNe['e_bias'] = numpy.zeros(nSNe,'f8')


    # Read in the points from B14 figure 
    # Fit a polynomial to the data
    # Determine the uncertainties

    bias = numpy.genfromtxt(JLA.get_full_path(params['biasPolynomial']),
                                  skip_header=3,
                                  usecols=(0, 1, 2, 3),
                                  dtype='S10,f8,f8,f8',
                                  names=['sample', 'redshift', 'bias', 'e_bias'])

    
    if options.plot:
        fig=plt.figure()
        ax=fig.add_subplot(111)
        colour={'nearby':'b','SNLS':'r','SDSS':'g'}

    for sample in numpy.unique(bias['sample']):
        selection=(bias['sample']==sample)
        guess=[0,0,0]
        
        plsq=leastsq(residuals, guess, args=(bias[selection]['bias'],
                                             bias[selection]['redshift'],
                                             bias[selection]['e_bias'],
                                             'poly'), full_output=1)

        if plsq[4] in [1,2,3,4]:
            print 'Solution for %s found' % (sample)

        if options.plot:
            ax.errorbar(bias[selection]['redshift'],
                    bias[selection]['bias'],
                    yerr=bias[selection]['e_bias'],
                    ecolor='k',
                    color=colour[sample],
                    fmt='o',
                    label=sample)
            z=numpy.arange(numpy.min(bias[selection]['redshift']),numpy.max(bias[selection]['redshift']),0.001)
            ax.plot(z,poly(z,plsq[0]),color=colour[sample])

        # For each SNe, determine the uncerainty in the correction. We use the covariance martix
        # prediction bounds for the fitted curve. 
        # https://www.astro.rug.nl/software/kapteyn/kmpfittutorial.html
        
        # Compute the chi-sq.
        chisq=(((bias[selection]['bias']-poly(bias[selection]['redshift'],plsq[0]))/bias[selection]['e_bias'])**2.).sum()
        dof=selection.sum()-len(guess)
        print "Reduced chi-square value for sample %s is %5.2e" % (sample, chisq / dof)

        alpha=0.315 # Confidence interval is 100 * (1-alpha)
        # Compute the upper alpha/2 vallue for the student t distribution with dof
        thresh=t.ppf((1-alpha/2.0), dof)
        
        if options.plot:
            # The following is only valid for polynomial fitting functions
            upper_curve=[]
            lower_curve=[]
            for x in z:
                vect=numpy.matrix([1,x,x**2.])
                offset=thresh * numpy.sqrt(chisq / dof * (vect*numpy.matrix(plsq[1])*vect.T)[0,0])
                upper_curve.append(poly(x,plsq[0])+offset)
                lower_curve.append(poly(x,plsq[0])-offset)

            ax.plot(z,lower_curve,'--',color=colour[sample])
            ax.plot(z,upper_curve,'--',color=colour[sample])

        # Compute the error in the bias
        # We increase the absolute vlaue
        # In other words, if the bias is negative, we subtract the error to make it even more negative
        # We assume 100% correlation between SNe
        for i,SN in enumerate(SNe):
            if JLA.survey(SN) == sample:
                if SN['zcmb'] > 0:
                    redshift = SN['zcmb']
                else:
                    redshift = SN['zhel']
                vect = numpy.matrix([1,redshift,redshift**2.])
                if poly(redshift,plsq[0]) > 0:
                    sign = 1
                else:
                    sign = -1

                SNe['e_bias'][i] = sign * thresh * numpy.sqrt(chisq / dof * (vect*numpy.matrix(plsq[1])*vect.T)[0,0])
                # We are getting some unrealistcally large values

    if options.plot:
        ax.legend()
        plt.show()
        plt.close()

    # Compute the bias matrix
    # 

    date = JLA.get_date()
    Zero=numpy.zeros(nSNe)
    H=numpy.concatenate((SNe['e_bias'],Zero,Zero)).reshape(3,nSNe).ravel(order='F')

    C_bias = numpy.matrix(H)

    fits.writeto('C_bias_%s.fits' % (date),C_bias.T*C_bias,clobber=True) 

    return None