-
Notifications
You must be signed in to change notification settings - Fork 5
Truncated gamma fit #243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Truncated gamma fit #243
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -181,25 +181,7 @@ def calculate_pval_from_gammafit_to_trials( | |||||
| if len(ts_vals) > n_max: | ||||||
| ts_vals = ts_vals[:n_max] | ||||||
|
|
||||||
| Ntot = len(ts_vals) | ||||||
| ts_eta = ts_vals[ts_vals > eta] | ||||||
| N_prime = len(ts_eta) | ||||||
| alpha = N_prime/Ntot | ||||||
|
|
||||||
| def obj(x): | ||||||
| return truncated_gamma_logpdf( | ||||||
| x[0], | ||||||
| x[1], | ||||||
| eta=eta, | ||||||
| ts_above_eta=ts_eta, | ||||||
| N_above_eta=N_prime) | ||||||
|
|
||||||
| x0 = [0.75, 1.8] # Initial values of function parameters. | ||||||
| bounds = [[0.1, 10], [0.1, 10]] # Ranges for the minimization fitter. | ||||||
| r = minimize(obj, x0, bounds=bounds) | ||||||
| pars = r.x | ||||||
|
|
||||||
| norm = alpha/gamma.sf(eta, a=pars[0], scale=pars[1]) | ||||||
| (pars, norm) = fit_truncated_gamma(vals=ts_vals, eta=eta) | ||||||
| p = norm * gamma.sf(ts_threshold, a=pars[0], scale=pars[1]) | ||||||
|
|
||||||
| # a correct calculation of the error in pvalue due to | ||||||
|
|
@@ -306,45 +288,46 @@ def truncated_gamma_logpdf( | |||||
| return -logl | ||||||
|
|
||||||
|
|
||||||
| def calculate_critical_ts_from_gamma( | ||||||
| ts, | ||||||
| h0_ts_quantile, | ||||||
| def fit_truncated_gamma( | ||||||
| vals, | ||||||
| eta=3.0): | ||||||
| """Calculates the critical test-statistic value corresponding | ||||||
| to h0_ts_quantile by fitting the ts distribution with a truncated | ||||||
| gamma function. | ||||||
| """ | ||||||
| Fits a truncated gamma function to a set of values. | ||||||
| Returns the best-fit parameters and the normalization constant | ||||||
| that accounts for the truncation of the function. | ||||||
|
|
||||||
| Parameters | ||||||
| ---------- | ||||||
| ts : (n_trials,)-shaped 1D ndarray | ||||||
| The ndarray holding the test-statistic values of the trials. | ||||||
| h0_ts_quantile : float | ||||||
| Null-hypothesis test statistic quantile. | ||||||
| eta : float, optional | ||||||
| vals : (n_trials,)-shaped 1D ndarray of float | ||||||
| eta : float | ||||||
| Test-statistic value at which the gamma function is truncated | ||||||
| from below. | ||||||
| from below. Default is 3.0. | ||||||
|
|
||||||
| Returns | ||||||
| ------- | ||||||
| critical_ts : float | ||||||
| pars : (2,)-shaped 1D array of float | ||||||
| `a` and `scale` parameters of the truncated gamma function. | ||||||
| norm : float | ||||||
| Normalization constant of the truncated gamma function. | ||||||
| """ | ||||||
|
|
||||||
| if not IMINUIT_LOADED: | ||||||
| raise ImportError( | ||||||
| 'The iminuit module was not imported! ' | ||||||
| 'This module is a requirement of the function ' | ||||||
| '"calculate_critical_ts_from_gamma"!') | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| Ntot = len(ts) | ||||||
| ts_eta = ts[ts > eta] | ||||||
| N_prime = len(ts_eta) | ||||||
| Ntot = len(vals) | ||||||
| vals_eta = vals[vals > eta] | ||||||
| N_prime = len(vals_eta) | ||||||
| alpha = N_prime/Ntot | ||||||
|
|
||||||
| def obj(x): | ||||||
| return truncated_gamma_logpdf( | ||||||
| x[0], | ||||||
| x[1], | ||||||
| eta=eta, | ||||||
| ts_above_eta=ts_eta, | ||||||
| ts_above_eta=vals_eta, | ||||||
| N_above_eta=N_prime) | ||||||
|
|
||||||
| x0 = [0.75, 1.8] # Initial values of function parameters. | ||||||
|
|
@@ -353,6 +336,33 @@ def obj(x): | |||||
| pars = r.x | ||||||
|
|
||||||
| norm = alpha/gamma.sf(eta, a=pars[0], scale=pars[1]) | ||||||
|
|
||||||
| return pars, norm | ||||||
|
|
||||||
|
|
||||||
| def calculate_critical_ts_from_gamma( | ||||||
| ts, | ||||||
| h0_ts_quantile, | ||||||
| eta=3.0): | ||||||
| """Calculates the critical test-statistic value corresponding | ||||||
| to h0_ts_quantile by fitting the ts distribution with a truncated | ||||||
| gamma function. | ||||||
|
|
||||||
| Parameters | ||||||
| ---------- | ||||||
| ts : (n_trials,)-shaped 1D ndarray | ||||||
| The ndarray holding the test-statistic values of the trials. | ||||||
| h0_ts_quantile : float | ||||||
| Null-hypothesis test statistic quantile. | ||||||
| eta : float, optional | ||||||
| Test-statistic value at which the gamma function is truncated | ||||||
| from below. | ||||||
|
|
||||||
| Returns | ||||||
| ------- | ||||||
| critical_ts : float | ||||||
| """ | ||||||
| (pars, norm) = fit_truncated_gamma(vals=ts, eta=eta) | ||||||
| critical_ts = gamma.ppf(1 - 1./norm*h0_ts_quantile, a=pars[0], scale=pars[1]) | ||||||
|
|
||||||
| if critical_ts < eta: | ||||||
|
|
||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.