def fitMassMC(tzero,period,oc,epoch,weight): z = numpy.polyfit(epoch, oc, 2) # the traditional chi-square fit print "The chi-square result: ", z #priors sig = pymc.Uniform("sig", 0.0, 100.0, value=1.) a = pymc.Uniform("a", -10.0, 10.0, value= 0.0) b = pymc.Uniform("b", -10.0, 10.0, value= 0.0) c = pymc.Uniform("c", -10.0, 10.0, value= 0.0) #model @pymc.deterministic(plot=False) def mod_quadratic(x=epoch, a=a, b=b, c=c): return a*x**2 + b*x + c #likelihood quad_y = pymc.Normal("y", mu=mod_quadratic, tau=1.0/sig**2, value=oc, observed=True) quad_model = pymc.Model([quad_y, a, b, c]) quadratic_mcmc = pymc.MCMC(quad_model) quadratic_mcmc.sample(1000000, 200) #pymc.Matplot.plot(quadratic_mcmc) a = quadratic_mcmc.trace('a')[:] b = quadratic_mcmc.trace('b')[:] c = quadratic_mcmc.trace('c')[:] print('Last value of a: {}, and the median value is {}'.format(a[-1], numpy.median(a))) print('Last value of b: {}, and the median value is {}'.format(b[-1], numpy.median(b))) print('Last value of c: {}, and the median value is {}'.format(c[-1], numpy.median(c))) print quadratic_mcmc.stats() xmin = min(epoch) xmax = max(epoch) xlen = len(epoch) fitx = numpy.linspace(xmin,xmax,xlen) fity = model(a[-1],b[-1],c[-1],fitx) fitPlot(epoch, oc, minType, method,fitx, fity)
def fitLiteLM(self, userA, ecc, w, tzero, per, npoc, npepoch, weight, limitations): l = Lite() l.parameterSummary() l["a"] = userA l["ecc"] = ecc l["w"] = w l["t0"] = tzero l["per"] = per freedomPars = [] if limitations[0] == False: freedomPars.append("t0") if limitations[1] == False: freedomPars.append("per") if limitations[2] == False: freedomPars.append("a") if limitations[3] == False: freedomPars.append("ecc") if limitations[4] == False: freedomPars.append("w") print freedomPars l.thaw(freedomPars) # free parameters print l.thaw l.fit(npepoch, npoc, yerr=weight) l.parameterSummary() model = l.evaluate(npepoch) sepoch, fity = zip(*sorted(zip(npepoch, model))) fitPlot(npepoch, npoc, minType, method, sepoch, fity)
def fitLiteLM(self,userA,ecc,w,tzero,per,npoc,npepoch,weight,limitations): l = Lite() #l.parameterSummary() l["a"] = userA l["ecc"] = ecc l["w"]=w l["t0"]=tzero l["per"]=per freedomPars = [] if limitations[0] == False: freedomPars.append("t0") if limitations[1] == False: freedomPars.append("per") if limitations[2] == False: freedomPars.append("a") if limitations[3] == False: freedomPars.append("ecc") if limitations[4] == False: freedomPars.append("w") ################## FIT MCMC ##################################### # l.thaw(freedomPars) #free parameters # l.fit(npepoch,npoc,yerr=weight) # # X0 = {"a":l["a"],"ecc":l["ecc"],"w":l["w"], "t0":l["t0"], "per":l["per"]} # Lims = {"a":[-10.,10.],"ecc":[-1.,1], "w":[-360.,360.], "t0":[0.,50000.], "per":[0.,100000.]} # steps = {"a":0.01, "ecc":0.01,"w":0.1, "t0":200, "per":500} # # l.fitMCMC(npepoch, npoc, X0, Lims, steps, yerr=weight, \ # iter=2500, burn=0, thin=1, \ # dbfile="mcmcExample.tmp") #------------------------------------------------------------------ # specific limits for MCMC paramters # ppa = {} # ppa["a"] = pymc.Uniform("a", value=l["a"], lower=-1., \ # upper=1.0, doc="Amplitude") # ppa["ecc"] = pymc.Uniform("ecc", value=l["ecc"], lower=0., \ # upper=1.0, doc="Eccentricity") # Use this for ppa # l.fitMCMC(npepoch, npoc, X0, Lims, steps, yerr=weight, \ # pymcPars=ppa,iter=2500, burn=0, thin=1, \ # dbfile="mcmcExample.tmp") ################################################################## ######## AUTO FIT is WORKING ##################################### # ranges = {"a":[-10.,10.],"ecc":[0.,1], "w":[0.,360.], "t0":[0.,50000.], "per":[0.,50000.]} # l.autoFitMCMC(npepoch, npoc, ranges, yerr=weight, iter=1000, picky=False) ################################################################## ######## emcee ################################################### l.thaw(freedomPars) #free parameters sampleArgs = {"iters":1000, "burn":200} priors = {"a":fuf.FuFPrior("limuniform", lower=-10.0, upper=10.), \ "ecc":fuf.FuFPrior("limuniform", lower=-1.0, upper=1.), \ "w":fuf.FuFPrior("limuniform", lower=-360.0, upper=360.), \ "t0":fuf.FuFPrior("limuniform", lower=0.0, upper=50000.), \ "per":fuf.FuFPrior("limuniform", lower=0.0, upper=50000.)} # Note that the filename should end in .emcee. Substitute this filename # in the following examples. l.fitEMCEE(npepoch, npoc, yerr=weight, sampleArgs=sampleArgs, \ dbfile="mcmcTA.emcee", priors=priors) ################################################################## l.parameterSummary() model = l.evaluate(npepoch) sepoch, fity = zip(*sorted(zip(npepoch, model))) fitPlot(npepoch, npoc, minType, method, sepoch,fity) #print l1, l2, l3, l4, l5, l6, l7, l8, l9, l10,l11, l12 #return l1, l2, l3, l4, l5, l6, l7, l8, l9, l10,l11, l12, fitx, fity
try: fitobj.fit(params0=paramsinitial) except Exception, mes: print "Something went wrong with fit: ", mes raise SystemExit # results l1 = "Fit status: ", fitobj.message l2 = "Best-fit parameters: ", fitobj.params l3 = "Covariance errors: ", fitobj.xerror l4 = "Standard errors ", fitobj.stderr l5 = "Chi^2 min: ", fitobj.chi2_min l6 = "Reduced Chi^2: ", fitobj.rchi2_min l7 = "Iterations: ", fitobj.niter l8 = "Number of function calls: ", fitobj.nfev l9 = "Number of free pars.: ", fitobj.nfree l10 = "Degrees of freedom: ", fitobj.dof l11 = "Number of pegged pars.: ", fitobj.npegged l12 = "Covariance matrix:\n", fitobj.covar xmin = min(epoch) xmax = max(epoch) xlen = len(epoch) fitx = numpy.linspace(xmin, xmax, xlen) fity = model(fitobj.params, fitx) fitPlot(epoch, oc, minType, method, fitx, fity) return l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, l11, l12, fitx, fity
def fitMassMC(self,tzero,per,npoc,npepoch,weight): l = massTransfer() #l.parameterSummary() # l["a"] = 0 # l["b"] = 0 # l["c"]= 0 ################## FIT MCMC ##################################### # l.thaw(["a","b","c"]) #free parameters # l.fit(npepoch,npoc,yerr=weight) # # X0 = {"a":l["a"],"b":l["b"],"c":l["c"]} # Lims = {"a":[-1e-20,1e20],"b":[-1e-20,1e20], "c":[-1e-20,1e20]} # steps = {"a":0.01, "b":0.01,"c":0.1} # # l.fitMCMC(npepoch, npoc, X0, Lims, steps, yerr=weight, \ # iter=1000, burn=0, thin=1, \ # dbfile="mcmcExample.tmp") #------------------------------------------------------------------ # specific limits for MCMC paramters # ppa = {} # ppa["a"] = pymc.Uniform("a", value=l["a"], lower=-1., \ # upper=1.0, doc="Amplitude") # ppa["ecc"] = pymc.Uniform("ecc", value=l["ecc"], lower=0., \ # upper=1.0, doc="Eccentricity") # Use this for ppa # l.fitMCMC(npepoch, npoc, X0, Lims, steps, yerr=weight, \ # pymcPars=ppa,iter=2500, burn=0, thin=1, \ # dbfile="mcmcExample.tmp") ################################################################## ######## AUTO FIT is WORKING ##################################### # ranges = {"a":[-1e-20,1e20],"b":[-1e-20,1e20], "c":[-1e-20,1e20]} # l.autoFitMCMC(npepoch, npoc, ranges, yerr=weight, iter=100000, picky=False) ################################################################## ######## emcee ################################################### l.thaw(["a","b","c"]) #free parameters sampleArgs = {"iters":100000, "burn":200} priors = {"a":fuf.FuFPrior("limuniform", lower=-1e-20, upper=1e-20), \ "b":fuf.FuFPrior("limuniform", lower=-1e-20, upper=1e-20), \ "c":fuf.FuFPrior("limuniform", lower=-1e-20, upper=1e-20)} # Note that the filename should end in .emcee. Substitute this filename # in the following examples. l.fitEMCEE(npepoch, npoc, yerr=weight, sampleArgs=sampleArgs, \ dbfile="mcmcTA.emcee", priors=priors) ################################################################## l.parameterSummary() model = l.evaluate(npepoch) sepoch, fity = zip(*sorted(zip(npepoch, model))) fitPlot(npepoch, npoc, minType, method, sepoch,fity)