Skip to content

Commit

Permalink
Merge pull request #6 from bencgreenberg/add-table
Browse files Browse the repository at this point in the history
Add table with values
  • Loading branch information
hummusonrails authored Apr 11, 2023
2 parents 5f37732 + e2f807f commit f2de41e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ To run the Bayesian Probability Calculator, you need to have Rust installed on y
Clone the repository:

```bash
git clone https://github.com/bencgreenberg/probability_calculator.git
git clone https://github.com/bencgreenberg/probability-cli.git
```

Change to the project directory:

```bash
cd bayesian_probability_calculator
cd probability-cli
```

Build and run the project:
Expand Down
55 changes: 54 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,18 @@ fn main() {
println!();
let posterior = bayesian(prior, likelihood, evidence);
let percentage_posterior = posterior * 100.0;

// Print the data that the user provided in a table
create_table(prior, likelihood, evidence, posterior);

println!(
"{}",
format!(
"Based on the information provided, the probability for '{}' is {:.2}%",
description.trim(),
percentage_posterior
)
.magenta()
.red().bold()
);
}

Expand Down Expand Up @@ -117,6 +121,55 @@ fn get_percentage(input: &str) -> Option<f64> {
}
}

/// Displays a formatted table with Bayesian probabilities.
///
/// The function presents four key probabilities: prior, likelihood, evidence, and posterior.
/// The table provides a clear visual representation of the probabilities to facilitate
/// understanding and decision-making.
///
/// # Arguments
///
/// * `prior` - The initial belief about the probability of an event before considering new evidence (as a decimal fraction).
/// * `likelihood` - The probability of observing the new evidence, assuming the event is true (as a decimal fraction).
/// * `evidence` - The probability of observing the new evidence, taking into account all possible scenarios (as a decimal fraction).
/// * `posterior` - The updated probability of the event occurring, given the new evidence (as a decimal fraction).
///
/// # Example
///
/// ```
/// create_table(0.5, 0.8, 0.7, 0.5714);
/// ```
///
/// This example creates a table with the following probabilities: prior = 50%, likelihood = 80%,
/// evidence = 70%, and posterior = 57.14%.
fn create_table(prior: f64, likelihood: f64, evidence: f64, posterior: f64) {
println!("In this table, we present four key probabilities based on the data you provided:");
println!(" - Prior: Your initial belief about the probability of an event before considering new evidence.");
println!(" - Likelihood: How probable the new evidence is, assuming the event is true.");
println!(" - Evidence: The probability of observing the new evidence, taking into account all possible scenarios.");
println!(" - Posterior: The updated probability of the event occurring, given the new evidence.");
println!();

// Print the header for the table
println!("{}", "+-------------+----------------+".cyan());
println!("| {} | {} |", "Probability".bold(), "Value".bold());
println!("{}", "+-------------+----------------+".cyan());

// Print the data in the table with colors
println!("| {:<11} | {:<14.2}% |", "Prior".yellow(), prior * 100.0);
println!("| {:<11} | {:<14.2}% |", "Likelihood".green(), likelihood * 100.0);
println!("| {:<11} | {:<14.2}% |", "Evidence".blue(), evidence * 100.0);
println!("| {:<11} | {:<14.2}% |", "Posterior".magenta(), posterior * 100.0);

// Print the footer for the table
println!("{}", "+-------------+----------------+".cyan());

println!();
println!("Use this table to understand how new evidence has updated the probability of the event.");
println!("Keep in mind that Bayesian reasoning is an iterative process, and you can update your probabilities as new evidence becomes available.");
println!();
}

/// Calculates the posterior probability using Bayesian reasoning.
///
/// # Arguments
Expand Down

0 comments on commit f2de41e

Please sign in to comment.