def probcal(Omega, cosi, m2, pf): pf.write() if m2 <= 0 or Omega > 360 or Omega < -360: return 0 sini = sqrt(1 - cosi**2) xdot = -1.* fac * x * mu * (cosi/sini) * sin((thetamu-Omega)*twopi/360.) touchparfile(parfile, NITS=1, PAASCNODE=Omega, SINI = sini, M2 = m2, XDOT = xdot) chisq, dof = tempofit(parfile, toafile = toafile) #print chisq, dof if chisq >= 99999.:return 0 #return (dof/chisq) #return chisqprob(chisq, dof) smallest = 308.28 #f = 1./sqrt(6.283185307179586) #return f*exp(smallest - chisq)*sqrt(chisq - smallest) return exp((smallest - chisq)/2.) #Ingrid/Paul?
def probcal(Omega, cosi, m2, pf): pf.write() if m2 <= 0 or Omega > 360 or Omega < -360: return 0 sini = sqrt(1 - cosi**2) xdot = -1. * fac * x * mu * (cosi / sini) * sin( (thetamu - Omega) * twopi / 360.) touchparfile(parfile, NITS=1, PAASCNODE=Omega, SINI=sini, M2=m2, XDOT=xdot) chisq, dof = tempofit(parfile, toafile=toafile) #print chisq, dof if chisq >= 99999.: return 0 #return (dof/chisq) #return chisqprob(chisq, dof) smallest = 308.28 #f = 1./sqrt(6.283185307179586) #return f*exp(smallest - chisq)*sqrt(chisq - smallest) return exp((smallest - chisq) / 2.) #Ingrid/Paul?
def mcmc(Chain, runtime, MarkovChain, mixingtime=1000, stepsize=1, seed=0 ): pb = progressBar(maxValue = runtime + mixingtime) cwd=os.getcwd() tmpdir = cwd+'/.'+uniquename() if not tmpdir == None: if os.path.exists(tmpdir): os.chdir(tmpdir) else: os.mkdir(tmpdir) os.chdir(tmpdir) os.system('cp %s/%s %s/%s' % (cwd, parfile, tmpdir, parfile)) os.system('cp %s/%s %s/%s' % (cwd, pulsefile, tmpdir, pulsefile)) motifile(toafile, cwd, tmpdir) touchparfile(parfile, NITS=1) pf = PARfile(parfile) #chisq, dof = tempofit(parfile, toafile = toafile, pulsefile = pulsefile) pf.matrix(toafile) pf.freezeall() pf.thawall('JUMP') p0 = probcal(pf) pmax = p0 ThisChain = [] c = 0 randomlist = uniform(0,1,stepsize) while c <= mixingtime + runtime: c+=1 npf = pf.randomnew(stepsize=stepsize) #randomnew(npf, stepsize) #only use this for 1713 p1 = probcal(npf) if c % 30 == 0:pb(c) if c > mixingtime: t = randomlist[c-mixingtime-1] if t < exp(p1-p0): Chain.Chain.append([npf.__dict__[p][0] for p in plist] + [ npf.chisq]) pf = npf p0 = p1 if p1 > pmax: pmax = p1 bestpar['BEST'] = [npf.__dict__[p][0] for p in plist] + [ npf.chisq] pickle.dump(bestpar, open('%s/bestpar.p' % cwd, 'wb', 0), protocol=2) else: Chain.Chain.append([pf.__dict__[p][0] for p in plist] + [ npf.chisq]) if c % (100+(seed%100)) == 0: MarkovChain.extend(Chain.Chain) ThisChain.extend(Chain.Chain) Chain.Chain = [] #empty the list #try: TC = np.array(ThisChain) dit = {'Chain':TC} pid = str(os.getpid()) try: os.remove(cwd+'/MChain.p'+pid) except:pass f = open(cwd+'/MChain.p'+pid, 'wb', 0) pickle.dump(dit, f, protocol=2) f.flush() f.close() del dit del TC MarkovChain.extend(Chain.Chain) os.chdir(cwd)
print options parfile = options.parfile toafile = options.toafile pulsefile = options.pulsefile steps = options.steps mixing = options.mixing rseed = options.seed stepsize = options.stepsize px = options.paral pf = PARfile(parfile) #pf = model(parfile) #touchparfile(parfile, NITS=1) #pf.thawall('JUMP_') pf.write('mcmc.par') touchparfile('mcmc.par', NITS=1) chisq, dof = tempofit('mcmc.par', toafile = toafile, pulsefile = pulsefile) #pf.tempofit(TOAfile(toafile), pulsefile = pulsefile) smallestchisq = chisq #print 'smallestchisq', smallestchisq, dof cwd=os.getcwd() plist = [x for x in pf.manifest if x in pf.parameters.keys() if not x.startswith('DMX') and not x.startswith('JUMP')] bestpar = {'BEST':[pf.__dict__[p][0] for p in plist] + [pf.PAASCNODE, chisq], 'parfile':pf.parfile, 'parameters':plist + ['PAASCNODE', 'chisq']} pickle.dump(bestpar, open('%s/bestpar.p' % cwd, 'w', 0), protocol=2) def run(argv):