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 probcal(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, pulsefile = pulsefile) pf.chisq = chisq #print dof, chisq #print parfile, toafile #print chisq, smallestchisq #if chisq >= 999999999.:return 0 #smallestchisq = 308.28 try: return exp((smallestchisq - chisq)/2.) #Ingrid/Paul? except OverflowError: print chisq, smallestchisq print pf.parfile raise OverflowError
def probcal(pf): global smallestchisq pf.write() m2 = float(str(pf.M2[0])) Omega = float(str(pf.PAASCNODE)) sini = float(str(pf.SINI[0])) if m2 <= 0 or Omega > 360 or Omega < -360 or sini > 1.: 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, pulsefile = pulsefile) #print dof, chisq, smallestchisq pf.chisq = chisq if chisq < smallestchisq: smallestchisq = chisq try: #return exp((smallestchisq - chisq)/2.) #Ingrid/Paul? return (smallestchisq - chisq)/2. #Ingrid/Paul? except OverflowError: print chisq, smallestchisq print pf.parfile raise OverflowError
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): s, MarkovChain = argv
def mcmc(Chain, runtime, mixingtime=1000, stepsize=1): #mixingtime = 1000 #runtime = 50000 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) MarkovChain = Chain pf = PARfile(parfile) #pf = model(parfile) #pf.thawall() pf.freezeall('DMX_0') #pf.parameters['SINI'] = '0' #pf.parameters['M2'] = '0' #pf.parameters['XDOT'] = '0' pf.write() chisq, dof = tempofit(parfile, toafile = toafile, pulsefile = pulsefile) #pf.tempofit(toafile, pulsefile = pulsefile) chisq, dof = chisq, dof pf.matrix(toafile) #pf.freezeall() #pf.thawall('JUMP_') pf.write() #if 'PAASCNODE'in pf.__dict__: #plist = [x for x in pf.manifest if x in pf.parameters.keys()] + ['PAASCNODE'] #dict = {'BEST':[pf.__dict__[p][0] for p in plist[:-1]] + [pf.__dict__[p] for p in plist[-1:]], 'parfile':pf.parfile, 'parameters':plist + ['chisq']} #else: plist = [x for x in pf.manifest if x in pf.parameters.keys()] dict = {'BEST':[pf.__dict__[p][0] for p in plist] + [pf.PAASCNODE, chisq], 'parfile':pf.parfile, 'parameters':plist + ['PAASCNODE', 'chisq']} pickle.dump(dict, open('%s/bestpar.p' % cwd, 'w'), protocol=2) p0 = probcal(pf) p = p0 #print 'P0', p0 #try: #MChain = pickle.load(open('MChain.p', 'r')) #except: #MChain = {'Chain':[]} #MChain['parameters'] = plist #pickle.dump(MChain, open('MChain.p', 'w')) n = count() m = count() while n.next() <= mixingtime + runtime: npf = pf.randomnew(stepsize=stepsize) randomnew(npf, stepsize) p1 = probcal(npf) c = m.next() if c % 30 == 0:pb(c) if p1 > p0: if c > mixingtime: MarkovChain.append([npf.__dict__[p][0] for p in plist] + [npf.PAASCNODE, npf.chisq]) pf = npf p0 = p1 if p1 > p: p = p1 #if 'PAASCNODE' in plist: #dict['BEST'] = [pf.__dict__[p][0] for p in plist[:-1]] + [pf.__dict__[p] for p in plist[-1:]] + [npf.chisq] #else: dict['BEST'] = [npf.__dict__[p][0] for p in plist] + [npf.PAASCNODE, npf.chisq] pickle.dump(dict, open('%s/bestpar.p' % cwd, 'w'), protocol=2) else: t = uniform(0,1,1)[0] if t < p1/p0: if c > mixingtime: MarkovChain.append([npf.__dict__[p][0] for p in plist] + [npf.PAASCNODE, npf.chisq]) pf = npf p0 = p1 else: if c > mixingtime: MarkovChain.append([pf.__dict__[p][0] for p in plist] + [npf.PAASCNODE, npf.chisq]) #print MarkovChain #print best print '%d points added to the Chain.' % len(MarkovChain) #OldChain = pickle.load(open('MChain.p','r'))['Chain'] #dict['Chain'] = OldChain + MarkovChain #dict['Chain'] = MarkovChain os.chdir(cwd)