def runOneChain(iChain,convConn): # priors: normp = prior.uniform(10**-4,10**-2.5,isLog=True) # powerlaw norm alphap = prior.uniform(2.0,3.5) # powerlaw power nHp = prior.uniform(0.5,3.0) # nH (absorption) scareap = prior.uniform(1e-10,1e-6,isLog=True) # Sc line area area1p = prior.uniform(10**-8,10**-5,isLog=True) # nuisance line 1 center1p = prior.uniform(3.5,3.75) # sigma1p = prior.uniform(0.001,0.1) # natural width area2p = prior.uniform(10**-7,10**-5,isLog=True) # nuisance line 2 center2p = prior.uniform(3.75,4.0) # sigma2p = prior.uniform(0.001,0.1) # natural width g.priors = [normp,alphap,nHp,scareap,area1p,center1p,sigma1p,area2p,center2p,sigma2p] # parameters ('name', x_init, isFrozen): names = ['norm', 'alpha', 'nH', 'Sc area', 'area1', 'center1', 'sigma1', 'area2', 'center2', 'sigma2'] g.initParams = [] for i in range(len(g.priors)): if i == 2: # nH set by hwang '12 g.initParams.append(cob.param(names[i], 2.0, True)) else: g.initParams.append(cob.param(names[i], g.priors[i].sample(), False)) for i,p in enumerate(g.initParams): p.setPrior(i) # which indices are thawed: for i,p in enumerate(g.initParams): if(not p.isFrozen): g.thawedIdxs.append(i) # mass vector for parameters: g.masses = ones(len(g.initParams)) g.masses[0] = 1.0 # powerlaw norm g.masses[1] = 1.0 # powerlaw alpha #g.masses[2] = 1.0 # nH g.masses[3] = 1.0 # Sc area g.masses[4] = 1.0 # area 1 g.masses[5] = 1.0 # center 1 g.masses[6] = 0.5 # sigma 1 g.masses[7] = 1.0 # area 2 g.masses[8] = 1.0 # center 2 g.masses[9] = 0.5 # sigma 2 # data and likelihood: epochs = ['4634','4635','4636','4637','4638'] #epochs = ['4637'] datasets = [] fnames = [] for e in epochs: #froot = g.datadir+'cpoor' froot = g.datadir+'eastegg'+'-'+e fnames.append(froot) datasets.append(cob.dataset(froot, [3.4,5], g.datadir+'../bkgd', g.Aeastegg/g.Abkgd)) g.likelihood = likelihood.ScLike(datasets, g.codedir+'models/phabs1e22.txt') g.dumpInfo(g.initParams, g.priors, g.masses, g.likelihood,fnames=fnames) # start the chain: fname = g.chaindir+'chain-'+str(iChain)+'.txt' ch = cob.chain(fname,convConn) # evolve the chain: # (the convergence monitor will only send anything to this end of # the pipe once all chains have converged. evolve until then.) while not convConn.poll(): ch.evolve()
# jmrv 06.2013 # # how unlikely are the theiling upper limit values? # --------------- import scipy.optimize as so import numpy as np import chmcGlobals as g import likelihood import chainObject as cob #params = [norm,alpha,nH,scarea,area1,center1,sigma1,area2,center2,sigma2] params0 = [-0.447, 3.18, 2.0, -5.0, -5.0, 3.65, 5., -4.0, 3.87, 20.] # data and likelihood: root = '/Users/jmrv/Documents/school/mit/research/snr/scandium/' datasets = [cob.dataset(root+'reduced/theiling/cpoor', [3.4,5])] #root+'reduced/bkgd', g.Acpoor/g.Abkgd)] like = likelihood.ScLike(datasets, root+'code/models/phabs1e22.txt') def fitSpectrum(p, thawed = range(10)): params = convertParams(p, thawed) loglike = like.evaluate(params) #print loglike return loglike # feed fitting function a subset of params, in log where appropriate def convertParams(p, thawed): params = np.array(params0) params[thawed] = p logPars = [0,3,4,7]
import numpy as np import matplotlib.pyplot as plt import chainObject as cob import chmcGlobals as g root = '/data1/fgastro/sc/reduced/nustar/' datasets = [] epochs = ['4634','4635','4636','4637','4638'] for e in epochs: f = root+'westegg'+'-'+e datasets.append(cob.dataset(f, [3.4,5], root+'../bkgd', g.Acpoor/g.Abkgd)) n = len(datasets[0].counts) counts = np.zeros(n) for d in datasets: counts += d.counts E = datasets[0].ch2energy chRange = datasets[0].chRange plt.plot(E, counts, 'k-', drawstyle='steps') plt.xlim(3.4, 5) plt.ylim(0,1.1*max(counts[chRange[0]:chRange[1]])) plt.title('755 ks exposure of NuSTAR West') plt.ylabel('counts') plt.xlabel('keV') plt.savefig('/data1/fgastro/sc/plots/nustar-west.pdf')
norm = 10**-0.447 alpha = 3.18 nH = 2.0 scarea = 1e-5 area1 = 10**-5.0 center1 = 3.65 sigma1 = 0.005 area2 = 10**-4.00 center2 = 3.87 sigma2 = 0.02 params = [norm,alpha,nH,scarea,area1,center1,sigma1,area2,center2,sigma2] # data and likelihood: root = '/data1/fgastro/sc/reduced/theiling/' datasets = [cob.dataset(root+'cpoor', [3.4,5],root+'../bkgd', g.Acpoor/g.Abkgd)] like = likelihood.ScLike(datasets, g.codedir+'models/phabs1e22.txt') scfluxes = [1e-9, 1e-8, 1e-7, 1e-6, 1e-5, 10**-4.64] for f in scfluxes: params[3] = f print f, like.evaluate(params) like.plotSample(params, '/data1/fgastro/sc/plots/theiling/theilingLimits/flux-'+str(f)+'.pdf') # results: # # 1e-09 614.08228974 # 1e-08 614.082133295 # 1e-07 614.080856568 # 1e-06 614.09683565 # 1e-05 617.105004215
def runOneChain(iChain,convConn): # priors: normp = prior.uniform(1e-6,1e1,isLog=True) # powerlaw norm alphap = prior.uniform(1,5) # powerlaw power nHp = prior.uniform(0.5,3.0) # nH (absorption) # based on hwang+ 2012 scareap = prior.uniform(1e-9,1e-3,isLog=True) # Sc line area area1p = prior.uniform(1e-9,1e-3,isLog=True) # nuisance line 1 center1p = prior.uniform(3.5,3.75) # sigma1p = prior.uniform(0.001,0.1) # natural width area2p = prior.uniform(1e-9,1e-2,isLog=True) # nuisance line 2 center2p = prior.uniform(3.75,4.0) # sigma2p = prior.uniform(0.001,0.1) # natural width g.priors = [ normp,alphap, \ nHp, \ scareap, \ area1p,center1p,sigma1p, \ area2p,center2p,sigma2p] # parameters ('name', x_init, isFrozen): norm = cob.param('norm',10**-1.4,False) alpha = cob.param('alpha',3.3,False) nH = cob.param('nH',2.0,True) scarea = cob.param('Sc area',1e-7,False) area1 = cob.param('area1',10**-5.2,False) center1 = cob.param('center1',3.69,False) sigma1 = cob.param('sigma1',0.005,False) area2 = cob.param('area2',10**-4.5,False) center2 = cob.param('center2',3.86,False) sigma2 = cob.param('sigma2',0.01,False) g.initParams = [ norm,alpha, \ nH, \ scarea, \ area1,center1,sigma1, \ area2,center2,sigma2] for i,p in enumerate(g.initParams): p.setPrior(i) # which indexes are thawed: for i,p in enumerate(g.initParams): if(not p.isFrozen): g.thawedIdxs.append(i) # mass vector for parameters: g.masses = ones(len(g.initParams)) g.masses[0] = 0.2 # powerlaw norm g.masses[1] = 0.2 # powerlaw alpha #g.masses[2] = 0.1 # nH g.masses[3] = 0.01 # Sc area g.masses[4] = 0.01 # area 1 g.masses[5] = 0.01 # center 1 g.masses[6] = 0.01 # sigma 1 g.masses[7] = 0.01 # area 2 g.masses[8] = 0.01 # center 2 g.masses[9] = 0.01 # sigma 2 # data and likelihood: #epochs = ['4634','4635','4636','4637','4638'] epochs = ['4637'] datasets = [] fnames = [] for e in epochs: #froot = g.datadir+'fe' froot = g.datadir+'eastegg'+'-'+e fnames.append(froot) datasets.append(cob.dataset(froot, [3.4,5], g.datadir+'../bkgd', g.Aeastegg/g.Abkgd)) g.likelihood = likelihood.ScLike(datasets, g.codedir+'models/phabs1e22.txt') g.dumpInfo(g.initParams, g.priors, g.masses, g.likelihood,fnames=fnames) # start the chain: fname = g.chaindir+'chain-'+str(iChain)+'.txt' ch = cob.chain(fname,convConn) # evolve the chain: # (the convergence monitor will only send anything to this end of # the pipe once all chains have converged. evolve until then.) while not convConn.poll(): ch.evolve()