Negative Log Likelihood Module

ps

An point source Likelihood obj. which can generate TS distribution for bg. only hypothesis and sig. + bg. hypothesis. ...

Attributes:
  • Srcdec (float) –

    Declination of point src. in deg.

  • Srcra (float) –

    RightAscension of point src. in deg.

  • data (dict) –

    Dict. consisting of numpy arrays of evt. info. atleast EVDec, EvRA, NKGSize

  • psrcsim (dict) –

    Dict. consisting of numpy arrays of simulated gamma evt. info. atleast EVDec, EvRA, NKGSize.

  • n (int) –

    n is the average of poisson distrubtion of source events injected after drawing from psrcsim

  • inj (bool) –

    If TRUE point source simulation will be injected before calculating TS.

Source code in g3py/psLLH.py
  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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
class ps:
    """
    An point source Likelihood obj. which can generate TS distribution for
    bg. only hypothesis and sig. + bg. hypothesis.
    ...

    Attributes
    ----------

    Srcdec : float
        Declination of point src. in deg.

    Srcra : float
        RightAscension of point src. in deg.

    data : dict
        Dict. consisting of numpy arrays of evt. info. atleast EVDec, EvRA, NKGSize

    psrcsim : dict
        Dict. consisting of numpy arrays of simulated gamma evt. info. atleast EVDec, EvRA, NKGSize.

    n: int
        n is the average of poisson distrubtion of source events injected after drawing from psrcsim

    inj: bool
        If TRUE point source simulation will be injected before calculating TS.

    Methods
    -------

    """

    def __init__(self, Srcdec, SrcRA, data, psrcdata=None, n=0, inj = False):
        """
        Constructs all the necessary attributes for the point source Likelihood obj.

        Parameters
        ----------
        Srcdec : float
            Declination of point src. in deg.

        Srcra : float
            RightAscension of point src. in deg.

        data : dict
            Dict. consisting of numpy arrays of evt. info. atleast EVDec, EvRA, NKGSize

        psrcsim : dict
            Dict. consisting of numpy arrays of simulated gamma evt. info. atleast EVDec, EvRA, NKGSize.

        n: int
            n is the average of poisson distrubtion of source events injected after drawing from psrcsim

        inj: bool
            If TRUE point source simulation will be injected before calculating TS.
        """

        self.Srcdec = Srcdec
        self.SrcRA = SrcRA

        (
            self.bckg_spt_pdf,
            self.bckg_spt_cdf,
            self.bckg_spt_inv_cdf,
            self.sindec_min,
            self.sindec_max,
        ) = IRF.spt_pdf()
        (
            self.bckg_spt_en_pdf2,
            self.sig_en_pdf2,
            self.sindec_min2,
            self.sindec_max2,
            self.log10Ne_min2,
            self.log10Ne_max2,
        ) = IRF.spt_en_pdf2()

        self.angres = (
            sA(
                data["ThetaOdd1"],
                data["PhiOdd1"],
                data["ThetaEven1"],
                data["PhiEven1"],
                rad="False",
            )
            / 2
        )
        self.angres = np.radians(self.angres)

        mask1 = self.angres == self.angres
        mask2 = np.log10(data["NKGSize"]) < self.log10Ne_max2

        mask = (mask1) * (mask2)
        self.angres = self.angres[mask]

        self.data = {}

        self.data["evsindec"] = np.sin(np.radians(data["EvDec1"][mask]))
        self.data["evra"] = np.radians(data["EvRa1"][mask])
        self.data["evlog10Ne"] = np.log10(data["NKGSize"][mask])

        if psrcdata is not None:

            self.psrc = {}
            self.psrc["evsindec"] = np.sin(np.radians(psrcdata["EvDec1"]))
            self.psrc["evra"] = np.radians(psrcdata["EvRa1"])
            self.psrc["evlog10Ne"] = np.log10(psrcdata["NKGSize"])


        self.inj = inj
        self.n = n

        # self.gam = 2.2

    def ClassicPdf(self, x, f):

        Srcdec = self.Srcdec
        SrcRA = self.SrcRA
        sindec, RA = x
        angres = np.radians(0.83)

        spcAng = sA(
            np.arcsin(sindec), RA, np.radians(Srcdec), np.radians(SrcRA), rad=True
        )

        s = (
            (1 / (2 * np.pi * (angres**2)))
            * np.exp(-(spcAng**2) / (2 * angres**2))
            * (f)
        )

        b = (1 / (2 * np.pi)) * np.exp(self.bckg_spt_pdf(sindec)) * (1 - f)

        return s + b

    def SpatialEnergyPdf_gamma(self, x, f, gam):

        Srcdec = self.Srcdec
        SrcRA = self.SrcRA

        evlog10Ne, evsindec, evra, evangres = x

        evangres = np.radians(0.83)  # evangres

        spcAng = sA(
            np.arcsin(evsindec), evra, np.radians(Srcdec), np.radians(SrcRA), rad=True
        )

        s = (
            (1 / (2 * np.pi * (evangres**2)))
            * np.exp(-(spcAng**2) / (2 * evangres**2))
            * np.exp(self.sig_en_pdf2((np.sin(np.radians(Srcdec)), evlog10Ne, gam)))
            * f
        )

        b = (
            (1 / (2 * np.pi))
            * np.exp(self.bckg_spt_en_pdf2((evsindec, evlog10Ne)))
            * (1 - f)
        )

        return s + b

    def TSClassicpdf(self, seed):
        """

        Calculates Test Statistic -2*ln( LLH(0)/LLH( ns, gamma) ).

        Parameters
        ----------

        seed: int
            seed for the scrambling of right ascension of background events (data)

        Returns
        -------

        TS: float

        NLL: float
            minimum value of Negative Log Likelihood

        f: float
            optimum value of source strength - ns, lies in [-1,1]

        valid: bool
            True / 1 if minimum of NLL satisfies iminuit valid minimum conditions.
        seed: int
            same as the seed set above
        Ntot: int
            total number of events

        method: int
            method used to minimize.
            1 - 'Minuit.migrad'
            2 - 'Powell'
            3 - 'Nelder-Mead'
            4 - 'L-BFGS-B'
            5 - 'SLSQP'
            6 - 'Minuit.scan'

            tries to minimize the NLL in this order if a method fails to minimize.
        """

        np.random.seed(seed)



        if self.inj:

            psrc = self.inject(seed, self.psrc)

            sindec = np.concatenate([self.data["evsindec"], psrc["evsindec"]])
            ra = np.concatenate([np.random.permutation(self.data["evra"]),  psrc["evra"]])

        else:
            sindec = self.data["evsindec"]
            ra = np.random.permutation(self.data["evra"])

        Ntot = sindec.shape[0]


        NLL = cost.UnbinnedNLL((sindec, ra), self.ClassicPdf)

        m = Minuit(NLL, f=np.random.uniform(0, 0.1))
        m.limits["f"] = (-0.0001, 1)
        m.tol = 10**-5
        m.precision = 2**-100
        m.scan(ncall=10)

        m.migrad()

        if (NLL(0) - NLL(m.values["f"])) > 0 and m.valid:
            return (
                (NLL(0) - NLL(m.values["f"])),
                NLL(0),
                NLL(1),
                NLL(m.values["f"]),
                m.values["f"],
                m.valid,
                seed,
                Ntot,
                1
            )  # , m, NLL

        m.scipy(method="Powell")

        if (NLL(0) - NLL(m.values["f"])) > 0 and m.valid:
            return (
                (NLL(0) - NLL(m.values["f"])),
                NLL(0),
                NLL(1),
                NLL(m.values["f"]),
                m.values["f"],
                m.valid,
                seed,
                Ntot,
                2
            )  # , m, NLL

        m.scipy(method="Nelder-Mead")
        if (NLL(0) - NLL(m.values["f"])) > 0 and m.valid:
            return (
                (NLL(0) - NLL(m.values["f"])),
                NLL(0),
                NLL(1),
                NLL(m.values["f"]),
                m.values["f"],
                m.valid,
                seed,
                Ntot,
                3
            )  # , m, NLL

        m.scipy(method="L-BFGS-B")

        if (NLL(0) - NLL(m.values["f"])) > 0 and m.valid:
            return (
                (NLL(0) - NLL(m.values["f"])),
                NLL(0),
                NLL(1),
                NLL(m.values["f"]),
                m.values["f"],
                m.valid,
                seed,
                Ntot,
                4
            )  # , m, NLL

        m.scipy(method="SLSQP")

        if (NLL(0) - NLL(m.values["f"])) > 0 and m.valid:
            return (
                (NLL(0) - NLL(m.values["f"])),
                NLL(0),
                NLL(1),
                NLL(m.values["f"]),
                m.values["f"],
                m.valid,
                seed,
                Ntot,
                5
            )  # , m, NLL

        m.scan(ncall=10)

        return (
            (NLL(0) - NLL(m.values["f"])),
            NLL(0),
            NLL(1),
            NLL(m.values["f"]),
            m.values["f"],
            m.valid,
            seed,
            Ntot,
            6
        )  # , m, NLL

    def TSSpatialEnergyPdf(self, seed):
        """

        Calculates Test Statistic -2*ln( LLH(0)/LLH( ns, gamma) ).

        Parameters
        ----------

        seed: int
            seed for the scrambling of right ascension of background events (data)

        Returns
        -------

        TS: float

        NLL: float
            minimum value of Negative Log Likelihood

        f: float
            optimum value of source strength - ns, lies in [-1,1]

        gam:
            optimum value of spectral index - gamma, lies in [0,4]

        valid: bool
            True / 1 if minimum of NLL satisfies iminuit valid minimum conditions.
        seed: int
            same as the seed set above
        Ntot: int
            total number of events

        method: int
            method used to minimize.
            1 - 'Minuit.migrad'
            2 - 'Powell'
            3 - 'Nelder-Mead'
            4 - 'L-BFGS-B'
            5 - 'SLSQP'
            6 - 'Minuit.scan'

            tries to minimize the NLL in this order if a method fails to minimize.
        """

        np.random.seed(seed)

        if self.inj:

            psrc = self.inject(seed, self.psrc)

            sindec = np.concatenate([self.data["evsindec"], psrc["evsindec"]])
            ra = np.concatenate([np.random.permutation(self.data["evra"]),  psrc["evra"]])
            log10Ne = np.concatenate([self.data["evlog10Ne"],  psrc["evlog10Ne"]])
            evangres = np.radians(.83)*np.ones_like(sindec) #self.angres

        else:
            sindec = self.data["evsindec"]
            ra = np.random.permutation(self.data["evra"])
            log10Ne = self.data["evlog10Ne"]
            evangres = np.radians(.83)*np.ones_like(sindec)

        Ntot = sindec.shape[0]

        NLL = cost.UnbinnedNLL(
            (log10Ne, sindec, ra, evangres), self.SpatialEnergyPdf_gamma
        )

        m = Minuit(NLL, f=np.random.uniform(0, 0.1), gam=np.random.uniform(1.5, 3.5))
        m.limits["f"] = (-0.0001, 1)
        m.limits["gam"] = (1, 4)
        m.tol = 10**-5
        m.precision = 2**-100
        m.scan(ncall=10)

        m.migrad()

        if (NLL(0, 2) - NLL(m.values["f"], m.values["gam"])) > 0 and m.valid:
            return (
                (NLL(0, 2) - NLL(m.values["f"], m.values["gam"])),
                NLL(m.values["f"], m.values["gam"]),
                m.values["f"],
                m.values["gam"],
                m.valid,
                seed,
                Ntot,
                1,
            )  # , m, NLL

        m.scipy(method="Powell")

        if (NLL(0, 2) - NLL(m.values["f"], m.values["gam"])) > 0 and m.valid:
            return (
                (NLL(0, 2) - NLL(m.values["f"], m.values["gam"])),
                NLL(m.values["f"], m.values["gam"]),
                m.values["f"],
                m.values["gam"],
                m.valid,
                seed,
                Ntot,
                2,
            )  # , m, NLL

        m.scipy(method="Nelder-Mead")
        if (NLL(0, 2) - NLL(m.values["f"], m.values["gam"])) > 0 and m.valid:
            return (
                (NLL(0, 2) - NLL(m.values["f"], m.values["gam"])),
                NLL(m.values["f"], m.values["gam"]),
                m.values["f"],
                m.values["gam"],
                m.valid,
                seed,
                Ntot,
                3,
            )  # , m, NLL

        m.scipy(method="L-BFGS-B")

        if (NLL(0, 2) - NLL(m.values["f"], m.values["gam"])) > 0 and m.valid:
            return (
                (NLL(0, 2) - NLL(m.values["f"], m.values["gam"])),
                NLL(m.values["f"], m.values["gam"]),
                m.values["f"],
                m.values["gam"],
                m.valid,
                seed,
                Ntot,
                4,
            )  # , m, NLL

        m.scipy(method="SLSQP")

        if (NLL(0, 2) - NLL(m.values["f"], m.values["gam"])) > 0 and m.valid:
            return (
                (NLL(0, 2) - NLL(m.values["f"], m.values["gam"])),
                NLL(m.values["f"], m.values["gam"]),
                m.values["f"],
                m.values["gam"],
                m.valid,
                seed,
                Ntot,
                5,
            )  # , m, NLL

        m.scan(ncall=10)

        return (
            (NLL(0, 2) - NLL(m.values["f"], m.values["gam"])),
            NLL(m.values["f"], m.values["gam"]),
            m.values["f"],
            m.values["gam"],
            m.valid,
            seed,
            Ntot,
            6,
        )  # , m, NLL

    def inject(self, seed, psrcdata):
        """

        Takes in Monte-Carlo generated point source events and randomly choose events
        following poisson distribution with average number of events = n.

        Parameters
        ----------

        data : dict
            contains numpy arrays of declination, right ascension, NKGSize etc.

        n : int
            average of poisson distribution

        Returns
        -------

        out: dict
            contains numpy arrays of same info. as input (data).
        """
        #np.random.seed(seed)
        NSrcev = np.random.poisson(self.n)
        Nevs = len(psrcdata[list(psrcdata.keys())[0]])
        index = np.random.choice(Nevs, NSrcev)
        out = {}
        for key in psrcdata:
            out[key] = psrcdata[key][index]

        return out

