Skip to content

Commit f2de41e

Browse files
Merge pull request #6 from bencgreenberg/add-table
Add table with values
2 parents 5f37732 + e2f807f commit f2de41e

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ To run the Bayesian Probability Calculator, you need to have Rust installed on y
2424
Clone the repository:
2525

2626
```bash
27-
git clone https://github.com/bencgreenberg/probability_calculator.git
27+
git clone https://github.com/bencgreenberg/probability-cli.git
2828
```
2929

3030
Change to the project directory:
3131

3232
```bash
33-
cd bayesian_probability_calculator
33+
cd probability-cli
3434
```
3535

3636
Build and run the project:

src/main.rs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,18 @@ fn main() {
6060
println!();
6161
let posterior = bayesian(prior, likelihood, evidence);
6262
let percentage_posterior = posterior * 100.0;
63+
64+
// Print the data that the user provided in a table
65+
create_table(prior, likelihood, evidence, posterior);
66+
6367
println!(
6468
"{}",
6569
format!(
6670
"Based on the information provided, the probability for '{}' is {:.2}%",
6771
description.trim(),
6872
percentage_posterior
6973
)
70-
.magenta()
74+
.red().bold()
7175
);
7276
}
7377

@@ -117,6 +121,55 @@ fn get_percentage(input: &str) -> Option<f64> {
117121
}
118122
}
119123

124+
/// Displays a formatted table with Bayesian probabilities.
125+
///
126+
/// The function presents four key probabilities: prior, likelihood, evidence, and posterior.
127+
/// The table provides a clear visual representation of the probabilities to facilitate
128+
/// understanding and decision-making.
129+
///
130+
/// # Arguments
131+
///
132+
/// * `prior` - The initial belief about the probability of an event before considering new evidence (as a decimal fraction).
133+
/// * `likelihood` - The probability of observing the new evidence, assuming the event is true (as a decimal fraction).
134+
/// * `evidence` - The probability of observing the new evidence, taking into account all possible scenarios (as a decimal fraction).
135+
/// * `posterior` - The updated probability of the event occurring, given the new evidence (as a decimal fraction).
136+
///
137+
/// # Example
138+
///
139+
/// ```
140+
/// create_table(0.5, 0.8, 0.7, 0.5714);
141+
/// ```
142+
///
143+
/// This example creates a table with the following probabilities: prior = 50%, likelihood = 80%,
144+
/// evidence = 70%, and posterior = 57.14%.
145+
fn create_table(prior: f64, likelihood: f64, evidence: f64, posterior: f64) {
146+
println!("In this table, we present four key probabilities based on the data you provided:");
147+
println!(" - Prior: Your initial belief about the probability of an event before considering new evidence.");
148+
println!(" - Likelihood: How probable the new evidence is, assuming the event is true.");
149+
println!(" - Evidence: The probability of observing the new evidence, taking into account all possible scenarios.");
150+
println!(" - Posterior: The updated probability of the event occurring, given the new evidence.");
151+
println!();
152+
153+
// Print the header for the table
154+
println!("{}", "+-------------+----------------+".cyan());
155+
println!("| {} | {} |", "Probability".bold(), "Value".bold());
156+
println!("{}", "+-------------+----------------+".cyan());
157+
158+
// Print the data in the table with colors
159+
println!("| {:<11} | {:<14.2}% |", "Prior".yellow(), prior * 100.0);
160+
println!("| {:<11} | {:<14.2}% |", "Likelihood".green(), likelihood * 100.0);
161+
println!("| {:<11} | {:<14.2}% |", "Evidence".blue(), evidence * 100.0);
162+
println!("| {:<11} | {:<14.2}% |", "Posterior".magenta(), posterior * 100.0);
163+
164+
// Print the footer for the table
165+
println!("{}", "+-------------+----------------+".cyan());
166+
167+
println!();
168+
println!("Use this table to understand how new evidence has updated the probability of the event.");
169+
println!("Keep in mind that Bayesian reasoning is an iterative process, and you can update your probabilities as new evidence becomes available.");
170+
println!();
171+
}
172+
120173
/// Calculates the posterior probability using Bayesian reasoning.
121174
///
122175
/// # Arguments

0 commit comments

Comments
 (0)