bicmath-statistics 0.1.0

Descriptive statistics, distributions, inference, and experiment analysis for BicMath.
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
//! Stratified experiment analysis.
//!
//! The functions here are generic and data-driven: nothing is specific to a
//! campaign. Each stratum carries a categorical per-person outcome distribution
//! for a treatment and a control arm, and the arm summaries are standardized to
//! a caller-supplied target mix. Positive outcome rates, negative outcome
//! rates, and the contribution difference are reported in separate blocks so
//! that a change in retention can never be conflated with a change in net
//! contribution.

use std::collections::{BTreeMap, BTreeSet};
use std::sync::Arc;

use bicmath_core::context::ExecContext;
use bicmath_core::contract::{
    Args, Assumption, CostClass, Example, FunctionDescriptor, Outcome, ParamDescriptor,
    SimpleFunction, Warning, require_mode,
};
use bicmath_core::error::{EngineError, ErrorCode};
use bicmath_core::schema::ValueSchema;
use bicmath_core::value::Value;

use crate::common::*;
use crate::mathfn::{normal_quantile, sqrt};

const MIX_TOLERANCE: f64 = 1e-9;

// ---------------------------------------------------------------------------
// Input model
// ---------------------------------------------------------------------------

#[derive(Clone, Debug)]
struct OutcomeCategory {
    value: f64,
    count: u64,
}

#[derive(Clone, Debug)]
struct ArmInput {
    assigned: u64,
    outcomes: Vec<OutcomeCategory>,
}

#[derive(Clone, Debug)]
struct StratumInput {
    name: String,
    target_weight: f64,
    treatment: ArmInput,
    control: ArmInput,
}

#[derive(Clone, Debug)]
struct ArmStats {
    assigned: u64,
    mean: f64,
    variance: f64,
    positive_count: u64,
    negative_count: u64,
    zero_count: u64,
    positive_rate: f64,
    negative_rate: f64,
    net_positive_rate: f64,
}

impl ArmStats {
    fn n(&self) -> f64 {
        self.assigned as f64
    }

    /// Per-person variance of the indicator 1{positive} - 1{negative}.
    fn net_variance(&self) -> f64 {
        self.positive_rate + self.negative_rate - self.net_positive_rate.powi(2)
    }
}

#[derive(Clone, Debug)]
struct StratumStats {
    name: String,
    target_weight: f64,
    weight: f64,
    treatment: ArmStats,
    control: ArmStats,
}

#[derive(Clone, Debug)]
struct Block {
    treatment: f64,
    control: f64,
    difference: f64,
    variance: f64,
    standard_error: f64,
    ci_lower: f64,
    ci_upper: f64,
}

impl Block {
    fn new(treatment: f64, control: f64, variance: f64, z: f64) -> Result<Block, EngineError> {
        if !variance.is_finite() {
            return Err(EngineError::domain("standardized variance is not finite"));
        }
        let variance = variance.max(0.0);
        let standard_error = sqrt(variance);
        let difference = treatment - control;
        Ok(Block {
            treatment,
            control,
            difference,
            variance,
            standard_error,
            ci_lower: difference - z * standard_error,
            ci_upper: difference + z * standard_error,
        })
    }
}

#[derive(Clone, Debug)]
struct PooledRates {
    treatment_positive_rate: f64,
    control_positive_rate: f64,
    positive_rate_difference: f64,
    treatment_negative_rate: f64,
    control_negative_rate: f64,
    negative_rate_difference: f64,
    treatment_net_positive_rate: f64,
    control_net_positive_rate: f64,
    net_positive_rate_difference: f64,
}

#[derive(Clone, Debug)]
struct Analysis {
    strata: Vec<StratumStats>,
    standardized: Block,
    positive_rate: Block,
    negative_rate: Block,
    net_positive_rate: Block,
    observed_mix_matches: bool,
    pooled: PooledRates,
    confidence: f64,
    z: f64,
    /// Sum of the target weights: the represented population scale used to
    /// convert per-person standardized quantities to totals.
    population: f64,
}

// ---------------------------------------------------------------------------
// Parsing
// ---------------------------------------------------------------------------

fn path_error(error: EngineError, path: &str) -> EngineError {
    error.with_path(path.to_string())
}

fn record_at<'a>(value: &'a Value, path: &str) -> Result<&'a BTreeMap<String, Value>, EngineError> {
    value.as_record().map_err(|e| path_error(e, path))
}

fn text_field(
    record: &BTreeMap<String, Value>,
    key: &str,
    path: &str,
) -> Result<String, EngineError> {
    match record.get(key) {
        Some(Value::Text(value)) => Ok(value.clone()),
        Some(other) => Err(path_error(
            EngineError::malformed(format!(
                "field {key:?} must be text, found {}",
                other.kind_name()
            )),
            path,
        )),
        None => Err(path_error(
            EngineError::malformed(format!("missing required field {key:?}")),
            path,
        )),
    }
}

fn number_field(
    record: &BTreeMap<String, Value>,
    key: &str,
    path: &str,
) -> Result<f64, EngineError> {
    match record.get(key) {
        Some(Value::Number(number)) => {
            number_to_f64(number).map_err(|e| path_error(e, &format!("{path}.{key}")))
        }
        Some(other) => Err(path_error(
            EngineError::malformed(format!(
                "field {key:?} must be a number, found {}",
                other.kind_name()
            )),
            path,
        )),
        None => Err(path_error(
            EngineError::malformed(format!("missing required field {key:?}")),
            path,
        )),
    }
}

fn array_field<'a>(
    record: &'a BTreeMap<String, Value>,
    key: &str,
    path: &str,
) -> Result<&'a [Value], EngineError> {
    match record.get(key) {
        Some(Value::Array(values)) => Ok(values),
        Some(other) => Err(path_error(
            EngineError::malformed(format!(
                "field {key:?} must be an array, found {}",
                other.kind_name()
            )),
            path,
        )),
        None => Err(path_error(
            EngineError::malformed(format!("missing required field {key:?}")),
            path,
        )),
    }
}

fn parse_arm(raw: Option<&Value>, path: &str) -> Result<ArmInput, EngineError> {
    let raw =
        raw.ok_or_else(|| path_error(EngineError::malformed("missing required arm record"), path))?;
    let record = record_at(raw, path)?;
    let assigned = match record.get("assigned") {
        Some(Value::Number(number)) => {
            non_negative_u64(&number_to_bigint(number, path)?, "assigned")?
        }
        Some(other) => {
            return Err(path_error(
                EngineError::malformed(format!(
                    "field \"assigned\" must be an integer, found {}",
                    other.kind_name()
                )),
                path,
            ));
        }
        None => {
            return Err(path_error(
                EngineError::malformed("missing required field \"assigned\""),
                path,
            ));
        }
    };
    if assigned == 0 {
        return Err(path_error(
            EngineError::domain("assigned must be at least 1"),
            path,
        ));
    }
    let outcomes = parse_outcomes(record, path)?;
    Ok(ArmInput { assigned, outcomes })
}

