perpetual 2.0.0

A self-generalizing gradient boosting machine that doesn't need hyperparameter optimization
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
//! Meta-learners for Heterogeneous Treatment Effect (HTE) estimation.
//!
//! Implements standard meta-algorithms (S-Learner, T-Learner, X-Learner, DR-Learner)
//! wrapping `PerpetualBooster`.

use crate::booster::core::PerpetualBooster;
use crate::constraints::ConstraintMap;
use crate::data::Matrix;
use crate::errors::PerpetualError;
use crate::objective::Objective;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;

// Helper to create a base booster configuration
#[allow(clippy::too_many_arguments)]
fn create_booster(
    budget: f32,
    objective: Objective,
    max_bin: u16,
    num_threads: Option<usize>,
    monotone_constraints: Option<ConstraintMap>,
    interaction_constraints: Option<Vec<Vec<usize>>>,
    force_children_to_bound_parent: bool,
    missing: f64,
    allow_missing_splits: bool,
    create_missing_branch: bool,
    terminate_missing_features: HashSet<usize>,
    missing_node_treatment: crate::booster::config::MissingNodeTreatment,
    log_iterations: usize,
    seed: u64,
    reset: Option<bool>,
    categorical_features: Option<HashSet<usize>>,
    timeout: Option<f32>,
    iteration_limit: Option<usize>,
    memory_limit: Option<f32>,
    stopping_rounds: Option<usize>,
) -> Result<PerpetualBooster, PerpetualError> {
    PerpetualBooster::new(
        objective,
        budget,
        0.0, // Set base_score to 0.0 for causal models to ensure stable diffs
        max_bin,
        num_threads,
        monotone_constraints,
        interaction_constraints,
        force_children_to_bound_parent,
        missing,
        allow_missing_splits,
        create_missing_branch,
        terminate_missing_features,
        missing_node_treatment,
        log_iterations,
        seed,
        reset,
        categorical_features,
        timeout,
        iteration_limit,
        memory_limit,
        stopping_rounds,
        false,
    )
}

fn extract_subsets(x: &Matrix<f64>, w: &[f64], y: &[f64]) -> (Vec<f64>, Vec<f64>, usize, Vec<f64>, Vec<f64>, usize) {
    let n = x.rows;
    let idx0: Vec<usize> = w
        .iter()
        .enumerate()
        .filter(|&(_, &v)| v == 0.0)
        .map(|(i, _)| i)
        .collect();
    let idx1: Vec<usize> = w
        .iter()
        .enumerate()
        .filter(|&(_, &v)| v == 1.0)
        .map(|(i, _)| i)
        .collect();

    let n0 = idx0.len();
    let n1 = idx1.len();

    let get_subset = |indices: &[usize]| -> (Vec<f64>, Vec<f64>) {
        let mut sub_x = Vec::with_capacity(indices.len() * x.cols);
        let mut sub_y = Vec::with_capacity(indices.len());
        for col in 0..x.cols {
            let col_data = &x.data[col * n..(col + 1) * n];
            for &i in indices {
                sub_x.push(col_data[i]);
            }
        }
        for &i in indices {
            sub_y.push(y[i]);
        }
        (sub_x, sub_y)
    };

    let (x0_data, y0) = get_subset(&idx0);
    let (x1_data, y1) = get_subset(&idx1);

    (x0_data, y0, n0, x1_data, y1, n1)
}

/// S-Learner (Single Learner).
///
/// Estimates $Y \approx \mu(X, W)$.
/// CATE(x) = $\mu(x, 1) - \mu(x, 0)$.
#[derive(Serialize, Deserialize)]
pub struct SLearner {
    pub model: PerpetualBooster,
}