__init__(Srcdec, SrcRA, data, psrcdata=None, n=0, inj=False)

Constructs all the necessary attributes for the point source Likelihood obj.

Parameters:
  • Srcdec (float) –

    Declination of point src. in deg.

  • Srcra (float) –

    RightAscension of point src. in deg.

  • data (dict) –

    Dict. consisting of numpy arrays of evt. info. atleast EVDec, EvRA, NKGSize

  • psrcsim (dict) –

    Dict. consisting of numpy arrays of simulated gamma evt. info. atleast EVDec, EvRA, NKGSize.

  • n

    n is the average of poisson distrubtion of source events injected after drawing from psrcsim

  • inj

    If TRUE point source simulation will be injected before calculating TS.

Source code in g3py/psLLH.py
 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
def __init__(self, Srcdec, SrcRA, data, psrcdata=None, n=0, inj = False):
    """
    Constructs all the necessary attributes for the point source Likelihood obj.

    Parameters
    ----------
    Srcdec : float
        Declination of point src. in deg.

    Srcra : float
        RightAscension of point src. in deg.

    data : dict
        Dict. consisting of numpy arrays of evt. info. atleast EVDec, EvRA, NKGSize

    psrcsim : dict
        Dict. consisting of numpy arrays of simulated gamma evt. info. atleast EVDec, EvRA, NKGSize.

    n: int
        n is the average of poisson distrubtion of source events injected after drawing from psrcsim

    inj: bool
        If TRUE point source simulation will be injected before calculating TS.
    """

    self.Srcdec = Srcdec
    self.SrcRA = SrcRA

    (
        self.bckg_spt_pdf,
        self.bckg_spt_cdf,
        self.bckg_spt_inv_cdf,
        self.sindec_min,
        self.sindec_max,
    ) = IRF.spt_pdf()
    (
        self.bckg_spt_en_pdf2,
        self.sig_en_pdf2,
        self.sindec_min2,
        self.sindec_max2,
        self.log10Ne_min2,
        self.log10Ne_max2,
    ) = IRF.spt_en_pdf2()

    self.angres = (
        sA(
            data["ThetaOdd1"],
            data["PhiOdd1"],
            data["ThetaEven1"],
            data["PhiEven1"],
            rad="False",
        )
        / 2
    )
    self.angres = np.radians(self.angres)

    mask1 = self.angres == self.angres
    mask2 = np.log10(data["NKGSize"]) < self.log10Ne_max2

    mask = (mask1) * (mask2)
    self.angres = self.angres[mask]

    self.data = {}

    self.data["evsindec"] = np.sin(np.radians(data["EvDec1"][mask]))
    self.data["evra"] = np.radians(data["EvRa1"][mask])
    self.data["evlog10Ne"] = np.log10(data["NKGSize"][mask])

    if psrcdata is not None:

        self.psrc = {}
        self.psrc["evsindec"] = np.sin(np.radians(psrcdata["EvDec1"]))
        self.psrc["evra"] = np.radians(psrcdata["EvRa1"])
        self.psrc["evlog10Ne"] = np.log10(psrcdata["NKGSize"])


    self.inj = inj
    self.n = n