fn number_to_bigint(
    number: &bicmath_core::number::Number,
    path: &str,
) -> Result<num_bigint::BigInt, EngineError> {
    match number {
        bicmath_core::number::Number::Integer(value) => Ok(value.clone()),
        other => other
            .to_exact_rational()
            .and_then(|rational| {
                if rational.is_integer() {
                    Some(rational.to_integer())
                } else {
                    None
                }
            })
            .ok_or_else(|| path_error(EngineError::malformed("expected an integer value"), path)),
    }
}

fn parse_outcomes(
    record: &BTreeMap<String, Value>,
    path: &str,
) -> Result<Vec<OutcomeCategory>, EngineError> {
    let raw = array_field(record, "outcomes", path)?;
    if raw.is_empty() {
        return Err(path_error(
            EngineError::domain("each arm must have at least one outcome category"),
            path,
        ));
    }
    let mut outcomes = Vec::with_capacity(raw.len());
    for (index, item) in raw.iter().enumerate() {
        let entry_path = format!("{path}.outcomes[{index}]");
        let entry = record_at(item, &entry_path)?;
        let value = number_field(entry, "value", &entry_path)?;
        let count = match entry.get("count") {
            Some(Value::Number(number)) => {
                non_negative_u64(&number_to_bigint(number, &entry_path)?, "count")?
            }
            Some(other) => {
                return Err(path_error(
                    EngineError::malformed(format!(
                        "field \"count\" must be an integer, found {}",
                        other.kind_name()
                    )),
                    &entry_path,
                ));
            }
            None => {
                return Err(path_error(
                    EngineError::malformed("missing required field \"count\""),
                    &entry_path,
                ));
            }
        };
        outcomes.push(OutcomeCategory { value, count });
    }
    Ok(outcomes)
}

fn parse_strata(args: &Args, name: &str) -> Result<Vec<StratumInput>, EngineError> {
    let items = args.array(name)?;
    if items.is_empty() {
        return Err(EngineError::domain("strata must not be empty").with_path(name.to_string()));
    }
    let mut seen = BTreeSet::new();
    let mut strata = Vec::with_capacity(items.len());
    for (index, item) in items.iter().enumerate() {
        let path = format!("{name}[{index}]");
        let record = record_at(item, &path)?;
        let stratum_name = text_field(record, "name", &path)?;
        if !seen.insert(stratum_name.clone()) {
            return Err(path_error(
                EngineError::malformed(format!("duplicate stratum name {stratum_name:?}")),
                &path,
            ));
        }
        let target_weight = number_field(record, "target_weight", &path)?;
        if target_weight < 0.0 {
            return Err(path_error(
                EngineError::domain("target_weight must be non-negative"),
                &format!("{path}.target_weight"),
            ));
        }
        let treatment = parse_arm(record.get("treatment"), &format!("{path}.treatment"))?;
        let control = parse_arm(record.get("control"), &format!("{path}.control"))?;
        if treatment.outcomes.iter().map(|o| o.count).sum::<u64>() != treatment.assigned {
            return Err(path_error(
                EngineError::domain(
                    "treatment outcome counts must sum exactly to treatment.assigned",
                ),
                &format!("{path}.treatment"),
            ));
        }
        if control.outcomes.iter().map(|o| o.count).sum::<u64>() != control.assigned {
            return Err(path_error(
                EngineError::domain("control outcome counts must sum exactly to control.assigned"),
                &format!("{path}.control"),
            ));
        }
        strata.push(StratumInput {
            name: stratum_name,
            target_weight,
            treatment,
            control,
        });
    }
    // The sum of target weights is validated during analysis; an all-zero mix
    // is rejected there, which also allows prospective inputs whose weights are
    // supplied only by the pooled combination.
    Ok(strata)
}

// ---------------------------------------------------------------------------
// Arm statistics
// ---------------------------------------------------------------------------

fn arm_stats(arm: &ArmInput, ddof: u64) -> Result<ArmStats, EngineError> {
    if arm.assigned <= ddof {
        return Err(insufficient(format!(
            "each arm needs more than variance_ddof = {ddof} assigned observations"
        )));
    }
    let n = arm.assigned as f64;
    let mut sum = 0.0f64;
    let mut sum_squares = 0.0f64;
    let mut positive_count = 0u64;
    let mut negative_count = 0u64;
    let mut zero_count = 0u64;
    for outcome in &arm.outcomes {
        let count = outcome.count as f64;
        sum += outcome.value * count;
        sum_squares += outcome.value * outcome.value * count;
        if outcome.value > 0.0 {
            positive_count += outcome.count;
        } else if outcome.value < 0.0 {
            negative_count += outcome.count;
        } else {
            zero_count += outcome.count;
        }
    }
    let mean = sum / n;
    let raw_variance = (sum_squares - n * mean * mean) / (n - ddof as f64);
    // The mandated formula can cancel catastrophically; treat only tiny
    // negative artifacts as zero and reject anything materially negative.
    let variance = if raw_variance < 0.0 {
        if raw_variance > -1e-9 * (1.0 + sum_squares.abs()) {
            0.0
        } else {
            return Err(EngineError::domain(
                "computed arm variance is negative; outcome values are too large for the \
                 mandated sum-of-squares formula",
            ));
        }
    } else {
        raw_variance
    };
    let positive_rate = positive_count as f64 / n;
    let negative_rate = negative_count as f64 / n;
    Ok(ArmStats {
        assigned: arm.assigned,
        mean,
        variance,
        positive_count,
        negative_count,
        zero_count,
        positive_rate,
        negative_rate,
        net_positive_rate: positive_rate - negative_rate,
    })
}

// ---------------------------------------------------------------------------
// Standardization
// ---------------------------------------------------------------------------

