1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
#! /usr/bin/env python
# author : S. Mandalia
# s.p.mandalia@qmul.ac.uk
#
# date : March 17, 2018
"""
HESE BSM flavour ratio analysis script
"""
from __future__ import absolute_import, division
import argparse
from functools import partial
import numpy as np
import GolemFitPy as gf
from utils import gf as gf_utils
from utils import likelihood as llh_utils
from utils import mcmc as mcmc_utils
from utils import misc as misc_utils
from utils import plot as plot_utils
from utils.enums import EnergyDependance, Likelihood, MCMCSeedType, ParamTag
from utils.fr import MASS_EIGENVALUES, normalise_fr, fr_to_angles
from utils.misc import enum_parse, Param, ParamSet
def define_nuisance():
"""Define the nuisance parameters to scan over with default values,
ranges and sigma.
"""
tag = ParamTag.NUISANCE
nuisance = ParamSet(
Param(name='convNorm', value=1., seed=[0.5, 2. ], ranges=[0. , 50.], std=0.3, tag=tag),
Param(name='promptNorm', value=0., seed=[0. , 6. ], ranges=[0. , 50.], std=0.05, tag=tag),
Param(name='muonNorm', value=1., seed=[0. , 2. ], ranges=[0. , 50.], std=0.1, tag=tag),
Param(name='astroNorm', value=1., seed=[4. , 10.], ranges=[0. , 50.], std=0.1, tag=tag),
Param(name='astroDeltaGamma', value=2., seed=[2. , 3. ], ranges=[-5., 5. ], std=0.1, tag=tag)
)
return nuisance
def nuisance_argparse(parser):
nuisance_paramset = define_nuisance()
for parm in nuisance_paramset:
parser.add_argument(
'--'+parm.name, type=float, default=parm.value,
help=parm.name+' to inject'
)
def get_paramsets(args):
"""Make the paramsets for generating the Asmimov MC sample and also running
the MCMC.
"""
asimov_paramset = []
mcmc_paramset = []
if args.likelihood == Likelihood.GOLEMFIT:
nuisance_paramset = define_nuisance()
asimov_paramset.extend(nuisance_paramset.params)
mcmc_paramset.extend(nuisance_paramset.params)
for parm in nuisance_paramset:
parm.value = args.__getattribute__(parm.name)
tag = ParamTag.BESTFIT
flavour_angles = fr_to_angles(args.measured_ratio)
asimov_paramset.extend([
Param(name='astroFlavorAngle1', value=flavour_angles[0], ranges=[0., 1.], std=0.2, tag=tag),
Param(name='astroFlavorAngle2', value=flavour_angles[1], ranges=[-1., 1.], std=0.2, tag=tag),
])
asimov_paramset = ParamSet(asimov_paramset)
if not args.fix_mixing and not args.fix_mixing_almost:
tag = ParamTag.MMANGLES
mcmc_paramset.extend([
Param(name='s_12^2', value=0.5, ranges=[0., 1.], std=0.2, tex=r'\tilde{s}_{12}^2', tag=tag),
Param(name='c_13^4', value=0.5, ranges=[0., 1.], std=0.2, tex=r'\tilde{c}_{13}^4', tag=tag),
Param(name='s_23^2', value=0.5, ranges=[0., 1.], std=0.2, tex=r'\tilde{s}_{23}^4', tag=tag),
Param(name='dcp', value=np.pi, ranges=[0., 2*np.pi], std=0.2, tex=r'\tilde{\delta_{CP}}', tag=tag)
])
if args.fix_mixing_almost:
tag = ParamTag.MMANGLES
mcmc_paramset.extend([
Param(name='s_23^2', value=0.5, ranges=[0., 1.], std=0.2, tex=r'\tilde{s}_{23}^4', tag=tag)
])
if not args.fix_scale:
logLam, scale_region = np.log10(args.scale), np.log10(args.scale_region)
lL_range = (logLam-scale_region, logLam+scale_region)
tag = ParamTag.SCALE
mcmc_paramset.append(
Param(name='logLam', value=logLam, ranges=lL_range, std=3,
tex=r'{\rm log}_{10}\Lambda'+plot_utils.get_units(args.dimension), tag=tag)
)
if not args.fix_source_ratio:
tag = ParamTag.SRCANGLES
mcmc_paramset.extend([
Param(name='s_phi4', value=0.5, ranges=[0., 1.], std=0.2, tex=r'sin^4(\phi)', tag=tag),
Param(name='c_2psi', value=0.5, ranges=[0., 1.], std=0.2, tex=r'cos(2\psi)', tag=tag)
])
mcmc_paramset = ParamSet(mcmc_paramset)
return asimov_paramset, mcmc_paramset
def process_args(args):
"""Process the input args."""
if args.fix_mixing and args.fix_scale:
raise NotImplementedError('Fixed mixing and scale not implemented')
if args.fix_mixing and args.fix_mixing_almost:
raise NotImplementedError(
'--fix-mixing and --fix-mixing-almost cannot be used together'
)
if args.run_bayes_factor and args.fix_scale:
raise NotImplementedError(
'--run-bayes-factor and --fix-scale cannot be used together'
)
args.measured_ratio = normalise_fr(args.measured_ratio)
if args.fix_source_ratio:
args.source_ratio = normalise_fr(args.source_ratio)
if args.energy_dependance is EnergyDependance.SPECTRAL:
args.binning = np.logspace(
np.log10(args.binning[0]), np.log10(args.binning[1]), args.binning[2]+1
)
if not args.fix_scale:
if args.energy_dependance is EnergyDependance.MONO:
args.scale = np.power(
10, np.round(np.log10(MASS_EIGENVALUES[1]/args.energy)) - \
np.log10(args.energy**(args.dimension-3))
)
elif args.energy_dependance is EnergyDependance.SPECTRAL:
args.scale = np.power(
10, np.round(
np.log10(MASS_EIGENVALUES[1]/np.power(10, np.average(np.log10(args.binning)))) \
- np.log10(np.power(10, np.average(np.log10(args.binning)))**(args.dimension-3))
)
)
"""Estimate the scale of when to expect the BSM term to contribute."""
def parse_args():
"""Parse command line arguments"""
parser = argparse.ArgumentParser(
description="BSM flavour ratio analysis",
formatter_class=misc_utils.SortingHelpFormatter,
)
parser.add_argument(
'--measured-ratio', type=int, nargs=3, default=[1, 1, 1],
help='Set the central value for the measured flavour ratio at IceCube'
)
parser.add_argument(
'--sigma-ratio', type=float, default=0.01,
help='Set the 1 sigma for the measured flavour ratio for a gaussian LLH'
)
parser.add_argument(
'--fix-source-ratio', type=misc_utils.parse_bool, default='False',
help='Fix the source flavour ratio'
)
parser.add_argument(
'--energy-dependance', default='spectral', type=partial(enum_parse, c=EnergyDependance),
choices=EnergyDependance,
help='Type of energy dependance to use in the BSM fit'
)
parser.add_argument(
'--spectral-index', default=-2, type=int,
help='Spectral index for spectral energy dependance'
)
parser.add_argument(
'--binning', default=[1e4, 1e7, 5], type=float, nargs=3,
help='Binning for spectral energy dependance'
)
parser.add_argument(
'--run-bayes-factor', type=misc_utils.parse_bool, default='False',
help='Make the bayes factor plot for the scale'
)
parser.add_argument(
'--bayes-bins', type=int, default=10,
help='Number of bins for the Bayes factor plot'
)
parser.add_argument(
'--bayes-live-points', type=int, default=400,
help='Number of live points for MultiNest evaluations'
)
parser.add_argument(
'--bayes-output', type=str, default='./mnrun/',
help='Folder to store MultiNest evaluations'
)
parser.add_argument(
'--source-ratio', type=int, nargs=3, default=[2, 1, 0],
help='Set the source flavour ratio for the case when you want to fix it'
)
parser.add_argument(
'--no-bsm', type=misc_utils.parse_bool, default='False',
help='Turn off BSM terms'
)
parser.add_argument(
'--fix-mixing', type=misc_utils.parse_bool, default='False',
help='Fix all mixing parameters to bi-maximal mixing'
)
parser.add_argument(
'--fix-mixing-almost', type=misc_utils.parse_bool, default='False',
help='Fix all mixing parameters except s23'
)
parser.add_argument(
'--fix-scale', type=misc_utils.parse_bool, default='False',
help='Fix the new physics scale'
)
parser.add_argument(
'--scale', type=float, default=1e-30,
help='Set the new physics scale'
)
parser.add_argument(
'--scale-region', type=float, default=1e10,
help='Set the size of the box to scan for the scale'
)
parser.add_argument(
'--dimension', type=int, default=3,
help='Set the new physics dimension to consider'
)
parser.add_argument(
'--energy', type=float, default=1000,
help='Set the energy scale'
)
parser.add_argument(
'--seed', type=int, default=99,
help='Set the random seed value'
)
parser.add_argument(
'--threads', type=misc_utils.thread_type, default='1',
help='Set the number of threads to use (int or "max")'
)
parser.add_argument(
'--outfile', type=str, default='./untitled',
help='Path to output chains'
)
llh_utils.likelihood_argparse(parser)
mcmc_utils.mcmc_argparse(parser)
gf_utils.gf_argparse(parser)
plot_utils.plot_argparse(parser)
nuisance_argparse(parser)
return parser.parse_args()
def main():
args = parse_args()
process_args(args)
misc_utils.print_args(args)
np.random.seed(args.seed)
asimov_paramset, mcmc_paramset = get_paramsets(args)
outfile = misc_utils.gen_outfile_name(args)
print '== {0:<25} = {1}'.format('outfile', outfile)
if args.run_mcmc:
if args.likelihood is Likelihood.GOLEMFIT:
fitter = gf_utils.setup_fitter(args, asimov_paramset)
else:
fitter = None
ln_prob = partial(
llh_utils.ln_prob, args=args, fitter=fitter,
asimov_paramset=asimov_paramset, mcmc_paramset=mcmc_paramset
)
ndim = len(mcmc_paramset)
if args.mcmc_seed_type == MCMCSeedType.UNIFORM:
p0 = mcmc_utils.flat_seed(
mcmc_paramset, nwalkers=args.nwalkers
)
elif args.mcmc_seed_type == MCMCSeedType.GAUSSIAN:
p0 = mcmc_utils.gaussian_seed(
mcmc_paramset, nwalkers=args.nwalkers
)
samples = mcmc_utils.mcmc(
p0 = p0,
ln_prob = ln_prob,
ndim = ndim,
nwalkers = args.nwalkers,
burnin = args.burnin,
nsteps = args.nsteps,
threads = 1
# TODO(shivesh): broken because you cannot pickle a GolemFitPy object
# threads = misc_utils.thread_factors(args.threads)[0]
)
mcmc_utils.save_chains(samples, outfile)
plot_utils.chainer_plot(
infile = outfile+'.npy',
outfile = outfile[:5]+outfile[5:].replace('data', 'plots'),
outformat = ['pdf'],
args = args,
mcmc_paramset = mcmc_paramset
)
if args.run_bayes_factor:
# TODO(shivesh)
import pymultinest
from pymultinest.solve import solve
from pymultinest.watch import ProgressPrinter
if not args.run_mcmc and args.likelihood is Likelihood.GOLEMFIT:
fitter = gf_utils.setup_fitter(args, asimov_paramset)
else:
fitter = None
sc_range = mcmc_paramset.from_tag(ParamTag.SCALE)[0].ranges
scales = np.linspace(
sc_range[0], sc_range[1], args.bayes_bins+1
)
p = mcmc_paramset.from_tag(ParamTag.SCALE, invert=True)
n_params = len(p)
prior_ranges = p.ranges
def CubePrior(cube, ndim, nparams):
# default are uniform priors
return ;
arr = []
for s in scales:
theta = np.zeros(n_params)
def lnProb(cube, ndim, nparams):
for i in range(ndim):
prange = prior_ranges[i][1] - prior_ranges[i][0]
theta[i] = prange*cube[i] + prior_ranges[i][0]
theta_ = np.array(theta.tolist() + [s])
return llh_utils.triangle_llh(
theta=theta_,
args=args,
asimov_paramset=asimov_paramset,
mcmc_paramset=mcmc_paramset,
fitter=fitter
)
prefix = 'mnrun_{0:.0E}'.format(np.power(10, s)) + '_' + outfile[2:]
print 'begin running evidence calculation for {0}'.format(prefix)
result = pymultinest.run(
LogLikelihood=lnProb,
Prior=CubePrior,
n_dims=n_params,
n_live_points=args.bayes_live_points,
outputfiles_basename=prefix,
# resume=False
)
analyzer = pymultinest.Analyzer(outputfiles_basename=prefix, n_params=n_params)
a_lnZ = analyzer.get_stats()['global evidence']
print 'Evidence = {0}'.format(a_lnZ)
arr.append([s, a_lnZ])
out = args.bayes_output+'fr_evidence_'+outfile[2:]+'.npy'
misc_utils.make_dir(out)
np.save(out, np.array(arr))
b_out = args.bayes_output+'fr_evidence_'+outfile[2:]
plot_utils.bayes_factor_plot(
infile=b_out+'.npy', outfile=b_out, outformat=['png'], args=args
)
print "DONE!"
main.__doc__ = __doc__
if __name__ == '__main__':
main()
|