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
//! BayesianNetworkModel evaluation — exact inference via enumeration.
//!
//! Implements `BayesianNetworkModel` where each `BayesianNetworkNodes` entry is either `DiscreteNode`
//! or `ContinuousNode`. `DiscreteNode`s carry `ValueProbability` marginals and `DiscreteConditionalProbability`
//! tables (parents → value probs). `ContinuousNode`s carry `ContinuousDistribution`s (Normal/Lognormal/Uniform/Triangular)
//! with `Mean`/`Variance`/`Lower`/`Upper` expressions that may reference parent fields.
//!
//! Scoring (inference) is exact enumeration over unobserved discrete variables:
//!
//! 1. Evidence is taken from `MiningSchema.active_fields` where input `values[FieldId]` is not `Missing`.
//! 2. Discrete domains are the union of `value_probabilities` and conditional table probabilities.
//! 3. All assignments to unobserved discrete nodes are enumerated; for each assignment:
//! - Continuous unobserved nodes' expected means are computed via `eval_ops` on their distribution `Mean` expressions.
//! - Node-local `DerivedField`s (e.g. `C3_Discretized` from `C3`) are re-evaluated via `eval_ops` to obtain discrete parent values.
//! - Joint weight = Π discrete probs × Π pdf(continuous evidence) is computed.
//! 4. For a discrete target, posterior = weight per value / total weight, predicted = argmax.
//! For a continuous target, posterior mean = Σ weight·expected / total weight.
use crate::base::{FieldId, Value};
use crate::ir::{
BayesianContinuousDistributionIr, BayesianNetworkIr, BayesianNodeIr, ContinuousBayesianNodeIr,
DiscreteBayesianNodeIr, DiscreteConditionalTableIr,
};
use std::collections::HashMap;
fn find_discrete_table<'a>(
dn: &'a DiscreteBayesianNodeIr,
temp: &[Value],
) -> Option<&'a DiscreteConditionalTableIr> {
if dn.conditional_tables.is_empty() {
return None;
}
for ct in &dn.conditional_tables {
let mut matches = true;
for pv in &ct.parent_values {
let idx = pv.parent.as_usize();
let actual = if idx < temp.len() {
temp[idx]
} else {
Value::Missing
};
match actual {
Value::Discrete(sid) => {
if sid != pv.value {
matches = false;
break;
}
}
_ => {
matches = false;
break;
}
}
}
if matches {
return Some(ct);
}
}
None
}
fn find_continuous_dist<'a>(
cn: &'a ContinuousBayesianNodeIr,
temp: &[Value],
) -> Option<&'a BayesianContinuousDistributionIr> {
if !cn.conditional_tables.is_empty() {
for ct in &cn.conditional_tables {
let mut matches = true;
for pv in &ct.parent_values {
let idx = pv.parent.as_usize();
let actual = if idx < temp.len() {
temp[idx]
} else {
Value::Missing
};
match actual {
Value::Discrete(sid) => {
if sid != pv.value {
matches = false;
break;
}
}
_ => {
matches = false;
break;
}
}
}
if matches {
if let Some(d) = ct.distributions.first() {
return Some(d);
}
}
}
cn.distributions.first()
} else {
cn.distributions.first()
}
}
/// Evaluate a [`BayesianNetworkIr`] against a dense `values` array.
pub fn evaluate_bayesian_network(model: &BayesianNetworkIr, values: &[Value]) -> Value {
// Determine primary target field
let target_fid_opt = model.mining_schema.target_field.or_else(|| {
// fallback: first node's field if mining schema has no target
model.nodes.first().map(|n| match n {
BayesianNodeIr::Discrete(d) => d.field,
BayesianNodeIr::Continuous(c) => c.field,
})
});
let target_fid = match target_fid_opt {
Some(f) => f,
None => return Value::Missing,
};
// Determine target node type
let target_is_discrete = model.nodes.iter().any(|n| match n {
BayesianNodeIr::Discrete(d) => d.field == target_fid,
BayesianNodeIr::Continuous(_) => false,
});
let target_is_continuous = !target_is_discrete
&& model.nodes.iter().any(|n| match n {
BayesianNodeIr::Continuous(c) => c.field == target_fid,
BayesianNodeIr::Discrete(_) => false,
});
// Build node lookup maps
let mut discrete_nodes: Vec<&DiscreteBayesianNodeIr> = Vec::new();
let mut continuous_nodes: Vec<&ContinuousBayesianNodeIr> = Vec::new();
for node in &model.nodes {
match node {
BayesianNodeIr::Discrete(d) => discrete_nodes.push(d),
BayesianNodeIr::Continuous(c) => continuous_nodes.push(c),
}
}
// Build evidence map for Bayesian node fields that are active and observed
// Need to know which FieldIds correspond to node fields; then evidence is values[FieldId] if not Missing
// Also need to collect field->node for quick lookup
let mut evidence: HashMap<FieldId, Value> = HashMap::new();
for &fid in &model.mining_schema.active_fields {
let idx = fid.as_usize();
if idx < values.len() {
let v = values[idx];
if !v.is_missing() {
// Check if fid is a Bayesian node field (or derived? derived evidence not needed)
let is_node_field = model.nodes.iter().any(|n| match n {
BayesianNodeIr::Discrete(d) => d.field == fid,
BayesianNodeIr::Continuous(c) => c.field == fid,
});
if is_node_field {
evidence.insert(fid, v);
} else {
// Could be group/order field, ignore? But also might be evidence for derived parents that are not nodes
// For now ignore
}
}
}
}
// Also consider any node field that has observed value even if not in active_fields (e.g., continuous evidence passed as discrete via active? Already covered)
// But in case values contains observed for a target field that is also active? Not.
// Build discrete domains
let mut discrete_domains: HashMap<FieldId, Vec<crate::base::SymbolId>> = HashMap::new();
for dn in &discrete_nodes {
let mut set = std::collections::HashSet::new();
let mut vec = Vec::new();
for vp in &dn.value_probabilities {
if set.insert(vp.value) {
vec.push(vp.value);
}
}
for ct in &dn.conditional_tables {
for vp in &ct.value_probabilities {
if set.insert(vp.value) {
vec.push(vp.value);
}
}
}
// Also include values from DataDictionary? but we have at least from tables
// If still empty, fallback to single value? Should not happen
if vec.is_empty() {
// Try to get from evidence? No
// leave empty -> enumeration will skip? Better to keep empty and treat as 0 combos?
}
discrete_domains.insert(dn.field, vec);
}
// Unobserved discrete fields = discrete nodes where evidence missing
let mut unobserved: Vec<(FieldId, Vec<crate::base::SymbolId>)> = Vec::new();
for dn in &discrete_nodes {
if !evidence.contains_key(&dn.field) {
if let Some(dom) = discrete_domains.get(&dn.field) {
if !dom.is_empty() {
unobserved.push((dn.field, dom.clone()));
}
}
}
}
// If no unobserved discrete, we have single assignment
// Enumerate
let total_combos: usize = if unobserved.is_empty() {
1
} else {
unobserved
.iter()
.map(|(_, dom)| dom.len())
.product::<usize>()
};
// Guard against explosion: cap at 1M? For our tests it's small
if total_combos > 1_000_000 {
// fallback to marginal most probable
return fallback_marginal(model, target_fid);
}
// Helper to evaluate param ops to f64
let eval_param = |ops: &[crate::ir::Op], temp: &[Value]| -> f64 {
let v = crate::engine::transform::vm::eval_ops(ops, temp);
match v {
Value::Continuous(f) => f,
Value::Discrete(sid) => {
// Try to parse discrete symbol as f64 if it looks numeric (for cases where Constant was string "2"? But Constant for continuous should be Continuous, not Discrete)
// We'll try to lookup symbol string if possible via thread local? But we don't have mapping here; assume 0
let _ = sid;
0.0
}
Value::Missing => f64::NAN,
}
};
// find_discrete_table and find_continuous_dist are top-level helpers
// Helpers for probability lookup
let prob_for_discrete =
|dn: &DiscreteBayesianNodeIr, sid: crate::base::SymbolId, temp: &[Value]| -> f64 {
if let Some(ct) = find_discrete_table(dn, temp) {
for vp in &ct.value_probabilities {
if vp.value == sid {
return vp.probability;
}
}
0.0
} else {
for vp in &dn.value_probabilities {
if vp.value == sid {
return vp.probability;
}
}
0.0
}
};
let pdf_for_continuous = |cn: &ContinuousBayesianNodeIr, x: f64, temp: &[Value]| -> f64 {
let dist = match find_continuous_dist(cn, temp) {
Some(d) => d,
None => return 0.0,
};
match dist {
BayesianContinuousDistributionIr::Normal { mean, variance } => {
let m = eval_param(mean, temp);
let v = eval_param(variance, temp);
if !m.is_finite() || !v.is_finite() || v <= 0.0 {
return 0.0;
}
let std = v.sqrt();
// Use statrs if available? We'll compute directly
let denom = std * (2.0 * std::f64::consts::PI).sqrt();
let num = (-0.5 * ((x - m) / std).powi(2)).exp();
num / denom
}
BayesianContinuousDistributionIr::Lognormal { mean, variance } => {
let m = eval_param(mean, temp);
let v = eval_param(variance, temp);
if !m.is_finite() || !v.is_finite() || v <= 0.0 || x <= 0.0 {
return 0.0;
}
let std = v.sqrt();
let denom = x * std * (2.0 * std::f64::consts::PI).sqrt();
let num = (-0.5 * ((x.ln() - m) / std).powi(2)).exp();
num / denom
}
BayesianContinuousDistributionIr::Uniform { lower, upper } => {
let l = eval_param(lower, temp);
let u = eval_param(upper, temp);
if !l.is_finite() || !u.is_finite() || u <= l {
return 0.0;
}
if x >= l && x <= u {
1.0 / (u - l)
} else {
0.0
}
}
BayesianContinuousDistributionIr::Triangular { mean, lower, upper } => {
let m = eval_param(mean, temp);
let l = eval_param(lower, temp);
let u = eval_param(upper, temp);
if !l.is_finite() || !u.is_finite() || !m.is_finite() || u <= l {
return 0.0;
}
if x < l || x > u {
return 0.0;
}
if x <= m {
if (m - l).abs() < 1e-9 {
return 0.0;
}
2.0 * (x - l) / ((u - l) * (m - l))
} else {
if (u - m).abs() < 1e-9 {
return 0.0;
}
2.0 * (u - x) / ((u - l) * (u - m))
}
}
}
};
let mean_for_continuous = |cn: &ContinuousBayesianNodeIr, temp: &[Value]| -> f64 {
let dist = match find_continuous_dist(cn, temp) {
Some(d) => d,
None => return 0.0,
};
match dist {
BayesianContinuousDistributionIr::Normal { mean, .. } => eval_param(mean, temp),
BayesianContinuousDistributionIr::Lognormal { mean, variance } => {
let m = eval_param(mean, temp);
let v = eval_param(variance, temp);
if m.is_finite() && v.is_finite() {
(m + v / 2.0).exp()
} else {
m
}
}
BayesianContinuousDistributionIr::Uniform { lower, upper } => {
let l = eval_param(lower, temp);
let u = eval_param(upper, temp);
if l.is_finite() && u.is_finite() {
(l + u) / 2.0
} else {
0.0
}
}
BayesianContinuousDistributionIr::Triangular { mean, lower, upper } => {
let m = eval_param(mean, temp);
if m.is_finite() {
m
} else {
let l = eval_param(lower, temp);
let u = eval_param(upper, temp);
if l.is_finite() && u.is_finite() {
(l + u + m) / 3.0
} else {
0.0
}
}
}
}
};
// Accumulators
let mut total_weight = 0.0;
let mut discrete_sums: HashMap<crate::base::SymbolId, f64> = HashMap::new();
let mut continuous_weighted_sum = 0.0;
// Precompute target discrete domain if needed
let target_domain_opt = if target_is_discrete {
discrete_domains.get(&target_fid).cloned()
} else {
None
};
// Enumerate
for combo_idx in 0..total_combos {
// Build temp values
let mut temp = values.to_vec();
// Ensure temp length covers all FieldIds up to max needed (if values shorter, extend with Missing)
let needed_len = {
let mut max_id = 0usize;
for node in &model.nodes {
let fid = match node {
BayesianNodeIr::Discrete(d) => d.field.as_usize(),
BayesianNodeIr::Continuous(c) => c.field.as_usize(),
};
if fid > max_id {
max_id = fid;
}
// also check derived fields
let dfs = match node {
BayesianNodeIr::Discrete(d) => &d.derived_fields,
BayesianNodeIr::Continuous(c) => &c.derived_fields,
};
for df in dfs {
let id = df.field_id.as_usize();
if id > max_id {
max_id = id;
}
}
}
// also consider mining schema fields
for &fid in &model.mining_schema.active_fields {
if fid.as_usize() > max_id {
max_id = fid.as_usize();
}
}
if let Some(tf) = model.mining_schema.target_field {
if tf.as_usize() > max_id {
max_id = tf.as_usize();
}
}
max_id + 1
};
if temp.len() < needed_len {
temp.resize(needed_len, Value::Missing);
}
// Decode combo into assignments for unobserved discrete nodes
let mut rem = combo_idx;
for (fid, domain) in &unobserved {
let dlen = domain.len();
let idx = rem % dlen;
rem /= dlen;
let sid = domain[idx];
let fidx = fid.as_usize();
if fidx < temp.len() {
temp[fidx] = Value::Discrete(sid);
}
}
// For discrete nodes that are observed, their values already in temp (evidence). Ensure they are preserved (they are from base values)
// But also for discrete nodes where evidence missing but domain assignment gave value, we have set.
// Compute continuous expected values for unobserved continuous nodes in order
for cn in &continuous_nodes {
let fidx = cn.field.as_usize();
if fidx < temp.len() && !temp[fidx].is_missing() {
continue; // observed
}
// Find distribution and compute mean
let mean = mean_for_continuous(cn, &temp);
if fidx < temp.len() {
if mean.is_finite() {
temp[fidx] = Value::Continuous(mean);
} else {
temp[fidx] = Value::Missing;
}
}
}
// Re-evaluate derived fields for all nodes (to compute discretization parents)
for node in &model.nodes {
let dfs = match node {
BayesianNodeIr::Discrete(d) => &d.derived_fields,
BayesianNodeIr::Continuous(c) => &c.derived_fields,
};
for df in dfs {
let v = crate::engine::transform::vm::eval_ops(&df.bytecode, &temp);
let idx = df.field_id.as_usize();
if idx < temp.len() {
temp[idx] = v;
}
}
}
// Compute joint weight
let mut w = 1.0;
// Discrete nodes contribution
for dn in &discrete_nodes {
let fidx = dn.field.as_usize();
let actual = if fidx < temp.len() {
temp[fidx]
} else {
Value::Missing
};
let sid = match actual {
Value::Discrete(s) => s,
Value::Missing => {
w = 0.0;
break;
}
Value::Continuous(_) => {
w = 0.0;
break;
}
};
let p = prob_for_discrete(dn, sid, &temp);
w *= p;
if w == 0.0 {
break;
}
}
if w == 0.0 {
continue;
}
// Continuous observed evidence contribution (pdf)
for cn in &continuous_nodes {
// Check if this continuous node is observed evidence (evidence map contains it)
if let Some(ev) = evidence.get(&cn.field) {
let x = match ev {
Value::Continuous(f) => *f,
Value::Discrete(_) => continue, // discrete evidence for continuous node? ignore
Value::Missing => continue,
};
let pdf = pdf_for_continuous(cn, x, &temp);
// If pdf is 0, weight becomes 0
if pdf <= 0.0 || !pdf.is_finite() {
w = 0.0;
break;
}
w *= pdf;
if w == 0.0 {
break;
}
}
}
if w == 0.0 {
continue;
}
total_weight += w;
if target_is_discrete {
let tidx = target_fid.as_usize();
let tv = if tidx < temp.len() {
temp[tidx]
} else {
Value::Missing
};
if let Value::Discrete(sid) = tv {
*discrete_sums.entry(sid).or_insert(0.0) += w;
}
} else if target_is_continuous {
let tidx = target_fid.as_usize();
let tv = if tidx < temp.len() {
temp[tidx]
} else {
Value::Missing
};
if let Value::Continuous(f) = tv {
if f.is_finite() {
continuous_weighted_sum += w * f;
}
} else {
// If target continuous value is Missing (maybe not computed), try mean_for_continuous
if let Some(cn) = continuous_nodes.iter().find(|c| c.field == target_fid) {
let m = mean_for_continuous(cn, &temp);
if m.is_finite() {
continuous_weighted_sum += w * m;
}
}
}
}
}
if total_weight == 0.0 || !total_weight.is_finite() {
return fallback_marginal(model, target_fid);
}
if target_is_discrete {
// Find max posterior
let mut best_sid: Option<crate::base::SymbolId> = None;
let mut best_prob = -1.0;
if let Some(domain) = target_domain_opt {
for sid in domain {
let sum = discrete_sums.get(&sid).copied().unwrap_or(0.0);
let prob = sum / total_weight;
if prob > best_prob {
best_prob = prob;
best_sid = Some(sid);
}
}
} else {
for (sid, sum) in &discrete_sums {
let prob = sum / total_weight;
if prob > best_prob {
best_prob = prob;
best_sid = Some(*sid);
}
}
}
if let Some(sid) = best_sid {
return Value::Discrete(sid);
} else {
return fallback_marginal(model, target_fid);
}
} else if target_is_continuous {
let mean = continuous_weighted_sum / total_weight;
if mean.is_finite() {
return Value::Continuous(mean);
} else {
return Value::Missing;
}
} else {
// Target field not found among Bayesian nodes - fallback to marginal of first node?
fallback_marginal(model, target_fid)
}
}
fn fallback_marginal(model: &BayesianNetworkIr, target: FieldId) -> Value {
for node in &model.nodes {
match node {
BayesianNodeIr::Discrete(d) if d.field == target => {
// return most probable marginal
let mut best: Option<(crate::base::SymbolId, f64)> = None;
for vp in &d.value_probabilities {
if best.is_none() || vp.probability > best.unwrap().1 {
best = Some((vp.value, vp.probability));
}
}
if let Some((sid, _)) = best {
return Value::Discrete(sid);
}
// fallback to first conditional value if no marginal
if let Some(ct) = d.conditional_tables.first() {
if let Some(vp) = ct.value_probabilities.first() {
return Value::Discrete(vp.value);
}
}
return Value::Missing;
}
BayesianNodeIr::Continuous(c) if c.field == target => {
if let Some(dist) = c.distributions.first() {
// Need dummy values for eval? Use empty
let dummy = vec![Value::Missing; 64];
let m = match dist {
BayesianContinuousDistributionIr::Normal { mean, .. } => {
crate::engine::transform::vm::eval_ops(mean, &dummy)
}
BayesianContinuousDistributionIr::Lognormal { mean, .. } => {
crate::engine::transform::vm::eval_ops(mean, &dummy)
}
BayesianContinuousDistributionIr::Uniform { lower, upper } => {
let l = crate::engine::transform::vm::eval_ops(lower, &dummy);
let u = crate::engine::transform::vm::eval_ops(upper, &dummy);
match (l, u) {
(Value::Continuous(lf), Value::Continuous(uf)) => {
Value::Continuous((lf + uf) / 2.0)
}
_ => Value::Missing,
}
}
BayesianContinuousDistributionIr::Triangular { mean, .. } => {
crate::engine::transform::vm::eval_ops(mean, &dummy)
}
};
return m;
}
return Value::Continuous(0.0);
}
_ => {}
}
}
Value::Missing
}
#[cfg(test)]
mod tests {
use super::*;
use crate::base::{FieldId, SymbolId, Value};
use crate::ir::*;
fn make_simple_bn() -> BayesianNetworkIr {
// Simple BN: A->C, B->C ; A,B roots with priors, C conditional as in spec exact example
let fid_a = FieldId(0);
let fid_b = FieldId(1);
let fid_c = FieldId(2);
let sid_0 = SymbolId(10);
let sid_1 = SymbolId(11);
let sid_2 = SymbolId(12);
// Discrete nodes A,B,C each categorical "0","1","2"
let node_a = DiscreteBayesianNodeIr {
name: "A".into(),
field: fid_a,
count: None,
value_probabilities: vec![
BayesianValueProbabilityIr {
value: sid_0,
probability: 0.4,
},
BayesianValueProbabilityIr {
value: sid_1,
probability: 0.6,
},
],
conditional_tables: vec![],
derived_fields: vec![],
};
let node_b = DiscreteBayesianNodeIr {
name: "B".into(),
field: fid_b,
count: None,
value_probabilities: vec![
BayesianValueProbabilityIr {
value: sid_0,
probability: 0.7,
},
BayesianValueProbabilityIr {
value: sid_1,
probability: 0.3,
},
],
conditional_tables: vec![],
derived_fields: vec![],
};
let node_c = DiscreteBayesianNodeIr {
name: "C".into(),
field: fid_c,
count: None,
value_probabilities: vec![],
conditional_tables: vec![
DiscreteConditionalTableIr {
parent_values: vec![
BayesianParentValueIr {
parent: fid_a,
value: sid_0,
},
BayesianParentValueIr {
parent: fid_b,
value: sid_0,
},
],
value_probabilities: vec![
BayesianValueProbabilityIr {
value: sid_0,
probability: 0.7,
},
BayesianValueProbabilityIr {
value: sid_1,
probability: 0.2,
},
BayesianValueProbabilityIr {
value: sid_2,
probability: 0.1,
},
],
count: None,
},
DiscreteConditionalTableIr {
parent_values: vec![
BayesianParentValueIr {
parent: fid_a,
value: sid_0,
},
BayesianParentValueIr {
parent: fid_b,
value: sid_1,
},
],
value_probabilities: vec![
BayesianValueProbabilityIr {
value: sid_0,
probability: 0.6,
},
BayesianValueProbabilityIr {
value: sid_1,
probability: 0.2,
},
BayesianValueProbabilityIr {
value: sid_2,
probability: 0.2,
},
],
count: None,
},
DiscreteConditionalTableIr {
parent_values: vec![
BayesianParentValueIr {
parent: fid_a,
value: sid_1,
},
BayesianParentValueIr {
parent: fid_b,
value: sid_0,
},
],
value_probabilities: vec![
BayesianValueProbabilityIr {
value: sid_0,
probability: 0.4,
},
BayesianValueProbabilityIr {
value: sid_1,
probability: 0.3,
},
BayesianValueProbabilityIr {
value: sid_2,
probability: 0.3,
},
],
count: None,
},
DiscreteConditionalTableIr {
parent_values: vec![
BayesianParentValueIr {
parent: fid_a,
value: sid_1,
},
BayesianParentValueIr {
parent: fid_b,
value: sid_1,
},
],
value_probabilities: vec![
BayesianValueProbabilityIr {
value: sid_0,
probability: 0.3,
},
BayesianValueProbabilityIr {
value: sid_1,
probability: 0.3,
},
BayesianValueProbabilityIr {
value: sid_2,
probability: 0.4,
},
],
count: None,
},
],
derived_fields: vec![],
};
BayesianNetworkIr {
function_name: "regression".into(),
model_name: None,
algorithm_name: None,
model_type: Some("General".into()),
inference_method: Some("Exact".into()),
is_scorable: true,
mining_schema: MiningSchemaIr {
active_fields: vec![fid_c],
target_field: Some(fid_a),
field_metas: vec![],
missing_value_replacement: None,
},
output: vec![],
targets: vec![],
nodes: vec![
BayesianNodeIr::Discrete(node_a),
BayesianNodeIr::Discrete(node_b),
BayesianNodeIr::Discrete(node_c),
],
}
}
#[test]
fn bayesian_exact_inference() {
let bn = make_simple_bn();
// Evidence C=2 should give posterior for A: P(A=0|C=2)=0.208, P(A=1|C=2)=0.792 => predicts 1
let mut vals = vec![Value::Missing; 3];
vals[2] = Value::Discrete(SymbolId(12)); // C=2
let pred = evaluate_bayesian_network(&bn, &vals);
assert_eq!(pred, Value::Discrete(SymbolId(11))); // A=1
}
#[test]
fn bayesian_no_evidence_marginal() {
let bn = make_simple_bn();
let vals = vec![Value::Missing; 3];
let pred = evaluate_bayesian_network(&bn, &vals);
// No evidence, marginal for A is 0.4 vs 0.6 => predicts 1
assert_eq!(pred, Value::Discrete(SymbolId(11)));
}
}