fn analyze(
    inputs: &[StratumInput],
    confidence: f64,
    variance_ddof: u64,
) -> Result<Analysis, EngineError> {
    let total_target: f64 = inputs.iter().map(|stratum| stratum.target_weight).sum();
    if total_target <= 0.0 {
        return Err(EngineError::domain(
            "the sum of target weights must be strictly positive",
        ));
    }
    let mut strata = Vec::with_capacity(inputs.len());
    for input in inputs {
        let treatment = arm_stats(&input.treatment, variance_ddof)?;
        let control = arm_stats(&input.control, variance_ddof)?;
        strata.push(StratumStats {
            name: input.name.clone(),
            target_weight: input.target_weight,
            weight: input.target_weight / total_target,
            treatment,
            control,
        });
    }
    let z = normal_quantile(1.0 - (1.0 - confidence) / 2.0, 0.0, 1.0)?;

    let mut treatment_mean = 0.0f64;
    let mut control_mean = 0.0f64;
    let mut mean_variance = 0.0f64;
    let mut treatment_positive = 0.0f64;
    let mut control_positive = 0.0f64;
    let mut positive_variance = 0.0f64;
    let mut treatment_negative = 0.0f64;
    let mut control_negative = 0.0f64;
    let mut negative_variance = 0.0f64;
    let mut treatment_net = 0.0f64;
    let mut control_net = 0.0f64;
    let mut net_variance = 0.0f64;

    for stratum in &strata {
        let weight = stratum.weight;
        let weight_squared = weight * weight;
        let treatment = &stratum.treatment;
        let control = &stratum.control;

        treatment_mean += weight * treatment.mean;
        control_mean += weight * control.mean;
        mean_variance +=
            weight_squared * (treatment.variance / treatment.n() + control.variance / control.n());

        treatment_positive += weight * treatment.positive_rate;
        control_positive += weight * control.positive_rate;
        positive_variance += weight_squared
            * (treatment.positive_rate * (1.0 - treatment.positive_rate) / treatment.n()
                + control.positive_rate * (1.0 - control.positive_rate) / control.n());

        treatment_negative += weight * treatment.negative_rate;
        control_negative += weight * control.negative_rate;
        negative_variance += weight_squared
            * (treatment.negative_rate * (1.0 - treatment.negative_rate) / treatment.n()
                + control.negative_rate * (1.0 - control.negative_rate) / control.n());

        treatment_net += weight * treatment.net_positive_rate;
        control_net += weight * control.net_positive_rate;
        net_variance += weight_squared
            * (treatment.net_variance() / treatment.n() + control.net_variance() / control.n());
    }

    let total_assigned: f64 = strata
        .iter()
        .map(|stratum| stratum.treatment.n() + stratum.control.n())
        .sum();
    let mut observed_mix_matches = true;
    for stratum in &strata {
        let observed = (stratum.treatment.n() + stratum.control.n()) / total_assigned;
        if (observed - stratum.weight).abs() > MIX_TOLERANCE {
            observed_mix_matches = false;
        }
    }

    let pooled = pooled_rates(&strata);

    Ok(Analysis {
        standardized: Block::new(treatment_mean, control_mean, mean_variance, z)?,
        positive_rate: Block::new(treatment_positive, control_positive, positive_variance, z)?,
        negative_rate: Block::new(treatment_negative, control_negative, negative_variance, z)?,
        net_positive_rate: Block::new(treatment_net, control_net, net_variance, z)?,
        strata,
        observed_mix_matches,
        pooled,
        confidence,
        z,
        population: total_target,
    })
}

fn pooled_rates(strata: &[StratumStats]) -> PooledRates {
    let mut treatment_assigned = 0.0f64;
    let mut control_assigned = 0.0f64;
    let mut treatment_positive = 0.0f64;
    let mut control_positive = 0.0f64;
    let mut treatment_negative = 0.0f64;
    let mut control_negative = 0.0f64;
    for stratum in strata {
        treatment_assigned += stratum.treatment.n();
        control_assigned += stratum.control.n();
        treatment_positive += stratum.treatment.positive_count as f64;
        control_positive += stratum.control.positive_count as f64;
        treatment_negative += stratum.treatment.negative_count as f64;
        control_negative += stratum.control.negative_count as f64;
    }
    let treatment_positive_rate = treatment_positive / treatment_assigned;
    let control_positive_rate = control_positive / control_assigned;
    let treatment_negative_rate = treatment_negative / treatment_assigned;
    let control_negative_rate = control_negative / control_assigned;
    let treatment_net = treatment_positive_rate - treatment_negative_rate;
    let control_net = control_positive_rate - control_negative_rate;
    PooledRates {
        treatment_positive_rate,
        control_positive_rate,
        positive_rate_difference: treatment_positive_rate - control_positive_rate,
        treatment_negative_rate,
        control_negative_rate,
        negative_rate_difference: treatment_negative_rate - control_negative_rate,
        treatment_net_positive_rate: treatment_net,
        control_net_positive_rate: control_net,
        net_positive_rate_difference: treatment_net - control_net,
    }
}

// ---------------------------------------------------------------------------
// Output construction
// ---------------------------------------------------------------------------

fn confidence_interval_value(lower: f64, upper: f64) -> Result<Value, EngineError> {
    Ok(record(vec![
        ("lower", float_value(lower)?),
        ("upper", float_value(upper)?),
    ]))
}

fn block_value(
    treatment_key: &str,
    control_key: &str,
    block: &Block,
    z: f64,
    confidence: f64,
) -> Result<Value, EngineError> {
    Ok(record(vec![
        (treatment_key, float_value(block.treatment)?),
        (control_key, float_value(block.control)?),
        ("difference", float_value(block.difference)?),
        ("variance", float_value(block.variance)?),
        ("standard_error", float_value(block.standard_error)?),
        ("z", float_value(z)?),
        (
            "confidence_interval",
            confidence_interval_value(block.ci_lower, block.ci_upper)?,
        ),
        ("confidence", float_value(confidence)?),
        ("method", text("normal_approximation_z_interval")),
    ]))
}

fn arm_value(arm: &ArmStats) -> Result<Value, EngineError> {
    Ok(record(vec![
        ("assigned", integer_value(arm.assigned)),
        ("positive_count", integer_value(arm.positive_count)),
        ("negative_count", integer_value(arm.negative_count)),
        ("zero_count", integer_value(arm.zero_count)),
        ("mean", float_value(arm.mean)?),
        ("variance", float_value(arm.variance)?),
        ("positive_rate", float_value(arm.positive_rate)?),
        ("negative_rate", float_value(arm.negative_rate)?),
        ("net_positive_rate", float_value(arm.net_positive_rate)?),
    ]))
}

