def mcmc(Chain, runtime, mixingtime=1000): #mixingtime = 1000 #runtime = 50000 pb = progressBar(maxValue=runtime) 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)) motifile(toafile, cwd, tmpdir) dict = pickle.load(open('%s/bestpar.p' % cwd, 'r')) Omega, cosi, m2 = dict['BEST'] MarkovChain = Chain pf = PARfile(parfile) pf.thawall() pf.write() pf.matrix(toafile) pf.freezeall() pf.write() p0 = probcal(Omega, cosi, m2, pf) p = p0 best = (Omega, cosi, m2) #dict = {'BEST':best} #pickle.dump(dict, open('bestpar.p', 'w')) n = count() m = count() while n.next() < mixingtime + runtime: npf = pf.randomnew() Ncosi = cosi + uniform(-0.05, 0.05) #Nm2 = m2 + normal(0, 0.02) NOmega = (Omega + normal(0, 5)) #Nsini = float(str(npf.SINI[0])) #Ncosi = -1. * sqrt(1 - Nsini**2) Nm2 = float(str(npf.M2[0])) #print Ncosi, cosi, Ncosi - cosi, Nm2 - m2 p1 = probcal(NOmega, Ncosi, Nm2, npf) c = m.next() if p1 > p0: if c > mixingtime: MarkovChain.append((NOmega, Ncosi, Nm2)) pb(c - mixingtime) pf = npf p0 = p1 cosi, m2, Omega = Ncosi, Nm2, NOmega if p1 > p: best = (NOmega, Ncosi, Nm2) p = p1 dict['BEST'] = best pickle.dump(dict, open('%s/bestpar.p' % cwd, 'w')) else: t = uniform(0, 1, 1)[0] if t < p1 / p0: if c > mixingtime: MarkovChain.append((NOmega, Ncosi, Nm2)) pb(c - mixingtime) pf = npf p0 = p1 cosi, m2, Omega = Ncosi, Nm2, NOmega else: if c > mixingtime: MarkovChain.append((Omega, cosi, m2)) #print MarkovChain print best print '%d new points generated.' % len(MarkovChain) #OldChain = pickle.load(open('MChain.p','r'))['Chain'] #dict['Chain'] = OldChain + MarkovChain #dict['Chain'] = MarkovChain #os.rmdir(tmpdir) os.chdir(cwd)
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)
#pf = PARfile('./1713.ext.PBDOT.par') #pf = PARfile('./1713.noguppi.par.t2.Jul30') #pf = PARfile('./1713.noguppi.par.mcmc') #pf = PARfile('./1713.ext.FD.par.t2') #pf = PARfile('./1713.DMX.final.par.t2') #pf = PARfile('./1713.DMX.noOMDOT.par.t2') #pf = PARfile('./1909.Paul.par') #pf = PARfile('./1909-3744.par') #pf = PARfile('./1713.Oct.mcmc.par') #pf = PARfile('./1713.Nov.test.par') #pf = PARfile('./mcmc.par') #pf = PARfile('./mcmcresult.par') #pf = PARfile('./J1713+0747.par') #pf = PARfile('./1713.final.par') #pf = PARfile('./Oct.T2.par') pf = PARfile('./Feb.T2.RN.par') def Pbdot_exc(psr, GdotOG, KD): GdotOG = GdotOG/secperday/365.24218967 M1 = psr['M1'] M2 = psr['M2'] q = M1/M2 return -2.* GdotOG * (1 - (1 + M2/2./(M1+M2))*psr['Sp']) - 4. * PI**2 *Tsun*M2 * KD * psr['Sp']**2 * q/(q+1)/psr['Pb']**2 J1713 = {'M1':M1(pf), 'M2':float(pf.M2[0]), 'Sp':Sp(pf), 'Pb':float(pf.PB[0])*secperday} #print J1713 #print Pbdot_exc(J1713, 6.e-13, 2.e-4) #print '1:', Pbdot_exc(J1713, 1.52789743984e-14, 0.00163158650413) J1012 = {'M2':0.16, 'M1':0.16*10.5, 'Pb':0.60467271355*secperday} J1012['Sp'] = 0.1*J1012['M1']
#pf = PARfile('./1713.ext.PBDOT.par') #pf = PARfile('./1713.noguppi.par.t2.Jul30') #pf = PARfile('./1713.noguppi.par.mcmc') #pf = PARfile('./1713.ext.FD.par.t2') #pf = PARfile('./1713.DMX.final.par.t2') #pf = PARfile('./1713.DMX.noOMDOT.par.t2') #pf = PARfile('./1909.Paul.par') #pf = PARfile('./1909-3744.par') #pf = PARfile('./1713.Oct.mcmc.par') #pf = PARfile('./1713.Nov.test.par') #pf = PARfile('./mcmc.par') #pf = PARfile('./mcmcresult.par') #pf = PARfile('./J1713+0747.par') #pf = PARfile('./1713.final.par') #pf = PARfile('./Oct.T2.par') pf = PARfile('./Feb.T2.RN.par') def Pbdot_exc(psr, GdotOG, KD): GdotOG = GdotOG / secperday / 365.24218967 M1 = psr['M1'] M2 = psr['M2'] q = M1 / M2 return -2. * GdotOG * ( 1 - (1 + M2 / 2. / (M1 + M2)) * psr['Sp'] ) - 4. * PI**2 * Tsun * M2 * KD * psr['Sp']**2 * q / (q + 1) / psr['Pb']**2 J1713 = { 'M1': M1(pf), 'M2': float(pf.M2[0]),
parser.add_option("-m", '--mixing', type='int', nargs=1, dest='mixing', help="number of mixing steps", default=1000) parser.add_option("-p", '--parallel', type='int', nargs=1, dest='paral', help="number of parallel processes") parser.add_option("-s", '--seed', type='int', nargs=1, dest='seed', default=int(os.getpid()), help="random number seed") parser.add_option("-z", '--stepsize', type='float', nargs=1, dest='stepsize', default=1., help="step size") (options, args) = parser.parse_args(args=sys.argv[1:]) 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)
err1 = A_x * fac1 err2 = A_z * sqrt(fac1**2 + fac2**2 + fac3**2) err = sqrt(err1**2 + err2**2) * float(Pb) return val, err if __name__ == '__main__': from datatools.tempo import PARfile #pf = PARfile('1909-3744.par') #pf = PARfile('./J1909.zww.par') #pf = PARfile('./J1713+0747.par') #pf = PARfile('./1713.Sep.par') #pf = PARfile('./mcmcresult.par') #pf = PARfile('./1713.Apr.par') pf = PARfile('./1713.all.par') if pf.PBDOT[0] > Decimal('1.e-10'):#tempo 1 units pf.PBDOT[0] *= Decimal('1.e-12') pf.PBDOT[1] *= Decimal('1.e-12') from round import shortform as SF Shl = Shlkovskii(pf) Gal = Pbdot_Gal(pf) GW = Pbdot_GW(pf) if pf.__dict__.has_key('PSR'): print pf.PSR else: print pf.PSRJ print 'PBdot(shlkovskii):', SF(Shl) print 'PBdot(Galaxtic):' , SF(Gal) print 'PBdot(GW):', SF(GW)
err1 = A_x * fac1 err2 = A_z * sqrt(fac1**2 + fac2**2 + fac3**2) err = sqrt(err1**2 + err2**2) * float(Pb) return val, err if __name__ == '__main__': from datatools.tempo import PARfile #pf = PARfile('1909-3744.par') #pf = PARfile('./J1909.zww.par') #pf = PARfile('./J1713+0747.par') #pf = PARfile('./1713.Sep.par') #pf = PARfile('./mcmcresult.par') #pf = PARfile('./1713.Apr.par') pf = PARfile('./Oct.T2.par') if pf.PBDOT[0] > Decimal('1.e-10'):#tempo 1 units pf.PBDOT[0] *= Decimal('1.e-12') pf.PBDOT[1] *= Decimal('1.e-12') from round import shortform as SF Shl = Shlkovskii(pf) Gal = Pbdot_Gal(pf) GW = Pbdot_GW(pf) if pf.__dict__.has_key('PSR'): print pf.PSR else: print pf.PSRJ print 'PBdot(shlkovskii):', SF(Shl) print 'PBdot(Galaxtic):' , SF(Gal) print 'PBdot(GW):', SF(GW)
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)
parser.add_option("-m", '--mixing', type='int', nargs=1, dest='mixing', help="number of mixing steps", default=1000) parser.add_option("-p", '--parallel', type='int', nargs=1, dest='paral', help="number of parallel processes") parser.add_option("-s", '--seed', type='int', nargs=1, dest='seed', default=int(os.getpid()), help="random number seed") parser.add_option("-z", '--stepsize', type='float', nargs=1, dest='stepsize', default=1., help="step size") (options, args) = parser.parse_args(args=sys.argv[1:]) 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) pf.freezeall() 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 def run(s): seed(s) # assigning different initial seed for the random number generator in different threads. #steps = 50000 with MChain() as Chain: #print steps, tmpdir
def mcmc(Chain, runtime, mixingtime=1000): #mixingtime = 1000 #runtime = 50000 pb = progressBar(maxValue = runtime) 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)) motifile(toafile, cwd, tmpdir) dict = pickle.load(open('%s/bestpar.p' % cwd, 'r')) Omega, cosi, m2 = dict['BEST'] MarkovChain = Chain pf = PARfile(parfile) pf.thawall() pf.write() pf.matrix(toafile) pf.freezeall() pf.write() p0 = probcal(Omega, cosi, m2, pf) p = p0 best = (Omega, cosi, m2 ) #dict = {'BEST':best} #pickle.dump(dict, open('bestpar.p', 'w')) n = count() m = count() while n.next() < mixingtime + runtime: npf = pf.randomnew() Ncosi = cosi + uniform(-0.05, 0.05) #Nm2 = m2 + normal(0, 0.02) NOmega = (Omega + normal(0, 5)) #Nsini = float(str(npf.SINI[0])) #Ncosi = -1. * sqrt(1 - Nsini**2) Nm2 = float(str(npf.M2[0])) #print Ncosi, cosi, Ncosi - cosi, Nm2 - m2 p1 = probcal(NOmega, Ncosi, Nm2, npf) c = m.next() if p1 > p0: if c > mixingtime: MarkovChain.append((NOmega, Ncosi, Nm2 )) pb(c - mixingtime) pf = npf p0 = p1 cosi, m2, Omega = Ncosi, Nm2, NOmega if p1 > p: best = (NOmega, Ncosi, Nm2) p = p1 dict['BEST'] = best pickle.dump(dict, open('%s/bestpar.p' % cwd, 'w')) else: t = uniform(0,1,1)[0] if t < p1/p0: if c > mixingtime: MarkovChain.append((NOmega, Ncosi, Nm2 )) pb(c - mixingtime) pf = npf p0 = p1 cosi, m2, Omega = Ncosi, Nm2, NOmega else: if c > mixingtime: MarkovChain.append((Omega, cosi, m2)) #print MarkovChain print best print '%d new points generated.' %len(MarkovChain) #OldChain = pickle.load(open('MChain.p','r'))['Chain'] #dict['Chain'] = OldChain + MarkovChain #dict['Chain'] = MarkovChain #os.rmdir(tmpdir) os.chdir(cwd)
dec = Dec(pf.DECJ[0]) pos = coord.SkyCoord(str(ra) + ' ' + str(dec)) l = pos.galactic.l.rad b = pos.galactic.b.rad L = pos.galactic.l.deg B = pos.galactic.b.deg Pb = float(pf.PB[0] * secperday) d = float(1 / pf.PX[0]) pf.DIST = d print 'L, B, D:', L, B, d #output = getoutput('./GalPotMcMillan2016/calcGalPdot.exe %s %s %s' % (L, B, d)) output = getoutput('./GalPotMcMillan2016/calcGalAcc.exe %s %s %s' % (L, B, d)) print output, np.sqrt(sum([float(f)**2 for f in output.split(' ')])) px = np.random.normal(float(pf.PX[0]), float(pf.PX[1]), 10000) res = np.array(spamit(McMillanPot, [(L, B, 1 / x) for x in px])) val = res.mean() * Pb fac2 = 8 / 240 #Omega_0 fac3 = 0.16 / 8.34 #R0 Reid et al 2014 fac1 = res.std() / res.mean() err = abs(sqrt(fac1**2 + fac2**2 + fac3**2) * val) return val, err if __name__ == '__main__': from datatools.tempo import tempofit, tempo2fit, touchparfile, uniquename, PARfile pf = PARfile('1713.cut.par') print Pbdot_Gal(pf)
err1 = A_x * fac1 err2 = A_z * sqrt(fac1**2 + fac2**2 + fac3**2) err = sqrt(err1**2 + err2**2) * float(Pb) return val, err if __name__ == '__main__': from datatools.tempo import PARfile #pf = PARfile('1909-3744.par') #pf = PARfile('./J1909.zww.par') #pf = PARfile('./J1713+0747.par') #pf = PARfile('./1713.Sep.par') #pf = PARfile('./mcmcresult.par') #pf = PARfile('./1713.Apr.par') #pf = PARfile('./Oct.T2.par') pf = PARfile('./Feb.T2.RN.par') if pf.PBDOT[0] > Decimal('1.e-10'): #tempo 1 units pf.PBDOT[0] *= Decimal('1.e-12') pf.PBDOT[1] *= Decimal('1.e-12') from round import shortform as SF Shl = Shlkovskii(pf) Gal = Pbdot_Gal(pf) GW = Pbdot_GW(pf) if pf.__dict__.has_key('PSR'): print pf.PSR else: print pf.PSRJ print 'PBdot(shlkovskii):', SF(Shl) print 'PBdot(Galaxtic):', SF(Gal) print 'PBdot(GW):', SF(GW)