Skip to content

Commit ea5a9be

Browse files
Truncated gamma fit (#243)
Add fit function for a generic truncated gamma function
1 parent 272e3fc commit ea5a9be

File tree

1 file changed

+45
-44
lines changed

1 file changed

+45
-44
lines changed

skyllh/core/utils/analysis.py

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,6 @@ def calculate_pval_from_gammafit_to_trials(
165165
-------
166166
p, p_sigma: tuple(float, float)
167167
"""
168-
if not IMINUIT_LOADED:
169-
raise ImportError(
170-
'The iminuit module was not imported! '
171-
'This module is a requirement for the function '
172-
'"calculate_pval_from_gammafit_to_trials"!')
173-
174168
if ts_threshold < eta:
175169
raise ValueError(
176170
'ts threshold value = %e, eta = %e. The calculation of the p-value'
@@ -181,25 +175,7 @@ def calculate_pval_from_gammafit_to_trials(
181175
if len(ts_vals) > n_max:
182176
ts_vals = ts_vals[:n_max]
183177

184-
Ntot = len(ts_vals)
185-
ts_eta = ts_vals[ts_vals > eta]
186-
N_prime = len(ts_eta)
187-
alpha = N_prime/Ntot
188-
189-
def obj(x):
190-
return truncated_gamma_logpdf(
191-
x[0],
192-
x[1],
193-
eta=eta,
194-
ts_above_eta=ts_eta,
195-
N_above_eta=N_prime)
196-
197-
x0 = [0.75, 1.8] # Initial values of function parameters.
198-
bounds = [[0.1, 10], [0.1, 10]] # Ranges for the minimization fitter.
199-
r = minimize(obj, x0, bounds=bounds)
200-
pars = r.x
201-
202-
norm = alpha/gamma.sf(eta, a=pars[0], scale=pars[1])
178+
(pars, norm) = fit_truncated_gamma(vals=ts_vals, eta=eta)
203179
p = norm * gamma.sf(ts_threshold, a=pars[0], scale=pars[1])
204180

205181
# a correct calculation of the error in pvalue due to
@@ -306,45 +282,43 @@ def truncated_gamma_logpdf(
306282
return -logl
307283

308284

309-
def calculate_critical_ts_from_gamma(
310-
ts,
311-
h0_ts_quantile,
312-
eta=3.0):
313-
"""Calculates the critical test-statistic value corresponding
314-
to h0_ts_quantile by fitting the ts distribution with a truncated
315-
gamma function.
285+
def fit_truncated_gamma(vals, eta):
286+
"""
287+
Fits a truncated gamma function to a set of values.
288+
Returns the best-fit parameters and the normalization constant
289+
that accounts for the truncation of the function.
316290
317291
Parameters
318292
----------
319-
ts : (n_trials,)-shaped 1D ndarray
320-
The ndarray holding the test-statistic values of the trials.
321-
h0_ts_quantile : float
322-
Null-hypothesis test statistic quantile.
323-
eta : float, optional
324-
Test-statistic value at which the gamma function is truncated
325-
from below.
293+
vals : (n_trials,)-shaped 1D ndarray of float
294+
eta : float
295+
Value at which the gamma function is truncated from below.
326296
327297
Returns
328298
-------
329-
critical_ts : float
299+
pars : (2,)-shaped 1D array of float
300+
`a` and `scale` parameters of the truncated gamma function.
301+
norm : float
302+
Normalization constant of the truncated gamma function.
330303
"""
304+
331305
if not IMINUIT_LOADED:
332306
raise ImportError(
333307
'The iminuit module was not imported! '
334308
'This module is a requirement of the function '
335309
'"calculate_critical_ts_from_gamma"!')
336310

337-
Ntot = len(ts)
338-
ts_eta = ts[ts > eta]
339-
N_prime = len(ts_eta)
311+
Ntot = len(vals)
312+
vals_eta = vals[vals > eta]
313+
N_prime = len(vals_eta)
340314
alpha = N_prime/Ntot
341315

342316
def obj(x):
343317
return truncated_gamma_logpdf(
344318
x[0],
345319
x[1],
346320
eta=eta,
347-
ts_above_eta=ts_eta,
321+
ts_above_eta=vals_eta,
348322
N_above_eta=N_prime)
349323

350324
x0 = [0.75, 1.8] # Initial values of function parameters.
@@ -353,6 +327,33 @@ def obj(x):
353327
pars = r.x
354328

355329
norm = alpha/gamma.sf(eta, a=pars[0], scale=pars[1])
330+
331+
return pars, norm
332+
333+
334+
def calculate_critical_ts_from_gamma(
335+
ts,
336+
h0_ts_quantile,
337+
eta=3.0):
338+
"""Calculates the critical test-statistic value corresponding
339+
to h0_ts_quantile by fitting the ts distribution with a truncated
340+
gamma function.
341+
342+
Parameters
343+
----------
344+
ts : (n_trials,)-shaped 1D ndarray
345+
The ndarray holding the test-statistic values of the trials.
346+
h0_ts_quantile : float
347+
Null-hypothesis test statistic quantile.
348+
eta : float, optional
349+
Test-statistic value at which the gamma function is truncated
350+
from below.
351+
352+
Returns
353+
-------
354+
critical_ts : float
355+
"""
356+
(pars, norm) = fit_truncated_gamma(vals=ts, eta=eta)
356357
critical_ts = gamma.ppf(1 - 1./norm*h0_ts_quantile, a=pars[0], scale=pars[1])
357358

358359
if critical_ts < eta:

0 commit comments

Comments
 (0)