TSClassicpdf(seed)

Calculates Test Statistic -2*ln( LLH(0)/LLH( ns, gamma) ).

Parameters:
  • seed

    seed for the scrambling of right ascension of background events (data)

Returns:
  • TS( float ) –
  • NLL( float ) –

    minimum value of Negative Log Likelihood

  • f( float ) –

    optimum value of source strength - ns, lies in [-1,1]

  • valid( bool ) –

    True / 1 if minimum of NLL satisfies iminuit valid minimum conditions.

  • seed( int ) –

    same as the seed set above

  • Ntot( int ) –

    total number of events

  • method( int ) –

    method used to minimize. 1 - 'Minuit.migrad' 2 - 'Powell' 3 - 'Nelder-Mead' 4 - 'L-BFGS-B' 5 - 'SLSQP' 6 - 'Minuit.scan'

    tries to minimize the NLL in this order if a method fails to minimize.

Source code in g3py/psLLH.py
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
def TSClassicpdf(self, seed):
    """

    Calculates Test Statistic -2*ln( LLH(0)/LLH( ns, gamma) ).

    Parameters
    ----------

    seed: int
        seed for the scrambling of right ascension of background events (data)

    Returns
    -------

    TS: float

    NLL: float
        minimum value of Negative Log Likelihood

    f: float
        optimum value of source strength - ns, lies in [-1,1]

    valid: bool
        True / 1 if minimum of NLL satisfies iminuit valid minimum conditions.
    seed: int
        same as the seed set above
    Ntot: int
        total number of events

    method: int
        method used to minimize.
        1 - 'Minuit.migrad'
        2 - 'Powell'
        3 - 'Nelder-Mead'
        4 - 'L-BFGS-B'
        5 - 'SLSQP'
        6 - 'Minuit.scan'

        tries to minimize the NLL in this order if a method fails to minimize.
    """

    np.random.seed(seed)



    if self.inj:

        psrc = self.inject(seed, self.psrc)

        sindec = np.concatenate([self.data["evsindec"], psrc["evsindec"]])
        ra = np.concatenate([np.random.permutation(self.data["evra"]),  psrc["evra"]])

    else:
        sindec = self.data["evsindec"]
        ra = np.random.permutation(self.data["evra"])

    Ntot = sindec.shape[0]


    NLL = cost.UnbinnedNLL((sindec, ra), self.ClassicPdf)

    m = Minuit(NLL, f=np.random.uniform(0, 0.1))
    m.limits["f"] = (-0.0001, 1)
    m.tol = 10**-5
    m.precision = 2**-100
    m.scan(ncall=10)

    m.migrad()

    if (NLL(0) - NLL(m.values["f"])) > 0 and m.valid:
        return (
            (NLL(0) - NLL(m.values["f"])),
            NLL(0),
            NLL(1),
            NLL(m.values["f"]),
            m.values["f"],
            m.valid,
            seed,
            Ntot,
            1
        )  # , m, NLL

    m.scipy(method="Powell")

    if (NLL(0) - NLL(m.values["f"])) > 0 and m.valid:
        return (
            (NLL(0) - NLL(m.values["f"])),
            NLL(0),
            NLL(1),
            NLL(m.values["f"]),
            m.values["f"],
            m.valid,
            seed,
            Ntot,
            2
        )  # , m, NLL

    m.scipy(method="Nelder-Mead")
    if (NLL(0) - NLL(m.values["f"])) > 0 and m.valid:
        return (
            (NLL(0) - NLL(m.values["f"])),
            NLL(0),
            NLL(1),
            NLL(m.values["f"]),
            m.values["f"],
            m.valid,
            seed,
            Ntot,
            3
        )  # , m, NLL

    m.scipy(method="L-BFGS-B")

    if (NLL(0) - NLL(m.values["f"])) > 0 and m.valid:
        return (
            (NLL(0) - NLL(m.values["f"])),
            NLL(0),
            NLL(1),
            NLL(m.values["f"]),
            m.values["f"],
            m.valid,
            seed,
            Ntot,
            4
        )  # , m, NLL

    m.scipy(method="SLSQP")

    if (NLL(0) - NLL(m.values["f"])) > 0 and m.valid:
        return (
            (NLL(0) - NLL(m.values["f"])),
            NLL(0),
            NLL(1),
            NLL(m.values["f"]),
            m.values["f"],
            m.valid,
            seed,
            Ntot,
            5
        )  # , m, NLL

    m.scan(ncall=10)

    return (
        (NLL(0) - NLL(m.values["f"])),
        NLL(0),
        NLL(1),
        NLL(m.values["f"]),
        m.values["f"],
        m.valid,
        seed,
        Ntot,
        6
    )  # , m, NLL