fn strata_value(analysis: &Analysis) -> Result<Value, EngineError> {
    let mut entries = Vec::with_capacity(analysis.strata.len());
    for stratum in &analysis.strata {
        let treatment = &stratum.treatment;
        let control = &stratum.control;
        entries.push(record(vec![
            ("name", text(stratum.name.clone())),
            ("target_weight", float_value(stratum.target_weight)?),
            ("weight", float_value(stratum.weight)?),
            ("treatment", arm_value(treatment)?),
            ("control", arm_value(control)?),
            (
                "difference",
                record(vec![
                    ("mean", float_value(treatment.mean - control.mean)?),
                    (
                        "positive_rate",
                        float_value(treatment.positive_rate - control.positive_rate)?,
                    ),
                    (
                        "negative_rate",
                        float_value(treatment.negative_rate - control.negative_rate)?,
                    ),
                    (
                        "net_positive_rate",
                        float_value(treatment.net_positive_rate - control.net_positive_rate)?,
                    ),
                ]),
            ),
        ]));
    }
    Ok(array_value(entries))
}

fn diagnostics_value(
    analysis: &Analysis,
    variance_ddof: u64,
    notes: &[String],
) -> Result<Value, EngineError> {
    let mut observed_weights = Vec::with_capacity(analysis.strata.len());
    let mut target_weights = Vec::with_capacity(analysis.strata.len());
    let total_assigned: f64 = analysis
        .strata
        .iter()
        .map(|stratum| stratum.treatment.n() + stratum.control.n())
        .sum();
    for stratum in &analysis.strata {
        observed_weights.push(float_value(
            (stratum.treatment.n() + stratum.control.n()) / total_assigned,
        )?);
        target_weights.push(float_value(stratum.weight)?);
    }
    let pooled = &analysis.pooled;
    Ok(record(vec![
        (
            "observed_mix_vs_target",
            bool_value(analysis.observed_mix_matches),
        ),
        ("observed_weights", array_value(observed_weights)),
        ("target_weights", array_value(target_weights)),
        (
            "pooled",
            record(vec![
                (
                    "treatment_positive_rate",
                    float_value(pooled.treatment_positive_rate)?,
                ),
                (
                    "control_positive_rate",
                    float_value(pooled.control_positive_rate)?,
                ),
                (
                    "positive_rate_difference",
                    float_value(pooled.positive_rate_difference)?,
                ),
                (
                    "treatment_negative_rate",
                    float_value(pooled.treatment_negative_rate)?,
                ),
                (
                    "control_negative_rate",
                    float_value(pooled.control_negative_rate)?,
                ),
                (
                    "negative_rate_difference",
                    float_value(pooled.negative_rate_difference)?,
                ),
                (
                    "treatment_net_positive_rate",
                    float_value(pooled.treatment_net_positive_rate)?,
                ),
                (
                    "control_net_positive_rate",
                    float_value(pooled.control_net_positive_rate)?,
                ),
                (
                    "net_positive_rate_difference",
                    float_value(pooled.net_positive_rate_difference)?,
                ),
            ]),
        ),
        ("variance_ddof", integer_value(variance_ddof)),
        (
            "notes",
            array_value(notes.iter().map(|note| text(note.clone())).collect()),
        ),
    ]))
}

fn assumption_texts(prospective: bool) -> Vec<&'static str> {
    let mut texts = vec![
        "treatment and control arms are independently assigned within each stratum",
        "the supplied target weights define the standardization mix and are not estimated from the data",
        "outcome values are complete and measured over comparable windows across arms unless the caller states otherwise",
        "the normal approximation is used for the sampling distribution of the standardized difference",
        "fixed_cost is treated as known and adds no sampling variance",
        "rollout_fraction scales the expected difference and its variance linearly; it is not a probabilistic guarantee",
        "a positive outcome rate difference is not the same as a positive contribution difference",
    ];
    if prospective {
        texts.push("the additional data is hypothetical and the result is not an observed result");
    }
    texts
}

fn build_output(
    analysis: &Analysis,
    rollout_fraction: f64,
    fixed_cost: f64,
    variance_ddof: u64,
    prospective: bool,
) -> Result<Value, EngineError> {
    let standardized = &analysis.standardized;
    let population = analysis.population;
    let scaled_difference = standardized.difference * rollout_fraction;
    let scaled_variance = standardized.variance * rollout_fraction * rollout_fraction;
    let scaled_standard_error = standardized.standard_error * rollout_fraction;
    let scaled_lower = standardized.ci_lower * rollout_fraction;
    let scaled_upper = standardized.ci_upper * rollout_fraction;
    // Totals are the per-person standardized quantities multiplied by the
    // represented population scale (the sum of target weights). Fixed cost is
    // applied at the total level after the interval and adds no variance.
    let total_difference = scaled_difference * population;
    let total_lower = scaled_lower * population;
    let total_upper = scaled_upper * population;
    let net_difference = total_difference - fixed_cost;
    let net_lower = total_lower - fixed_cost;
    let net_upper = total_upper - fixed_cost;
    let mut notes = vec![
        "the standardized estimate is computed from the supplied target mix, not from the observed assigned mix"
            .to_string(),
        "pooled rates are reported for diagnostics only and do not replace the standardized estimate"
            .to_string(),
    ];
    if !analysis.observed_mix_matches {
        notes.push(
            "the observed assigned mix differs from the target weights by more than 1e-9; pooled rates are not a substitute for the standardized estimate"
                .to_string(),
        );
    }
    Ok(record(vec![
        ("prospective", bool_value(prospective)),
        ("strata", strata_value(analysis)?),
        (
            "standardized",
            record(vec![
                ("treatment_mean", float_value(standardized.treatment)?),
                ("control_mean", float_value(standardized.control)?),
                ("difference", float_value(standardized.difference)?),
                ("variance", float_value(standardized.variance)?),
                ("standard_error", float_value(standardized.standard_error)?),
                ("z", float_value(analysis.z)?),
                (
                    "confidence_interval",
                    confidence_interval_value(standardized.ci_lower, standardized.ci_upper)?,
                ),
                ("confidence", float_value(analysis.confidence)?),
                ("df", Value::Null),
                ("method", text("normal_approximation_z_interval")),
            ]),
        ),
        (
            "positive_rate",
            block_value(
                "treatment_rate",
                "control_rate",
                &analysis.positive_rate,
                analysis.z,
                analysis.confidence,
            )?,
        ),
        (
            "negative_rate",
            block_value(
                "treatment_rate",
                "control_rate",
                &analysis.negative_rate,
                analysis.z,
                analysis.confidence,
            )?,
        ),
        (
            "net_positive_rate",
            block_value(
                "treatment_rate",
                "control_rate",
                &analysis.net_positive_rate,
                analysis.z,
                analysis.confidence,
            )?,
        ),
        (
            "scaled",
            record(vec![
                ("rollout_fraction", float_value(rollout_fraction)?),
                ("difference", float_value(scaled_difference)?),
                ("variance", float_value(scaled_variance)?),
                ("standard_error", float_value(scaled_standard_error)?),
                (
                    "confidence_interval",
                    confidence_interval_value(scaled_lower, scaled_upper)?,
                ),
                ("total", float_value(total_difference)?),
                (
                    "total_confidence_interval",
                    confidence_interval_value(total_lower, total_upper)?,
                ),
                ("fixed_cost", float_value(fixed_cost)?),
                ("net_difference", float_value(net_difference)?),
                (
                    "net_confidence_interval",
                    confidence_interval_value(net_lower, net_upper)?,
                ),
            ]),
        ),
        (
            "diagnostics",
            diagnostics_value(analysis, variance_ddof, &notes)?,
        ),
        (
            "assumptions",
            array_value(
                assumption_texts(prospective)
                    .into_iter()
                    .map(text)
                    .collect(),
            ),
        ),
    ]))
}

