DisTreebution.UQ package#
Submodules#
DisTreebution.UQ.Conformalisation module#
- class DisTreebution.UQ.Conformalisation.Conformalisation(settings=None, params=None)[source]#
Bases:
objectHandles conformalisation processes on tree-based models, including preprocessing of trees for training and test data, optionally with group information, and performing operations on leaf values.
- Parameters:
Methods
- preprocess_trees(trees, x_train, x_test)[source]#
- Preprocesses the given trees to extract and organize leaf values for each sample in the training and test sets.
- Handles both standard and PQRT tree types.
- preprocess_trees_with_groups(trees, x_train, x_test, max_depth_group)[source]#
- Preprocesses the given trees to extract leaf values and group information for each sample in the training and test sets,
- up to a specified maximum group depth.
- operation_leaf(ls, q, weights=None)[source]#
- Computes the q-th quantile value from a sorted list of leaf values, optionally using sample weights.
- preprocess_trees(trees, x)[source]#
Preprocesses the given trees to extract and organize leaf values for each sample in the dataset. Handles both standard and PQRT tree types.
- Parameters:
- trees list
List of tree objects to preprocess.
- x array-like
Input data for which to extract leaf values.
- Returns dict:
A dictionary mapping tree IDs to another dictionary that maps sample IDs to sorted leaf values. Format: {tree_id: {sample_id: sorted_leaf_values}}
- preprocess_trees_with_groups(trees, x_train, max_depth_group)[source]#
Preprocesses the given trees to extract leaf values and group information for each sample in the training set, up to a specified maximum group depth.
- Parameters:
- Returns tuple:
treeID2trainID2values (dict): A dictionary mapping tree IDs to another dictionary
that maps training sample IDs to sorted leaf values.
treeID2trainID2group (dict): A dictionary mapping tree IDs to another dictionary
that maps training sample IDs to their group information.
- operation_leaf(ls, q, weights=None)[source]#
Computes the q-th quantile value from a sorted list of leaf values, optionally using sample weights.
- Parameters:
- ls array-like
Sorted list or array of leaf values.
- q float
Quantile to compute, between 0 and 1.
- weights array-like
Weights for each value in ls. If None, uniform weights are used.
- Returns float:
The q-th quantile value from the list.
DisTreebution.UQ.Conformalisation_CQR module#
- class DisTreebution.UQ.Conformalisation_CQR.Conformalisation_CQR(settings=None, params=None)[source]#
Bases:
ConformalisationConformalized Quantile Regression (CQR) for tree-based models.
Implements CQR-style conformalization on tree ensembles producing quantile predictions. Extends
Conformalisationwith methods to compute global and group-wise conformity thresholds and to predict conformalized quantile sets on new inputs.- conformalize_split(trees, x_calib, y_calib, alpha, nominal_quantiles=None)[source]#
Compute CQR conformity thresholds for nominal quantiles.
The method computes conformity scores for each provided nominal quantile and sets
self.conf_threshto a list of thresholds corresponding to each nominal quantile.- Parameters:
- trees list
List of trained tree models.
- x_calib numpy.ndarray
Calibration input features.
- y_calib numpy.ndarray
Calibration target values.
- alpha float
Significance level for conformalization (e.g. 0.1 for 90% sets).
- nominal_quantiles float or list of float
Nominal quantile(s) to conformalize. A single float will be converted to a list.
- Returns:
None. Sets
self.conf_threshto a list of thresholds.- Return type:
None
- Raises:
AssertionError – If
nominal_quantilesis not provided.
- predict_conformal_set_split(trees, x_test)[source]#
Predict conformalized quantile sets on test inputs.
Uses
self.conf_threshcomputed byconformalize_split()to produce, for each nominal quantile, a dictionary mapping test sample indices to conformal intervals [low, up].- Parameters:
- trees list
List of trained tree models.
- x_test numpy.ndarray
Test input features.
- Returns:
Mapping from nominal quantile index to a mapping of test index to [low, up].
- Return type:
- Raises:
AssertionError – If
self.conf_threshhas not been computed.
- conformalize_split_group_coverage(trees, x_calib, y_calib, alpha, nominal_quantiles=None, max_depth_group=None)[source]#
Compute group-wise CQR thresholds to guarantee group coverage.
For each nominal quantile and for each group defined by tree partitions up to
max_depth_group, compute an empirical threshold stored inself.group2conf_thresh.- Parameters:
- trees list
List of trained tree models.
- x_calib numpy.ndarray
Calibration input features.
- y_calib numpy.ndarray
Calibration target values.
- alpha float
Significance level for conformalization.
- nominal_quantiles float or list of float
Nominal quantile(s) to conformalize.
- max_depth_group int
Maximum depth used to define groups in the tree partitioning.
- Returns:
None. Sets
self.group2conf_thresh.- Return type:
None
- Raises:
AssertionError – If
nominal_quantilesormax_depth_groupis not provided.
- predict_conformal_set_split_group_coverage(trees, x_test)[source]#
Predict group-aware conformalized quantile sets for test inputs.
Uses thresholds in
self.group2conf_threshto produce per-group conformal intervals for each nominal quantile.- Parameters:
- trees list
List of trained tree models.
- x_test numpy.ndarray
Test input features.
- Returns:
Tuple
(treeID2testID2group[0], sample2predset)wheresample2predsetmaps nominal-quantile indices to per-sample [low, up].- Return type:
- get_low_up_test_i(yj, qhat_low, qhat_up, qhat_low_test, qhat_up_test, qmedian, qmedian_test, t=None)[source]#
Compute conformalized lower and upper quantiles for one test sample.
Behavior depends on
self.settings['nested_set']and supports variants ‘CQR’, ‘CQR-m’, and ‘CQR-r’. Iftis not provided the method searches for the smallest adjustment making the calibration valueyjfall inside the adjusted interval.- Parameters:
- yj float
Calibration output value.
- qhat_low float
Predicted lower quantile on calibration data.
- qhat_up float
Predicted upper quantile on calibration data.
- qhat_low_test float
Predicted lower quantile for the test sample.
- qhat_up_test float
Predicted upper quantile for the test sample.
- qmedian float
Predicted median on calibration data.
- qmedian_test float
Predicted median for the test sample.
- t float or None, optional
Optional conformalization adjustment parameter; if
Noneit will be computed.
- Returns:
(low_test, up_test, t)wherelow_testandup_testare the adjusted bounds andtis the used threshold.- Return type:
- get_low_up_score(i, q, y_train, sample2calib_trees, treeID2trainID2values_low, treeID2testID2values_low, treeID2trainID2values_up, treeID2testID2values_up, treeID2trainID2values_median, treeID2testID2values_median, t_fixed=None)[source]#
Compute conformalized quantile predictions and conformity scores.
Aggregates tree-level quantile estimates according to
self.settings['type_aggregation_trees']and for each calibration sample returns a conformalized lower bound, upper bound and a conformity score index.- Parameters:
- i int
Index of the test sample.
- q float
Nominal quantile level (e.g. 0.1).
- y_train list or numpy.ndarray
Calibration output values.
- sample2calib_trees dict
Mapping from calibration sample index to list of tree IDs.
- treeID2trainID2values_low dict
Mapping of tree ID to training sample IDs to lower quantile leaf values.
- treeID2testID2values_low dict
Mapping of tree ID to test sample IDs to lower quantile leaf values.
- treeID2trainID2values_up dict
Mapping of tree ID to training sample IDs to upper quantile leaf values.
- treeID2testID2values_up dict
Mapping of tree ID to test sample IDs to upper quantile leaf values.
- treeID2trainID2values_median dict
Mapping of tree ID to training sample IDs to median quantile leaf values.
- treeID2testID2values_median dict
Mapping of tree ID to test sample IDs to median quantile leaf values.
- t_fixed float or None, optional
Optional fixed conformalization threshold/index. If
Nonethe method will compute it per calibration sample.
- Returns:
Tuple
(lower, upper, conf_scores)where each is a list with one entry per calibration sample iny_train.- Return type:
DisTreebution.UQ.Conformalisation_distributional module#
- class DisTreebution.UQ.Conformalisation_distributional.Conformalisation_distributional(settings=None, params=None)[source]#
Bases:
ConformalisationConformalisation for distributional tree-based models.
This class implements conformalisation utilities for tree ensembles that produce distributional (quantile/interval) predictions. It extends
Conformalisationand provides methods to compute conformity thresholds on a calibration set and to produce conformal prediction sets for test inputs.- Parameters:
- conformalize_split(trees, x_calib, y_calib, alpha)[source]#
Compute conformity threshold from a calibration set.
The method computes conformity scores on the provided calibration dataset and sets
self.conf_threshto the empirical quantile corresponding toalpha.- Parameters:
- trees list
List of trained tree models.
- x_calib numpy.ndarray
Calibration input data.
- y_calib numpy.ndarray
Calibration output data.
- alpha float
Significance level for conformalization (e.g. 0.1 for 90% sets).
- Returns:
None. Sets
self.conf_threshon success.- Return type:
None
- predict_conformal_set_split(trees, x_test)[source]#
Predict conformal prediction sets for test inputs using a fixed threshold.
- Parameters:
- trees list
List of trained tree models.
- x_test numpy.ndarray
Test input data.
- Returns:
Mapping from test sample index to conformal prediction set [low, up].
- Return type:
- Raises:
AssertionError – If
self.conf_threshhas not been set by a previous calibration.
- conformalize_split_group_coverage(trees, x_calib, y_calib, alpha, max_depth_group=None, get_res_on_calib=False)[source]#
Compute group-specific conformity thresholds from a calibration set.
For each group (as defined by the tree partitioning up to
max_depth_group) this method computes an empirical conformity threshold and stores the results inself.group2conf_thresh.- Parameters:
- trees list
List of trained tree models.
- x_calib numpy.ndarray
Calibration input data.
- y_calib numpy.ndarray
Calibration output data.
- alpha float
Significance level for conformalization.
- max_depth_group int, optional
Maximum depth for defining groups (tree levels).
- get_res_on_calib bool, optional
If True, also return calibration set statistics.
- Returns:
None. On success sets
self.group2conf_thresh. Ifget_res_on_calibis True the method additionally computes and would return calibration group sizes and coverages (the caller should call the corresponding predict method to retrieve them).- Return type:
None
- predict_conformal_set_split_group_coverage(trees, x_test, get_res_on_calib=False, x_calib=None, y_calib=None)[source]#
Predict conformal sets for test inputs with group-wise thresholds.
Uses
self.group2conf_thresh(computed byconformalize_split_group_coverage()) to produce conformal sets for each test sample, adapting the threshold to the group that a sample belongs to.- Parameters:
- trees list
List of trained tree models.
- x_test numpy.ndarray
Test input data.
- get_res_on_calib bool, optional
If True, also compute and return calibration-set statistics.
- x_calib numpy.ndarray, optional
Calibration input data (required when
get_res_on_calibis True).- y_calib numpy.ndarray, optional
Calibration output data (required when
get_res_on_calibis True).
- Returns:
When
get_res_on_calibis True returns a tuple(group2sizecalib, group2covcalib, treeID2testID2group, sample2predset). Otherwise returns(treeID2testID2group[0], sample2predset).- Return type:
- Raises:
AssertionError – If
self.group2conf_threshhas not been computed.
- get_low_up_test_i(yj, qhat_low, qhat_up, qhat_low_test, qhat_up_test, t=None)[source]#
Select lower/upper bounds for a test sample using conformity thresholds.
The method inspects arrays of calibration quantile estimates and their corresponding test quantile estimates and either uses the provided threshold index
tor searches for the largest index where the calibration value coversyj.- Parameters:
- yj float
Calibration sample true value.
- qhat_low numpy.ndarray
Lower quantile estimates from calibration.
- qhat_up numpy.ndarray
Upper quantile estimates from calibration.
- qhat_low_test numpy.ndarray
Lower quantile estimates for the test sample.
- qhat_up_test numpy.ndarray
Upper quantile estimates for the test sample.
- t int or None, optional
Optional fixed threshold index to use. If
Nonethe method searches.
- Returns:
(low_j, up_j, t_idx)wherelow_jandup_jare the selected bounds for the test sample andt_idxis the chosen threshold index.- Return type:
- get_low_up_score(i, y_train, sample2calib_trees, treeID2trainID2values, treeID2testID2values, t_fixed=None)[source]#
Compute lower/upper bounds and conformity scores for one test index.
This method aggregates quantile estimates across trees (according to the aggregation configured in
self.settings['type_aggregation_trees']) and returns, for the test sample at indexi, three lists: the lower bounds, the upper bounds and the conformity score indices.- Parameters:
- i int
Index of the test sample.
- y_train numpy.ndarray or list
Array of calibration/training output values used to compute scores.
- sample2calib_trees dict
Mapping from sample indices to lists of tree IDs used for calibration.
- treeID2trainID2values dict
Mapping from tree ID to a mapping of training sample IDs to leaf values.
- treeID2testID2values dict
Mapping from tree ID to a mapping of test sample IDs to leaf values.
- t_fixed int or float, optional
Optional fixed threshold/index to use when selecting bounds. If
Nonethe method will search.
- Returns:
A tuple
(lower, upper, conf_scores)where each entry is a list with one element per calibration sample iny_train.- Return type:
DisTreebution.UQ.UQ module#
- class DisTreebution.UQ.UQ.UQ(type_tree='PQRT', nested_set='CQR', type_conformal='split', group_coverage=False, type_aggregation_trees='vr', params=None)[source]#
Bases:
objectUncertainty quantification and conformal prediction helper.
This class wraps training of forest-like quantile/regression trees, conformalization, and construction/analysis of predictive sets.
It centralizes configuration (
settings), builds a non-conformal quantile estimator (noconf) for querying, selects a conformal backend (conf) based on settings, and exposes utilities to train trees, conformalize them, predict conformal sets, and extract quantile estimates.Constructor Parameters
- Parameters:
- type_tree str
Type of tree learner used. Examples:
'PMQRT'– per-tree quantile regression'CRPS'– CRPS-optimized regression'RT'– quadratic regression tree
Other strings may be supported by external tree classes.
- nested_set str
Strategy for nested prediction sets (e.g.
'CQR','distri'). Used to choose the conformal backend. - If it contains'CQR'→Conformalisation_CQRbackend. - If it contains'distri'→Conformalisation_distributionalbackend.- type_conformal str or None
Controls conformalization mode. If
None, a genericConformalisationbackend is instantiated; otherwisenested_setdetermines which backend is used.- group_coverage bool
Whether to use group-aware conformal methods and prediction routines (calls to
conf.*_group_coverage).- type_aggregation_trees str
How per-tree quantiles are aggregated across trees. Examples: -
'vr'– quantile bagging: average quantiles across trees -'vr-avg'– distributional bagging: aggregates conditional CDFs before querying- params dict
Miscellaneous hyperparameters, may include: -
'nTrees'– number of trees to train -'treeID2quantiles_train'– mapping tree IDs → quantiles (for PMQRT) -'max_depth'– maximum depth for trees -'min_samples_split'– minimum samples to split -'IG_biais_correction'– whether to use leave-one-out or Mallows Cp to correct the estimate of information gains -'max_depth_group'– max depth for group coverage trees -'list_distri_low_quantiles'– lower quantiles for distributional conformal prediction
Behavior / Side Effects
self.settingsis a dict of configuration options.self.paramsstores the params dictionary.self.noconfis always constructed as aConformalisation_CQRinstance with the same settings exceptnested_set='CQR'(used for querying quantiles without conformalization).self.confis chosen based ontype_conformalandnested_set.
Public Attributes
- Variables:
Example:
uq = UQ(type_tree='CRPS', nested_set='CQR', params={'nTrees': 100, 'max_depth': 6}) trees, sample2calib = uq.train_trees(X_train, y_train) uq.conformalize(trees, X_calib, y_calib, alpha=0.1) sample2predset = uq.predict_conformal_set(trees, X_test) widths, coverages = uq.compute_width_coverage(sample2predset, y_test)
- compute_width_coverage(sample2predset, y_test)[source]#
Computes the total width of prediction sets and coverage indicators for each test sample.
For each test sample, this method calculates:
The sum of the widths of all predicted intervals (sub-intervals) associated with the sample.
Whether the true value falls within any of the predicted intervals (coverage).
- Parameters:
- Returns:
widths (numpy.ndarray) – Total widths of the predicted intervals for each test sample.
coverages (numpy.ndarray) – Binary indicators (1 if the true value is covered by any interval,
0 otherwise) for each test sample.
- Return type:
- compute_group_width_coverage(sample2predset, y_test, testID2group)[source]#
Computes the average prediction interval width and empirical coverage per group.
Groups are defined by the
testID2groupmapping.- Parameters:
- sample2predset list[list[tuple]]
A list where each element corresponds to a test sample and contains one or more tuples of the form
(lower_bound, upper_bound)representing predicted intervals.- y_test array-like
The true target values for the test samples.
- testID2group dict
A mapping from test sample indices to group identifiers.
- Returns:
group2width (dict) – Mapping
group_id → average interval widthacross all samples in the group.group2cov (dict) – Mapping
group_id → empirical coverage, i.e., the fraction of samples in the group
whose true value lies within any of the predicted intervals.
- Return type:
- train_trees(x_train, y_train, max_depth_ref_tree=-1)[source]#
Trains an ensemble of regression trees and tracks calibration indices for Out-of-Bag (OOB) conformal methods.
- Parameters:
- x_train numpy.ndarray
Training feature matrix of shape
(n_train, n_features).- y_train numpy.ndarray
Training target values of shape
(n_train,).- max_depth_ref_tree int
If not equal to
-1, a reference tree is constructed and fitted (used by certain tree-training routines). If-1, no reference tree is used.
- Returns:
trees (list) – List of fitted tree objects, ordered by training loop.
sample2calib_trees (dict) – Mapping
sample_index → list of tree indicesfor which
the sample was placed in the calibration set (i.e., excluded from that tree’s training set). Useful for split-conformal calibration.
- Return type:
- conformalize(trees, x_calib, y_calib, alpha, **kwargs)[source]#
Runs conformalization (split-conformal) on the provided trees and calibration data.
- Parameters:
- trees list
List of fitted tree objects (the same trees returned by
train_trees()).- x_calib numpy.ndarray
Calibration feature matrix.
- y_calib numpy.ndarray
Calibration target values.
- alpha float
Miscoverage level (e.g.,
0.1for 90% coverage).- kwargs
Additional keyword arguments forwarded to the conformal backend. For CQR nested sets, the
nominal_quantilesparameter should be provided.
Behavior:
If
self.settings['group_coverage']isTrue, calls
self.conf.conformalize_split_group_coverage(...). - Otherwise, callsself.conf.conformalize_split(...). - The conformal backend is expected to update its internal state used bypredict_conformal_set().
- predict_conformal_set(trees, x_test, return_testID2group=False, **kwargs)[source]#
Predict conformalized sets for test points using the conformal backend.
- Parameters:
- trees list
Trained trees (same as passed to
conformalize()).- x_test numpy.ndarray
Test feature matrix of shape (n_test, n_features).
- return_testID2group bool
When
group_coverageisTrue, optionally return the mapping from test indices to groups alongside the prediction sets.- kwargs
Additional keyword arguments forwarded to the conformal backend’s prediction routine.
- Returns:
If
group_coverageisFalse:
sample2predset – Sequence-like (length n_test) of (low, up) prediction interval pairs.
If
group_coverageisTrue:If
return_testID2groupisFalse:sample2predset as above.
If
return_testID2groupisTrue:Tuple
(testID2group, sample2predset)wheretestID2groupis the mapping used.
Notes:
Delegates to either
predict_conformal_set_split()or
predict_conformal_set_split_group_coverage()onself.conf.
- get_quantile_estimate(trees, x_test, quantiles)[source]#
Query aggregate quantile estimates for each test sample from the (non- or pre-) conformal tree predictions.
- Parameters:
- trees list
Trained tree objects.
- x_test numpy.ndarray
Test features matrix of shape (n_test, n_features).
- quantiles iterable
List or array of quantile levels to estimate (values in [0, 1]).
- Returns:
sample2quantiles (dict) – Mapping from sample index to list of estimated quantile values. The inner list has the same ordering as the input quantiles argument.
Behavior / Implementation details:
Calls
self.conf.preprocess_trees(trees, x_test)to obtain a structure
treeID2testID2valuesmapping tree IDs to test sample leaf/value indexes.For each requested quantile
q, finds the nearest available query quantile
in
self.conf.quantiles_query(via argmin on absolute distance) and obtains the set of tree IDs associated with that query fromself.conf.quantile_query2treeIDs.
DisTreebution.UQ.datasets module#
DisTreebution.UQ.utils module#
- DisTreebution.UQ.utils.crps_from_quantiles(quantiles, tau, y)[source]#
Compute CRPS from predicted quantiles.
Parameters: - quantiles: List or numpy array of predicted quantiles. - tau: List or numpy array of corresponding quantile levels (e.g., [0.1, 0.2, …, 0.9]). - y: The observed value.
Returns: - crps: The computed CRPS score.