def dualBandExample(filename='../data/SDSSJ203817.37+003029.8.fits', constraints=None,basefilename='SDSSJ203817.37+003029.8', covarType='SF'): """ NAME: dualBandExample PURPOSE: show an example of a power-law structure function GP covariance function fit to an SDSS quasar for g and r INPUT: filename - filename with the data constraints - if None, use all constraints, if [] use no constraints! basefilename - basefilename for plots covarType - 'SF' or 'DRW' OUTPUT: writes several plots HISTORY: 2010-08-11 - Written - Bovy (NYU) """ file= fu.table_fields(filename) mjd_g= nu.array(file.mjd_g)/365.25 g= nu.array(file.g) err_g= nu.array(file.err_g) mjd_r= nu.array(file.mjd_r)/365.25 r= nu.array(file.r) err_r= nu.array(file.err_r) mask= (mjd_g != 0)*(g < 20.6)*(g > 19.7)#Adjust for non-g g= g[mask] g-= nu.mean(g) err_g= err_g[mask] mjd_g= mjd_g[mask] mjd_g-= nu.amin(mjd_g) meanErr_g= nu.mean(err_g) r= r[mask] r-= nu.mean(r) err_r= err_r[mask] mjd_r= mjd_r[mask] mjd_r-= nu.amin(mjd_r) meanErr_r= nu.mean(err_r) meanErr_gr= nu.mean(nu.sqrt(err_r**2.+err_g**2.)) nu.random.seed(4) nGP=5 nx=201 params_mean= () if covarType == 'SF': from powerlawSFgr import covarFunc params= {'logGamma': array([-7.33271548]), 'logGammagr': array([-10.5]), 'gamma': array([ 0.4821092]), 'gammagr': array([ 0.5])} params= {'logGamma': array([-7.79009776]), 'logGammagr': array([-28.0487848]), 'gamma': array([ 0.45918053]), 'gammagr': array([ 0.21333858])} else: from OUgr import covarFunc params= {'logl': array([ 1.94844503]), 'loglgr': array([ 7.36282174]), 'logagr2': array([ 1.0196474]), 'loga2': array([-0.00588868])} params= {'logl':-1.37742591,'loga':-3.47341754, 'loglgr': -2.3777,'logagr': -4.} params= {'logl': array([-1.38968195]), 'loglgr': array([-2.46684501]), 'logagr2': array([-6.62320832]), 'loga2': array([-3.52099305])} paramsSF= params cf= covarFunc(**params) params_covar= (cf) ndata= len(g) if constraints is None: listx= [(t,'g') for t in mjd_g] listx.extend([(t,'r') for t in mjd_r]) listy= [m for m in g] listy.extend([m for m in r]) listy= nu.array(listy) noise= [m for m in err_g] noise.extend([m for m in err_r]) noise= nu.array(noise) trainSet= trainingSet(listx=listx,listy=listy,noise=noise) constraints= trainSet else: constraints= nu.array([]) useconstraints= constraints txs= nu.linspace(-0.1,6.5,nx) xs= [(txs[ii],'g') for ii in range(nx)] xs.extend([(txs[ii],'r') for ii in range(nx)]) GPsamples= eval_gp(xs,mean,covar,(),params_covar,nGP=nGP,constraints=useconstraints,tiny_cholesky=.00000001) thismean= calc_constrained_mean(xs,mean,params_mean,covar,params_covar,useconstraints) thiscovar= calc_constrained_covar(xs,covar,params_covar,useconstraints) #Calculate loglike if isinstance(constraints,trainingSet): (params,packing)= pack_params(cf) covarFuncName= inspect.getmodule(cf).__name__ thisCovarClass= __import__(covarFuncName) loglike= marginalLikelihood(params,constraints,packing, thisCovarClass) plot.bovy_print() pyplot.plot(txs,GPsamples[0,:nx],'-',color='0.25') if isinstance(constraints,trainingSet): pyplot.plot(mjd_g,g,'k.',zorder=5,ms=10) title= re.split(r'_',basefilename)[0] if covarType == 'SF': method= '\mathrm{power-law\ structure\ functions}' else: method= '\mathrm{damped\ random\ walk}' plot.bovy_text(r'$\mathrm{'+title+'\ / \ '+method+r'}$',title=True) if isinstance(constraints,trainingSet): plot.bovy_text(r'$\log P({\bf x}|\mathrm{parameters}) = %5.2f$' %(-loglike), top_left=True) #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75') for ii in range(1,nGP): pyplot.plot(txs,GPsamples[ii,:nx],'-',color=str(0.25+ii*.5/(nGP-1))) #pyplot.plot(txs,thismean[:nx],'k-',linewidth=2) if isinstance(constraints,trainingSet): pyplot.errorbar(6.15,-0.25,yerr=meanErr_g,color='k') pyplot.xlabel(r'$\mathrm{MJD-constant}\ [\mathrm{yr}]$') pyplot.ylabel(r'$g-\langle g\rangle\ [\mathrm{mag}]$') pyplot.xlim(-0.1,6.5) pyplot.ylim(-0.6,0.6) plot._add_ticks() plot.bovy_end_print(basefilename+'_fullg.ps') plot.bovy_print() pyplot.figure() pyplot.plot(txs,GPsamples[0,nx-1:-1],'-',color='0.25') if isinstance(constraints,trainingSet): pyplot.plot(mjd_r,r,'k.',zorder=5,ms=10) #plot.bovy_text(r'$\mathrm{'+title+'\ / \ '+method+r'}$',title=True) #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75') for ii in range(1,nGP): pyplot.plot(txs,GPsamples[ii,nx-1:-1],'-',color=str(0.25+ii*.5/(nGP-1))) #pyplot.plot(txs,thismean[nx-1:-1],'k-',linewidth=2) if isinstance(constraints,trainingSet): pyplot.errorbar(6.15,-0.25,yerr=meanErr_r,color='k') pyplot.xlabel(r'$\mathrm{MJD-constant}\ [\mathrm{yr}]$') pyplot.ylabel(r'$r-\langle r\rangle\ [\mathrm{mag}]$') pyplot.xlim(-0.1,6.5) pyplot.ylim(-0.6,0.6) plot._add_ticks() plot.bovy_end_print(basefilename+'_fullr.ps') plot.bovy_print() pyplot.figure() ii= 0 colors= nu.array([GPsamples[ii,jj]-GPsamples[ii,jj+nx] for jj in range(nx)]) if not isinstance(constraints,trainingSet): colors= colors-nu.mean(colors) pyplot.plot(txs,colors,'-',color='0.25') if isinstance(constraints,trainingSet): plot.bovy_plot(mjd_g,g-r,'k.',zorder=5,ms=10,overplot=True) #plot.bovy_text(r'$\mathrm{'+basefilename+r'}$',title=True) #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75') for ii in range(1,nGP): colors= nu.array([GPsamples[ii,jj]-GPsamples[ii,jj+nx] for jj in range(nx)]) if not isinstance(constraints,trainingSet): colors= colors-nu.mean(colors) pyplot.plot(txs,colors,'-',color=str(0.25+ii*.5/(nGP-1))) plotthismean= nu.zeros(nx) for ii in range(nx): plotthismean[ii]= thismean[ii]-thismean[ii+nx] #pyplot.plot(txs,plotthismean,'k-',linewidth=2) if isinstance(constraints,trainingSet): pyplot.errorbar(6.15,-0.18,yerr=meanErr_gr,color='k') pyplot.xlabel(r'$\mathrm{MJD-constant}\ [\mathrm{yr}]$') pyplot.ylabel(r'$g-r- \langle g - r \rangle\ [\mathrm{mag}]$') pyplot.xlim(-0.1,6.5) if isinstance(constraints,trainingSet): pyplot.ylim(-0.25,.25) else: pass #pyplot.ylim(-10.,10.) plot._add_ticks() plot.bovy_end_print(basefilename+'_color.ps') if covarType == 'DRW': return #Plot structure functions #g plot.bovy_print() pyplot.figure() for ii in range(nGP): thisSample= GPsamples[ii,:nx] pyplot.loglog(sc.arange(1.,len(thisSample)/2)*(txs[1]-txs[0]), 2.*sc.var(thisSample)\ -2.*sc.correlate(thisSample-sc.mean(thisSample),thisSample-sc.mean(thisSample),"same")[1:len(thisSample)/2][::-1]/len(thisSample), color=str(0.25+ii*.5/(nGP-1))) xline= [(txs[1]-txs[0]),txs[len(txs)/2]] pyplot.loglog(xline,(nu.exp(paramsSF['logGamma'])*nu.array(xline))**(paramsSF['gamma']),'k--') pyplot.xlabel(r'$\Delta t\ [\mathrm{yr}]$') pyplot.ylabel(r'$\mathrm{structure\ function\ in}\ g$') #plot.bovy_text(r'$\mathrm{SDSSJ203817.37+003029.8}$',title=True) plot.bovy_end_print(basefilename+'_structfuncg.ps') #r plot.bovy_print() pyplot.figure() for ii in range(nGP): thisSample= GPsamples[ii,nx-1:-1] pyplot.loglog(sc.arange(1.,len(thisSample)/2)*(txs[1]-txs[0]), 2.*sc.var(thisSample)\ -2.*sc.correlate(thisSample-sc.mean(thisSample),thisSample-sc.mean(thisSample),"same")[1:len(thisSample)/2][::-1]/len(thisSample), color=str(0.25+ii*.5/(nGP-1))) xline= [(txs[1]-txs[0]),txs[len(txs)/2]] pyplot.loglog(xline,(nu.exp(paramsSF['logGamma'])*nu.array(xline))**(paramsSF['gamma']),'k--') pyplot.xlabel(r'$\Delta t\ [\mathrm{yr}]$') pyplot.ylabel(r'$\mathrm{structure\ function\ in}\ r$') #plot.bovy_text(r'$\mathrm{SDSSJ203817.37+003029.8}$',title=True) plot.bovy_end_print(basefilename+'_structfuncr.ps') #g-r plot.bovy_print() pyplot.figure() for ii in range(nGP): thisSample= nu.array([GPsamples[ii,jj]-GPsamples[ii,jj+nx] for jj in range(nx)]) pyplot.loglog(sc.arange(1.,len(thisSample)/2)*(txs[1]-txs[0]), 2.*sc.var(thisSample)\ -2.*sc.correlate(thisSample-sc.mean(thisSample),thisSample-sc.mean(thisSample),"same")[1:len(thisSample)/2][::-1]/len(thisSample), color=str(0.25+ii*.5/(nGP-1))) xline= [(txs[1]-txs[0]),txs[len(txs)/2]] pyplot.loglog(xline,(nu.exp(paramsSF['logGammagr'])*nu.array(xline))**(paramsSF['gammagr']),'k--') pyplot.xlabel(r'$\Delta t\ [\mathrm{yr}]$') pyplot.ylabel(r'$\mathrm{color\ structure\ function}$') #plot.bovy_text(r'$\mathrm{SDSSJ203817.37+003029.8}$',title=True) plot.bovy_end_print(basefilename+'_structfuncgr.ps')
def showExamplegr(filename='../data/SDSSJ203817.37+003029.8.fits', constraints=None,basefilename='SDSSJ203817.37+003029.8'): """ NAME: showExamplegr PURPOSE: show an example of a power-law structure function GP covariance function fit to an SDSS quasar for g and r INPUT: filename - filename with the data constraints - if None, use all constraints, if [] use no constraints! basefilename - basefilename for plots OUTPUT: writes several plots HISTORY: 2010-08-11 - Written - Bovy (NYU) """ file= fu.table_fields(filename) mjd_g= nu.array(file.mjd_g)/365.25 g= nu.array(file.g) err_g= nu.array(file.err_g) mjd_r= nu.array(file.mjd_r)/365.25 r= nu.array(file.r) err_r= nu.array(file.err_r) mask= (mjd_g != 0)*(g < 20.6)*(g > 19.7)#Adjust for non-g g= g[mask] g-= nu.mean(g) err_g= err_g[mask] mjd_g= mjd_g[mask] mjd_g-= nu.amin(mjd_g) meanErr_g= nu.mean(err_g) r= r[mask] r-= nu.mean(r) err_r= err_r[mask] mjd_r= mjd_r[mask] mjd_r-= nu.amin(mjd_r) meanErr_r= nu.mean(err_r) meanErr_gr= nu.mean(nu.sqrt(err_r**2.+err_g**2.)) nu.random.seed(4) nGP=5 nx=201 params_mean= () from powerlawSFgr import covarFunc params= {'logGamma': array([-9.27821954]), 'logGammagr': array([-19.85172122]), 'gamma': array([ 0.45932629]), 'gammagr': array([ 0.29784021])} cf= covarFunc(**params) params_covar= (cf) ndata= len(g) if constraints is None: listx= [(t,'g') for t in mjd_g] listx.extend([(t,'r') for t in mjd_r]) listy= [m for m in g] listy.extend([m for m in r]) listy= nu.array(listy) noise= [m for m in err_g] noise.extend([m for m in err_r]) noise= nu.array(noise) trainSet= trainingSet(listx=listx,listy=listy,noise=noise) constraints= trainSet else: constraints= nu.array([]) useconstraints= constraints txs= nu.linspace(-0.1,6.5,nx) xs= [(txs[ii],'g') for ii in range(nx)] xs.extend([(txs[ii],'r') for ii in range(nx)]) GPsamples= eval_gp(xs,mean,covar,(),params_covar,nGP=nGP,constraints=useconstraints,tiny_cholesky=.000001) thismean= calc_constrained_mean(xs,mean,params_mean,covar,params_covar,useconstraints) thiscovar= calc_constrained_covar(xs,covar,params_covar,useconstraints) plot.bovy_print() pyplot.plot(txs,GPsamples[0,:nx],'-',color='0.25') if isinstance(constraints,trainingSet): pyplot.plot(mjd_g,g,'k.',zorder=5,ms=10) plot.bovy_text(r'$\mathrm{'+basefilename+r'}$',title=True) #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75') for ii in range(1,nGP): pyplot.plot(txs,GPsamples[ii,:nx],'-',color=str(0.25+ii*.5/(nGP-1))) pyplot.plot(txs,thismean[:nx],'k-',linewidth=2) if isinstance(constraints,trainingSet): pyplot.errorbar(6.15,-0.25,yerr=meanErr_g,color='k') pyplot.xlabel(r'$\mathrm{MJD-constant}\ [\mathrm{yr}]$') pyplot.ylabel(r'$g-\langle g\rangle\ [\mathrm{mag}]$') pyplot.xlim(-0.1,6.5) plot._add_ticks() plot.bovy_end_print(basefilename+'_full.ps') plot.bovy_print() pyplot.figure() pyplot.plot(txs,GPsamples[0,nx-1:-1],'-',color='0.25') if isinstance(constraints,trainingSet): pyplot.plot(mjd_r,r,'k.',zorder=5,ms=10) plot.bovy_text(r'$\mathrm{'+basefilename+r'}$',title=True) #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75') for ii in range(1,nGP): pyplot.plot(txs,GPsamples[ii,nx-1:-1],'-',color=str(0.25+ii*.5/(nGP-1))) pyplot.plot(txs,thismean[nx-1:-1],'k-',linewidth=2) if isinstance(constraints,trainingSet): pyplot.errorbar(6.15,-0.25,yerr=meanErr_r,color='k') pyplot.xlabel(r'$\mathrm{MJD-constant}\ [\mathrm{yr}]$') pyplot.ylabel(r'$r-\langle r\rangle\ [\mathrm{mag}]$') pyplot.xlim(-0.1,6.5) plot._add_ticks() plot.bovy_end_print(basefilename+'_fullr.ps') plot.bovy_print() pyplot.figure() ii= 0 colors= nu.array([GPsamples[ii,jj]-GPsamples[ii,jj+nx] for jj in range(nx)]) colors= colors-nu.mean(colors) pyplot.plot(txs,colors,'-',color='0.25') if isinstance(constraints,trainingSet): plot.bovy_plot(mjd_g,g-r,'k.',zorder=5,ms=10,overplot=True) plot.bovy_text(r'$\mathrm{'+basefilename+r'}$',title=True) #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75') for ii in range(1,nGP): colors= nu.array([GPsamples[ii,jj]-GPsamples[ii,jj+nx] for jj in range(nx)]) colors= colors-nu.mean(colors) pyplot.plot(txs,colors,'-',color=str(0.25+ii*.5/(nGP-1))) plotthismean= nu.zeros(nx) for ii in range(nx): plotthismean[ii]= thismean[ii]-thismean[ii+nx] pyplot.plot(txs,plotthismean,'k-',linewidth=2) if isinstance(constraints,trainingSet): pyplot.errorbar(6.15,-0.18,yerr=meanErr_gr,color='k') pyplot.xlabel(r'$\mathrm{MJD-constant}\ [\mathrm{yr}]$') pyplot.ylabel(r'$g-r- \langle g - r \rangle\ [\mathrm{mag}]$') pyplot.xlim(-0.1,6.5) if isinstance(constraints,trainingSet): pyplot.ylim(-0.25,.25) else: pass #pyplot.ylim(-10.,10.) plot._add_ticks() plot.bovy_end_print(basefilename+'_color.ps') plot.bovy_print() pyplot.figure() ii= 2 colors= nu.array([GPsamples[ii,jj]-GPsamples[ii,jj+nx] for jj in range(nx)]) pyplot.plot(colors,GPsamples[ii,:nx],'-',color='0.25') if isinstance(constraints,trainingSet): pyplot.plot(g-r,g,'k.',zorder=5,ms=10) plot.bovy_text(r'$\mathrm{'+basefilename+r'}$',title=True) #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75') for ii in range(1,nGP): colors= nu.array([GPsamples[ii,jj]-GPsamples[ii,jj+nx] for jj in range(nx)]) pyplot.plot(colors,GPsamples[ii,0:nx],'-',color=str(0.25+ii*.5/(nGP-1))) colors= nu.array([thismean[jj]-thismean[jj+nx] for jj in range(nx)]) pyplot.plot(colors,thismean[:nx],'k-',linewidth=2) if isinstance(constraints,trainingSet): pyplot.errorbar(.13,-0.3,yerr=meanErr_g,xerr=meanErr_gr,color='k') pyplot.xlabel(r'$g-r-\langle g-r\rangle\ [\mathrm{mag}]$') pyplot.ylabel(r'$g-\langle g\rangle\ [\mathrm{mag}]$') pyplot.xlim(-.2,.2) plot._add_ticks() plot.bovy_end_print(basefilename+'_ggr.ps') return plot.bovy_print() pyplot.plot(xs,GPsamples[0,:],'-',color='0.25') if not constraints == []: plot.bovy_plot(mjd_g,g,'k.',zorder=5,ms=10,overplot=True) #plot.bovy_text(r'$\mathrm{SDSSJ203817.37+003029.8}$',title=True) #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75') for ii in range(1,nGP): pyplot.plot(xs,GPsamples[ii,:],'-',color=str(0.25+ii*.5/(nGP-1))) #pyplot.plot(xs,thismean,'k-',linewidth=2) if not constraints == []: pyplot.errorbar(6.15,-0.1,yerr=meanErr_g,color='k') pyplot.xlabel(r'$\mathrm{MJD-constant}\ [\mathrm{yr}]$') pyplot.ylabel(r'$m-\langle m\rangle\ [\mathrm{mag}]$') pyplot.xlim(3,6.5) plot._add_ticks() plot.bovy_end_print(basefilename+'_zoom.ps') plot.bovy_print() pyplot.figure() for ii in range(nGP): pyplot.loglog(sc.arange(1.,len(GPsamples[ii,:])/2)*(xs[1]-xs[0]), 2.*sc.var(GPsamples[ii,:])-2.*sc.correlate(GPsamples[ii,:]-sc.mean(GPsamples[ii,:]),GPsamples[ii,:]-sc.mean(GPsamples[ii,:]),"same")[1:len(GPsamples[ii,:])/2][::-1]/len(GPsamples[ii,:]), color=str(0.25+ii*.5/(nGP-1))) xline= [(xs[1]-xs[0]),xs[len(xs)/2]] pyplot.loglog(xline,nu.exp(-3.02045715)*nu.array(xline)**(0.5868293),'k--') pyplot.xlabel(r'$\Delta t\ [\mathrm{yr}]$') pyplot.ylabel(r'$\mathrm{structure\ function}$') #plot.bovy_text(r'$\mathrm{SDSSJ203817.37+003029.8}$',title=True) plot.bovy_end_print(basefilename+'_structfunc.ps')
def showExampleDRW(filename='../data/SDSSJ203817.37+003029.8.fits', constraints=None,basefilename='SDSSJ203817.37+003029.8'): """ NAME: showExampleDRW PURPOSE: show an example of a pDRW structure function GP covariance function fit to an SDSS quasar for g and r INPUT: filename - filename with the data constraints - if None, use all constraints, if [] use no constraints! basefilename - basefilename for plots OUTPUT: writes several plots HISTORY: 2010-08-11 - Written - Bovy (NYU) """ file= fu.table_fields(filename) mjd_g= nu.array(file.mjd_g)/365.25 g= nu.array(file.g) err_g= nu.array(file.err_g) mjd_r= nu.array(file.mjd_r)/365.25 r= nu.array(file.r) err_r= nu.array(file.err_r) mask= (mjd_g != 0)*(g < 20.6)*(g > 19.7)#Adjust for non-g g= g[mask] g-= nu.mean(g) err_g= err_g[mask] mjd_g= mjd_g[mask] mjd_g-= nu.amin(mjd_g) meanErr_g= nu.mean(err_g) r= r[mask] r-= nu.mean(r) err_r= err_r[mask] mjd_r= mjd_g#mjd_r[mask] #mjd_r-= nu.amin(mjd_r) meanErr_r= nu.mean(err_r) meanErr_gr= nu.mean(nu.sqrt(err_r**2.+err_g**2.)) nu.random.seed(4) nGP=5 nx=201 params_mean= () from drwcross import covarFunc #params= {'logS': array([1.63714183]), 'logtau': array([0.]), 'logB': array([ 2.32288434])} #params= {'logtau': array([-1.36399325]), 'logB': array([-519.45950737]), 'logS': array([-3.51531852])} params= {'logS': array([-1.08795813]), 'logtau': array([ 1.2790154]), 'logB': array([-1.01029709]), 'logC': array([-79.88349478]), 'gammagr': array([ 7.90536799]), 'logGammagr': array([ 2.8001978])} cf= covarFunc(**params) params_covar= (cf) ndata= len(g) if constraints is None: listx= [(t,'g') for t in mjd_g] listx.extend([(t,'r') for t in mjd_r]) listy= [m for m in g] listy.extend([m for m in r]) listy= nu.array(listy) noise= [m for m in err_g] noise.extend([m for m in err_r]) noise= nu.array(noise) trainSet= trainingSet(listx=listx,listy=listy,noise=noise) constraints= trainSet else: constraints= nu.array([]) useconstraints= constraints txs= nu.linspace(-0.1,6.5,nx) xs= [(txs[ii],'g') for ii in range(nx)] xs.extend([(txs[ii],'r') for ii in range(nx)]) GPsamples= eval_gp(xs,mean,covar,(),params_covar,nGP=nGP,constraints=useconstraints,tiny_cholesky=.000001) thismean= calc_constrained_mean(xs,mean,params_mean,covar,params_covar,useconstraints) thiscovar= calc_constrained_covar(xs,covar,params_covar,useconstraints) plot.bovy_print() pyplot.plot(txs,GPsamples[0,:nx],'-',color='0.25') if isinstance(constraints,trainingSet): pyplot.plot(mjd_g,g,'k.',zorder=5,ms=10) plot.bovy_text(r'$\mathrm{'+basefilename+r'}$',title=True) #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75') for ii in range(1,nGP): pyplot.plot(txs,GPsamples[ii,:nx],'-',color=str(0.25+ii*.5/(nGP-1))) pyplot.plot(txs,thismean[:nx],'k-',linewidth=2) if isinstance(constraints,trainingSet): pyplot.errorbar(6.15,-0.25,yerr=meanErr_g,color='k') pyplot.xlabel(r'$\mathrm{MJD-constant}\ [\mathrm{yr}]$') pyplot.ylabel(r'$g-\langle g\rangle\ [\mathrm{mag}]$') pyplot.xlim(-0.1,6.5) plot._add_ticks() plot.bovy_end_print(basefilename+'_full.ps') plot.bovy_print() pyplot.figure() pyplot.plot(txs,GPsamples[0,nx-1:-1],'-',color='0.25') if isinstance(constraints,trainingSet): pyplot.plot(mjd_r,r,'k.',zorder=5,ms=10) plot.bovy_text(r'$\mathrm{'+basefilename+r'}$',title=True) #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75') for ii in range(1,nGP): pyplot.plot(txs,GPsamples[ii,nx-1:-1],'-',color=str(0.25+ii*.5/(nGP-1))) pyplot.plot(txs,thismean[nx-1:-1],'k-',linewidth=2) if isinstance(constraints,trainingSet): pyplot.errorbar(6.15,-0.25,yerr=meanErr_r,color='k') pyplot.xlabel(r'$\mathrm{MJD-constant}\ [\mathrm{yr}]$') pyplot.ylabel(r'$r-\langle r\rangle\ [\mathrm{mag}]$') pyplot.xlim(-0.1,6.5) plot._add_ticks() plot.bovy_end_print(basefilename+'_fullr.ps') plot.bovy_print() pyplot.figure() ii= 0 colors= nu.array([GPsamples[ii,jj]-GPsamples[ii,jj+nx] for jj in range(nx)]) colors= colors-nu.mean(colors) pyplot.plot(txs,colors,'-',color='0.25') if isinstance(constraints,trainingSet): plot.bovy_plot(mjd_g,g-r,'k.',zorder=5,ms=10,overplot=True) plot.bovy_text(r'$\mathrm{'+basefilename+r'}$',title=True) #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75') for ii in range(1,nGP): colors= nu.array([GPsamples[ii,jj]-GPsamples[ii,jj+nx] for jj in range(nx)]) colors= colors-nu.mean(colors) pyplot.plot(txs,colors,'-',color=str(0.25+ii*.5/(nGP-1))) plotthismean= nu.zeros(nx) for ii in range(nx): plotthismean[ii]= thismean[ii]-thismean[ii+nx] pyplot.plot(txs,plotthismean,'k-',linewidth=2) if isinstance(constraints,trainingSet): pyplot.errorbar(6.15,-0.18,yerr=meanErr_gr,color='k') pyplot.xlabel(r'$\mathrm{MJD-constant}\ [\mathrm{yr}]$') pyplot.ylabel(r'$g-r- \langle g - r \rangle\ [\mathrm{mag}]$') pyplot.xlim(-0.1,6.5) if isinstance(constraints,trainingSet): pyplot.ylim(-0.25,.25) else: pass #pyplot.ylim(-10.,10.) plot._add_ticks() plot.bovy_end_print(basefilename+'_color.ps') plot.bovy_print() pyplot.figure() ii= 2 colors= nu.array([GPsamples[ii,jj]-GPsamples[ii,jj+nx] for jj in range(nx)]) pyplot.plot(colors,GPsamples[ii,:nx],'-',color='0.25') if isinstance(constraints,trainingSet): pyplot.plot(g-r,g,'k.',zorder=5,ms=10) plot.bovy_text(r'$\mathrm{'+basefilename+r'}$',title=True) #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75') for ii in range(1,nGP): colors= nu.array([GPsamples[ii,jj]-GPsamples[ii,jj+nx] for jj in range(nx)]) pyplot.plot(colors,GPsamples[ii,0:nx],'-',color=str(0.25+ii*.5/(nGP-1))) colors= nu.array([thismean[jj]-thismean[jj+nx] for jj in range(nx)]) pyplot.plot(colors,thismean[:nx],'k-',linewidth=2) if isinstance(constraints,trainingSet): pyplot.errorbar(.13,-0.3,yerr=meanErr_g,xerr=meanErr_gr,color='k') pyplot.xlabel(r'$g-r-\langle g-r\rangle\ [\mathrm{mag}]$') pyplot.ylabel(r'$g-\langle g\rangle\ [\mathrm{mag}]$') pyplot.xlim(-.2,.2) plot._add_ticks() plot.bovy_end_print(basefilename+'_ggr.ps') return
def singleBandExample(filename='../data/SDSSJ203817.37+003029.8.fits',band='g', constraints=None,basefilename='SDSSJ203817.37+003029.8', covarType='SF'): """ NAME: singleBandExample PURPOSE: show an example of a power-law structure function or damped-random walk GP covariance function fit to an SDSS quasar INPUT: filename - filename with the data band - band to consider constraints - if None, use all constraints, if [] use no constraints! basefilename - basefilename for plots covarType - 'SF' or 'DRW' OUTPUT: writes several plots HISTORY: 2010-06-20 - Written - Bovy (NYU) """ file= fu.table_fields(filename) if band == 'u': mjd_g= nu.array(file.mjd_u)/365.25 g= nu.array(file.u) err_g= nu.array(file.err_u) elif band == 'g': mjd_g= nu.array(file.mjd_g)/365.25 g= nu.array(file.g) err_g= nu.array(file.err_g) elif band == 'r': mjd_g= nu.array(file.mjd_r)/365.25 g= nu.array(file.r) err_g= nu.array(file.err_r) elif band == 'i': mjd_g= nu.array(file.mjd_i)/365.25 g= nu.array(file.i) err_g= nu.array(file.err_i) elif band == 'z': mjd_g= nu.array(file.mjd_z)/365.25 g= nu.array(file.z) err_g= nu.array(file.err_z) mask= (mjd_g != 0)*(g < 20.6)*(g > 19.7)#Adjust for non-g g= g[mask] g-= nu.mean(g) err_g= err_g[mask] mjd_g= mjd_g[mask] mjd_g-= nu.amin(mjd_g) meanErr_g= nu.mean(err_g) nu.random.seed(4) nGP=5 nx=201 params_mean= () if covarType == 'SF': from powerlawSF import covarFunc params= {'gamma': array([ 0.49500723]), 'logA': array([-3.36044037])} else: from OU_ARD import covarFunc params= {'logl': array([-1.37742591]), 'loga2': array([-3.47341754])} cf= covarFunc(**params) params_covar= (cf) ndata= len(g) if constraints is None: listx= mjd_g listy= g noise= err_g trainSet= trainingSet(listx=listx,listy=listy,noise=noise) constraints= trainSet else: constraints= None useconstraints= constraints xs= nu.linspace(-0.1,6.5,nx) GPsamples= eval_gp(xs,mean,covar,(),params_covar,nGP=nGP,constraints=useconstraints) thismean= calc_constrained_mean(xs,mean,params_mean,covar,params_covar,useconstraints) thiscovar= calc_constrained_covar(xs,covar,params_covar,useconstraints) #If unconstrained, subtract the mean if constraints is None: for ii in range(nGP): GPsamples[ii,:]= GPsamples[ii,:]-nu.mean(GPsamples[ii,:]) #Calculate loglike if not constraints is None: (params,packing)= pack_params(cf) covarFuncName= inspect.getmodule(cf).__name__ thisCovarClass= __import__(covarFuncName) loglike= marginalLikelihood(params,constraints,packing, thisCovarClass) plot.bovy_print() pyplot.plot(xs,GPsamples[0,:],'-',color='0.25') if not constraints is None: plot.bovy_plot(mjd_g,g,'k.',zorder=5,ms=10,overplot=True) title= re.split(r'_',basefilename)[0] if covarType == 'SF': method= '\mathrm{power-law\ structure\ function}' else: method= '\mathrm{damped\ random\ walk}' plot.bovy_text(r'$\mathrm{'+title+'\ / \ '+method+r'}$',title=True) if not constraints is None: plot.bovy_text(r'$\log P({\bf x}|\mathrm{parameters}) = %5.2f$' %(-loglike), top_left=True) #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75') for ii in range(nGP): pyplot.plot(xs,GPsamples[ii,:],'-',color=str(0.25+ii*.5/(nGP-1))) #pyplot.plot(xs,thismean,'k-',linewidth=2) if not constraints is None: pyplot.errorbar(6.15,-0.25,yerr=meanErr_g,color='k') pyplot.xlabel(r'$\mathrm{MJD-constant}\ [\mathrm{yr}]$') pyplot.ylabel(r'$'+band+r'-\langle '+band+r' \rangle\ [\mathrm{mag}]$') pyplot.xlim(-0.1,6.5) if constraints is None: pyplot.ylim(-.6,.6) else: pyplot.ylim(-.6,.6) plot._add_ticks() plot.bovy_end_print(basefilename+'_full.ps') plot.bovy_print() pyplot.plot(xs,GPsamples[0,:],'-',color='0.25') if not constraints is None: plot.bovy_plot(mjd_g,g,'k.',zorder=5,ms=10,overplot=True) #plot.bovy_text(r'$\mathrm{SDSSJ203817.37+003029.8}$',title=True) #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75') for ii in range(1,nGP): pyplot.plot(xs,GPsamples[ii,:],'-',color=str(0.25+ii*.5/(nGP-1))) #pyplot.plot(xs,thismean,'k-',linewidth=2) if not constraints == []: pyplot.errorbar(6.15,-0.25,yerr=meanErr_g,color='k') pyplot.xlabel(r'$\mathrm{MJD-constant}\ [\mathrm{yr}]$') pyplot.ylabel(r'$'+band+'-\langle '+band+'\rangle\ [\mathrm{mag}]$') pyplot.xlim(3,6.5) plot._add_ticks() plot.bovy_end_print(basefilename+'_zoom.ps') plot.bovy_print() pyplot.figure() for ii in range(nGP): pyplot.loglog(sc.arange(1.,len(GPsamples[ii,:])/2)*(xs[1]-xs[0]), 2.*sc.var(GPsamples[ii,:])-2.*sc.correlate(GPsamples[ii,:]-sc.mean(GPsamples[ii,:]),GPsamples[ii,:]-sc.mean(GPsamples[ii,:]),"same")[1:len(GPsamples[ii,:])/2][::-1]/len(GPsamples[ii,:]), color=str(0.25+ii*.5/(nGP-1))) xline= [(xs[1]-xs[0]),xs[len(xs)/2]] pyplot.loglog(xline,nu.exp(-3.36044037)*nu.array(xline)**(0.49500723),'k--') pyplot.xlabel(r'$\Delta t\ [\mathrm{yr}]$') pyplot.ylabel(r'$\mathrm{structure\ function}$') #plot.bovy_text(r'$\mathrm{SDSSJ203817.37+003029.8}$',title=True) plot.bovy_end_print(basefilename+'_structfunc.ps')
def showExample(filename='../data/SDSSJ203817.37+003029.8.fits',band='g', constraints=None,basefilename='SDSSJ203817.37+003029.8'): """ NAME: showExample PURPOSE: show an example of a power-law structure function GP covariance function fit to an SDSS quasar INPUT: filename - filename with the data band - band to consider constraints - if None, use all constraints, if [] use no constraints! basefilename - basefilename for plots OUTPUT: writes several plots HISTORY: 2010-06-20 - Written - Bovy (NYU) """ file= fu.table_fields(filename) if band == 'u': mjd_g= nu.array(file.mjd_u)/365.25 g= nu.array(file.u) err_g= nu.array(file.err_u) elif band == 'g': mjd_g= nu.array(file.mjd_g)/365.25 g= nu.array(file.g) err_g= nu.array(file.err_g) elif band == 'r': mjd_g= nu.array(file.mjd_r)/365.25 g= nu.array(file.r) err_g= nu.array(file.err_r) elif band == 'i': mjd_g= nu.array(file.mjd_i)/365.25 g= nu.array(file.i) err_g= nu.array(file.err_i) elif band == 'z': mjd_g= nu.array(file.mjd_z)/365.25 g= nu.array(file.z) err_g= nu.array(file.err_z) mask= (mjd_g != 0)*(g < 20.6)*(g > 19.7)#Adjust for non-g g= g[mask] g-= nu.mean(g) err_g= err_g[mask] mjd_g= mjd_g[mask] mjd_g-= nu.amin(mjd_g) meanErr_g= nu.mean(err_g) nu.random.seed(4) nGP=5 nx=201 params_mean= () from powerlawSFgr import covarFunc params= {'logA': array([ 1.63714183]), 'gamma': array([ 0.60216581])} params= {'gamma': array([ 0.49500723]), 'logA': array([-3.36044037])} cf= covarFunc(**params) params_covar= (cf) ndata= len(g) if constraints is None: listx= mjd_g listy= g noise= err_g trainSet= trainingSet(listx=listx,listy=listy,noise=noise) constraints= trainSet else: constraints= None useconstraints= constraints xs= nu.linspace(-0.1,6.5,nx) GPsamples= eval_gp(xs,mean,covar,(),params_covar,nGP=nGP,constraints=useconstraints) thismean= calc_constrained_mean(xs,mean,params_mean,covar,params_covar,useconstraints) thiscovar= calc_constrained_covar(xs,covar,params_covar,useconstraints) plot.bovy_print() pyplot.plot(xs,GPsamples[0,:],'-',color='0.25') if not constraints is None: plot.bovy_plot(mjd_g,g,'k.',zorder=5,ms=10,overplot=True) plot.bovy_text(r'$\mathrm{'+basefilename+r'}$',title=True) #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75') for ii in range(nGP): pyplot.plot(xs,GPsamples[ii,:],'-',color=str(0.25+ii*.5/(nGP-1))) #pyplot.plot(xs,thismean,'k-',linewidth=2) if not constraints == []: pyplot.errorbar(6.15,-0.25,yerr=meanErr_g,color='k') pyplot.xlabel(r'$\mathrm{MJD-constant}\ [\mathrm{yr}]$') pyplot.ylabel(r'$m-\langle m\rangle\ [\mathrm{mag}]$') pyplot.xlim(-0.1,6.5) pyplot.ylim(-1.,1.) plot._add_ticks() plot.bovy_end_print(basefilename+'_full.ps') plot.bovy_print() pyplot.plot(xs,GPsamples[0,:],'-',color='0.25') if not constraints is None: plot.bovy_plot(mjd_g,g,'k.',zorder=5,ms=10,overplot=True) #plot.bovy_text(r'$\mathrm{SDSSJ203817.37+003029.8}$',title=True) #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75') for ii in range(1,nGP): pyplot.plot(xs,GPsamples[ii,:],'-',color=str(0.25+ii*.5/(nGP-1))) #pyplot.plot(xs,thismean,'k-',linewidth=2) if not constraints == []: pyplot.errorbar(6.15,-0.25,yerr=meanErr_g,color='k') pyplot.xlabel(r'$\mathrm{MJD-constant}\ [\mathrm{yr}]$') pyplot.ylabel(r'$m-\langle m\rangle\ [\mathrm{mag}]$') pyplot.xlim(3,6.5) plot._add_ticks() plot.bovy_end_print(basefilename+'_zoom.ps') plot.bovy_print() pyplot.figure() for ii in range(nGP): pyplot.loglog(sc.arange(1.,len(GPsamples[ii,:])/2)*(xs[1]-xs[0]), 2.*sc.var(GPsamples[ii,:])-2.*sc.correlate(GPsamples[ii,:]-sc.mean(GPsamples[ii,:]),GPsamples[ii,:]-sc.mean(GPsamples[ii,:]),"same")[1:len(GPsamples[ii,:])/2][::-1]/len(GPsamples[ii,:]), color=str(0.25+ii*.5/(nGP-1))) xline= [(xs[1]-xs[0]),xs[len(xs)/2]] pyplot.loglog(xline,nu.exp(-3.02045715)*nu.array(xline)**(0.5868293),'k--') pyplot.xlabel(r'$\Delta t\ [\mathrm{yr}]$') pyplot.ylabel(r'$\mathrm{structure\ function}$') #plot.bovy_text(r'$\mathrm{SDSSJ203817.37+003029.8}$',title=True) plot.bovy_end_print(basefilename+'_structfunc.ps')
def dualBandExample(filename='../data/SDSSJ203817.37+003029.8.fits', constraints=None, basefilename='SDSSJ203817.37+003029.8', covarType='SF'): """ NAME: dualBandExample PURPOSE: show an example of a power-law structure function GP covariance function fit to an SDSS quasar for g and r INPUT: filename - filename with the data constraints - if None, use all constraints, if [] use no constraints! basefilename - basefilename for plots covarType - 'SF' or 'DRW' OUTPUT: writes several plots HISTORY: 2010-08-11 - Written - Bovy (NYU) """ file = fu.table_fields(filename) mjd_g = nu.array(file.mjd_g) / 365.25 g = nu.array(file.g) err_g = nu.array(file.err_g) mjd_r = nu.array(file.mjd_r) / 365.25 r = nu.array(file.r) err_r = nu.array(file.err_r) mask = (mjd_g != 0) * (g < 20.6) * (g > 19.7) #Adjust for non-g g = g[mask] g -= nu.mean(g) err_g = err_g[mask] mjd_g = mjd_g[mask] mjd_g -= nu.amin(mjd_g) meanErr_g = nu.mean(err_g) r = r[mask] r -= nu.mean(r) err_r = err_r[mask] mjd_r = mjd_r[mask] mjd_r -= nu.amin(mjd_r) meanErr_r = nu.mean(err_r) meanErr_gr = nu.mean(nu.sqrt(err_r**2. + err_g**2.)) nu.random.seed(4) nGP = 5 nx = 201 params_mean = () if covarType == 'SF': from powerlawSFgr import covarFunc params = { 'logGamma': array([-7.33271548]), 'logGammagr': array([-10.5]), 'gamma': array([0.4821092]), 'gammagr': array([0.5]) } params = { 'logGamma': array([-7.79009776]), 'logGammagr': array([-28.0487848]), 'gamma': array([0.45918053]), 'gammagr': array([0.21333858]) } else: from OUgr import covarFunc params = { 'logl': array([1.94844503]), 'loglgr': array([7.36282174]), 'logagr2': array([1.0196474]), 'loga2': array([-0.00588868]) } params = { 'logl': -1.37742591, 'loga': -3.47341754, 'loglgr': -2.3777, 'logagr': -4. } params = { 'logl': array([-1.38968195]), 'loglgr': array([-2.46684501]), 'logagr2': array([-6.62320832]), 'loga2': array([-3.52099305]) } paramsSF = params cf = covarFunc(**params) params_covar = (cf) ndata = len(g) if constraints is None: listx = [(t, 'g') for t in mjd_g] listx.extend([(t, 'r') for t in mjd_r]) listy = [m for m in g] listy.extend([m for m in r]) listy = nu.array(listy) noise = [m for m in err_g] noise.extend([m for m in err_r]) noise = nu.array(noise) trainSet = trainingSet(listx=listx, listy=listy, noise=noise) constraints = trainSet else: constraints = nu.array([]) useconstraints = constraints txs = nu.linspace(-0.1, 6.5, nx) xs = [(txs[ii], 'g') for ii in range(nx)] xs.extend([(txs[ii], 'r') for ii in range(nx)]) GPsamples = eval_gp(xs, mean, covar, (), params_covar, nGP=nGP, constraints=useconstraints, tiny_cholesky=.00000001) thismean = calc_constrained_mean(xs, mean, params_mean, covar, params_covar, useconstraints) thiscovar = calc_constrained_covar(xs, covar, params_covar, useconstraints) #Calculate loglike if isinstance(constraints, trainingSet): (params, packing) = pack_params(cf) covarFuncName = inspect.getmodule(cf).__name__ thisCovarClass = __import__(covarFuncName) loglike = marginalLikelihood(params, constraints, packing, thisCovarClass) plot.bovy_print() pyplot.plot(txs, GPsamples[0, :nx], '-', color='0.25') if isinstance(constraints, trainingSet): pyplot.plot(mjd_g, g, 'k.', zorder=5, ms=10) title = re.split(r'_', basefilename)[0] if covarType == 'SF': method = '\mathrm{power-law\ structure\ functions}' else: method = '\mathrm{damped\ random\ walk}' plot.bovy_text(r'$\mathrm{' + title + '\ / \ ' + method + r'}$', title=True) if isinstance(constraints, trainingSet): plot.bovy_text(r'$\log P({\bf x}|\mathrm{parameters}) = %5.2f$' % (-loglike), top_left=True) #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75') for ii in range(1, nGP): pyplot.plot(txs, GPsamples[ii, :nx], '-', color=str(0.25 + ii * .5 / (nGP - 1))) #pyplot.plot(txs,thismean[:nx],'k-',linewidth=2) if isinstance(constraints, trainingSet): pyplot.errorbar(6.15, -0.25, yerr=meanErr_g, color='k') pyplot.xlabel(r'$\mathrm{MJD-constant}\ [\mathrm{yr}]$') pyplot.ylabel(r'$g-\langle g\rangle\ [\mathrm{mag}]$') pyplot.xlim(-0.1, 6.5) pyplot.ylim(-0.6, 0.6) plot._add_ticks() plot.bovy_end_print(basefilename + '_fullg.ps') plot.bovy_print() pyplot.figure() pyplot.plot(txs, GPsamples[0, nx - 1:-1], '-', color='0.25') if isinstance(constraints, trainingSet): pyplot.plot(mjd_r, r, 'k.', zorder=5, ms=10) #plot.bovy_text(r'$\mathrm{'+title+'\ / \ '+method+r'}$',title=True) #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75') for ii in range(1, nGP): pyplot.plot(txs, GPsamples[ii, nx - 1:-1], '-', color=str(0.25 + ii * .5 / (nGP - 1))) #pyplot.plot(txs,thismean[nx-1:-1],'k-',linewidth=2) if isinstance(constraints, trainingSet): pyplot.errorbar(6.15, -0.25, yerr=meanErr_r, color='k') pyplot.xlabel(r'$\mathrm{MJD-constant}\ [\mathrm{yr}]$') pyplot.ylabel(r'$r-\langle r\rangle\ [\mathrm{mag}]$') pyplot.xlim(-0.1, 6.5) pyplot.ylim(-0.6, 0.6) plot._add_ticks() plot.bovy_end_print(basefilename + '_fullr.ps') plot.bovy_print() pyplot.figure() ii = 0 colors = nu.array( [GPsamples[ii, jj] - GPsamples[ii, jj + nx] for jj in range(nx)]) if not isinstance(constraints, trainingSet): colors = colors - nu.mean(colors) pyplot.plot(txs, colors, '-', color='0.25') if isinstance(constraints, trainingSet): plot.bovy_plot(mjd_g, g - r, 'k.', zorder=5, ms=10, overplot=True) #plot.bovy_text(r'$\mathrm{'+basefilename+r'}$',title=True) #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75') for ii in range(1, nGP): colors = nu.array( [GPsamples[ii, jj] - GPsamples[ii, jj + nx] for jj in range(nx)]) if not isinstance(constraints, trainingSet): colors = colors - nu.mean(colors) pyplot.plot(txs, colors, '-', color=str(0.25 + ii * .5 / (nGP - 1))) plotthismean = nu.zeros(nx) for ii in range(nx): plotthismean[ii] = thismean[ii] - thismean[ii + nx] #pyplot.plot(txs,plotthismean,'k-',linewidth=2) if isinstance(constraints, trainingSet): pyplot.errorbar(6.15, -0.18, yerr=meanErr_gr, color='k') pyplot.xlabel(r'$\mathrm{MJD-constant}\ [\mathrm{yr}]$') pyplot.ylabel(r'$g-r- \langle g - r \rangle\ [\mathrm{mag}]$') pyplot.xlim(-0.1, 6.5) if isinstance(constraints, trainingSet): pyplot.ylim(-0.25, .25) else: pass #pyplot.ylim(-10.,10.) plot._add_ticks() plot.bovy_end_print(basefilename + '_color.ps') if covarType == 'DRW': return #Plot structure functions #g plot.bovy_print() pyplot.figure() for ii in range(nGP): thisSample = GPsamples[ii, :nx] pyplot.loglog(sc.arange(1.,len(thisSample)/2)*(txs[1]-txs[0]), 2.*sc.var(thisSample)\ -2.*sc.correlate(thisSample-sc.mean(thisSample),thisSample-sc.mean(thisSample),"same")[1:len(thisSample)/2][::-1]/len(thisSample), color=str(0.25+ii*.5/(nGP-1))) xline = [(txs[1] - txs[0]), txs[len(txs) / 2]] pyplot.loglog(xline, (nu.exp(paramsSF['logGamma']) * nu.array(xline))**(paramsSF['gamma']), 'k--') pyplot.xlabel(r'$\Delta t\ [\mathrm{yr}]$') pyplot.ylabel(r'$\mathrm{structure\ function\ in}\ g$') #plot.bovy_text(r'$\mathrm{SDSSJ203817.37+003029.8}$',title=True) plot.bovy_end_print(basefilename + '_structfuncg.ps') #r plot.bovy_print() pyplot.figure() for ii in range(nGP): thisSample = GPsamples[ii, nx - 1:-1] pyplot.loglog(sc.arange(1.,len(thisSample)/2)*(txs[1]-txs[0]), 2.*sc.var(thisSample)\ -2.*sc.correlate(thisSample-sc.mean(thisSample),thisSample-sc.mean(thisSample),"same")[1:len(thisSample)/2][::-1]/len(thisSample), color=str(0.25+ii*.5/(nGP-1))) xline = [(txs[1] - txs[0]), txs[len(txs) / 2]] pyplot.loglog(xline, (nu.exp(paramsSF['logGamma']) * nu.array(xline))**(paramsSF['gamma']), 'k--') pyplot.xlabel(r'$\Delta t\ [\mathrm{yr}]$') pyplot.ylabel(r'$\mathrm{structure\ function\ in}\ r$') #plot.bovy_text(r'$\mathrm{SDSSJ203817.37+003029.8}$',title=True) plot.bovy_end_print(basefilename + '_structfuncr.ps') #g-r plot.bovy_print() pyplot.figure() for ii in range(nGP): thisSample = nu.array( [GPsamples[ii, jj] - GPsamples[ii, jj + nx] for jj in range(nx)]) pyplot.loglog(sc.arange(1.,len(thisSample)/2)*(txs[1]-txs[0]), 2.*sc.var(thisSample)\ -2.*sc.correlate(thisSample-sc.mean(thisSample),thisSample-sc.mean(thisSample),"same")[1:len(thisSample)/2][::-1]/len(thisSample), color=str(0.25+ii*.5/(nGP-1))) xline = [(txs[1] - txs[0]), txs[len(txs) / 2]] pyplot.loglog(xline, (nu.exp(paramsSF['logGammagr']) * nu.array(xline))**(paramsSF['gammagr']), 'k--') pyplot.xlabel(r'$\Delta t\ [\mathrm{yr}]$') pyplot.ylabel(r'$\mathrm{color\ structure\ function}$') #plot.bovy_text(r'$\mathrm{SDSSJ203817.37+003029.8}$',title=True) plot.bovy_end_print(basefilename + '_structfuncgr.ps')
def singleBandExample(filename='../data/SDSSJ203817.37+003029.8.fits', band='g', constraints=None, basefilename='SDSSJ203817.37+003029.8', covarType='SF'): """ NAME: singleBandExample PURPOSE: show an example of a power-law structure function or damped-random walk GP covariance function fit to an SDSS quasar INPUT: filename - filename with the data band - band to consider constraints - if None, use all constraints, if [] use no constraints! basefilename - basefilename for plots covarType - 'SF' or 'DRW' OUTPUT: writes several plots HISTORY: 2010-06-20 - Written - Bovy (NYU) """ file = fu.table_fields(filename) if band == 'u': mjd_g = nu.array(file.mjd_u) / 365.25 g = nu.array(file.u) err_g = nu.array(file.err_u) elif band == 'g': mjd_g = nu.array(file.mjd_g) / 365.25 g = nu.array(file.g) err_g = nu.array(file.err_g) elif band == 'r': mjd_g = nu.array(file.mjd_r) / 365.25 g = nu.array(file.r) err_g = nu.array(file.err_r) elif band == 'i': mjd_g = nu.array(file.mjd_i) / 365.25 g = nu.array(file.i) err_g = nu.array(file.err_i) elif band == 'z': mjd_g = nu.array(file.mjd_z) / 365.25 g = nu.array(file.z) err_g = nu.array(file.err_z) mask = (mjd_g != 0) * (g < 20.6) * (g > 19.7) #Adjust for non-g g = g[mask] g -= nu.mean(g) err_g = err_g[mask] mjd_g = mjd_g[mask] mjd_g -= nu.amin(mjd_g) meanErr_g = nu.mean(err_g) nu.random.seed(4) nGP = 5 nx = 201 params_mean = () if covarType == 'SF': from powerlawSF import covarFunc params = {'gamma': array([0.49500723]), 'logA': array([-3.36044037])} else: from OU_ARD import covarFunc params = {'logl': array([-1.37742591]), 'loga2': array([-3.47341754])} cf = covarFunc(**params) params_covar = (cf) ndata = len(g) if constraints is None: listx = mjd_g listy = g noise = err_g trainSet = trainingSet(listx=listx, listy=listy, noise=noise) constraints = trainSet else: constraints = None useconstraints = constraints xs = nu.linspace(-0.1, 6.5, nx) GPsamples = eval_gp(xs, mean, covar, (), params_covar, nGP=nGP, constraints=useconstraints) thismean = calc_constrained_mean(xs, mean, params_mean, covar, params_covar, useconstraints) thiscovar = calc_constrained_covar(xs, covar, params_covar, useconstraints) #If unconstrained, subtract the mean if constraints is None: for ii in range(nGP): GPsamples[ii, :] = GPsamples[ii, :] - nu.mean(GPsamples[ii, :]) #Calculate loglike if not constraints is None: (params, packing) = pack_params(cf) covarFuncName = inspect.getmodule(cf).__name__ thisCovarClass = __import__(covarFuncName) loglike = marginalLikelihood(params, constraints, packing, thisCovarClass) plot.bovy_print() pyplot.plot(xs, GPsamples[0, :], '-', color='0.25') if not constraints is None: plot.bovy_plot(mjd_g, g, 'k.', zorder=5, ms=10, overplot=True) title = re.split(r'_', basefilename)[0] if covarType == 'SF': method = '\mathrm{power-law\ structure\ function}' else: method = '\mathrm{damped\ random\ walk}' plot.bovy_text(r'$\mathrm{' + title + '\ / \ ' + method + r'}$', title=True) if not constraints is None: plot.bovy_text(r'$\log P({\bf x}|\mathrm{parameters}) = %5.2f$' % (-loglike), top_left=True) #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75') for ii in range(nGP): pyplot.plot(xs, GPsamples[ii, :], '-', color=str(0.25 + ii * .5 / (nGP - 1))) #pyplot.plot(xs,thismean,'k-',linewidth=2) if not constraints is None: pyplot.errorbar(6.15, -0.25, yerr=meanErr_g, color='k') pyplot.xlabel(r'$\mathrm{MJD-constant}\ [\mathrm{yr}]$') pyplot.ylabel(r'$' + band + r'-\langle ' + band + r' \rangle\ [\mathrm{mag}]$') pyplot.xlim(-0.1, 6.5) if constraints is None: pyplot.ylim(-.6, .6) else: pyplot.ylim(-.6, .6) plot._add_ticks() plot.bovy_end_print(basefilename + '_full.ps') plot.bovy_print() pyplot.plot(xs, GPsamples[0, :], '-', color='0.25') if not constraints is None: plot.bovy_plot(mjd_g, g, 'k.', zorder=5, ms=10, overplot=True) #plot.bovy_text(r'$\mathrm{SDSSJ203817.37+003029.8}$',title=True) #pyplot.fill_between(xs,thismean-sc.sqrt(sc.diagonal(thiscovar)),thismean+sc.sqrt(sc.diagonal(thiscovar)),color='.75') for ii in range(1, nGP): pyplot.plot(xs, GPsamples[ii, :], '-', color=str(0.25 + ii * .5 / (nGP - 1))) #pyplot.plot(xs,thismean,'k-',linewidth=2) if not constraints == []: pyplot.errorbar(6.15, -0.25, yerr=meanErr_g, color='k') pyplot.xlabel(r'$\mathrm{MJD-constant}\ [\mathrm{yr}]$') pyplot.ylabel(r'$' + band + '-\langle ' + band + '\rangle\ [\mathrm{mag}]$') pyplot.xlim(3, 6.5) plot._add_ticks() plot.bovy_end_print(basefilename + '_zoom.ps') plot.bovy_print() pyplot.figure() for ii in range(nGP): pyplot.loglog( sc.arange(1., len(GPsamples[ii, :]) / 2) * (xs[1] - xs[0]), 2. * sc.var(GPsamples[ii, :]) - 2. * sc.correlate(GPsamples[ii, :] - sc.mean(GPsamples[ii, :]), GPsamples[ii, :] - sc.mean(GPsamples[ii, :]), "same")[1:len(GPsamples[ii, :]) / 2][::-1] / len(GPsamples[ii, :]), color=str(0.25 + ii * .5 / (nGP - 1))) xline = [(xs[1] - xs[0]), xs[len(xs) / 2]] pyplot.loglog(xline, nu.exp(-3.36044037) * nu.array(xline)**(0.49500723), 'k--') pyplot.xlabel(r'$\Delta t\ [\mathrm{yr}]$') pyplot.ylabel(r'$\mathrm{structure\ function}$') #plot.bovy_text(r'$\mathrm{SDSSJ203817.37+003029.8}$',title=True) plot.bovy_end_print(basefilename + '_structfunc.ps')