def fitSigz(parser): (options,args)= parser.parse_args() if len(args) == 0: parser.print_help() return if os.path.exists(args[0]):#Load savefile savefile= open(args[0],'rb') params= pickle.load(savefile) samples= pickle.load(savefile) savefile.close() if _DEBUG: print "Printing mean and std dev of samples ..." for ii in range(len(params)): xs= numpy.array([s[ii] for s in samples]) print numpy.mean(xs), numpy.std(xs) else: #First read the data if _VERBOSE: print "Reading and parsing data ..." XYZ,vxvyvz,cov_vxvyvz,rawdata= readData(metal=options.metal, sample=options.sample) vxvyvz= vxvyvz.astype(numpy.float64) cov_vxvyvz= cov_vxvyvz.astype(numpy.float64) R= ((8.-XYZ[:,0])**2.+XYZ[:,1]**2.)**(0.5) XYZ[:,2]+= _ZSUN d= numpy.fabs((XYZ[:,2]-numpy.mean(numpy.fabs(XYZ[:,2])))) #Optimize likelihood if _VERBOSE: print "Optimizing the likelihood ..." if options.model.lower() == 'hwr': if options.metal == 'rich': params= numpy.array([0.02,numpy.log(25.),0.,0.,numpy.log(6.)]) elif options.metal == 'poor': params= numpy.array([0.02,numpy.log(40.),0.,0.,numpy.log(15.)]) else: params= numpy.array([0.02,numpy.log(30.),0.,0.,numpy.log(15.)]) like_func= _HWRLikeMinus pdf_func= _HWRLike #Slice sampling keywords step= [0.01,0.05,0.3,0.3,0.3] create_method=['full','step_out','step_out', 'step_out','step_out'] isDomainFinite=[[True,True],[False,False], [False,False],[False,False], [False,False]] domain=[[0.,1.],[0.,0.],[0.,0.],[0.,0.], [0.,4.6051701859880918]] elif options.model.lower() == 'isotherm': if options.metal == 'rich': params= numpy.array([0.02,numpy.log(25.),numpy.log(6.)]) elif options.metal == 'poor': params= numpy.array([0.02,numpy.log(40.),numpy.log(15.)]) else: params= numpy.array([0.02,numpy.log(30.),numpy.log(15.)]) like_func= _IsothermLikeMinus pdf_func= _IsothermLike #Slice sampling keywords step= [0.01,0.05,0.3] create_method=['full','step_out','step_out'] isDomainFinite=[[True,True],[False,False], [False,True]] domain=[[0.,1.],[0.,0.],[0.,4.6051701859880918]] params= optimize.fmin_powell(like_func,params, args=(XYZ,vxvyvz,cov_vxvyvz,R,d)) if _VERBOSE: print "Optimal likelihood:", params #Now sample if _VERBOSE: print "Sampling the likelihood ..." samples= bovy_mcmc.slice(params, step, pdf_func, (XYZ,vxvyvz,cov_vxvyvz,R,d), create_method=create_method, isDomainFinite=isDomainFinite, domain=domain, nsamples=options.nsamples) if _DEBUG: print "Printing mean and std dev of samples ..." for ii in range(len(params)): xs= numpy.array([s[ii] for s in samples]) print numpy.mean(xs), numpy.std(xs) if _VERBOSE: print "Saving ..." savefile= open(args[0],'wb') pickle.dump(params,savefile) pickle.dump(samples,savefile) savefile.close() if options.noplots: return None #Plot if options.plotfunc: #First plot the best fit zs= numpy.linspace(0.3,1.2,1001) ds= zs-1. func= zfunc maxys= math.exp(params[1])+params[2]*ds+params[3]*ds**2. if options.xmin is None or options.xmax is None: xrange= [numpy.amin(zs)-0.2,numpy.amax(zs)+0.1] else: xrange= [options.xmin,options.xmax] if options.ymin is None or options.ymax is None: yrange= [numpy.amin(ys)-1.,numpy.amax(ys)+1.] else: yrange= [options.ymin,options.ymax] #Now plot the mean and std-dev from the posterior zmean= numpy.zeros(len(zs)) nsigs= 3 zsigs= numpy.zeros((len(zs),2*nsigs)) fs= numpy.zeros((len(zs),len(samples))) ds= zs-1. for ii in range(len(samples)): thisparams= samples[ii] fs[:,ii]= math.exp(thisparams[1])+thisparams[2]*ds+thisparams[3]*ds**2. #Record mean and std-devs zmean[:]= numpy.mean(fs,axis=1) bovy_plot.bovy_print() bovy_plot.bovy_plot(zs,zmean,'k-',xrange=xrange,yrange=yrange, xlabel=options.xlabel, ylabel=options.ylabel) for ii in range(nsigs): for jj in range(len(zs)): thisf= sorted(fs[jj,:]) thiscut= 0.5*special.erfc((ii+1.)/math.sqrt(2.)) zsigs[jj,2*ii]= thisf[int(math.floor(thiscut*len(samples)))] thiscut= 1.-thiscut zsigs[jj,2*ii+1]= thisf[int(math.floor(thiscut*len(samples)))] colord, cc= (1.-0.75)/nsigs, 1 nsigma= nsigs pyplot.fill_between(zs,zsigs[:,0],zsigs[:,1],color='0.75') while nsigma > 1: pyplot.fill_between(zs,zsigs[:,cc+1],zsigs[:,cc-1], color='%f' % (.75+colord*cc)) pyplot.fill_between(zs,zsigs[:,cc],zsigs[:,cc+2], color='%f' % (.75+colord*cc)) cc+= 1. nsigma-= 1 bovy_plot.bovy_plot(zs,zmean,'k-',overplot=True) #bovy_plot.bovy_plot(zs,maxys,'w--',overplot=True) bovy_plot.bovy_end_print(options.plotfile) else: xs= numpy.array([s[options.d1] for s in samples]) ys= numpy.array([s[options.d2] for s in samples]) if options.expd1: xs= numpy.exp(xs) if options.expd2: ys= numpy.exp(ys) if options.xmin is None or options.xmax is None: xrange= [numpy.amin(xs),numpy.amax(xs)] else: xrange= [options.xmin,options.xmax] if options.ymin is None or options.ymax is None: yrange= [numpy.amin(ys),numpy.amax(ys)] else: yrange= [options.ymin,options.ymax] bovy_plot.bovy_print() bovy_plot.scatterplot(xs,ys,'k,',onedhists=True,xrange=xrange, yrange=yrange,xlabel=options.xlabel, ylabel=options.ylabel) maxx, maxy= params[options.d1], params[options.d2] if options.expd1: maxx= math.exp(maxx) if options.expd2: maxy= math.exp(maxy) bovy_plot.bovy_plot([maxx],[maxy],'wx', overplot=True,ms=10.,mew=2.) bovy_plot.bovy_end_print(options.plotfile)
def sampleGP(trainingSet,covar,mean=None,nsamples=100, step=None,fix=None,metropolis=False,markovpy=False): """ NAME: sampleGP PURPOSE: sample a GP INPUT: trainingSet - a trainingSet instance covar - an instance of your covariance function of choice, with initialized parameters mean= an instance of your mean function of choice, with initialized parameters nsamples - number of samples desired step= step-size for slice creation or metropolis sampling (number or list) fix= None or list of parameters to hold fixed metropolis= if True, use Metropolis sampling markovpy= if True, use markovpy sampling OUTPUT: list of outcovarFunc HISTORY: 2010-08-08 - Written - Bovy (NYU) """ #Put in dummy mean if mean is None if mean is None: from flexgp.zeroMean import meanFunc mean= meanFunc() noMean= True out= [covar] else: noMean= False out= [(covar,mean)] #Pack the covariance parameters (params,packing)= pack_params(covar,mean,fix) if step is None: step= [0.1 for ii in range(len(params))] #Grab the covariance class covarFuncName= inspect.getmodule(covar).__name__ thisCovarClass= __import__(covarFuncName) meanFuncName= inspect.getmodule(mean).__name__ thisMeanClass= __import__(meanFuncName) #Set up isDomainFinite, domain, and create_method, even when metropolis isDomainFinite, domain, create_method= [], [], [] covarIsDomainFinite= covar.isDomainFinite() covarDomain= covar.paramsDomain() covarCreate= covar.create_method() meanIsDomainFinite= mean.isDomainFinite() meanDomain= mean.paramsDomain() meanCreate= mean.create_method() for ii in range(len(packing.GPhyperPackingList)): p= packing.GPhyperPackingList[ii] try: for jj in range(packing.GPhyperPackingDim[ii]): isDomainFinite.append(covarIsDomainFinite[p]) domain.append(covarDomain[p]) create_method.append(covarCreate[p]) except KeyError: for jj in range(len(packing.GPhyperPackingDim[ii])): isDomainFinite.append(meanIsDomainFinite[p]) domain.append(meanDomain[p]) create_method.append(meanCreate[p]) if len(packing.GPhyperPackingList) == 1: #one-d isDomainFinite= isDomainFinite[0] domain= domain[0] create_method= create_method[0] if isinstance(step,(list,numpy.ndarray)): step= step[0] if not metropolis and not markovpy: #slice sample the marginal likelihood samples= bovy_mcmc.slice(params,step, _lnpdf,(trainingSet,packing,thisCovarClass, thisMeanClass), isDomainFinite=isDomainFinite, domain=domain, nsamples=nsamples, create_method=create_method) elif metropolis: samples, faccept= bovy_mcmc.metropolis(params,step, _lnpdf,(trainingSet,packing, thisCovarClass, thisMeanClass), symmetric=True, nsamples=nsamples) if numpy.any((faccept < 0.15)) or numpy.any((faccept > 0.6)): print "WARNING: Metropolis acceptance ratio was < 0.15 or > 0.6 for a direction" print "Full acceptance ratio list:" print faccept elif markovpy: samples= bovy_mcmc.markovpy(params,step, _lnpdf,(trainingSet,packing,thisCovarClass, thisMeanClass), isDomainFinite=isDomainFinite, domain=domain, nsamples=nsamples) print nsamples, len(samples) if noMean: for ii in range(nsamples): hyperParamsDict= unpack_params(samples[ii],packing) out.append(packing.covarFunc(**hyperParamsDict)) return out else: for ii in range(nsamples): hyperParamsDict= unpack_params(samples[ii],packing) out.append((packing.covarFunc(**hyperParamsDict), packing.meanFunc(**hyperParamsDict))) return out