impl SLearner {
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        budget: f32,
        // ... (other args passed to create_booster)
        max_bin: u16,
        num_threads: Option<usize>,
        monotone_constraints: Option<ConstraintMap>,
        interaction_constraints: Option<Vec<Vec<usize>>>,
        force_children_to_bound_parent: bool,
        missing: f64,
        allow_missing_splits: bool,
        create_missing_branch: bool,
        terminate_missing_features: HashSet<usize>,
        missing_node_treatment: crate::booster::config::MissingNodeTreatment,
        log_iterations: usize,
        seed: u64,
        reset: Option<bool>,
        categorical_features: Option<HashSet<usize>>,
        timeout: Option<f32>,
        iteration_limit: Option<usize>,
        memory_limit: Option<f32>,
        stopping_rounds: Option<usize>,
    ) -> Result<Self, PerpetualError> {
        let model = create_booster(
            budget,
            Objective::SquaredLoss,
            max_bin,
            num_threads,
            monotone_constraints,
            interaction_constraints,
            force_children_to_bound_parent,
            missing,
            allow_missing_splits,
            create_missing_branch,
            terminate_missing_features,
            missing_node_treatment,
            log_iterations,
            seed,
            reset,
            categorical_features,
            timeout,
            iteration_limit,
            memory_limit,
            stopping_rounds,
        )?;
        Ok(Self { model })
    }

    pub fn fit(&mut self, x: &Matrix<f64>, w: &[f64], y: &[f64]) -> Result<(), PerpetualError> {
        // Concatenate W as a new feature to X
        let rows = x.rows;
        let x_cols = x.cols;
        let mut data = Vec::with_capacity(x.data.len() + rows);
        data.extend_from_slice(x.data);
        data.extend_from_slice(w);

        let matrix_aug = Matrix::new(&data, rows, x_cols + 1);
        self.model.fit(&matrix_aug, y, None, None)
    }

    pub fn predict(&self, x: &Matrix<f64>) -> Vec<f64> {
        let rows = x.rows;
        let x_cols = x.cols;

        // Predict mu(X, 1)
        let mut data_1 = Vec::with_capacity(x.data.len() + rows);
        data_1.extend_from_slice(x.data);
        data_1.resize(data_1.len() + rows, 1.0);
        let matrix_1 = Matrix::new(&data_1, rows, x_cols + 1);
        let mu1 = self.model.predict(&matrix_1, true);

        // Predict mu(X, 0)
        let mut data_0 = Vec::with_capacity(x.data.len() + rows);
        data_0.extend_from_slice(x.data);
        data_0.resize(data_0.len() + rows, 0.0);
        let matrix_0 = Matrix::new(&data_0, rows, x_cols + 1);
        let mu0 = self.model.predict(&matrix_0, true);

        mu1.iter().zip(mu0.iter()).map(|(m1, m0)| m1 - m0).collect()
    }
}

// ---------------------------------------------------------------------------
// T-Learner
// ---------------------------------------------------------------------------

/// T-Learner (Two Learners).
///
/// Estimates $\mu_0(X)$ on control data and $\mu_1(X)$ on treated data.
/// CATE(x) = $\mu_1(x) - \mu_0(x)$.
#[derive(Serialize, Deserialize)]
pub struct TLearner {
    pub mu0: PerpetualBooster,
    pub mu1: PerpetualBooster,
}

impl TLearner {
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        budget: f32,
        // ... (other args)
        max_bin: u16,
        num_threads: Option<usize>,
        monotone_constraints: Option<ConstraintMap>,
        interaction_constraints: Option<Vec<Vec<usize>>>,
        force_children_to_bound_parent: bool,
        missing: f64,
        allow_missing_splits: bool,
        create_missing_branch: bool,
        terminate_missing_features: HashSet<usize>,
        missing_node_treatment: crate::booster::config::MissingNodeTreatment,
        log_iterations: usize,
        seed: u64,
        reset: Option<bool>,
        categorical_features: Option<HashSet<usize>>,
        timeout: Option<f32>,
        iteration_limit: Option<usize>,
        memory_limit: Option<f32>,
        stopping_rounds: Option<usize>,
    ) -> Result<Self, PerpetualError> {
        let mu0 = create_booster(
            budget,
            Objective::SquaredLoss,
            max_bin,
            num_threads,
            monotone_constraints.clone(),
            interaction_constraints.clone(),
            force_children_to_bound_parent,
            missing,
            allow_missing_splits,
            create_missing_branch,
            terminate_missing_features.clone(),
            missing_node_treatment,
            log_iterations,
            seed + 1,
            reset,
            categorical_features.clone(),
            timeout,
            iteration_limit,
            memory_limit,
            stopping_rounds,
        )?;
        let mu1 = create_booster(
            budget,
            Objective::SquaredLoss,
            max_bin,
            num_threads,
            monotone_constraints,
            interaction_constraints,
            force_children_to_bound_parent,
            missing,
            allow_missing_splits,
            create_missing_branch,
            terminate_missing_features,
            missing_node_treatment,
            log_iterations,
            seed + 1, // slight seed variation
            reset,
            categorical_features,
            timeout,
            iteration_limit,
            memory_limit,
            stopping_rounds,
        )?;
        Ok(Self { mu0, mu1 })
    }

    pub fn fit(&mut self, x: &Matrix<f64>, w: &[f64], y: &[f64]) -> Result<(), PerpetualError> {
        let (x0_data, y0, n0, x1_data, y1, n1) = extract_subsets(x, w, y);
        let matrix0 = Matrix::new(&x0_data, n0, x.cols);
        let matrix1 = Matrix::new(&x1_data, n1, x.cols);

        self.mu0.fit(&matrix0, &y0, None, None)?;
        self.mu1.fit(&matrix1, &y1, None, None)?;

        Ok(())
    }

    pub fn predict(&self, x: &Matrix<f64>) -> Vec<f64> {
        let p1 = self.mu1.predict(x, true);
        let p0 = self.mu0.predict(x, true);
        p1.iter().zip(p0.iter()).map(|(a, b)| a - b).collect()
    }
}