fn attach_common(mut outcome: Outcome, analysis: &Analysis, prospective: bool) -> Outcome {
    if !analysis.observed_mix_matches {
        outcome = outcome.with_warning(Warning::new(
            "observed_mix_differs_from_target",
            "the observed assigned mix differs from the supplied target weights by more than 1e-9",
        ));
    }
    if prospective {
        outcome = outcome.with_warning(Warning::new(
            "prospective_hypothetical_data",
            "the result uses hypothetical additional data and is not an observed result",
        ));
    }
    for (index, statement) in assumption_texts(prospective).iter().enumerate() {
        outcome = outcome.with_assumption(Assumption::unverified(
            format!("stratified_{index}"),
            *statement,
        ));
    }
    outcome
}

// ---------------------------------------------------------------------------
// stratified_experiment
// ---------------------------------------------------------------------------

fn stratified_descriptor() -> FunctionDescriptor {
    FunctionDescriptor::new(
        "statistics.stratified_experiment",
        "statistics",
        "1.0.0",
        "Stratified experiment analysis",
        "Standardize per-stratum treatment-minus-control differences to a target mix.",
    )
    .with_description(
        "strata is an array of records {name, target_weight, treatment, control}, where each \
         arm is {assigned, outcomes: [{value, count}]}. Outcome counts must sum exactly to \
         assigned. For each arm the mean of the per-person outcome and the unbiased sample \
         variance (sum(value^2 * count) - n * mean^2) / (n - variance_ddof) are computed, \
         together with the positive, negative, and net-positive outcome rates. Strata are \
         standardized with weight = target_weight / sum(target_weight); the standardized \
         treatment-minus-control variance is sum(weight^2 * (var_t / n_t + var_c / n_c)) and \
         the interval is estimate +/- z * se with z = normal_quantile(1 - (1 - confidence)/2) \
         (method normal_approximation_z_interval). Rate differences use the per-arm binomial \
         variance p (1 - p) / n, and the net-positive rate uses the per-person variance \
         p_positive + p_negative - net^2. Contribution, positive-rate, negative-rate, and \
         net-positive-rate differences are reported in separate blocks; a positive outcome \
         rate difference is not a positive contribution difference. The sum of target weights \
         is also the represented population scale: per-person standardized quantities are \
         multiplied by it to report totals. fixed_cost is subtracted from the scaled total \
         contribution after the interval and adds no sampling variance. rollout_fraction \
         scales the expected difference and its variance by fraction^2; the full and scaled \
         results are both reported.",
    )
    .with_parameters(vec![
        ParamDescriptor::required(
            "strata",
            "Array of stratum records {name, target_weight, treatment, control}.",
            array_schema(ValueSchema::Any),
        ),
        ParamDescriptor::optional(
            "confidence",
            "Confidence level in (0, 1); default 0.95.",
            any_number_schema(),
        ),
        ParamDescriptor::optional(
            "fixed_cost",
            "Known fixed cost subtracted from the scaled contribution difference; default 0.",
            any_number_schema(),
        ),
        ParamDescriptor::optional(
            "rollout_fraction",
            "Fraction of the population receiving the treatment; in [0, 1], default 1.",
            any_number_schema(),
        ),
        ParamDescriptor::optional(
            "variance_ddof",
            "Delta degrees of freedom for the per-arm variance; default 1.",
            integer_schema(),
        ),
    ])
    .with_output(
        record_schema(
            vec![
                bicmath_core::schema::FieldSchema::required("prospective", bool_schema()),
                bicmath_core::schema::FieldSchema::required(
                    "strata",
                    array_schema(ValueSchema::Any),
                ),
                bicmath_core::schema::FieldSchema::required("standardized", ValueSchema::Any),
                bicmath_core::schema::FieldSchema::required("positive_rate", ValueSchema::Any),
                bicmath_core::schema::FieldSchema::required("negative_rate", ValueSchema::Any),
                bicmath_core::schema::FieldSchema::required("net_positive_rate", ValueSchema::Any),
                bicmath_core::schema::FieldSchema::required("scaled", ValueSchema::Any),
                bicmath_core::schema::FieldSchema::required("diagnostics", ValueSchema::Any),
                bicmath_core::schema::FieldSchema::required(
                    "assumptions",
                    array_schema(text_schema()),
                ),
            ],
            true,
        ),
        "Standardized experiment analysis with separate contribution and rate blocks.",
    )
    .with_modes(inferential_modes())
    .with_cost(CostClass::Linear)
    .with_method_ref("docs/methods/statistics.md#stratified_experiment")
    .with_examples(vec![
        Example::new(
            "outcome counts must sum to assigned",
            example_args(&[(
                "strata",
                serde_json::json!([{
                    "name": "beginners",
                    "target_weight": 1,
                    "treatment": {"assigned": 3, "outcomes": [{"value": 10, "count": 2}]},
                    "control": {"assigned": 2, "outcomes": [{"value": 10, "count": 2}]}
                }]),
            )]),
        )
        .with_error(ErrorCode::DomainViolation),
    ])
}

fn invoke_stratified(args: &Args, ctx: &ExecContext) -> Result<Outcome, EngineError> {
    require_mode(
        ctx,
        &inferential_modes(),
        "statistics.stratified_experiment",
    )?;
    let strata = parse_strata(args, "strata")?;
    let confidence = confidence_param(args)?;
    let fixed_cost = optional_f64_param(args, "fixed_cost")?.unwrap_or(0.0);
    let rollout_fraction = optional_f64_param(args, "rollout_fraction")?.unwrap_or(1.0);
    if !(0.0..=1.0).contains(&rollout_fraction) {
        return Err(EngineError::domain("rollout_fraction must be in [0, 1]")
            .with_path("rollout_fraction".to_string()));
    }
    let variance_ddof = match args.optional_integer("variance_ddof")? {
        Some(value) => non_negative_u64(&value, "variance_ddof")?,
        None => 1,
    };
    let analysis = analyze(&strata, confidence, variance_ddof)?;
    let value = build_output(
        &analysis,
        rollout_fraction,
        fixed_cost,
        variance_ddof,
        false,
    )?;
    Ok(attach_common(Outcome::approximate(value), &analysis, false))
}

