-
-
Save RandyMcMillan/3f2e12f4e118f5b479cb5cd4f64149b3 to your computer and use it in GitHub Desktop.
bayes_theorum.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct Probability { | |
| value: f64, | |
| } | |
| impl Probability { | |
| fn new(p: f64) -> Self { | |
| assert!( | |
| (0.0..=1.0).contains(&p), | |
| "Probability must be between 0 and 1" | |
| ); | |
| Self { value: p } | |
| } | |
| } | |
| /// Calculates P(A|B) using Bayes' Theorem: P(A|B) = (P(B|A) * P(A)) / P(B) | |
| fn bayes_theorem(p_b_given_a: f64, p_a: f64, p_b: f64) -> f64 { | |
| (p_b_given_a * p_a) / p_b | |
| } | |
| fn main() { | |
| let p_a = 0.10; // Prior: Probability of having a rare disease | |
| let p_b_given_a = 0.99; // Likelihood: Test is 99% accurate for positive cases | |
| let p_b = 0.05; // Evidence: Total probability of testing positive | |
| let result = bayes_theorem(p_b_given_a, p_a, p_b); | |
| println!("The probability of A given B is: {:.2}%", result * 100.0); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=b0f72a82805ddba436b6511c39e2f702