Forests
onlinecml.forests.causal_hoeffding_tree.CausalHoeffdingTree
Bases: BaseOnlineEstimator
Online causal tree with a CATE-variance split criterion.
Grows a binary decision tree one observation at a time using the Hoeffding bound to guarantee that splits are chosen with high probability from the same feature as a batch learner would choose, given enough data.
Improvements over a naive causal tree:
- Multi-threshold split search: instead of a single running-mean threshold, evaluates 5 quantile-based candidates per feature and picks the best, improving split location accuracy.
- Linear leaf models: each leaf maintains separate River
LinearRegressionmodels for the treated and control arms.predict_onereturnsmu1(x) - mu0(x)(individual CATE) rather than a flat leaf mean. - Doubly robust leaf CATE: the per-leaf ATE baseline used for split
scoring is the running mean of the DR pseudo-outcome
mu1 - mu0 + W(Y-mu1)/p - (1-W)(Y-mu0)/(1-p), correcting for within-leaf confounding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grace_period
|
int
|
Minimum observations a leaf must collect before attempting a split. Default 200. |
200
|
delta
|
float
|
Confidence parameter for the Hoeffding bound. Default 1e-5. |
1e-05
|
tau
|
float
|
Tie-breaking threshold. Default 0.05. |
0.05
|
max_depth
|
int or None
|
Maximum tree depth. |
10
|
min_arm_samples
|
int
|
Minimum per-arm observations required per child for split scoring and for switching to linear-model predictions. Default 5. |
5
|
mtry
|
int or None
|
Number of features randomly considered at each split attempt.
|
None
|
outcome_range
|
float
|
Upper bound on |
10.0
|
clip_ps
|
float
|
Propensity score clipping bounds |
0.1
|
seed
|
int or None
|
Random seed for the mtry RNG. Default None. |
None
|
Notes
Split score (maximised):
.. math::
\text{score}(j) = \frac{n_L}{n}(\hat{\tau}_L - \hat{\tau})^2
+ \frac{n_R}{n}(\hat{\tau}_R - \hat{\tau})^2
where :math:\hat{\tau} is the DR-corrected leaf CATE and
:math:\hat{\tau}_k = \bar{Y}_{1,k} - \bar{Y}_{0,k} for child k.
References
Domingos, P. and Hulten, G. (2000). Mining high-speed data streams. KDD, 71-80.
Wager, S. and Athey, S. (2018). Estimation and inference of heterogeneous treatment effects using random forests. JASA, 113(523), 1228-1242.
Examples:
>>> from onlinecml.datasets import HeterogeneousCausalStream
>>> from onlinecml.forests import CausalHoeffdingTree
>>> tree = CausalHoeffdingTree(grace_period=50, delta=0.01, seed=42)
>>> for x, w, y, _ in HeterogeneousCausalStream(n=1000, seed=0):
... tree.learn_one(x, w, y)
>>> isinstance(tree.predict_one({'x0': 1.0, 'x1': 0.0, 'x2': 0.0, 'x3': 0.0, 'x4': 0.0}), float)
True
Source code in onlinecml/forests/causal_hoeffding_tree.py
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 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 | |
n_leaves
property
Number of leaf nodes.
n_nodes
property
Total number of nodes (internal + leaf) in the tree.
learn_one(x, treatment, outcome, propensity=None)
Process one observation and potentially grow the tree.
Uses a predict-first-then-learn protocol: all three leaf models (treated, control, propensity) predict before being updated, so the DR pseudo-outcome is computed from out-of-sample predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
dict
|
Covariate dictionary. |
required |
treatment
|
int
|
Treatment indicator (0 or 1). |
required |
outcome
|
float
|
Observed outcome. |
required |
propensity
|
float or None
|
If provided, uses this logged propensity instead of the leaf PS model for the DR correction. |
None
|
Source code in onlinecml/forests/causal_hoeffding_tree.py
predict_one(x)
Predict CATE for a single unit using the leaf's linear models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
dict
|
Covariate dictionary. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Estimated CATE: |
Source code in onlinecml/forests/causal_hoeffding_tree.py
onlinecml.forests.online_causal_forest.OnlineCausalForest
Bases: BaseOnlineEstimator
Ensemble of CausalHoeffdingTrees for online CATE estimation.
Grows n_trees independent CausalHoeffdingTree instances in parallel.
Each tree receives a random subsample of each observation (Poisson bootstrap,
Oza 2001). The forest CATE prediction is the mean of all tree predictions.
Each tree is monitored for concept drift via an ADWIN detector on its normalised prediction signal. On drift detection the affected tree is reset and starts growing from scratch, while the remaining trees continue uninterrupted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_trees
|
int
|
Number of trees in the ensemble. Default 10. |
10
|
grace_period
|
int
|
Grace period passed to each |
200
|
delta
|
float
|
Hoeffding confidence parameter for each tree. Default 1e-5. |
1e-05
|
tau
|
float
|
Tie-breaking threshold for each tree. Default 0.05. |
0.05
|
max_depth
|
int or None
|
Maximum tree depth. Default 10. |
10
|
subsample_rate
|
float
|
Expected number of times each tree sees each observation (Poisson
bootstrap |
1.0
|
mtry
|
int or None
|
Number of features randomly considered at each split attempt per tree.
|
None
|
min_arm_samples
|
int
|
Passed to each tree. Default 5. |
5
|
outcome_range
|
float
|
Passed to each tree. Upper bound on |
10.0
|
clip_ps
|
float
|
Propensity clipping bounds for DR correction within leaves. Default 0.1. |
0.1
|
drift_detection
|
bool
|
If |
True
|
seed
|
int or None
|
Random seed for the subsampling RNG. |
None
|
Notes
Online bagging (Oza 2001): each incoming observation is presented to tree
k exactly Poisson(subsample_rate) times.
Drift detection follows the ARF approach: each tree's prediction is
normalised to [0, 1] using the running mean ± 3σ window and fed to
ADWIN. When ADWIN raises an alarm, the tree and its detector are reset.
References
Oza, N.C. (2001). Online bagging and boosting. Proc. American Statistical Association, 229-234.
Gomes, H.M. et al. (2017). Adaptive random forests for evolving data stream classification. Machine Learning, 106(9), 1469-1495.
Examples:
>>> from onlinecml.datasets import LinearCausalStream
>>> from onlinecml.forests import OnlineCausalForest
>>> forest = OnlineCausalForest(n_trees=5, grace_period=50, seed=0)
>>> for x, w, y, _ in LinearCausalStream(n=500, seed=0):
... forest.learn_one(x, w, y)
>>> isinstance(forest.predict_one({'x0': 0.5, 'x1': -0.3, 'x2': 0.0, 'x3': 0.1, 'x4': -0.2}), float)
True
Source code in onlinecml/forests/online_causal_forest.py
15 16 17 18 19 20 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 | |
n_leaves
property
Number of leaf nodes in each tree.
n_nodes
property
Number of nodes in each tree.
learn_one(x, treatment, outcome, propensity=None)
Process one observation, updating all trees via online bagging.
After each tree update, optionally checks for concept drift using its ADWIN detector and resets the tree if drift is detected.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
dict
|
Covariate dictionary. |
required |
treatment
|
int
|
Treatment indicator (0 or 1). |
required |
outcome
|
float
|
Observed outcome. |
required |
propensity
|
float or None
|
If provided, passed to each tree's DR correction. |
None
|
Source code in onlinecml/forests/online_causal_forest.py
predict_ate()
Return the current ATE estimate as the mean of tree DR-corrected ATEs.
Each tree maintains a running mean of its own DR pseudo-outcomes. The forest ATE is their simple average, which converges to the true ATE without cold-start bias from untrained linear leaf models.
Returns:
| Type | Description |
|---|---|
float
|
Mean ATE across all trees. Returns |
Source code in onlinecml/forests/online_causal_forest.py
predict_ci(alpha=0.05)
Return a confidence interval for the ATE as the mean of tree CIs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alpha
|
float
|
Significance level. Default 0.05 gives a 95% CI. |
0.05
|
Returns:
| Name | Type | Description |
|---|---|---|
lower |
float
|
Mean lower bound across all tree CIs. |
upper |
float
|
Mean upper bound across all tree CIs. |
Source code in onlinecml/forests/online_causal_forest.py
predict_one(x)
Predict CATE as the mean across all tree predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
dict
|
Covariate dictionary. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Mean CATE across all trees. Returns |