// ---------------------------------------------------------------------------
// prospective_pool
// ---------------------------------------------------------------------------

fn prospective_descriptor() -> FunctionDescriptor {
    FunctionDescriptor::new(
        "statistics.prospective_pool",
        "statistics",
        "1.0.0",
        "Prospective pooled experiment analysis",
        "Pool observed and hypothetical additional strata and standardize the combination.",
    )
    .with_description(
        "Combines two strata specifications with the same structure into pooled per-stratum \
         arm summaries (assigned counts and outcome counts summed by value), then applies the \
         same standardization as stratified_experiment. target_weights may override the \
         pooled target weights as either an array aligned with the original strata order or a \
         record keyed by stratum name. The output is labelled prospective: true and carries a \
         warning that the result uses hypothetical additional data and is not an observed \
         result.",
    )
    .with_parameters(vec![
        ParamDescriptor::required(
            "original",
            "Observed strata, same structure as stratified_experiment.",
            array_schema(ValueSchema::Any),
        ),
        ParamDescriptor::required(
            "additional",
            "Hypothetical additional strata with the same names and structure.",
            array_schema(ValueSchema::Any),
        ),
        ParamDescriptor::optional(
            "target_weights",
            "Optional override: an array aligned with original strata order, or a record keyed by name.",
            ValueSchema::Any,
        ),
        ParamDescriptor::optional(
            "confidence",
            "Confidence level in (0, 1); default 0.95.",
            any_number_schema(),
        ),
    ])
    .with_output(
        record_schema(
            vec![
                bicmath_core::schema::FieldSchema::required("prospective", bool_schema()),
                bicmath_core::schema::FieldSchema::required("strata", array_schema(ValueSchema::Any)),
                bicmath_core::schema::FieldSchema::required("standardized", ValueSchema::Any),
                bicmath_core::schema::FieldSchema::required("positive_rate", ValueSchema::Any),
                bicmath_core::schema::FieldSchema::required("negative_rate", ValueSchema::Any),
                bicmath_core::schema::FieldSchema::required("net_positive_rate", ValueSchema::Any),
                bicmath_core::schema::FieldSchema::required("scaled", ValueSchema::Any),
                bicmath_core::schema::FieldSchema::required("diagnostics", ValueSchema::Any),
                bicmath_core::schema::FieldSchema::required(
                    "assumptions",
                    array_schema(text_schema()),
                ),
            ],
            true,
        ),
        "Prospective pooled analysis, labelled with prospective: true.",
    )
    .with_modes(inferential_modes())
    .with_cost(CostClass::Linear)
    .with_method_ref("docs/methods/statistics.md#prospective_pool")
    .with_examples(vec![Example::new(
        "stratum names must match",
        example_args(&[
            (
                "original",
                serde_json::json!([{
                    "name": "a",
                    "target_weight": 1,
                    "treatment": {"assigned": 1, "outcomes": [{"value": 1, "count": 1}]},
                    "control": {"assigned": 1, "outcomes": [{"value": 0, "count": 1}]}
                }]),
            ),
            (
                "additional",
                serde_json::json!([{
                    "name": "b",
                    "target_weight": 1,
                    "treatment": {"assigned": 1, "outcomes": [{"value": 1, "count": 1}]},
                    "control": {"assigned": 1, "outcomes": [{"value": 0, "count": 1}]}
                }]),
            ),
        ]),
    )
    .with_error(ErrorCode::MalformedInput)])
}

fn canonical_bits(value: f64) -> u64 {
    if value == 0.0 {
        0.0f64.to_bits()
    } else {
        value.to_bits()
    }
}

fn merge_arms(original: &ArmInput, additional: &ArmInput) -> ArmInput {
    let mut merged: BTreeMap<u64, (f64, u64)> = BTreeMap::new();
    for arm in [original, additional] {
        for outcome in &arm.outcomes {
            let entry = merged
                .entry(canonical_bits(outcome.value))
                .or_insert((outcome.value, 0));
            entry.1 += outcome.count;
        }
    }
    ArmInput {
        assigned: original.assigned + additional.assigned,
        outcomes: merged
            .into_values()
            .map(|(value, count)| OutcomeCategory { value, count })
            .collect(),
    }
}

fn pool_strata(
    original: &[StratumInput],
    additional: &[StratumInput],
) -> Result<Vec<StratumInput>, EngineError> {
    let original_names: BTreeSet<&str> = original
        .iter()
        .map(|stratum| stratum.name.as_str())
        .collect();
    for stratum in additional {
        if !original_names.contains(stratum.name.as_str()) {
            return Err(EngineError::malformed(format!(
                "stratum {:?} is present in additional but not in original",
                stratum.name
            )));
        }
    }
    let additional_by_name: BTreeMap<&str, &StratumInput> = additional
        .iter()
        .map(|stratum| (stratum.name.as_str(), stratum))
        .collect();
    let mut pooled = Vec::with_capacity(original.len());
    for stratum in original {
        let extra = additional_by_name
            .get(stratum.name.as_str())
            .ok_or_else(|| {
                EngineError::malformed(format!(
                    "stratum {:?} is missing from additional",
                    stratum.name
                ))
            })?;
        pooled.push(StratumInput {
            name: stratum.name.clone(),
            target_weight: stratum.target_weight + extra.target_weight,
            treatment: merge_arms(&stratum.treatment, &extra.treatment),
            control: merge_arms(&stratum.control, &extra.control),
        });
    }
    Ok(pooled)
}

