def compute_model(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.interpolate import interp1d # ----------- Read in the configuration file ------------ params=JLA.build_dictionary(options.config) # ----------- Read in the SN ordering ------------------------ SNeList = 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] redshift = SNe['zcmb'] replace=(redshift < 0) # For SNe that do not have the CMB redshift redshift[replace]=SNe[replace]['zhel'] print len(redshift) if options.raw: # Data from the bottom left hand figure of Mosher et al. 2014. # This is option ii) that is descibed above offsets=Table.read(JLA.get_full_path(params['modelOffset']),format='ascii.csv') Delta_M=interp1d(offsets['z'], offsets['offset'], kind='linear',bounds_error=False,fill_value='extrapolate')(redshift) else: Om_0=0.303 # JLA value in the wCDM model cosmo1 = FlatwCDM(name='SNLS3+WMAP7', H0=70.0, Om0=Om_0, w0=-1.0) cosmo2 = FlatwCDM(name='SNLS3+WMAP7', H0=70.0, Om0=Om_0, w0=-1.024) Delta_M=5*numpy.log10(cosmo1.luminosity_distance(redshift)/cosmo2.luminosity_distance(redshift)) # Build the covariance matrix. Note that only magnitudes are affected Zero=numpy.zeros(nSNe) H=numpy.concatenate((Delta_M,Zero,Zero)).reshape(3,nSNe).ravel(order='F') C_model=numpy.matrix(H).T * numpy.matrix(H) date = JLA.get_date() fits.writeto('C_model_%s.fits' % (date),numpy.array(C_model),clobber=True) return None
def compute_model(options): import numpy import astropy.io.fits as fits import JLA_library as JLA from astropy.table import Table from astropy.cosmology import FlatwCDM # ----------- Read in the configuration file ------------ params = JLA.build_dictionary(options.config) # ----------- Read in the SN ordering ------------------------ SNeList = 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) #z=numpy.array([]) #offset=numpy.array([]) Om_0 = 0.303 # JLA value in the wCDM model cosmo1 = FlatwCDM(name='SNLS3+WMAP7', H0=70.0, Om0=Om_0, w0=-1.0) cosmo2 = FlatwCDM(name='SNLS3+WMAP7', H0=70.0, Om0=Om_0, w0=-1.024) # For the JLA SNe redshift = SNe['zcmb'] replace = (redshift < 0) # For the non JLA SNe redshift[replace] = SNe[replace]['zhel'] Delta_M = 5 * numpy.log10( cosmo1.luminosity_distance(redshift) / cosmo2.luminosity_distance(redshift)) # Build the covariance matrix. Note that only magnitudes are affected Zero = numpy.zeros(nSNe) H = numpy.concatenate((Delta_M, Zero, Zero)).reshape(3, nSNe).ravel(order='F') C_model = numpy.matrix(H).T * numpy.matrix(H) date = JLA.get_date() fits.writeto('C_model_%s.fits' % (date), numpy.array(C_model), clobber=True) return None
def compute_model(options): import numpy import astropy.io.fits as fits import JLA_library as JLA from astropy.table import Table from astropy.cosmology import FlatwCDM # ----------- Read in the configuration file ------------ params=JLA.build_dictionary(options.config) # ----------- Read in the SN ordering ------------------------ SNeList = 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) #z=numpy.array([]) #offset=numpy.array([]) Om_0=0.303 # JLA value in the wCDM model cosmo1 = FlatwCDM(name='SNLS3+WMAP7', H0=70.0, Om0=Om_0, w0=-1.0) cosmo2 = FlatwCDM(name='SNLS3+WMAP7', H0=70.0, Om0=Om_0, w0=-1.024) # For the JLA SNe redshift = SNe['zcmb'] replace=(redshift < 0) # For the non JLA SNe redshift[replace]=SNe[replace]['zhel'] Delta_M=5*numpy.log10(cosmo1.luminosity_distance(redshift)/cosmo2.luminosity_distance(redshift)) # Build the covariance matrix. Note that only magnitudes are affected Zero=numpy.zeros(nSNe) H=numpy.concatenate((Delta_M,Zero,Zero)).reshape(3,nSNe).ravel(order='F') C_model=numpy.matrix(H).T * numpy.matrix(H) date = JLA.get_date() fits.writeto('C_model_%s.fits' % (date),numpy.array(C_model),clobber=True) return None
def lnlike(theta, x, y, z, xerr, yerr, zerr): alpha, beta, h0 = theta Or = 4.153e-5 * h0**(-2) Om = 0.3 w0 = -1.0 cosmo = FlatwCDM(H0=h0 * 100, Om0=Om, w0=w0) #--------------------------------------------------------------------------- ixG = np.where(z > 10) ixH = np.where(z < 10) Mum = z * 0.0 MumErr = z * 0.0 Mum[ixG] = z[ixG] MumErr[ixG] = zerr[ixG] Mum[ixH] = 5.0 * np.log10(cosmo.luminosity_distance(z[ixH]).value) + 25.0 MumErr[ixH] = (5.0 / np.log(10.0)) * (zerr[ixH] / z[ixH]) Mu = 2.5 * (beta * x + alpha) - 2.5 * y - 100.195 MuErr = 2.5 * np.sqrt((yerr)**2 + beta**2 * (xerr)**2) R = (Mu - Mum) W = 1.0 / (MuErr**2 + MumErr**2) xsq = np.sum(R**2 * W) llq = -0.5 * xsq return (llq, xsq, R, Mum)
def simulateData(): # the number of transients nTrans = 30 # set the state of the random number generator seed = 0 numpy.random.seed(seed) # simulated data in the dictionary observation, including photometry at peak, # spectroscopic redshift, and spectroscopic type. # the convention is SNIa are '0', SNII are '1' # the current implementation is barebones observation = dict() observation["specz"] = numpy.random.uniform(low=0.1, high=0.8, size=nTrans) observation["zprob"] = numpy.zeros(nTrans) + 1.0 spectype = numpy.random.uniform(low=0, high=1, size=nTrans) snIarate = 1.0 / (1 + inputs.rate_II_r) observation["spectype"] = numpy.zeros(nTrans, dtype=int) observation["spectype"][spectype > snIarate] = 1 luminosity = (1.0 - observation["spectype"]) * numpy.exp(inputs.logL_snIa) * 10 ** ( numpy.random.normal(0, inputs.sigma_snIa / 2.5, size=nTrans) ) + observation["spectype"] * numpy.exp(inputs.logL_snII) * 10 ** ( numpy.random.normal(0, inputs.sigma_snII / 2.5, size=nTrans) ) cosmo = FlatwCDM(H0=72, Om0=inputs.Om0, w0=inputs.w0) ld = cosmo.luminosity_distance(observation["specz"]).value # h0 = (const.c/cosmo.H0).to(u.Mpc).value observation["counts"] = luminosity / 4 / numpy.pi / ld / ld * 10 ** (inputs.Z / 2.5) # plt.scatter(observation['specz'],-2.5*numpy.log10(observation['counts'])) found = observation["counts"] >= fluxthreshold nTrans = found.sum() observation["specz"] = numpy.reshape(observation["specz"][found], (nTrans, 1)) observation["zprob"] = numpy.reshape(observation["zprob"][found], (nTrans, 1)) observation["spectype"] = observation["spectype"][found] # observation['spectype'][0] = -1 # case of no spectral type observation["counts"] = observation["counts"][found] return observation
def simulateData(): # the number of transients nTrans = 15 # set the state of the random number generator seed=0 numpy.random.seed(seed) # simulated data in the dictionary observation, including photometry at peak, # spectroscopic redshift, and spectroscopic type. # the convention is SNIa are '0', SNII are '1' # the current implementation is barebones observation=dict() observation['specz'] = numpy.random.uniform(low=0.1, high=0.8, size=nTrans) observation['zprob'] = numpy.zeros(nTrans)+1. spectype = numpy.random.uniform(low=0, high=1, size=nTrans) observation['spectype'] = spectype.round().astype('int') luminosity = (1.-observation['spectype'])*10**(numpy.random.normal(0, 0.1/2.5, size=nTrans)) \ + observation['spectype']*.5**10**(numpy.random.normal(0, 0.4/2.5,size=nTrans)) cosmo = FlatwCDM(H0=72, Om0=0.28, w0=-1) ld = cosmo.luminosity_distance(observation['specz']).value h0 = (const.c/cosmo.H0).to(u.Mpc).value observation['counts'] = luminosity / 4/numpy.pi/ld/ld*10**(0.02/2.5) count_lim = .4e-8 found = observation['counts'] >= count_lim nTrans = found.sum() observation['specz'] = numpy.reshape(observation['specz'][found],(nTrans,1)) observation['zprob'] = numpy.reshape(observation['zprob'][found],(nTrans,1)) observation['spectype'] =observation['spectype'][found] observation['spectype'][0] = -1 # case of no spectral type observation['counts'] =observation['counts'][found] return observation
def distance(self, z): cosmology = FlatwCDM(**self.__par_values__) return cosmology.luminosity_distance(z).value
def luminosity_distance(z, Om0, w0): cosmo = FlatwCDM(H0=72, Om0=Om0, w0=w0) return cosmo.luminosity_distance(z).value
def simulateData(): # the number of transients nTrans = 30 # set the state of the random number generator seed=0 numpy.random.seed(seed) # simulated data in the dictionary observation, including photometry at peak, # spectroscopic redshift, and spectroscopic type. # the convention is SNIa are '0', SNII are '1' # the current implementation is barebones observation=dict() observation['specz'] = numpy.random.uniform(low=0.1, high=0.8, size=nTrans) observation['zprob'] = numpy.zeros(nTrans)+1. spectype = numpy.random.uniform(low=0, high=1, size=nTrans) snIarate = 1./(1+inputs.rate_II_r) observation['spectype'] = numpy.zeros(nTrans,dtype=int) observation['spectype'][spectype > snIarate]=1 luminosity = (1.-observation['spectype'])*numpy.exp(inputs.logL_snIa)*10**(numpy.random.normal(0, inputs.sigma_snIa/2.5, size=nTrans)) \ + observation['spectype']*numpy.exp(inputs.logL_snII)*10**(numpy.random.normal(0, inputs.sigma_snII/2.5,size=nTrans)) cosmo = FlatwCDM(H0=72, Om0=inputs.Om0, w0=inputs.w0) ld = cosmo.luminosity_distance(observation['specz']).value # h0 = (const.c/cosmo.H0).to(u.Mpc).value npts = 2 cov = numpy.zeros((2,2)) cov[0,0] = 1e-20 cov[1,1] = 1e-20 cov[0,1] = 0 #for now uncorrelated as algorithm handles that cov[1,0] = 0 #for now uncorrelated as algorithm handles that invcov = numpy.linalg.inv(cov) observation['counts'] = [] observation['counts_invcov']=[] observation['mjds']=[] for i in xrange(nTrans): ans = numpy.random.multivariate_normal(numpy.zeros(npts)+ luminosity[i] / 4/numpy.pi/ld[i]/ld[i]*10**(inputs.Z/2.5), cov) observation['counts'].append(ans) observation['counts_invcov'].append(invcov) observation['mjds'].append(numpy.arange(2.)) #luminosity / 4/numpy.pi/ld/ld*10**(inputs.Z/2.5) # plt.scatter(observation['specz'],-2.5*numpy.log10(observation['counts'])) #at least one must be above threshold nthreshold = 1 found = [] # found = observation['counts'] >= fluxthreshold for i in xrange(nTrans): nabove = (observation['counts'][i] >= fluxthreshold).sum() found.append(nabove >= nthreshold) found = numpy.array(found) nTrans = found.sum() observation['specz'] = [numpy.array([dum]) for dum in observation['specz'][found]] observation['zprob'] = [numpy.array([dum]) for dum in observation['zprob'][found]] observation['spectype'] = observation['spectype'][found] observation['spectype'][0] = -1 # case of no spectral type observation['specz'][0] = numpy.array([observation['specz'][0][0], 0.2]) observation['zprob'][0] = numpy.array([0.6,0.4]) observation['spectype'][1] = -1 # case of no spectral type observation['specz'][1] = numpy.array([observation['specz'][1][0], 0.8]) observation['zprob'][1] = numpy.array([0.3,0.7]) # observation['counts'] =observation['counts'][found] # observation['counts_cov'] =observation['counts_cov'][found] observation['counts'] =[observation['counts'][i] for i in xrange(len(found)) if found[i]] observation['counts_invcov'] = [observation['counts_invcov'][i] for i in xrange(len(found)) if found[i]] observation['mjds'] = [observation['mjds'][i] for i in xrange(len(found)) if found[i]] return observation
def compute_rel_size(options): import numpy import astropy.io.fits as fits from astropy.table import Table import JLA_library as JLA from astropy.cosmology import FlatwCDM import os # ----------- Read in the configuration file ------------ params = JLA.build_dictionary(options.config) # ---------- Read in the SNe list ------------------------- SNeList = numpy.genfromtxt(options.SNlist, usecols=(0, 2), dtype='S30,S200', names=['id', 'lc']) for i, SN in enumerate(SNeList): SNeList['id'][i] = SNeList['id'][i].replace('lc-', '').replace('.list', '') # ----------- Read in the data JLA -------------------------- lcfile = JLA.get_full_path(params[options.lcfits]) SNe = Table.read(lcfile, format='fits') nSNe = len(SNe) print 'There are %d SNe in this sample' % (nSNe) # sort it to match the listing in options.SNlist indices = JLA.reindex_SNe(SNeList['id'], SNe) SNe = SNe[indices] # ---------- Compute the Jacobian ---------------------- # The Jacobian is an m by 4 matrix, where m is the number of SNe # The columns are ordered in terms of Om, w, alpha and beta J = [] JLA_result = { 'Om': 0.303, 'w': -1.00, 'alpha': 0.141, 'beta': 3.102, 'M_B': -19.05 } offset = {'Om': 0.01, 'w': 0.01, 'alpha': 0.01, 'beta': 0.01, 'M_B': 0.01} nFit = 4 cosmo1 = FlatwCDM(name='SNLS3+WMAP7', H0=70.0, Om0=JLA_result['Om'], w0=JLA_result['w']) # Varying Om cosmo2 = FlatwCDM(name='SNLS3+WMAP7', H0=70.0, Om0=JLA_result['Om'] + offset['Om'], w0=JLA_result['w']) J.append(5 * numpy.log10((cosmo1.luminosity_distance(SNe['zcmb']) / cosmo2.luminosity_distance(SNe['zcmb']))[:, 0])) # varying alpha J.append(1.0 * offset['alpha'] * SNe['x1'][:, 0]) # varying beta J.append(-1.0 * offset['beta'] * SNe['color'][:, 0]) # varying M_B J.append(offset['M_B'] * numpy.ones(nSNe)) J = numpy.matrix( numpy.concatenate((J)).reshape(nSNe, nFit, order='F') * 100.) # Set up the covariance matrices systematic_terms = [ 'bias', 'cal', 'host', 'dust', 'model', 'nonia', 'pecvel', 'stat' ] covmatrices = { 'bias': params['bias'], 'cal': params['cal'], 'host': params['host'], 'dust': params['dust'], 'model': params['model'], 'nonia': params['nonia'], 'pecvel': params['pecvel'], 'stat': params['stat'] } if options.type in systematic_terms: print "Using %s for the %s term" % (options.name, options.type) covmatrices[options.type] = options.name # Combine the matrices to compute the full covariance matrix, and compute its inverse if options.all: #read in the user provided matrix, otherwise compute it, and write it out C = fits.getdata(JLA.get_full_path(params['all'])) else: C = add_covar_matrices(covmatrices, params['diag']) date = JLA.get_date() fits.writeto('C_total_%s.fits' % (date), C, clobber=True) Cinv = numpy.matrix(C).I # Construct eta, a 3n vector eta = numpy.zeros(3 * nSNe) for i, SN in enumerate(SNe): eta[3 * i] = SN['mb'] eta[3 * i + 1] = SN['x1'] eta[3 * i + 2] = SN['color'] # Construct A, a n x 3n matrix A = numpy.zeros(nSNe * 3 * nSNe).reshape(nSNe, 3 * nSNe) for i in range(nSNe): A[i, 3 * i] = 1.0 A[i, 3 * i + 1] = JLA_result['alpha'] A[i, 3 * i + 2] = -JLA_result['beta'] # ---------- Compute W ---------------------- # W has shape m * 3n, where m is the number of fit paramaters. W = (J.T * Cinv * J).I * J.T * Cinv * numpy.matrix(A) # Note that (J.T * Cinv * J) is a m x m matrix, where m is the number of fit parameters # ----------- Compute V_x, where x represents the systematic uncertainty result = [] for term in systematic_terms: cov = numpy.matrix(fits.getdata(JLA.get_full_path(covmatrices[term]))) if 'C_stat' in covmatrices[term]: # Add diagonal term from Eq. 13 to the magnitude sigma = numpy.genfromtxt( JLA.get_full_path(params['diag']), comments='#', usecols=(0, 1, 2), dtype='f8,f8,f8', names=['sigma_coh', 'sigma_lens', 'sigma_pecvel']) for i in range(nSNe): cov[3 * i, 3 * i] += sigma['sigma_coh'][i]**2 + sigma[ 'sigma_lens'][i]**2 + sigma['sigma_pecvel'][i]**2 V = W * cov * W.T result.append(V[0, 0]) print '%20s\t%5s\t%5s\t%s' % ('Term', 'sigma', 'var', 'Percentage') for i, term in enumerate(systematic_terms): if options.type != None and term == options.type: print '* %18s\t%5.4f\t%5.4f\t%4.1f' % (term, numpy.sqrt( result[i]), result[i], result[i] / numpy.sum(result) * 100.) else: print '%20s\t%5.4f\t%5.4f\t%4.1f' % (term, numpy.sqrt( result[i]), result[i], result[i] / numpy.sum(result) * 100.) print '%20s\t%5.4f' % ('Total', numpy.sqrt(numpy.sum(result))) return
def compute_rel_size(options): import numpy import astropy.io.fits as fits from astropy.table import Table import JLA_library as JLA from astropy.cosmology import FlatwCDM import os # ----------- Read in the configuration file ------------ params=JLA.build_dictionary(options.config) # ---------- Read in the SNe list ------------------------- SNeList=numpy.genfromtxt(options.SNlist,usecols=(0,2),dtype='S30,S200',names=['id','lc']) for i,SN in enumerate(SNeList): SNeList['id'][i]=SNeList['id'][i].replace('lc-','').replace('.list','') # ----------- Read in the data JLA -------------------------- lcfile = JLA.get_full_path(params[options.lcfits]) SNe = Table.read(lcfile, format='fits') nSNe=len(SNe) print 'There are %d SNe in this sample' % (nSNe) # sort it to match the listing in options.SNlist indices = JLA.reindex_SNe(SNeList['id'], SNe) SNe=SNe[indices] # ---------- Compute the Jacobian ---------------------- # The Jacobian is an m by 4 matrix, where m is the number of SNe # The columns are ordered in terms of Om, w, alpha and beta J=[] JLA_result={'Om':0.303,'w':-1.00,'alpha':0.141,'beta':3.102,'M_B':-19.05} offset={'Om':0.01,'w':0.01,'alpha':0.01,'beta':0.01,'M_B':0.01} nFit=4 cosmo1 = FlatwCDM(name='SNLS3+WMAP7', H0=70.0, Om0=JLA_result['Om'], w0=JLA_result['w']) # Varying Om cosmo2 = FlatwCDM(name='SNLS3+WMAP7', H0=70.0, Om0=JLA_result['Om']+offset['Om'], w0=JLA_result['w']) J.append(5*numpy.log10((cosmo1.luminosity_distance(SNe['zcmb'])/cosmo2.luminosity_distance(SNe['zcmb']))[:,0])) # varying alpha J.append(1.0*offset['alpha']*SNe['x1'][:,0]) # varying beta J.append(-1.0*offset['beta']*SNe['color'][:,0]) # varying M_B J.append(offset['M_B']*numpy.ones(nSNe)) J = numpy.matrix(numpy.concatenate((J)).reshape(nSNe,nFit,order='F') * 100.) # Set up the covariance matrices systematic_terms = ['bias', 'cal', 'host', 'dust', 'model', 'nonia', 'pecvel', 'stat'] covmatrices = {'bias':params['bias'], 'cal':params['cal'], 'host':params['host'], 'dust':params['dust'], 'model':params['model'], 'nonia':params['nonia'], 'pecvel':params['pecvel'], 'stat':params['stat']} if options.type in systematic_terms: print "Using %s for the %s term" % (options.name,options.type) covmatrices[options.type]=options.name # Combine the matrices to compute the full covariance matrix, and compute its inverse if options.all: #read in the user provided matrix, otherwise compute it, and write it out C=fits.getdata(JLA.get_full_path(params['all'])) else: C=add_covar_matrices(covmatrices,params['diag']) date=JLA.get_date() fits.writeto('C_total_%s.fits' % (date), C, clobber=True) Cinv=numpy.matrix(C).I # Construct eta, a 3n vector eta=numpy.zeros(3*nSNe) for i,SN in enumerate(SNe): eta[3*i]=SN['mb'] eta[3*i+1]=SN['x1'] eta[3*i+2]=SN['color'] # Construct A, a n x 3n matrix A=numpy.zeros(nSNe*3*nSNe).reshape(nSNe,3*nSNe) for i in range(nSNe): A[i,3*i]=1.0 A[i,3*i+1]=JLA_result['alpha'] A[i,3*i+2]=-JLA_result['beta'] # ---------- Compute W ---------------------- # W has shape m * 3n, where m is the number of fit paramaters. W=(J.T * Cinv * J).I * J.T* Cinv* numpy.matrix(A) # Note that (J.T * Cinv * J) is a m x m matrix, where m is the number of fit parameters # ----------- Compute V_x, where x represents the systematic uncertainty result=[] for term in systematic_terms: cov=numpy.matrix(fits.getdata(JLA.get_full_path(covmatrices[term]))) if 'C_stat' in covmatrices[term]: # Add diagonal term from Eq. 13 to the magnitude sigma = numpy.genfromtxt(JLA.get_full_path(params['diag']),comments='#',usecols=(0,1,2),dtype='f8,f8,f8',names=['sigma_coh','sigma_lens','sigma_pecvel']) for i in range(nSNe): cov[3*i,3*i] += sigma['sigma_coh'][i] ** 2 + sigma['sigma_lens'][i] ** 2 + sigma['sigma_pecvel'][i] ** 2 V=W * cov * W.T result.append(V[0,0]) print '%20s\t%5s\t%5s\t%s' % ('Term','sigma','var','Percentage') for i,term in enumerate(systematic_terms): if options.type!=None and term==options.type: print '* %18s\t%5.4f\t%5.4f\t%4.1f' % (term,numpy.sqrt(result[i]),result[i],result[i]/numpy.sum(result)*100.) else: print '%20s\t%5.4f\t%5.4f\t%4.1f' % (term,numpy.sqrt(result[i]),result[i],result[i]/numpy.sum(result)*100.) print '%20s\t%5.4f' % ('Total',numpy.sqrt(numpy.sum(result))) return