use crate::base::Value;
use crate::ir::NaiveBayesIr;
fn gaussian_pdf(x: f64, mean: f64, variance: f64) -> f64 {
if variance <= 0.0 {
return 0.0;
}
let denom = (2.0 * std::f64::consts::PI * variance).sqrt();
let num = (-((x - mean) * (x - mean)) / (2.0 * variance)).exp();
num / denom
}
pub fn evaluate_naive_bayes(nb: &NaiveBayesIr, values: &[Value]) -> Value {
if nb.bayes_output_counts.is_empty() {
return Value::Missing;
}
let total_prior: f64 = nb.bayes_output_counts.iter().map(|c| c.count).sum();
if total_prior == 0.0 {
return Value::Discrete(nb.bayes_output_counts[0].value);
}
let mut log_probs: std::collections::HashMap<crate::base::SymbolId, f64> =
std::collections::HashMap::new();
let mut best_score = f64::NEG_INFINITY;
let mut best_value: Option<crate::base::SymbolId> = None;
for target_count in &nb.bayes_output_counts {
let prior = target_count.count / total_prior;
if prior <= 0.0 {
log_probs.insert(target_count.value, f64::NEG_INFINITY);
continue;
}
let mut log_prob = prior.ln();
for bayes_input in &nb.bayes_inputs {
let fid = bayes_input.field.as_usize();
let actual = if fid < values.len() {
values[fid]
} else {
Value::Missing
};
if actual.is_missing() {
continue;
}
let mut found = false;
for tvs in &bayes_input.target_value_stats {
if tvs.value == target_count.value {
if let (Value::Continuous(x), Some(mean), Some(var)) =
(actual, tvs.mean, tvs.variance)
{
let pdf = gaussian_pdf(x, mean, var);
if pdf > 0.0 {
log_prob += pdf.ln();
} else {
log_prob += f64::NEG_INFINITY;
}
found = true;
break;
}
}
}
if found {
continue;
}
for pc in &bayes_input.pair_counts {
let matches = match actual {
Value::Discrete(sid) => sid == pc.value,
Value::Continuous(_f) => {
false
}
_ => false,
};
if matches {
let mut count = 0.0;
let mut total_for_input = 0.0;
for tc in &pc.target_counts {
total_for_input += tc.count;
if tc.value == target_count.value {
count = tc.count;
}
}
if total_for_input > 0.0 {
let prob = count / total_for_input;
if prob > 0.0 {
log_prob += prob.ln();
} else {
log_prob += f64::NEG_INFINITY;
}
}
found = true;
break;
}
}
if !found {
}
}
log_probs.insert(target_count.value, log_prob);
if log_prob > best_score {
best_score = log_prob;
best_value = Some(target_count.value);
}
}
if nb.threshold > 0.0 {
if let Some(best_sid) = best_value {
if let Some(&best_log) = log_probs.get(&best_sid) {
let mut sum_exp = 0.0;
let mut max_log = f64::NEG_INFINITY;
for &lp in log_probs.values() {
if lp > max_log {
max_log = lp;
}
}
for &lp in log_probs.values() {
if lp.is_finite() {
sum_exp += (lp - max_log).exp();
}
}
let best_prob = if sum_exp > 0.0 {
(best_log - max_log).exp() / sum_exp
} else {
0.0
};
if best_prob < nb.threshold {
return Value::Missing;
}
}
}
}
if let Some(v) = best_value {
Value::Discrete(v)
} else {
Value::Missing
}
}