def optK(self, k): from optimize import fmin loopcounts = 0 lastfval = 10e10 fval = 10e9 try: while fval < lastfval: lastfval = fval lastk = k self.estRadiance2(k) k, fval, warnflag = fmin(self.eval, k, fulloutput=1, printmessg=0) print(k, fval) loopcounts += 1 except KeyboardInterrupt: pass print(loopcounts, "minimization loops") return lastk
def doPk_isolike_noconv(pkd,kl,cov,snl=6.,kmin=0.02,kmax=.3,npar=3,sp=1.,spa=.001,mina=.8,maxa=1.2,chi2fac=1.,Nmock=1000,v='n',wo=''): #chi2fac should be hartlap factor from time import time from optimize import fmin print (np) b = baofitPk(pkd,kl,cov,min=kmin,max=kmax) b.snl = snl #b.Bp = Bp #b.np = npar #b.H = np.zeros((npar,b.nbin)) chi2fac = (Nmock-b.nbin-2.)/(Nmock-1.) print (b.nbin,chi2fac) #for i in range(0,b.nbin): # for j in range(0,npar): # b.H[j][i] = 1./b.rl[i]**j alphl= [] chil = [] likl = [] chim = 1000 na = int((maxa-mina)/spa) likm = -1 pt = 0 A0 = 0 A1 = 1. A2 = 0 A3 = 0 A4 = 0 b.alph = 1. B = 1. for i in range(0,na): b.alph = mina+spa*i+spa/2. inl = np.array([B,A0,A1,A2,A3,A4]) #inl = np.array(B) #inl = B #(B,A0,A1,A2) = fmin(b.chi_templ_alphfXX,inl,disp=False) #if npar > 0: (B,A0,A1,A2,A3,A4) = fmin(b.chi_temp_noconv,inl,disp=False) chi = b.chi_temp_noconv((B,A0,A1,A2,A3,A4))*chi2fac if v == 'y': print (b.alph,chi,B)#[0],b.A0[0],b.A1[0],b.A2[0] #single values getting output as arrays, silly, but works so not worrying about it alphl.append(b.alph) chil.append(chi) if chi < chim: chim = chi alphm = b.alph Bm = B #A0m = b.A0 #A1m = b.A1 #A2m = b.A2 #print alphm,chim,Bm,A0m,A1m,A2m #fo = open('BAOisobestfit'+wo+'.dat','w') b.alph = alphm #if npar > 0: # b.chi_templ_alphfXX((Bm),wo='y',fw=wo) #fo.write(str(alphm)+' '+str(chim)+' '+str(Bm[0])+' '+str(A0m[0])+' '+str(A1m[0])+' '+str(A2m[0])+'\n') #fo.close() return chil
def plot_exct(inputname,output_plot,titlename,xlab = 'ext'): print "test5" pp = PdfPages(output_plot) plt.clf() plt.minorticks_on() d=np.loadtxt(inputname).transpose() chin = sum((d[1]-1.)**2./d[2]**2.) print chin lf = linfit(d[0],d[1],d[2]) inl = np.array([1.,0]) b,m = fmin(lf.chilin,inl) print 'b='+str(b)+' m='+str(m) chilin = sum((d[1]-(m*d[0]+b))**2./d[2]**2.) print chilin plt.errorbar(d[0],d[1],d[2],fmt='ko') ol = np.ones((len(d[0]))) plt.plot(d[0],ol,'k:') plt.plot(d[0],m*d[0]+b,'k--') if xlab == '': plt.xlabel(sys,size=16) else: plt.xlabel(xlab,size=16) plt.ylabel(r'$N_{\rm gal}/N_{\rm ran}$ (normalized)',size=16) plt.ylim(.7,1.19) plt.text(min(d[0])+0.1*(max(d[0])-min(d[0])),1.1,r'$\chi^2$ null ='+str(chin)[:4],color='k') plt.text(min(d[0])+0.1*(max(d[0])-min(d[0])),1.08,r'$\chi^2$ lin ='+str(chilin)[:4],color='k') #plt.title(r'galaxy density vs. $i$-band depth for v0.7 eboss QSOs, 0.9 < z < 2.2') plt.title(titlename) pp.savefig() pp.close() return True
def findlinmb(band,sys,zmin,zmax,wm=''): #finds linear fit parameters from optimize import fmin d = np.loadtxt(inputdir+'Y1RM42x3pt'+str(zmin)+str(zmax)+'v'+sys+band+wm+'jackerr.dat').transpose() lf = linfit(d[0],d[1],d[2]) inl = np.array([1.,0]) b0,m0 = fmin(lf.chilin,inl) return b0,m0
def fit_gaussian(xs, ys): """Fit a gaussian (returning amplitude, sigma, offset) to a set of x values and their corresponding y values.""" def fitfunc(args): amp, sig, off = args sum = ((ys - gaussian(amp, sig, off, xs))**2).sum() return sum return optimize.fmin(fitfunc, (ys.sum(), 50, ys.argmax()), disp=0)
def findlinmb(band, sys, zmin, zmax, wm=''): #finds linear fit parameters from optimize import fmin d = np.loadtxt(inputdir + 'Y1RM42x3pt' + str(zmin) + str(zmax) + 'v' + sys + band + wm + 'jackerr.dat').transpose() lf = linfit(d[0], d[1], d[2]) inl = np.array([1., 0]) b0, m0 = fmin(lf.chilin, inl) return b0, m0
def ComputePositions(self): """Optimize the estimated radio positions to match the measured distances between radios.""" global global_distances #HL: without this global statement the global variable is masked by a local one global_distances = self.distances log.debug("global_distances="+str(global_distances)) if np.all( (self.positions==np.zeros( (self.N*2) ) ) ): self.InitializePositions() p = np.array(self.Positions3Mto2V(self.positions)) log.debug("positions sent to optimizer="+str(p)) x1 = optim.fmin(CostFun, p, xtol=1e-4,ftol=1e-5,maxiter=100,maxfun=100) self.positions = self.Positions2Vto3M(x1)
def test_optimize(): m = Mesh() x = 10*np.random.rand(8) print x global_distances = Positions2Distances(x) print global_distances x3 = m.Positions2Vto3M(x) print x3 print np.size(x3) global_distances2 = m.SetPositions(np.reshape(x3,(4,3))) print m.positions print global_distances2 x2 = x+np.random.rand(8) print x2 y = CostFun(x2) print y x0 = optim.fmin(CostFun,np.random.rand(8)) print x0 print CostFun(x0)
def estRadiance2(self, k): score = self.eval(k) print("previous score =", score) print("LINEAR RADIANCE ESTIMATION (FAST)") oldradiance = self.radiance.copy() self.estRadiance(k) # initialize with linear nscore = self.eval(k) print("linear score =", nscore) if nscore < score: return # good enough, do nothing self.radiance = oldradiance print("NONLINEAR RADIANCE ESTIMATION (SLOW)") from optimize import fmin for i in range(len(self.radiance)): if not i % 500: print(i, "out of", len(self.radiance)) for j in range(3): val = fmin(self.evalRad, (self.radiance[i, j], ), (i, j), printmessg=0) self.radiance[i, j] = val[0] nnscore = self.eval(k) print("nonlinear score =", nnscore)
def doxi_isolikeNna(N,xid,covd,modl,rl,rmin=50,rmax=150,sp=1.,Bp=.4,rminb=50.,rmaxb=80.,spa=.001,mina=.8,maxa=1.2,chi2fac=1.,v='n',wo=''): #chi2fac should be hartlap factor from time import time from optimize import fmin b = baofit_isoN(N,xid,covd,modl,rl,sp=sp) b.Bp = Bp bb = baofit_iso(xid[0],covd,modl[0],rl,rmin=rminb,rmax=rmaxb,sp=sp) #bb is to set bias prior bb.H = np.zeros((3,bb.nbin)) #for j in range(0,N): for i in range(0,bb.nbin): bb.H[0][i] = 1. bb.H[1][i] = 1./bb.rl[i] bb.H[2][i] = 1./bb.rl[i]**2. alphl= [] chil = [] likl = [] chim = 1000 na = int((maxa-mina)/spa) likm = -1 pt = 0 A0 = 0 A1 = 1. A2 = 0 b.alph = 1. bb.alph = b.alph #b.alph = 0 B = .1 chiBmin = 1000 while B < 2.: bl = [B] #for i in range(0,N): # bl.append(B) chiB = bb.chi_templ_alphfXXn(bl)*chi2fac if chiB < chiBmin: chiBmin = chiB BB = B B += .01 print ('best-fit bias factor is '+str(BB)+' '+str(chiBmin)) b.BB = BB #b.BB = 1. #switch to this to make bias prior centered on input rather than fit value B = [BB,BB,0,0,0,0,0,0] for i in range(0,na): b.alph = mina+spa*i+spa/2. #inl = np.array([B,A0,A1,A2]) #inl = np.array(B) inl = B #(B,A0,A1,A2) = fmin(b.chi_templ_alphfXX,inl,disp=False) B = fmin(b.chi_templ_alphfXXna,inl,disp=False) #B = fmin(b.chi_templ_alphfXXn,inl,disp=False) #chi = b.chi_templ_alphfXX((B,A0,A1,A2))*chi2fac chi = b.chi_templ_alphfXXna((B))*chi2fac if v == 'y': print (b.alph,chi,B[0],b.A0[0],b.A1[0],b.A2[0]) #single values getting output as arrays, silly, but works so not worrying about it alphl.append(b.alph) chil.append(chi) if chi < chim: chim = chi alphm = b.alph Bm = B A0m = b.A0 A1m = b.A1 A2m = b.A2 print (alphm,chim,Bm) #fo = open('BAOisobestfit'+wo+'.dat','w') b.alph = alphm #b.chi_templ_alphfXX((Bm),wo='y',fw=wo) #fo.write(str(alphm)+' '+str(chim)+' '+str(Bm[0])+' '+str(A0m[0])+' '+str(A1m[0])+' '+str(A2m[0])+'\n') #fo.close() return chil
def doxi_isolike(xid,covd,modl,modsmoothl,rl,rmin=50,rmax=150,npar=3,sp=1.,Bp=.4,rminb=50.,rmaxb=50.,spa=.001,mina=.8,maxa=1.2,chi2fac=1.,Nmock=1000,v='n',wo='',diro='',cov2=''): #chi2fac should be hartlap factor from time import time from optimize import fmin print (np) b = baofit_iso(xid,covd,modl,modsmoothl,rl,rmin=rmin,rmax=rmax,sp=sp,cov2=cov2) b.Bp = Bp b.np = npar b.H = np.zeros((npar,b.nbin)) chi2fac = (Nmock-b.nbin-2.)/(Nmock-1.) print (b.nbin,chi2fac) for i in range(0,b.nbin): for j in range(0,npar): b.H[j][i] = 1./b.rl[i]**j if rmin == rmaxb: rmaxb += (b.rl[1]-b.rl[0])*1.1 #increase rmaxb by one bin size if set poorly bb = baofit_iso(xid,covd,modl,modsmoothl,rl,rmin=rmin,rmax=rmaxb,sp=sp,cov2=cov2) #bb is to set bias prior bb.np = npar bb.H = np.zeros((npar,bb.nbin)) for i in range(0,bb.nbin): for j in range(0,npar): bb.H[0][i] = 1./bb.rl[i]**j alphl= [] chil = [] likl = [] chim = 1000 na = int((maxa-mina)/spa) likm = -1 pt = 0 A0 = 0 A1 = 1. A2 = 0 b.alph = 1. bb.alph = b.alph #b.alph = 0 B = .1 chiBmin = 1000 Bmax = 10. while B < Bmax: bl = [B] chiB = bb.chi_templ_alphfXXn(bl)*chi2fac if chiB < chiBmin: chiBmin = chiB BB = B B += .01 print ('best-fit bias factor is '+str(BB)+' '+str(chiBmin)) #print BB,Bmax,Bmax-.01 if BB >= Bmax-.011: print( 'WARNING, best-fit bias is at max tested value') #else: # print BB,Bmax,Bmax-.01 b.BB = BB #b.BB = 1. #switch to this to make bias prior centered on input rather than fit value B = BB for i in range(0,na): b.alph = mina+spa*i+spa/2. #inl = np.array([B,A0,A1,A2]) #inl = np.array(B) inl = B #(B,A0,A1,A2) = fmin(b.chi_templ_alphfXX,inl,disp=False) if npar > 0: B = fmin(b.chi_templ_alphfXX,inl,disp=False) chi = b.chi_templ_alphfXX((B))*chi2fac else: B = fmin(b.chi_templ_alphfXXn,inl,disp=False) chi = b.chi_templ_alphfXXn((B))*chi2fac #B = fmin(b.chi_templ_alphfXXn,inl,disp=False) #chi = b.chi_templ_alphfXX((B,A0,A1,A2))*chi2fac if v == 'y': print (b.alph,chi,B[0],b.A0[0],b.A1[0],b.A2[0]) #single values getting output as arrays, silly, but works so not worrying about it alphl.append(b.alph) chil.append(chi) if chi < chim: chim = chi alphm = b.alph Bm = B #A0m = b.A0 #A1m = b.A1 #A2m = b.A2 #print alphm,chim,Bm,A0m,A1m,A2m #fo = open('BAOisobestfit'+wo+'.dat','w') b.alph = alphm if npar > 0: b.chi_templ_alphfXX((Bm),wo='y',fw=wo,diro=diro) #fo.write(str(alphm)+' '+str(chim)+' '+str(Bm[0])+' '+str(A0m[0])+' '+str(A1m[0])+' '+str(A2m[0])+'\n') #fo.close() return chil
## der_y = asarray(der_y) ## return der_y x = asarray(x) xm = x[1:-1] xm_m1 = x[:-2] xm_p1 = x[2:] der = numpy.zeros_like(x) der[1:-1] = 200 * (xm - xm_m1**2) - 400 * (xm_p1 - xm**2) * xm - 2 * (1 - xm) der[0] = -400 * x[0] * (x[1] - x[0]**2) - 2 * (1 - x[0]) der[-1] = 200 * (x[-1] - x[-2]**2) return der x0 = [0.8, 1.2, 0.7, 0.3, 0.2] x = fmin(rosen, x0) #x = fmin_bfgs(rosen, x0, fprime=rosen_der, maxiter=10) print x #from EnergyFunction import * ##differential equation, 2d ##prob = ProblemInformation() ##prob.setDim(2) ##prob.setBasis('sigmoid') ##prob.setRepresention('implicit') ##param=matrix([5,2,4,5,3,2,6,2,3,1,2,3]) ##prob.setDomain(map(lambda x,y: [x,y], numpy.linspace(0,1,10), numpy.linspace(0,0,10))) ##prob.setDifferentialEquation("F[0][0]-F[1][0]") ##prob.setDirichlet([[0,0]],[1]) ##print "begin" ##x = fmin_bfgs(EnergyFunction.function, param, fprime=EnergyFunction.jacobian, maxiter=80, args=["DifferentialEquation",prob])
def doxi_isolike(xid, covd, modl, modsmoothl, rl, bs=8, rmin=50, rmax=150, npar=3, sp=1., Bp=.4, rminb=50., rmaxb=50., spa=.001, mina=.8, maxa=1.2, Nmock=1000, v='', wo='', diro=''): ''' 1D fit to monopole for alpha_iso returns list of chi2(alpha) xid is array for data covd is the cov matrix modl is the BAO template modsmoothl is the no BAO template rl is the array of r bin centers bs is the bin size for xi rmin is the minimum for the bin center rmax is maximum of the bin center npar is the number of polynomial terms (might only work for 3 or 0) sp is spacing of the BAO template Bp is size of the log(B/Bbest) prior rminb is the minimum bin center for finding Bbest rmaxb is the maximum bin center for finding Bbest spa is the spacing for the alpha grid mina is the minimum alpha maxa is the maximum alpha Nmock is the number of mocks used to create the cov matrix wo is string for writing output files v = 'y' will print info diro is directory for output ''' # from time import time from optimize import fmin print(np) b = baofit_iso(xid, covd, modl, modsmoothl, rl, rmin=rmin, rmax=rmax, sp=sp, bs=bs) b.Bp = Bp b.np = npar b.H = np.zeros((npar, b.nbin)) chi2fac = (Nmock - b.nbin - 2.) / (Nmock - 1.) print(b.nbin, chi2fac) for i in range(0, b.nbin): for j in range(0, npar): b.H[j][i] = 1. / b.rl[i]**j if rmin == rmaxb: rmaxb += (b.rl[1] - b.rl[0]) * 1.1 #increase rmaxb by one bin size if set poorly bb = baofit_iso(xid, covd, modl, modsmoothl, rl, rmin=rmin, rmax=rmaxb, sp=sp, bs=bs) #bb is to set bias prior bb.np = npar bb.H = np.zeros((npar, bb.nbin)) for i in range(0, bb.nbin): for j in range(0, npar): bb.H[0][i] = 1. / bb.rl[i]**j alphl = [] chil = [] likl = [] chim = 1000 na = int((maxa - mina) / spa) likm = -1 pt = 0 A0 = 0 A1 = 1. A2 = 0 b.alph = 1. bb.alph = b.alph #b.alph = 0 B = .1 chiBmin = 1000 Bmax = 10. #simple search for best-fit B while B < Bmax: bl = [B] chiB = bb.chi_templ_alphfXXn(bl) * chi2fac if chiB < chiBmin: chiBmin = chiB BB = B B += .01 print('best-fit bias factor is ' + str(BB) + ' ' + str(chiBmin)) if BB >= Bmax - .011: print('WARNING, best-fit bias is at max tested value') b.BB = BB #b.BB = 1. #switch to this to make bias prior centered on input rather than fit value B = BB for i in range(0, na): b.alph = mina + spa * i + spa / 2. #inl = np.array([B,A0,A1,A2]) #from older version without solving for nuisance terms analytically inl = B #(B,A0,A1,A2) = fmin(b.chi_templ_alphfXX,inl,disp=False) #from older version without solving for nuisance terms analytically if npar > 0: B = fmin(b.chi_templ_alphfXX, inl, disp=False) chi = b.chi_templ_alphfXX((B)) * chi2fac else: B = fmin(b.chi_templ_alphfXXn, inl, disp=False) chi = b.chi_templ_alphfXXn((B)) * chi2fac #chi = b.chi_templ_alphfXX((B,A0,A1,A2))*chi2fac #from older version without solving for nuisance terms analytically if v == 'y': print( b.alph, chi, B[0], b.A0[0], b.A1[0], b.A2[0] ) #single values getting output as arrays, silly, but works so not worrying about it alphl.append(b.alph) chil.append(chi) if chi < chim: chim = chi alphm = b.alph Bm = B #A0m = b.A0 #from older version without solving for nuisance terms analytically #A1m = b.A1 #A2m = b.A2 b.alph = alphm if npar > 0: b.chi_templ_alphfXX((Bm), wo='y', fw=wo, diro=diro) return chil
def doPk_isolike_noconv(pkd, kl, cov, snl=6., kmin=0.02, kmax=.3, npar=3, sp=1., spa=.001, mina=.8, maxa=1.2, chi2fac=1., Nmock=1000, v='n', wo=''): #chi2fac should be hartlap factor from time import time from optimize import fmin print np b = baofitPk(pkd, kl, cov, min=kmin, max=kmax) b.snl = snl #b.Bp = Bp #b.np = npar #b.H = np.zeros((npar,b.nbin)) chi2fac = (Nmock - b.nbin - 2.) / (Nmock - 1.) print b.nbin, chi2fac #for i in range(0,b.nbin): # for j in range(0,npar): # b.H[j][i] = 1./b.rl[i]**j alphl = [] chil = [] likl = [] chim = 1000 na = int((maxa - mina) / spa) likm = -1 pt = 0 A0 = 0 A1 = 1. A2 = 0 A3 = 0 A4 = 0 b.alph = 1. B = 1. for i in range(0, na): b.alph = mina + spa * i + spa / 2. inl = np.array([B, A0, A1, A2, A3, A4]) #inl = np.array(B) #inl = B #(B,A0,A1,A2) = fmin(b.chi_templ_alphfXX,inl,disp=False) #if npar > 0: (B, A0, A1, A2, A3, A4) = fmin(b.chi_temp_noconv, inl, disp=False) chi = b.chi_temp_noconv((B, A0, A1, A2, A3, A4)) * chi2fac if v == 'y': print b.alph, chi, B #[0],b.A0[0],b.A1[0],b.A2[0] #single values getting output as arrays, silly, but works so not worrying about it alphl.append(b.alph) chil.append(chi) if chi < chim: chim = chi alphm = b.alph Bm = B #A0m = b.A0 #A1m = b.A1 #A2m = b.A2 #print alphm,chim,Bm,A0m,A1m,A2m #fo = open('BAOisobestfit'+wo+'.dat','w') b.alph = alphm #if npar > 0: # b.chi_templ_alphfXX((Bm),wo='y',fw=wo) #fo.write(str(alphm)+' '+str(chim)+' '+str(Bm[0])+' '+str(A0m[0])+' '+str(A1m[0])+' '+str(A2m[0])+'\n') #fo.close() return chil
def doxi_isolikeNna(N, xid, covd, modl, rl, rmin=50, rmax=150, sp=1., Bp=.4, rminb=50., rmaxb=80., spa=.001, mina=.8, maxa=1.2, chi2fac=1., v='n', wo=''): #chi2fac should be hartlap factor from time import time from optimize import fmin b = baofit_isoN(N, xid, covd, modl, rl, sp=sp) b.Bp = Bp bb = baofit_iso(xid[0], covd, modl[0], rl, rmin=rminb, rmax=rmaxb, sp=sp) #bb is to set bias prior bb.H = np.zeros((3, bb.nbin)) #for j in range(0,N): for i in range(0, bb.nbin): bb.H[0][i] = 1. bb.H[1][i] = 1. / bb.rl[i] bb.H[2][i] = 1. / bb.rl[i]**2. alphl = [] chil = [] likl = [] chim = 1000 na = int((maxa - mina) / spa) likm = -1 pt = 0 A0 = 0 A1 = 1. A2 = 0 b.alph = 1. bb.alph = b.alph #b.alph = 0 B = .1 chiBmin = 1000 while B < 2.: bl = [B] #for i in range(0,N): # bl.append(B) chiB = bb.chi_templ_alphfXXn(bl) * chi2fac if chiB < chiBmin: chiBmin = chiB BB = B B += .01 print 'best-fit bias factor is ' + str(BB) + ' ' + str(chiBmin) b.BB = BB #b.BB = 1. #switch to this to make bias prior centered on input rather than fit value B = [BB, BB, 0, 0, 0, 0, 0, 0] for i in range(0, na): b.alph = mina + spa * i + spa / 2. #inl = np.array([B,A0,A1,A2]) #inl = np.array(B) inl = B #(B,A0,A1,A2) = fmin(b.chi_templ_alphfXX,inl,disp=False) B = fmin(b.chi_templ_alphfXXna, inl, disp=False) #B = fmin(b.chi_templ_alphfXXn,inl,disp=False) #chi = b.chi_templ_alphfXX((B,A0,A1,A2))*chi2fac chi = b.chi_templ_alphfXXna((B)) * chi2fac if v == 'y': print b.alph, chi, B[0], b.A0[0], b.A1[0], b.A2[ 0] #single values getting output as arrays, silly, but works so not worrying about it alphl.append(b.alph) chil.append(chi) if chi < chim: chim = chi alphm = b.alph Bm = B A0m = b.A0 A1m = b.A1 A2m = b.A2 print alphm, chim, Bm #fo = open('BAOisobestfit'+wo+'.dat','w') b.alph = alphm #b.chi_templ_alphfXX((Bm),wo='y',fw=wo) #fo.write(str(alphm)+' '+str(chim)+' '+str(Bm[0])+' '+str(A0m[0])+' '+str(A1m[0])+' '+str(A2m[0])+'\n') #fo.close() return chil
def doxi_isolike(xid, covd, modl, modsmoothl, rl, rmin=50, rmax=150, npar=3, sp=1., Bp=.4, rminb=50., rmaxb=50., spa=.001, mina=.8, maxa=1.2, chi2fac=1., Nmock=1000, v='n', wo='', cov2=''): #chi2fac should be hartlap factor from time import time from optimize import fmin print np b = baofit_iso(xid, covd, modl, modsmoothl, rl, rmin=rmin, rmax=rmax, sp=sp, cov2=cov2) b.Bp = Bp b.np = npar b.H = np.zeros((npar, b.nbin)) chi2fac = (Nmock - b.nbin - 2.) / (Nmock - 1.) print b.nbin, chi2fac for i in range(0, b.nbin): for j in range(0, npar): b.H[j][i] = 1. / b.rl[i]**j if rmin == rmaxb: rmaxb += (b.rl[1] - b.rl[0]) * 1.1 #increase rmaxb by one bin size if set poorly bb = baofit_iso(xid, covd, modl, modsmoothl, rl, rmin=rmin, rmax=rmaxb, sp=sp, cov2=cov2) #bb is to set bias prior bb.np = npar bb.H = np.zeros((npar, bb.nbin)) for i in range(0, bb.nbin): for j in range(0, npar): bb.H[0][i] = 1. / bb.rl[i]**j alphl = [] chil = [] likl = [] chim = 1000 na = int((maxa - mina) / spa) likm = -1 pt = 0 A0 = 0 A1 = 1. A2 = 0 b.alph = 1. bb.alph = b.alph #b.alph = 0 B = .1 chiBmin = 1000 Bmax = 10. while B < Bmax: bl = [B] chiB = bb.chi_templ_alphfXXn(bl) * chi2fac if chiB < chiBmin: chiBmin = chiB BB = B B += .01 print 'best-fit bias factor is ' + str(BB) + ' ' + str(chiBmin) #print BB,Bmax,Bmax-.01 if BB >= Bmax - .011: print 'WARNING, best-fit bias is at max tested value' #else: # print BB,Bmax,Bmax-.01 b.BB = BB #b.BB = 1. #switch to this to make bias prior centered on input rather than fit value B = BB for i in range(0, na): b.alph = mina + spa * i + spa / 2. #inl = np.array([B,A0,A1,A2]) #inl = np.array(B) inl = B #(B,A0,A1,A2) = fmin(b.chi_templ_alphfXX,inl,disp=False) if npar > 0: B = fmin(b.chi_templ_alphfXX, inl, disp=False) chi = b.chi_templ_alphfXX((B)) * chi2fac else: B = fmin(b.chi_templ_alphfXXn, inl, disp=False) chi = b.chi_templ_alphfXXn((B)) * chi2fac #B = fmin(b.chi_templ_alphfXXn,inl,disp=False) #chi = b.chi_templ_alphfXX((B,A0,A1,A2))*chi2fac if v == 'y': print b.alph, chi, B[0], b.A0[0], b.A1[0], b.A2[ 0] #single values getting output as arrays, silly, but works so not worrying about it alphl.append(b.alph) chil.append(chi) if chi < chim: chim = chi alphm = b.alph Bm = B #A0m = b.A0 #A1m = b.A1 #A2m = b.A2 #print alphm,chim,Bm,A0m,A1m,A2m #fo = open('BAOisobestfit'+wo+'.dat','w') b.alph = alphm if npar > 0: b.chi_templ_alphfXX((Bm), wo='y', fw=wo) #fo.write(str(alphm)+' '+str(chim)+' '+str(Bm[0])+' '+str(A0m[0])+' '+str(A1m[0])+' '+str(A2m[0])+'\n') #fo.close() return chil