// ---------------------------------------------------------------------------
// X-Learner
// ---------------------------------------------------------------------------

/// X-Learner.
///
/// 4-stage meta-learner suited for imbalanced treatment groups.
#[derive(Serialize, Deserialize)]
pub struct XLearner {
    pub mu0: PerpetualBooster,
    pub mu1: PerpetualBooster,
    pub tau0: PerpetualBooster,
    pub tau1: PerpetualBooster,
    pub propensity: PerpetualBooster,
}

impl XLearner {
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        budget: f32,
        propensity_budget: Option<f32>,
        // ... (other args)
        max_bin: u16,
        num_threads: Option<usize>,
        monotone_constraints: Option<ConstraintMap>,
        interaction_constraints: Option<Vec<Vec<usize>>>,
        force_children_to_bound_parent: bool,
        missing: f64,
        allow_missing_splits: bool,
        create_missing_branch: bool,
        terminate_missing_features: HashSet<usize>,
        missing_node_treatment: crate::booster::config::MissingNodeTreatment,
        log_iterations: usize,
        seed: u64,
        reset: Option<bool>,
        categorical_features: Option<HashSet<usize>>,
        timeout: Option<f32>,
        iteration_limit: Option<usize>,
        memory_limit: Option<f32>,
        stopping_rounds: Option<usize>,
    ) -> Result<Self, PerpetualError> {
        let p_budget = propensity_budget.unwrap_or(budget);

        // Helper to clone args
        let make = |obj: Objective, b: f32, s: u64| {
            create_booster(
                b,
                obj,
                max_bin,
                num_threads,
                monotone_constraints.clone(),
                interaction_constraints.clone(),
                force_children_to_bound_parent,
                missing,
                allow_missing_splits,
                create_missing_branch,
                terminate_missing_features.clone(),
                missing_node_treatment,
                log_iterations,
                s,
                reset,
                categorical_features.clone(),
                timeout,
                iteration_limit,
                memory_limit,
                stopping_rounds,
            )
        };

        Ok(Self {
            mu0: make(Objective::SquaredLoss, budget, seed)?,
            mu1: make(Objective::SquaredLoss, budget, seed + 1)?,
            tau0: make(Objective::SquaredLoss, budget, seed + 2)?,
            tau1: make(Objective::SquaredLoss, budget, seed + 3)?,
            propensity: make(Objective::LogLoss, p_budget, seed + 4)?,
        })
    }

    pub fn fit(&mut self, x: &Matrix<f64>, w: &[f64], y: &[f64]) -> Result<(), PerpetualError> {
        let (x0_data, y0, n0, x1_data, y1, n1) = extract_subsets(x, w, y);
        let matrix0 = Matrix::new(&x0_data, n0, x.cols);
        let matrix1 = Matrix::new(&x1_data, n1, x.cols);

        // 2. Stage 1: Fit mu0 and mu1
        self.mu0.fit(&matrix0, &y0, None, None)?;
        self.mu1.fit(&matrix1, &y1, None, None)?;

        // 3. Impute effects
        // D1 = Y1 - mu0(X1)
        let mu0_on_1 = self.mu0.predict(&matrix1, true);
        let d1: Vec<f64> = y1.iter().zip(mu0_on_1.iter()).map(|(yi, m)| yi - m).collect();

        // D0 = mu1(X0) - Y0
        let mu1_on_0 = self.mu1.predict(&matrix0, true);
        let d0: Vec<f64> = mu1_on_0.iter().zip(y0.iter()).map(|(m, yi)| m - yi).collect();

        // 4. Stage 3: Fit tau0 and tau1
        self.tau1.fit(&matrix1, &d1, None, None)?;
        self.tau0.fit(&matrix0, &d0, None, None)?;

        // 5. Stage 4: Propensity
        self.propensity.fit(x, w, None, None)?;

        Ok(())
    }

    pub fn predict(&self, x: &Matrix<f64>) -> Vec<f64> {
        let t0 = self.tau0.predict(x, true);
        let t1 = self.tau1.predict(x, true);

        let log_odds = self.propensity.predict(x, true);
        let p: Vec<f64> = log_odds.iter().map(|lo| 1.0 / (1.0 + (-lo).exp())).collect();

        // CATE = p * t0 + (1-p) * t1
        t0.iter()
            .zip(t1.iter())
            .zip(p.iter())
            .map(|((t0_i, t1_i), p_i)| p_i * t0_i + (1.0 - p_i) * t1_i)
            .collect()
    }
}

