def simul(isodir): params = parameters() eps = 0.01 #feh params.set("m0y", 0.9, -1, 1, True) params.set("m0cphi", -0.001, -pi / 2 + eps, 0, True) params.set("m0cr", 0.01, 0, 2, True) params.set("m1y", -0.9, -1, 1, True) params.set("m1cphi", 1.67, pi / 2 + eps, pi, True) params.set("m1cr", 1.06, 0, 2, True) params.set("sigma", 0.6, 0, 1, True) #sfr params.set("s0y", 1.11, 0.0, 2, True) params.set("s0cphi", 0.0003, 0, pi / 2 - eps, True) params.set("s0cr", 1, 0, 1, True) params.set("s1x", 9.9, 8, 10, True) params.set("s1y", 5.9, 1.5, 10, True) params.set("s1cphi", 4.61, pi / 2 + eps, pi * 3 / 2 - eps, True) params.set("s1cr", 0.49, 0, 0.5, True) params.set("s2y", 0.89, 0, 2, True) params.set("s2cphi", 3.04, pi / 2 + eps, pi - eps, True) params.set("s2cr", 0.001, 0, 2, True) if len(sys.argv) == 2: #run with a param to start from the beginning params.save() params.load() data = iso.readfits(isodir + "/datarr.fits") isos = iso.readisos(isodir) t = utils.frange(8, 10.25, 0.001) def f(par): params.setvalues(par) w = utils.calculateweights(t, sfr(t, params)) #isow=iso.getisosweights(w,10.**t,metallicity(t,params),isos) isow = iso.getisosweights_gauss(w, 10.**t, metallicity(t, params), isos, params.sigma) m = iso.computeCMD(isow, isos) m = utils.normalize(m, sum(data.flat)) return utils.loglikelihood(m, data) d = numarray.maximum(data, 1e-20) llhC = sum((d * numarray.log(d)).flat) def b(par, value, iter): params.setvalues(par) params.save() print "henry:", value, "tom:", 2.0 * (value + llhC), "iter:", iter optimization.minmax(optimization.fmin_simplex, f, params.getvalues(), params.min(), params.max(), b)
def simul(isodir): params=parameters() eps=0.01 #feh params.set("m0y" ,0.9, -1,1,True) params.set("m0cphi",-0.001, -pi/2+eps,0,True) params.set("m0cr" ,0.01, 0,2,True) params.set("m1y" ,-0.9, -1,1,True) params.set("m1cphi",1.67, pi/2+eps,pi,True) params.set("m1cr" ,1.06, 0,2,True) params.set("sigma" ,0.6, 0,1, True) #sfr params.set("s0y" ,1.11, 0.0,2,True) params.set("s0cphi",0.0003, 0,pi/2-eps,True) params.set("s0cr" ,1, 0,1,True) params.set("s1x" ,9.9, 8,10,True) params.set("s1y" ,5.9, 1.5,10,True) params.set("s1cphi",4.61, pi/2+eps,pi*3/2-eps,True) params.set("s1cr" ,0.49, 0,0.5,True) params.set("s2y" ,0.89, 0,2,True) params.set("s2cphi",3.04, pi/2+eps,pi-eps,True) params.set("s2cr" ,0.001, 0,2,True) if len(sys.argv) == 2: #run with a param to start from the beginning params.save() params.load() data=iso.readfits(isodir+"/datarr.fits") isos = iso.readisos(isodir) t=utils.frange(8,10.25,0.001) def f(par): params.setvalues(par) w=utils.calculateweights(t,sfr(t,params)) #isow=iso.getisosweights(w,10.**t,metallicity(t,params),isos) isow=iso.getisosweights_gauss(w,10.**t,metallicity(t,params),isos, params.sigma) m=iso.computeCMD(isow,isos) m=utils.normalize(m,sum(data.flat)) return utils.loglikelihood(m,data) d = numarray.maximum(data,1e-20) llhC=sum( (d*numarray.log(d)).flat ) def b(par,value,iter): params.setvalues(par) params.save() print "henry:",value,"tom:",2.0*(value+llhC),"iter:",iter optimization.minmax(optimization.fmin_simplex,f, params.getvalues(),params.min(),params.max(),b)
import numpy as numarray from chemev import iso, utils, optimization if not optimization.have_mcmc: print "PyMC not installed correctly. Check the PYTHONPATH." print "PYTHONPATH=", sys.path sys.exit() print "reading isochrones" #isodir="m31iso" #isodir="m117halo" isodir = "../../isochrones/117/halo" data = iso.readfits(isodir + "/datarr.fits") isos = iso.readisos(isodir) print "fitting" pars = eval(file("bestfit117").readline()) ndata = sum(data.flat) d = numarray.maximum(data, 1e-20) llhC = sum((d * numarray.log(d)).flat) iter = 0 out = file("mcmc.out", "w") def f(params): global iter m = iso.computeCMD(params, isos) C = ndata / sum(m.flat)
def simul(isodir): params = parameters() eps = 0.01 #feh params.set("m0y", 0.9, -1, 1, True) params.set("m0cphi", -0.001, -pi / 2 + eps, 0, True) params.set("m0cr", 0.01, 0, 2, True) params.set("m1y", -0.9, -1, 1, True) params.set("m1cphi", 1.67, pi / 2 + eps, pi, True) params.set("m1cr", 1.06, 0, 2, True) # params.set("sigma" ,0.6, 0,1, True) #sfr params.set("s0y", 1.11, 0.0, 2, True) params.set("s0cphi", 0.0003, 0, pi / 2 - eps, True) params.set("s0cr", 1, 0, 1, True) params.set("s1x", 9.9, 8, 10, True) params.set("s1y", 5.9, 1.5, 10, True) params.set("s1cphi", 4.61, pi / 2 + eps, pi * 3 / 2 - eps, True) params.set("s1cr", 0.49, 0, 0.5, True) params.set("s2y", 0.89, 0, 2, True) params.set("s2cphi", 3.04, pi / 2 + eps, pi - eps, True) params.set("s2cr", 0.001, 0, 2, True) #if len(sys.argv) == 2: #run with a param to start from the beginning params.save() params.load() data = iso.readfits(isodir + "/datarr.fits") isos = iso.readisos(isodir) t = utils.frange(8, 10.25, 0.001) ndata = sum(data.flat) def f(par): params.setvalues(par) w = utils.calculateweights(t, sfr(t, params)) isow = iso.getisosweights(w, 10.**t, metallicity(t, params), isos) #isow=iso.getisosweights_gauss(w,10.**t,metallicity(t,params),isos, # params.sigma) m = iso.computeCMD(isow, isos) m = utils.normalize(m, ndata) return utils.loglikelihood(m, data) d = maximum(data, 1e-20) llhC = sum((d * log(d)).flat) def b(par, value, iter): params.setvalues(par) params.save() print "henry:", value, "tom:", 2.0 * (value + llhC), "iter:", iter #always try a different initial guess: params.randomize() sfr_initial = sfr(t, params) met_initial = metallicity(t, params) print "start" #pars=optimization.minmax(optimization.fmin_bfgs, pars = optimization.minmax( optimization.fmin_de, #pars=optimization.minmax(optimization.fmin_anneal, f, params.getvalues(), params.min(), params.max(), callback=b, iter=100, #logistics = optimization.ReflectLogistics) logistics=optimization.ExpLogistics)
def simul(isodir): params=parameters() eps=0.01 #feh params.set("m0y" ,0.9, -1,1,True) params.set("m0cphi",-0.001, -pi/2+eps,0,True) params.set("m0cr" ,0.01, 0,2,True) params.set("m1y" ,-0.9, -1,1,True) params.set("m1cphi",1.67, pi/2+eps,pi,True) params.set("m1cr" ,1.06, 0,2,True) # params.set("sigma" ,0.6, 0,1, True) #sfr params.set("s0y" ,1.11, 0.0,2,True) params.set("s0cphi",0.0003, 0,pi/2-eps,True) params.set("s0cr" ,1, 0,1,True) params.set("s1x" ,9.9, 8,10,True) params.set("s1y" ,5.9, 1.5,10,True) params.set("s1cphi",4.61, pi/2+eps,pi*3/2-eps,True) params.set("s1cr" ,0.49, 0,0.5,True) params.set("s2y" ,0.89, 0,2,True) params.set("s2cphi",3.04, pi/2+eps,pi-eps,True) params.set("s2cr" ,0.001, 0,2,True) #if len(sys.argv) == 2: #run with a param to start from the beginning params.save() params.load() data=iso.readfits(isodir+"/datarr.fits") isos = iso.readisos(isodir) t=utils.frange(8,10.25,0.001) ndata = sum(data.flat) def f(par): params.setvalues(par) w=utils.calculateweights(t,sfr(t,params)) isow=iso.getisosweights(w,10.**t,metallicity(t,params),isos) #isow=iso.getisosweights_gauss(w,10.**t,metallicity(t,params),isos, # params.sigma) m=iso.computeCMD(isow,isos) m=utils.normalize(m,ndata) return utils.loglikelihood(m,data) d = maximum(data,1e-20) llhC=sum( (d*log(d)).flat ) def b(par,value,iter): params.setvalues(par) params.save() print "henry:",value,"tom:",2.0*(value+llhC),"iter:",iter #always try a different initial guess: params.randomize() sfr_initial = sfr(t,params) met_initial = metallicity(t,params) print "start" #todo: #- the reflectlogistics randomizes the parameters once more, thus #the sfr_initial and met_initial is nonsense then. fix it in #ReflectLogistics.fracinv() #- let the simplex method restart automatically #pars=optimization.minmax(optimization.fmin_bfgs, pars=optimization.minmax(optimization.fmin_simplex, #pars=optimization.minmax(optimization.fmin_anneal, f,params.getvalues(),params.min(),params.max(), callback=b, iter=100, #logistics = optimization.ReflectLogistics) logistics = optimization.ExpLogistics)
m=utils.normalize(iso.computeCMD(params,isos),ndata) return utils.loglikelihood(m,data) d = maximum(data,1e-20) llhC=sum( (d*log(d)).flat ) def b(params,value,iter): "Print status everytime a best fit is found." m=iso.computeCMD(params,isos) C=sum(data.flat)/sum(m.flat) x=array(params) bestfit[0] = x*C file("bestfit117","w").write(str(list(bestfit[0]))) print "henry:",value,"tom:",2.0*(value+llhC),"iter:",iter,"norm:",C a= optimization.minc(optimization.fmin_bfgs,f,pars,callback=b,iter=iter) b(a, f(a), -1) return a print "reading isochrones" data=iso.readfits(chemev.isodir+"/117/stream/datarr.fits") isos = iso.readisos(chemev.isodir+"/117/stream") pars=[1.0]*117 print "fitting" pars=fitting_iteration(pars,200) print "done"
def b(params, value, iter): "Print status everytime a best fit is found." m = iso.computeCMD(params, isos) C = sum(data.flat) / sum(m.flat) x = array(params) bestfit[0] = x * C file("bestfit117", "w").write(str(list(bestfit[0]))) print "henry:", value, "tom:", 2.0 * (value + llhC), "iter:", iter, "norm:", C a = optimization.minc(optimization.fmin_bfgs, f, pars, callback=b, iter=iter) b(a, f(a), -1) return a print "reading isochrones" data = iso.readfits(chemev.isodir + "/117/disk/datarr.fits") isos = iso.readisos(chemev.isodir + "/117/disk") pars = [1.0] * 117 print "fitting" pars = fitting_iteration(pars, 200) print "done"
import numpy as numarray from chemev import iso,utils,optimization if not optimization.have_mcmc: print "PyMC not installed correctly. Check the PYTHONPATH." print "PYTHONPATH=",sys.path sys.exit() print "reading isochrones" #isodir="m31iso" #isodir="m117halo" isodir="../../isochrones/117/halo" data=iso.readfits(isodir+"/datarr.fits") isos = iso.readisos(isodir) print "fitting" pars=eval(file("bestfit117").readline()) ndata=sum(data.flat) d = numarray.maximum(data,1e-20) llhC=sum( (d*numarray.log(d)).flat ) iter=0 out=file("mcmc.out","w") def f(params): global iter m=iso.computeCMD(params,isos) C=ndata/sum(m.flat) x=numarray.array(params) # out.write(str(list(x*C))+"\n")
def simul(isodir): params = parameters() eps = 0.01 #feh params.set("m0y", 0.9, -1, 1, True) params.set("m0cphi", -0.001, -pi / 2 + eps, 0, True) params.set("m0cr", 0.01, 0, 2, True) params.set("m1y", -0.9, -1, 1, True) params.set("m1cphi", 1.67, pi / 2 + eps, pi, True) params.set("m1cr", 1.06, 0, 2, True) # params.set("sigma" ,0.6, 0,1, True) #sfr params.set("s0y", 1.11, 0.0, 2, True) params.set("s0cphi", 0.0003, 0, pi / 2 - eps, True) params.set("s0cr", 1, 0, 1, True) params.set("s1x", 9.9, 8, 10, True) params.set("s1y", 5.9, 1.5, 10, True) params.set("s1cphi", 4.61, pi / 2 + eps, pi * 3 / 2 - eps, True) params.set("s1cr", 0.49, 0, 0.5, True) params.set("s2y", 0.89, 0, 2, True) params.set("s2cphi", 3.04, pi / 2 + eps, pi - eps, True) params.set("s2cr", 0.001, 0, 2, True) #if len(sys.argv) == 2: #run with a param to start from the beginning params.save() params.load() data = iso.readfits(isodir + "/datarr.fits") isos = iso.readisos(isodir) t = utils.frange(8, 10.25, 0.001) ndata = sum(data.flat) def f(params): w = utils.calculateweights(t, sfr(t, params)) isow = iso.getisosweights(w, 10.**t, metallicity(t, params), isos) #a3=time.time() m = iso.computeCMD(isow, isos) #a4=time.time() m = utils.normalize(m, ndata) l = utils.loglikelihood(m, data) #print a4-a3 return l d = maximum(data, 1e-20) llhC = sum((d * log(d)).flat) def b(par, value, iter): params.setvalues(par) params.save() print "henry:", value, "tom:", 2.0 * (value + llhC), "iter:", iter print "start" bestfit = (0, 0) it = 0 while 1: it += 1 params.randomize() #ti = time.time() r = f(params) #ti -= time.time() #print "time:",ti if r < bestfit[1]: bestfit = (params.getvalues(), r) print "%d:" % it, r
d = maximum(data,1e-20) llhC=sum( (d*log(d)).flat ) def b(params,value,iter): "Print status everytime a best fit is found." m=iso.computeCMD(params,isos) C=sum(data.flat)/sum(m.flat) x=array(params) bestfit[0] = x*C file("bestfit117","w").write(str(list(bestfit[0]))) print "henry:",value,"tom:",2.0*(value+llhC),"iter:",iter,"norm:",C optimization.minc(optimization.fmin_simplex,f,pars,callback=b,iter=iter) return bestfit[0] print "reading isochrones" data=iso.readfits(chemev.isodir+"/117/halo/datarr.fits") isos = iso.readisos(chemev.isodir+"/117/halo") pars=[1.0]*117 print "fitting" #this is the hardwired empirical key to achieve the best fit. :) best_fit_path = [800, 1800, 1500, 2000]+ [2000]*100 print "we are going to do this amount of iterations:",best_fit_path for iter in best_fit_path: print "### doing %d iterations ###"%iter pars=fitting_iteration(pars,iter) print "done"
#! /usr/bin/env python """Writes all the information about the best fit to the file "protocol" """ import sys from chemev import iso, isodir, utils print "reading isochrones" data = iso.readfits(isodir + "/117/halo/datarr.fits") isos = iso.readisos(isodir + "/117/halo") isow = eval(open("bestfit117").readline()) model = iso.computeCMD(isow, isos) model = utils.normalize(model, sum(data.flat)) def plot_residuals(d, m, aspect=0.2): """ todo: the same color range for both data and model. the rediduals are ok """ import pylab pylab.figure() pylab.subplot(221) pylab.imshow(d, origin='lower', interpolation="nearest", aspect=aspect) pylab.title("data") pylab.colorbar() pylab.subplot(222) pylab.imshow(m, origin='lower', interpolation="nearest", aspect=aspect) pylab.title("model") pylab.colorbar() pylab.subplot(223) residuals = d - m
m=utils.normalize(iso.computeCMD(params,isos),ndata) return utils.loglikelihood(m,data) d = maximum(data,1e-20) llhC=sum( (d*log(d)).flat ) def b(params,value,iter): "Print status everytime a best fit is found." m=iso.computeCMD(params,isos) C=sum(data.flat)/sum(m.flat) x=array(params) bestfit[0] = x*C file("bestfit117","w").write(str(list(bestfit[0]))) print "henry:",value,"tom:",2.0*(value+llhC),"iter:",iter,"norm:",C a= optimization.minc(optimization.fmin_bfgs,f,pars,callback=b,iter=iter) b(a, f(a), -1) return a print "reading isochrones" data=iso.readfits(chemev.isodir+"/117/disk/datarr.fits") isos = iso.readisos(chemev.isodir+"/117/disk") pars=[1.0]*117 print "fitting" pars=fitting_iteration(pars,200) print "done"
def b(params, value, iter): "Print status everytime a best fit is found." m = iso.computeCMD(params, isos) C = sum(data.flat) / sum(m.flat) x = array(params) bestfit[0] = x * C file("bestfit117", "w").write(str(list(bestfit[0]))) print "henry:", value, "tom:", 2.0 * (value + llhC), "iter:", iter, "norm:", C a = optimization.minc(optimization.fmin_bfgs, f, pars, callback=b, iter=iter) b(a, f(a), -1) return a print "reading isochrones" data = iso.readfits(chemev.isodir + "/696/halo/datarr.fits") isos = iso.readisos(chemev.isodir + "/696/halo") pars = [1.0] * 696 print "fitting" pars = fitting_iteration(pars, 200) print "done"
#! /usr/bin/env python """Writes all the information about the best fit to the file "protocol" """ import sys from chemev import iso, isodir, utils print "reading isochrones" data = iso.readfits(isodir+"/117/halo/datarr.fits") isos = iso.readisos(isodir+"/117/halo") isow = eval(open("bestfit117").readline()) model = iso.computeCMD(isow,isos) model = utils.normalize(model,sum(data.flat)) def plot_residuals(d,m,aspect=0.2): """ todo: the same color range for both data and model. the rediduals are ok """ import pylab pylab.figure() pylab.subplot(221) pylab.imshow(d,origin='lower',interpolation="nearest",aspect=aspect) pylab.title("data") pylab.colorbar() pylab.subplot(222) pylab.imshow(m,origin='lower',interpolation="nearest",aspect=aspect) pylab.title("model") pylab.colorbar() pylab.subplot(223) residuals = d-m pylab.imshow(residuals,origin='lower',interpolation="nearest",aspect=aspect)
def simul(isodir): params=parameters() eps=0.01 #feh params.set("m0y" ,0.9, -1,1,True) params.set("m0cphi",-0.001, -pi/2+eps,0,True) params.set("m0cr" ,0.01, 0,2,True) params.set("m1y" ,-0.9, -1,1,True) params.set("m1cphi",1.67, pi/2+eps,pi,True) params.set("m1cr" ,1.06, 0,2,True) # params.set("sigma" ,0.6, 0,1, True) #sfr params.set("s0y" ,1.11, 0.0,2,True) params.set("s0cphi",0.0003, 0,pi/2-eps,True) params.set("s0cr" ,1, 0,1,True) params.set("s1x" ,9.9, 8,10,True) params.set("s1y" ,5.9, 1.5,10,True) params.set("s1cphi",4.61, pi/2+eps,pi*3/2-eps,True) params.set("s1cr" ,0.49, 0,0.5,True) params.set("s2y" ,0.89, 0,2,True) params.set("s2cphi",3.04, pi/2+eps,pi-eps,True) params.set("s2cr" ,0.001, 0,2,True) #if len(sys.argv) == 2: #run with a param to start from the beginning params.save() params.load() data=iso.readfits(isodir+"/datarr.fits") isos = iso.readisos(isodir) t=utils.frange(8,10.25,0.001) ndata = sum(data.flat) def f(params): w=utils.calculateweights(t,sfr(t,params)) isow=iso.getisosweights(w,10.**t,metallicity(t,params),isos) #a3=time.time() m=iso.computeCMD(isow,isos) #a4=time.time() m=utils.normalize(m,ndata) l= utils.loglikelihood(m,data) #print a4-a3 return l d = maximum(data,1e-20) llhC=sum( (d*log(d)).flat ) def b(par,value,iter): params.setvalues(par) params.save() print "henry:",value,"tom:",2.0*(value+llhC),"iter:",iter print "start" bestfit = (0,0) it = 0 while 1: it += 1 params.randomize() #ti = time.time() r = f(params) #ti -= time.time() #print "time:",ti if r < bestfit[1]: bestfit = (params.getvalues(), r) print "%d:"%it, r
def b(params, value, iter): "Print status everytime a best fit is found." m = iso.computeCMD(params, isos) C = sum(data.flat) / sum(m.flat) x = array(params) bestfit[0] = x * C file("bestfit117", "w").write(str(list(bestfit[0]))) print "henry:", value, "tom:", 2.0 * (value + llhC), "iter:", iter, "norm:", C a = optimization.minc(optimization.fmin_bfgs, f, pars, callback=b, iter=iter) b(a, f(a), -1) return a print "reading isochrones" data = iso.readfits(chemev.isodir + "/117/stream/datarr.fits") isos = iso.readisos(chemev.isodir + "/117/stream") pars = [1.0] * 117 print "fitting" pars = fitting_iteration(pars, 200) print "done"