Wrappers¶
uncertainty_flow.wrappers
¶
Wrappers for adding uncertainty quantification to sklearn models.
AdaptiveConformalForecaster
¶
Bases: BaseUncertaintyModel
Adaptive Conformal Inference wrapper for sequential prediction.
Wraps a fitted BaseUncertaintyModel and adjusts prediction interval
width dynamically. After each observation, call :meth:update (or
:meth:update_batch) to adapt the coverage level.
The adaptive rule (Gibbs & Candes 2021):
alpha_{t+1} = alpha_t + gamma * (alpha_t - 1(|y_t - yhat_t| > q_{1-alpha_t}))
If recent coverage is too low (errors exceed intervals), alpha grows, widening intervals. If coverage is too high, alpha shrinks, narrowing intervals.
Examples:
>>> from sklearn.ensemble import GradientBoostingRegressor
>>> from uncertainty_flow.wrappers import ConformalRegressor
>>> from uncertainty_flow.wrappers import AdaptiveConformalForecaster
>>>
>>> base = ConformalRegressor(GradientBoostingRegressor())
>>> base.fit(df_train, target="y")
>>> aci = AdaptiveConformalForecaster(model=base)
>>> aci.fit(df_calib, target="y")
>>>
>>> for t in range(n_steps):
... pred = aci.predict(df.iloc[t:t+1])
... aci.update(y_true[t])
Source code in uncertainty_flow/wrappers/adaptive_conformal.py
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 | |
current_alpha
property
¶
Current adaptive miscoverage level.
__init__(model, alpha=0.1, gamma=0.01)
¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
BaseUncertaintyModel
|
A fitted BaseUncertaintyModel. |
required |
alpha
|
float
|
Initial miscoverage level (default 0.1 → 90% coverage). |
0.1
|
gamma
|
float
|
Learning rate for alpha adaptation (default 0.01). |
0.01
|
Source code in uncertainty_flow/wrappers/adaptive_conformal.py
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 | |
fit(data, target=None, **kwargs)
¶
Initialize ACI with calibration conformal scores.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
PolarsInput
|
Calibration dataset for computing initial nonconformity scores. |
required |
target
|
TargetSpec | None
|
Target column name. |
None
|
Returns:
| Type | Description |
|---|---|
AdaptiveConformalForecaster
|
self |
Source code in uncertainty_flow/wrappers/adaptive_conformal.py
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 | |
predict(data, steps=1)
¶
Generate adaptive prediction intervals.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
PolarsInput
|
Input data for prediction. |
required |
steps
|
int
|
Number of steps ahead (propagates alpha adjustment for multi-step forecasts). Default 1. |
1
|
Returns:
| Type | Description |
|---|---|
DistributionPrediction
|
DistributionPrediction with intervals reflecting current alpha_t. |
Source code in uncertainty_flow/wrappers/adaptive_conformal.py
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 | |
update(y_true)
¶
Update alpha after observing a true value.
Must be called after :meth:predict with the corresponding true
observation. Updates internal conformal scores and adapts alpha.
For univariate models, pass a scalar. For multivariate, pass an array-like with one value per target.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_true
|
float | int | ndarray
|
Observed true value (scalar for univariate, array for multivariate). |
required |
Source code in uncertainty_flow/wrappers/adaptive_conformal.py
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 | |
update_batch(y_true)
¶
Sequentially update alpha for a batch of observations.
Calls :meth:update for each observation in order. This is
equivalent to calling update() in a loop but more convenient
for offline replay and testing.
.. note::
This method assumes you have already called predict() for
each corresponding observation and are now providing the true
values in sequence. It does NOT call predict() internally.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_true
|
Series | ndarray
|
Array or Series of observed true values.
Shape |
required |
Source code in uncertainty_flow/wrappers/adaptive_conformal.py
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 | |
ConformalRegressor
¶
Bases: BaseUncertaintyModel
Wrap any scikit-learn regressor with conformal prediction.
Coverage guarantee: ✅ (exchangeability assumption) Non-crossing: ✅ (post-sort)
Examples:
>>> from sklearn.ensemble import GradientBoostingRegressor
>>> from uncertainty_flow.wrappers import ConformalRegressor
>>> import polars as pl
>>>
>>> df = pl.DataFrame({
... "feature1": [1, 2, 3, 4, 5],
... "feature2": [2, 4, 6, 8, 10],
... "target": [1.5, 3.5, 5.5, 7.5, 9.5],
... })
>>> base = GradientBoostingRegressor(random_state=42)
>>> model = ConformalRegressor(base_model=base, random_state=42)
>>> model.fit(df, target="target")
>>> pred = model.predict(df)
>>> pred.interval(0.9)
Source code in uncertainty_flow/wrappers/conformal.py
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 | |
uncertainty_drivers_
property
¶
Return residual correlation analysis results.
__init__(base_model, calibration_method='holdout', calibration_size=0.2, coverage_target=0.9, auto_tune=True, uncertainty_features=None, random_state=None)
¶
Initialize ConformalRegressor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_model
|
BaseEstimator
|
Any sklearn-compatible regressor |
required |
calibration_method
|
str
|
"holdout" or "cross" |
'holdout'
|
calibration_size
|
float
|
Fraction of data for calibration (0-1) |
0.2
|
coverage_target
|
float
|
Default coverage level for intervals |
0.9
|
auto_tune
|
bool
|
Whether to tune supported hyperparameters before final fit |
True
|
uncertainty_features
|
list[str] | None
|
Optional hint for heteroscedastic features |
None
|
random_state
|
int | None
|
Random seed |
None
|
Source code in uncertainty_flow/wrappers/conformal.py
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 | |
fit(data, target=None, **kwargs)
¶
Fit the conformal regressor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
PolarsInput
|
Polars DataFrame or LazyFrame with features and target |
required |
target
|
TargetSpec | None
|
Target column name(s) |
None
|
**kwargs
|
Additional parameters (unused) |
{}
|
Returns:
| Type | Description |
|---|---|
ConformalRegressor
|
self (for method chaining) |
Source code in uncertainty_flow/wrappers/conformal.py
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 | |
predict(data)
¶
Generate probabilistic predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
PolarsInput
|
Polars DataFrame or LazyFrame with features |
required |
Returns:
| Type | Description |
|---|---|
DistributionPrediction
|
DistributionPrediction with quantile predictions |
Source code in uncertainty_flow/wrappers/conformal.py
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 | |
ConformalClassifier
¶
Conformal classifier with Adaptive Prediction Sets.
Wraps any sklearn classifier with a predict_proba method.
Examples:
>>> from sklearn.ensemble import RandomForestClassifier
>>> import polars as pl
>>> from uncertainty_flow.wrappers import ConformalClassifier
>>>
>>> df = pl.DataFrame({
... "x1": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
... "x2": [2, 4, 6, 8, 10, 12, 14, 16, 18, 20],
... "label": ["a", "a", "a", "a", "a", "b", "b", "b", "b", "b"],
... })
>>> model = ConformalClassifier(
... base_model=RandomForestClassifier(random_state=42),
... coverage_target=0.9,
... random_state=42,
... )
>>> model.fit(df, target="label")
>>> pred = model.predict(df)
>>> pred.set(0)
>>> pred.size
Source code in uncertainty_flow/wrappers/conformal_classifier.py
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 | |
metadata
property
¶
Return persisted metadata, or None for unfitted models.
predict_batch(data, batch_size=1000)
¶
Generate prediction sets in chunks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
PolarsInput
|
Feature DataFrame. |
required |
batch_size
|
int
|
Rows per batch. |
1000
|
Yields:
| Type | Description |
|---|---|
PredictionSet
|
|
Source code in uncertainty_flow/wrappers/conformal_classifier.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | |
save(path, include_metadata=True)
¶
Persist the conformal classifier via the standard .uf archive.
Source code in uncertainty_flow/wrappers/conformal_classifier.py
189 190 191 192 193 | |
load(path, **kwargs)
classmethod
¶
Load a conformal classifier from a .uf archive.
Source code in uncertainty_flow/wrappers/conformal_classifier.py
195 196 197 198 199 200 201 202 203 | |
ConformalForecaster
¶
Bases: BaseUncertaintyModel
Time series forecasting with conformal prediction.
Coverage guarantee: ✅ (with temporal correction) Non-crossing: ✅ (post-sort)
Examples:
>>> from sklearn.ensemble import GradientBoostingRegressor
>>> from uncertainty_flow.wrappers import ConformalForecaster
>>> import polars as pl
>>>
>>> df = pl.DataFrame({
... "date": range(10),
... "price": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
... })
>>> model = ConformalForecaster(
... base_model=GradientBoostingRegressor(),
... targets="price",
... horizon=3,
... lags=2,
... )
>>> model.fit(df)
>>> pred = model.predict(df)
Source code in uncertainty_flow/wrappers/conformal_ts.py
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 | |
uncertainty_drivers_
property
¶
Return residual correlation analysis results.
__init__(base_model, horizon, targets, copula_family='auto', lags=1, calibration_method='holdout', calibration_size=0.2, auto_tune=True, uncertainty_features=None, random_state=None)
¶
Initialize ConformalForecaster.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_model
|
BaseEstimator
|
Any sklearn-compatible regressor |
required |
horizon
|
int
|
Forecast horizon (steps ahead) |
required |
targets
|
str | list[str]
|
Target column name(s) |
required |
copula_family
|
str
|
( "auto" (BIC selection) or one of "gaussian", "clayton", "gumbel", "frank". " "Use "independent" for no inter-target correlation." |
'auto'
|
lags
|
int | list[int]
|
Lag order(s) to generate |
1
|
calibration_method
|
str
|
"holdout" or "cross" |
'holdout'
|
calibration_size
|
float
|
Fraction for calibration (from END) |
0.2
|
auto_tune
|
bool
|
Whether to tune supported hyperparameters before final fit |
True
|
uncertainty_features
|
list[str] | None
|
Optional hint for heteroscedastic features |
None
|
random_state
|
int | None
|
Random seed |
None
|
Source code in uncertainty_flow/wrappers/conformal_ts.py
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 | |
fit(data, target=None, **kwargs)
¶
Fit the conformal forecaster.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
PolarsInput
|
Polars DataFrame or LazyFrame with time series data |
required |
target
|
TargetSpec | None
|
Target column name(s) - uses self.targets if not provided |
None
|
**kwargs
|
Additional parameters (unused) |
{}
|
Returns:
| Type | Description |
|---|---|
ConformalForecaster
|
self (for method chaining) |
Source code in uncertainty_flow/wrappers/conformal_ts.py
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 | |
predict(data, steps=None)
¶
Generate probabilistic forecasts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
PolarsInput
|
Polars DataFrame or LazyFrame |
required |
steps
|
int | None
|
Number of steps to forecast (default: self.horizon) |
None
|
Returns:
| Type | Description |
|---|---|
DistributionPrediction
|
DistributionPrediction with quantile forecasts |
Source code in uncertainty_flow/wrappers/conformal_ts.py
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 | |
EnsembleBootstrapPI
¶
Bases: BaseUncertaintyModel
Ensemble Bootstrap Prediction Intervals (EnbPI).
Trains n_estimators bootstrap copies of a sklearn regressor, then
constructs prediction intervals from the ensemble distribution with
conformal calibration via stored nonconformity scores.
Usage pattern
fit(train_data, target)— trains the bootstrap ensemble.predict(test_data)— returns calibrated prediction intervals.update(y_true)— after observing the true value, updates the nonconformity score pool for future predictions.
Examples:
>>> from sklearn.ensemble import GradientBoostingRegressor
>>> import polars as pl
>>> from uncertainty_flow.wrappers import EnsembleBootstrapPI
>>>
>>> df = pl.DataFrame({
... "x": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
... "y": [1.5, 3.5, 5.5, 7.5, 9.5, 9.0, 7.0, 5.0, 3.0, 1.0],
... })
>>> model = EnsembleBootstrapPI(
... base_model=GradientBoostingRegressor(random_state=42),
... n_estimators=20,
... random_state=42,
... )
>>> model.fit(df, target="y")
>>> pred = model.predict(df)
>>> model.update(df["y"][0])
Source code in uncertainty_flow/wrappers/enbpi.py
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 | |