// ---------------------------------------------------------------------------
// DR-Learner (AIPW)
// ---------------------------------------------------------------------------

/// DR-Learner (Doubly Robust).
///
/// Uses AIPW pseudo-outcomes to regress the CATE.
#[derive(Serialize, Deserialize)]
pub struct DRLearner {
    pub mu0: PerpetualBooster,
    pub mu1: PerpetualBooster,
    pub propensity: PerpetualBooster,
    pub effect: PerpetualBooster,
}

impl DRLearner {
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        budget: f32,
        propensity_budget: Option<f32>,
        // ... (other args)
        max_bin: u16,
        num_threads: Option<usize>,
        monotone_constraints: Option<ConstraintMap>,
        interaction_constraints: Option<Vec<Vec<usize>>>,
        force_children_to_bound_parent: bool,
        missing: f64,
        allow_missing_splits: bool,
        create_missing_branch: bool,
        terminate_missing_features: HashSet<usize>,
        missing_node_treatment: crate::booster::config::MissingNodeTreatment,
        log_iterations: usize,
        seed: u64,
        reset: Option<bool>,
        categorical_features: Option<HashSet<usize>>,
        timeout: Option<f32>,
        iteration_limit: Option<usize>,
        memory_limit: Option<f32>,
        stopping_rounds: Option<usize>,
    ) -> Result<Self, PerpetualError> {
        let p_budget = propensity_budget.unwrap_or(budget);

        // Helper to clone args
        let make = |obj: Objective, b: f32, s: u64| {
            create_booster(
                b,
                obj,
                max_bin,
                num_threads,
                monotone_constraints.clone(),
                interaction_constraints.clone(),
                force_children_to_bound_parent,
                missing,
                allow_missing_splits,
                create_missing_branch,
                terminate_missing_features.clone(),
                missing_node_treatment,
                log_iterations,
                s,
                reset,
                categorical_features.clone(),
                timeout,
                iteration_limit,
                memory_limit,
                stopping_rounds,
            )
        };

        Ok(Self {
            mu0: make(Objective::SquaredLoss, budget, seed)?,
            mu1: make(Objective::SquaredLoss, budget, seed + 1)?,
            propensity: make(Objective::LogLoss, p_budget, seed + 2)?,
            effect: make(Objective::SquaredLoss, budget, seed + 3)?,
        })
    }

    pub fn fit(&mut self, x: &Matrix<f64>, w: &[f64], y: &[f64]) -> Result<(), PerpetualError> {
        let n = x.rows;

        // 1. Split data (Similar reuse to T/X Learner, effectively T-Learner step)
        let (x0_data, y0, n0, x1_data, y1, n1) = extract_subsets(x, w, y);
        let matrix0 = Matrix::new(&x0_data, n0, x.cols);
        let matrix1 = Matrix::new(&x1_data, n1, x.cols);

        self.mu0.fit(&matrix0, &y0, None, None)?;
        self.mu1.fit(&matrix1, &y1, None, None)?;

        // 2. Propensity
        self.propensity.fit(x, w, None, None)?;

        // 3. Compute Pseudo-outcomes
        let mu0_hat = self.mu0.predict(x, true);
        let mu1_hat = self.mu1.predict(x, true);
        let log_odds = self.propensity.predict(x, true);
        let p_hat: Vec<f64> = log_odds.iter().map(|lo| 1.0 / (1.0 + (-lo).exp())).collect();

        // Using PolicyObjective helper or manual calculation?
        // Let's use PolicyObjective logic but manual here to avoid constructing extra structs if not needed.
        // Gamma = (mu1 - mu0) + W(Y - mu1)/p - (1-W)(Y - mu0)/(1-p)

        let mut gamma = Vec::with_capacity(n);
        for i in 0..n {
            let m1 = mu1_hat[i];
            let m0 = mu0_hat[i];
            let p = p_hat[i].clamp(1e-3, 1.0 - 1e-3);
            let wi = w[i];
            let yi = y[i];

            let term1 = m1 - m0;
            let term2 = wi * (yi - m1) / p;
            let term3 = (1.0 - wi) * (yi - m0) / (1.0 - p);

            gamma.push(term1 + term2 - term3);
        }

        // 4. Fit Effect Model
        self.effect.fit(x, &gamma, None, None)?;

        Ok(())
    }

    pub fn predict(&self, x: &Matrix<f64>) -> Vec<f64> {
        self.effect.predict(x, true)
    }
}