-
-
Save rust-play/82096ab3b1e766cff1c95b14e00043b6 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
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); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment