radio-bayesian-2026-11-14.rs
// radio.rs — Honest Bayesian compliance radio (Rust)
// © Albert Lane. All Rights Reserved. Proprietary and Confidential.
// https://albertlane.net
// Same logic as radio_bayesian.py: Bayes to reduce FALSE ALARMS,
// never to force 100% compliance. All frames logged.
//
// Build: rustc radio.rs -o radio (or cargo)
// Run: ./radio
#[derive(Debug, Clone)]
pub struct BayesianCompliance {
pub posterior: f64,
pub p_evidence_given_compliant: f64,
pub p_evidence_given_noncompliant: f64,
pub observations: u64,
}
impl BayesianCompliance {
pub fn new() -> Self {
Self {
posterior: 0.5, // neutral prior, NOT 1.0
p_evidence_given_compliant: 0.92,
p_evidence_given_noncompliant: 0.18,
observations: 0,
}
}
/// Update with one frame. Returns new posterior.
pub fn observe(&mut self, looks_compliant: bool) -> f64 {
let (lik_c, lik_n) = if looks_compliant {
(self.p_evidence_given_compliant, self.p_evidence_given_noncompliant)
} else {
(1.0 - self.p_evidence_given_compliant, 1.0 - self.p_evidence_given_noncompliant)
};
let prior = self.posterior;
let num = lik_c * prior;
let den = num + lik_n * (1.0 - prior);
self.posterior = if den > 0.0 { num / den } else { prior };
self.observations += 1;
self.posterior
}
pub fn report(&self) -> String {
let verdict = if self.posterior > 0.85 {
"likely_compliant"
} else if self.posterior > 0.4 {
"uncertain"
} else {
"likely_noncompliant"
};
format!(
"posterior_compliant={:.4} observations={} verdict={} note=estimate_from_observed_frames_not_a_guarantee",
self.posterior, self.observations, verdict
)
}
}
/// Honest structural check — same RSS 2.0 rules as Python version.
pub fn check_frame(xml: &str) -> (bool, &'static str) {
if !xml.contains("<rss") || !xml.contains("version=\"2.0\"") {
return (false, "missing rss 2.0 root");
}
if !xml.contains("<channel>") {
return (false, "missing channel");
}
if !xml.contains("<title>") || !xml.contains("<link>") || !xml.contains("<description>") {
return (false, "missing required channel title/link/description");
}
(true, "well-formed RSS 2.0 frame")
}
fn main() {
let mut radio = BayesianCompliance::new();
let frames = vec![
"<?xml version=\"1.0\"?><rss version=\"2.0\"><channel><title>A</title><link>rss://ghost-rss.py/nodes/A</link><description>x</description></channel></rss>",
"not xml at all",
"<?xml version=\"1.0\"?><rss version=\"2.0\"><channel><title>B</title><link>rss://ghost-rss.py/nodes/B</link><description>x</description></channel></rss>",
];
for f in frames {
let (ok, why) = check_frame(f);
let p = radio.observe(ok);
println!("looks_compliant={} ({}) -> posterior={:.4}", ok, why, p);
}
println!("{}", radio.report());
}