fn apply_target_weights(strata: &mut [StratumInput], raw: &Value) -> Result<(), EngineError> {
    match raw {
        Value::Array(items) => {
            if items.len() != strata.len() {
                return Err(EngineError::malformed(format!(
                    "target_weights has {} entries but there are {} strata",
                    items.len(),
                    strata.len()
                ))
                .with_path("target_weights".to_string()));
            }
            for (stratum, item) in strata.iter_mut().zip(items.iter()) {
                let number = item.as_number().map_err(|e| {
                    EngineError::malformed(format!(
                        "target_weights entries must be numbers: {}",
                        e.message
                    ))
                    .with_path("target_weights".to_string())
                })?;
                let weight = number_to_f64(number)?;
                if weight < 0.0 {
                    return Err(EngineError::domain("target_weights must be non-negative")
                        .with_path("target_weights".to_string()));
                }
                stratum.target_weight = weight;
            }
        }
        Value::Record(record) => {
            for stratum in strata.iter_mut() {
                let raw_weight = record.get(&stratum.name).ok_or_else(|| {
                    EngineError::malformed(format!(
                        "target_weights is missing stratum {:?}",
                        stratum.name
                    ))
                    .with_path("target_weights".to_string())
                })?;
                let number = raw_weight.as_number().map_err(|e| {
                    EngineError::malformed(format!(
                        "target_weights entries must be numbers: {}",
                        e.message
                    ))
                    .with_path("target_weights".to_string())
                })?;
                let weight = number_to_f64(number)?;
                if weight < 0.0 {
                    return Err(EngineError::domain("target_weights must be non-negative")
                        .with_path("target_weights".to_string()));
                }
                stratum.target_weight = weight;
            }
            for key in record.keys() {
                if !strata.iter().any(|stratum| &stratum.name == key) {
                    return Err(EngineError::malformed(format!(
                        "target_weights has unknown stratum {key:?}"
                    ))
                    .with_path("target_weights".to_string()));
                }
            }
        }
        other => {
            return Err(EngineError::malformed(format!(
                "target_weights must be an array or a record, found {}",
                other.kind_name()
            ))
            .with_path("target_weights".to_string()));
        }
    }
    Ok(())
}

fn invoke_prospective(args: &Args, ctx: &ExecContext) -> Result<Outcome, EngineError> {
    require_mode(ctx, &inferential_modes(), "statistics.prospective_pool")?;
    let original = parse_strata(args, "original")?;
    let additional = parse_strata(args, "additional")?;
    let mut pooled = pool_strata(&original, &additional)?;
    if let Some(raw) = args.get("target_weights")
        && !raw.is_null()
    {
        apply_target_weights(&mut pooled, raw)?;
    }
    let confidence = confidence_param(args)?;
    let analysis = analyze(&pooled, confidence, 1)?;
    let value = build_output(&analysis, 1.0, 0.0, 1, true)?;
    Ok(attach_common(Outcome::approximate(value), &analysis, true))
}

// ---------------------------------------------------------------------------
// Registration
// ---------------------------------------------------------------------------

