From 2f0f50dc758eb9638318de4f483914b696d0436b Mon Sep 17 00:00:00 2001 From: Vincent Wieland <90847066+vwiela@users.noreply.github.com> Date: Mon, 9 Oct 2023 18:28:54 +0200 Subject: [PATCH] Update ensemble.py (#1124) * Update ensemble.py Add an option for computing cutoff values for pointwise confidence regions. Needed for ensemble simulations. * Update ensemble.py Fix string literal. * Style changes ensemble.py Change style of string representation. * Fix string name & black * Update C.py Add simultaneous and pointwise strings for the choice of confidence regions. * Update strings ensemble.py Changed strings to constants stored in C.py. * Fix typo C.py Fix typo. * Style and quality ensemble.py Use black for consistent code style. --------- Co-authored-by: Doresic Co-authored-by: Paul Jonas Jost <70631928+PaulJonasJost@users.noreply.github.com> --- pypesto/C.py | 3 +++ pypesto/ensemble/ensemble.py | 27 +++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/pypesto/C.py b/pypesto/C.py index 61f4eb449..c5b65452a 100644 --- a/pypesto/C.py +++ b/pypesto/C.py @@ -35,6 +35,9 @@ ENSEMBLE_TYPE = 'ensemble_type' PREDICTIONS = 'predictions' +SIMULTANEOUS = 'simultaneous' +POINTWISE = 'pointwise' + LOWER_BOUND = 'lower_bound' UPPER_BOUND = 'upper_bound' PREEQUILIBRATION_CONDITION_ID = 'preequilibrationConditionId' diff --git a/pypesto/ensemble/ensemble.py b/pypesto/ensemble/ensemble.py index 178c1c2b1..d2ab53df9 100644 --- a/pypesto/ensemble/ensemble.py +++ b/pypesto/ensemble/ensemble.py @@ -29,12 +29,14 @@ OUTPUT, OUTPUT_SENSI, PERCENTILE, + POINTWISE, PREDICTION_ARRAYS, PREDICTION_ID, PREDICTION_RESULTS, PREDICTION_SUMMARY, PREDICTIONS, PREDICTOR, + SIMULTANEOUS, STANDARD_DEVIATION, SUMMARY, TIMEPOINTS, @@ -1204,7 +1206,11 @@ def get_percentile_label(percentile: Union[float, int, str]) -> str: return f'{PERCENTILE} {percentile}' -def calculate_cutoff(result: Result, percentile: float = 0.95): +def calculate_cutoff( + result: Result, + percentile: float = 0.95, + cr_option: str = SIMULTANEOUS, +): """ Calculate the cutoff of the ensemble. @@ -1220,6 +1226,10 @@ def calculate_cutoff(result: Result, percentile: float = 0.95): The percentile of the chi^2 distribution. Between 0 and 100. Higher values will result in a more lax cutoff. If the value is greater than 100, the cutoff will be returned as np.inf. + cr_option: + The type of confidence region, which determines the degree of freedom of + the chi^2 distribution for the cutoff value. It can take 'simultaneous' or + 'pointwise'. Returns ------- @@ -1230,10 +1240,19 @@ def calculate_cutoff(result: Result, percentile: float = 0.95): f"percentile={percentile} is too large. Choose " f"0<=percentile<=100." ) + if cr_option not in [SIMULTANEOUS, POINTWISE]: + raise ValueError( + "Confidence region must be either simultaneous or pointwise." + ) + # optimal point as base: fval_opt = result.optimize_result[0].fval - # degrees of freedom is equal to the number of parameters - df = result.problem.dim - range = chi2.ppf(q=percentile / 100, df=df) + if cr_option == SIMULTANEOUS: + # degrees of freedom is equal to the number of parameters + df = result.problem.dim + elif cr_option == POINTWISE: + # degrees of freedom is equal to 1 + df = 1 + range = chi2.ppf(q=percentile / 100, df=df) return fval_opt + range