TSSpatialEnergyPdf(seed)

Calculates Test Statistic -2*ln( LLH(0)/LLH( ns, gamma) ).

Parameters:
  • seed

    seed for the scrambling of right ascension of background events (data)

Returns:
  • TS( float ) –
  • NLL( float ) –

    minimum value of Negative Log Likelihood

  • f( float ) –

    optimum value of source strength - ns, lies in [-1,1]

  • gam

    optimum value of spectral index - gamma, lies in [0,4]

  • valid( bool ) –

    True / 1 if minimum of NLL satisfies iminuit valid minimum conditions.

  • seed( int ) –

    same as the seed set above

  • Ntot( int ) –

    total number of events

  • method( int ) –

    method used to minimize. 1 - 'Minuit.migrad' 2 - 'Powell' 3 - 'Nelder-Mead' 4 - 'L-BFGS-B' 5 - 'SLSQP' 6 - 'Minuit.scan'

    tries to minimize the NLL in this order if a method fails to minimize.

Source code in g3py/psLLH.py
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
def TSSpatialEnergyPdf(self, seed):
    """

    Calculates Test Statistic -2*ln( LLH(0)/LLH( ns, gamma) ).

    Parameters
    ----------

    seed: int
        seed for the scrambling of right ascension of background events (data)

    Returns
    -------

    TS: float

    NLL: float
        minimum value of Negative Log Likelihood

    f: float
        optimum value of source strength - ns, lies in [-1,1]

    gam:
        optimum value of spectral index - gamma, lies in [0,4]

    valid: bool
        True / 1 if minimum of NLL satisfies iminuit valid minimum conditions.
    seed: int
        same as the seed set above
    Ntot: int
        total number of events

    method: int
        method used to minimize.
        1 - 'Minuit.migrad'
        2 - 'Powell'
        3 - 'Nelder-Mead'
        4 - 'L-BFGS-B'
        5 - 'SLSQP'
        6 - 'Minuit.scan'

        tries to minimize the NLL in this order if a method fails to minimize.
    """

    np.random.seed(seed)

    if self.inj:

        psrc = self.inject(seed, self.psrc)

        sindec = np.concatenate([self.data["evsindec"], psrc["evsindec"]])
        ra = np.concatenate([np.random.permutation(self.data["evra"]),  psrc["evra"]])
        log10Ne = np.concatenate([self.data["evlog10Ne"],  psrc["evlog10Ne"]])
        evangres = np.radians(.83)*np.ones_like(sindec) #self.angres

    else:
        sindec = self.data["evsindec"]
        ra = np.random.permutation(self.data["evra"])
        log10Ne = self.data["evlog10Ne"]
        evangres = np.radians(.83)*np.ones_like(sindec)

    Ntot = sindec.shape[0]

    NLL = cost.UnbinnedNLL(
        (log10Ne, sindec, ra, evangres), self.SpatialEnergyPdf_gamma
    )

    m = Minuit(NLL, f=np.random.uniform(0, 0.1), gam=np.random.uniform(1.5, 3.5))
    m.limits["f"] = (-0.0001, 1)
    m.limits["gam"] = (1, 4)
    m.tol = 10**-5
    m.precision = 2**-100
    m.scan(ncall=10)

    m.migrad()

    if (NLL(0, 2) - NLL(m.values["f"], m.values["gam"])) > 0 and m.valid:
        return (
            (NLL(0, 2) - NLL(m.values["f"], m.values["gam"])),
            NLL(m.values["f"], m.values["gam"]),
            m.values["f"],
            m.values["gam"],
            m.valid,
            seed,
            Ntot,
            1,
        )  # , m, NLL

    m.scipy(method="Powell")

    if (NLL(0, 2) - NLL(m.values["f"], m.values["gam"])) > 0 and m.valid:
        return (
            (NLL(0, 2) - NLL(m.values["f"], m.values["gam"])),
            NLL(m.values["f"], m.values["gam"]),
            m.values["f"],
            m.values["gam"],
            m.valid,
            seed,
            Ntot,
            2,
        )  # , m, NLL

    m.scipy(method="Nelder-Mead")
    if (NLL(0, 2) - NLL(m.values["f"], m.values["gam"])) > 0 and m.valid:
        return (
            (NLL(0, 2) - NLL(m.values["f"], m.values["gam"])),
            NLL(m.values["f"], m.values["gam"]),
            m.values["f"],
            m.values["gam"],
            m.valid,
            seed,
            Ntot,
            3,
        )  # , m, NLL

    m.scipy(method="L-BFGS-B")

    if (NLL(0, 2) - NLL(m.values["f"], m.values["gam"])) > 0 and m.valid:
        return (
            (NLL(0, 2) - NLL(m.values["f"], m.values["gam"])),
            NLL(m.values["f"], m.values["gam"]),
            m.values["f"],
            m.values["gam"],
            m.valid,
            seed,
            Ntot,
            4,
        )  # , m, NLL

    m.scipy(method="SLSQP")

    if (NLL(0, 2) - NLL(m.values["f"], m.values["gam"])) > 0 and m.valid:
        return (
            (NLL(0, 2) - NLL(m.values["f"], m.values["gam"])),
            NLL(m.values["f"], m.values["gam"]),
            m.values["f"],
            m.values["gam"],
            m.valid,
            seed,
            Ntot,
            5,
        )  # , m, NLL

    m.scan(ncall=10)

    return (
        (NLL(0, 2) - NLL(m.values["f"], m.values["gam"])),
        NLL(m.values["f"], m.values["gam"]),
        m.values["f"],
        m.values["gam"],
        m.valid,
        seed,
        Ntot,
        6,
    )  # , m, NLL

inject(seed, psrcdata)

Takes in Monte-Carlo generated point source events and randomly choose events following poisson distribution with average number of events = n.

Parameters:
  • data (dict) –

    contains numpy arrays of declination, right ascension, NKGSize etc.

  • n (int) –

    average of poisson distribution

Returns:
  • out( dict ) –

    contains numpy arrays of same info. as input (data).

Source code in g3py/psLLH.py
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
def inject(self, seed, psrcdata):
    """

    Takes in Monte-Carlo generated point source events and randomly choose events
    following poisson distribution with average number of events = n.

    Parameters
    ----------

    data : dict
        contains numpy arrays of declination, right ascension, NKGSize etc.

    n : int
        average of poisson distribution

    Returns
    -------

    out: dict
        contains numpy arrays of same info. as input (data).
    """
    #np.random.seed(seed)
    NSrcev = np.random.poisson(self.n)
    Nevs = len(psrcdata[list(psrcdata.keys())[0]])
    index = np.random.choice(Nevs, NSrcev)
    out = {}
    for key in psrcdata:
        out[key] = psrcdata[key][index]

    return out