pub fn functions() -> Vec<Arc<dyn bicmath_core::contract::Function>> {
    vec![
        SimpleFunction::arc(stratified_descriptor(), invoke_stratified),
        SimpleFunction::arc(prospective_descriptor(), invoke_prospective),
    ]
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::BTreeMap;

    fn campaign_strata() -> serde_json::Value {
        serde_json::json!([
            {
                "name": "beginners",
                "target_weight": 30000,
                "treatment": {
                    "assigned": 8000,
                    "outcomes": [
                        {"value": 100, "count": 320},
                        {"value": -10, "count": 80},
                        {"value": 0, "count": 7600}
                    ]
                },
                "control": {
                    "assigned": 2000,
                    "outcomes": [
                        {"value": 100, "count": 76},
                        {"value": -10, "count": 4},
                        {"value": 0, "count": 1920}
                    ]
                }
            },
            {
                "name": "advanced",
                "target_weight": 10000,
                "treatment": {
                    "assigned": 2000,
                    "outcomes": [
                        {"value": 100, "count": 320},
                        {"value": -10, "count": 80},
                        {"value": 0, "count": 1600}
                    ]
                },
                "control": {
                    "assigned": 8000,
                    "outcomes": [
                        {"value": 100, "count": 1140},
                        {"value": -10, "count": 60},
                        {"value": 0, "count": 6800}
                    ]
                }
            }
        ])
    }

    fn call(id: &str, raw: serde_json::Value) -> Result<Outcome, EngineError> {
        let module = crate::module();
        let function = module
            .functions
            .iter()
            .find(|f| f.descriptor().id == id)
            .expect("function exists");
        let ctx = ExecContext::scientific();
        let args_json = raw.as_object().expect("object args");
        let mut values = BTreeMap::new();
        for (name, value) in args_json {
            let param = function
                .descriptor()
                .parameter(name)
                .expect("parameter exists");
            values.insert(
                name.clone(),
                param
                    .schema
                    .coerce(value, name, &ctx.limits, true)
                    .expect("argument coerces"),
            );
        }
        function.invoke(&Args::new(values), &ctx)
    }

    fn field<'a>(value: &'a Value, name: &str) -> &'a Value {
        match value {
            Value::Record(fields) => fields.get(name).expect("field exists"),
            other => panic!("expected record, got {other:?}"),
        }
    }

    fn as_f64(value: &Value) -> f64 {
        match value {
            Value::Number(number) => number.to_f64().expect("number"),
            other => panic!("expected number, got {other:?}"),
        }
    }

    fn close(actual: f64, expected: f64, tolerance: f64) {
        assert!(
            (actual - expected).abs() <= tolerance,
            "expected {expected}, got {actual} (tolerance {tolerance})"
        );
    }

    #[test]
    fn campaign_fixture_matches_expected_values() {
        let outcome = call(
            "statistics.stratified_experiment",
            serde_json::json!({
                "strata": campaign_strata(),
                "fixed_cost": 12000,
                "variance_ddof": 1
            }),
        )
        .unwrap();

        // Per-stratum means and differences.
        let strata = field(&outcome.value, "strata");
        let Value::Array(strata) = strata else {
            panic!("strata must be an array");
        };
        let beginners = &strata[0];
        let advanced = &strata[1];
        close(
            as_f64(field(field(beginners, "treatment"), "mean")),
            3.90,
            1e-12,
        );
        close(
            as_f64(field(field(beginners, "control"), "mean")),
            3.78,
            1e-12,
        );
        close(
            as_f64(field(field(beginners, "difference"), "mean")),
            0.12,
            1e-12,
        );
        close(
            as_f64(field(field(advanced, "treatment"), "mean")),
            15.60,
            1e-12,
        );
        close(
            as_f64(field(field(advanced, "control"), "mean")),
            14.175,
            1e-12,
        );
        close(
            as_f64(field(field(advanced, "difference"), "mean")),
            1.425,
            1e-12,
        );

        // Standardized contribution difference.
        let standardized = field(&outcome.value, "standardized");
        close(as_f64(field(standardized, "difference")), 0.44625, 1e-12);
        let se = as_f64(field(standardized, "standard_error"));
        close(se, 0.426_854_469_9, 1e-6 * 0.426_854_469_9);

        // Total contribution and interval over 40000 people.
        let total = 40000.0;
        let total_difference = as_f64(field(standardized, "difference")) * total;
        close(total_difference, 17850.0, 1e-6);
        let total_se = se * total;
        close(total_se, 17_074.178_796, 1e-6 * 17_074.178_796);

        let scaled = field(&outcome.value, "scaled");
        close(as_f64(field(scaled, "total")), 17850.0, 1e-6);
        let net_difference = as_f64(field(scaled, "net_difference"));
        close(net_difference, 5850.0, 1e-9);

        // The published fixture endpoints round the critical value to z = 1.96.
        // Assert that arithmetic within 0.05 as required, then verify the
        // implementation, which uses the exact z = normal_quantile(0.975).
        let published_lower = 5850.0 - 1.96 * total_se;
        let published_upper = 5850.0 + 1.96 * total_se;
        assert!((published_lower - (-27615.39)).abs() < 0.05);
        assert!((published_upper - 39315.39).abs() < 0.05);
        let net_interval = field(scaled, "net_confidence_interval");
        let net_lower = as_f64(field(net_interval, "lower"));
        let net_upper = as_f64(field(net_interval, "upper"));
        let z = 1.959_963_984_540_054;
        close(net_lower, 5850.0 - z * total_se, 1e-9);
        close(net_upper, 5850.0 + z * total_se, 1e-9);

        // Positive, negative, and net-positive rate differences.
        let positive = field(&outcome.value, "positive_rate");
        let negative = field(&outcome.value, "negative_rate");
        let net = field(&outcome.value, "net_positive_rate");
        close(as_f64(field(positive, "difference")), 0.005875, 1e-12);
        close(as_f64(field(negative, "difference")), 0.014125, 1e-12);
        close(as_f64(field(net, "difference")), -0.00825, 1e-12);
        close(as_f64(field(positive, "difference")) * total, 235.0, 1e-9);
        close(as_f64(field(negative, "difference")) * total, 565.0, 1e-9);
        close(as_f64(field(net, "difference")) * total, -330.0, 1e-9);
    }

    #[test]
    fn rollout_fraction_scales_variance_linearly() {
        let outcome = call(
            "statistics.stratified_experiment",
            serde_json::json!({
                "strata": campaign_strata(),
                "fixed_cost": 12000,
                "rollout_fraction": "0.85",
                "variance_ddof": 1
            }),
        )
        .unwrap();
        let full = field(&outcome.value, "standardized");
        let scaled = field(&outcome.value, "scaled");
        let full_se = as_f64(field(full, "standard_error"));
        let scaled_se = as_f64(field(scaled, "standard_error"));
        close(scaled_se, 0.85 * full_se, 1e-12);
        close(as_f64(field(scaled, "difference")), 0.44625 * 0.85, 1e-12);
        close(as_f64(field(scaled, "total")), 17850.0 * 0.85, 1e-9);
        close(
            as_f64(field(scaled, "net_difference")),
            17850.0 * 0.85 - 12000.0,
            1e-9,
        );
        let full_var = as_f64(field(full, "variance"));
        let scaled_var = as_f64(field(scaled, "variance"));
        close(scaled_var, 0.85 * 0.85 * full_var, 1e-12);
    }

    #[test]
    fn observed_mix_mismatch_warns() {
        // Target weights 25/75 but the observed assignment mix is 50/50.
        let outcome = call(
            "statistics.stratified_experiment",
            serde_json::json!({
                "strata": [
                    {
                        "name": "a",
                        "target_weight": 1,
                        "treatment": {"assigned": 8000, "outcomes": [{"value": 1, "count": 8000}]},
                        "control": {"assigned": 2000, "outcomes": [{"value": 1, "count": 2000}]}
                    },
                    {
                        "name": "b",
                        "target_weight": 3,
                        "treatment": {"assigned": 2000, "outcomes": [{"value": 1, "count": 2000}]},
                        "control": {"assigned": 8000, "outcomes": [{"value": 1, "count": 8000}]}
                    }
                ]
            }),
        )
        .unwrap();
        assert!(
            outcome
                .warnings
                .iter()
                .any(|warning| warning.code == "observed_mix_differs_from_target")
        );
        let diagnostics = field(&outcome.value, "diagnostics");
        assert_eq!(
            field(diagnostics, "observed_mix_vs_target"),
            &Value::Bool(false)
        );
    }

    #[test]
    fn ragged_and_zero_margin_tables_are_rejected() {
        let err = call(
            "statistics.chi_square_contingency",
            serde_json::json!({"table": [[1, 2], [3]]}),
        )
        .unwrap_err();
        assert_eq!(err.code, ErrorCode::MalformedInput);
        let err = call(
            "statistics.chi_square_contingency",
            serde_json::json!({"table": [[1, 0], [3, 0]]}),
        )
        .unwrap_err();
        assert_eq!(err.code, ErrorCode::DomainViolation);
    }

    #[test]
    fn count_mismatch_is_rejected() {
        let err = call(
            "statistics.stratified_experiment",
            serde_json::json!({
                "strata": [{
                    "name": "a",
                    "target_weight": 1,
                    "treatment": {"assigned": 3, "outcomes": [{"value": 1, "count": 2}]},
                    "control": {"assigned": 3, "outcomes": [{"value": 1, "count": 3}]}
                }]
            }),
        )
        .unwrap_err();
        assert_eq!(err.code, ErrorCode::DomainViolation);
    }

    #[test]
    fn prospective_pool_pools_counts_and_flags_hypothetical() {
        let original = campaign_strata();
        let additional = serde_json::json!([
            {
                "name": "beginners",
                "target_weight": 0,
                "treatment": {"assigned": 1000, "outcomes": [{"value": 100, "count": 40}, {"value": 0, "count": 960}]},
                "control": {"assigned": 1000, "outcomes": [{"value": 100, "count": 30}, {"value": 0, "count": 970}]}
            },
            {
                "name": "advanced",
                "target_weight": 0,
                "treatment": {"assigned": 1000, "outcomes": [{"value": 100, "count": 200}, {"value": 0, "count": 800}]},
                "control": {"assigned": 1000, "outcomes": [{"value": 100, "count": 150}, {"value": 0, "count": 850}]}
            }
        ]);
        let outcome = call(
            "statistics.prospective_pool",
            serde_json::json!({
                "original": original,
                "additional": additional,
                "target_weights": {"beginners": 30000, "advanced": 10000}
            }),
        )
        .unwrap();
        assert_eq!(field(&outcome.value, "prospective"), &Value::Bool(true));
        assert!(
            outcome
                .warnings
                .iter()
                .any(|warning| warning.code == "prospective_hypothetical_data")
        );
        let strata = field(&outcome.value, "strata");
        let Value::Array(strata) = strata else {
            panic!("strata must be an array");
        };
        let beginners_treatment = field(&strata[0], "treatment");
        assert_eq!(as_f64(field(beginners_treatment, "assigned")), 9000.0);
    }
}