Skip to content

Commit

Permalink
feat(lib): add prob validation, fix flag for set-prior
Browse files Browse the repository at this point in the history
  • Loading branch information
ChosunOne committed Feb 20, 2022
1 parent c6494f9 commit 410a0a9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]


## 0.1.2
* Add validation for inputted probabilities.
* Fix bug where you would be unable to set a non-default prior.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ask-bayes"
version = "0.1.1"
version = "0.1.2"
edition = "2021"
authors = ["ChosunOne <[email protected]>"]
description = "CLI tool for Bayesian inference"
Expand Down
19 changes: 13 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ pub struct Args {
#[clap(short, long)]
pub name: String,
/// The prior probability of the hypothesis P(H)
#[clap(short, long, default_value_t = 0.5)]
#[clap(short, long, default_value_t = 0.5, validator = validate_probability)]
pub prior: f64,
/// The likelihood of the evidence P(E|H)
#[clap(short, long, default_value_t = 0.5)]
#[clap(short, long, default_value_t = 0.5, validator = validate_probability)]
pub likelihood: f64,
/// The likelihood of the evidence P(E|¬H)
#[clap(long, default_value_t = 0.5)]
#[clap(long, default_value_t = 0.5, validator = validate_probability)]
pub likelihood_not: f64,
/// Indicates whether supporting evidence is observed
#[clap(short, long, default_value_t = Evidence::Observed)]
Expand All @@ -131,16 +131,15 @@ pub struct Args {
#[clap(
short,
long,
group = "set_prior",
conflicts_with = "prior",
requires = "prior",
conflicts_with = "likelihood",
conflicts_with = "likelihood-not",
conflicts_with = "evidence",
conflicts_with = "update-prior",
conflicts_with = "get-prior"
)]
pub set_prior: bool,
/// Removes the prior probability of the hypothesis P(H) from the database
/// Removes the prior probability of the hypothesis P(H) from the database.
/// Incompatible with other flags aside from `--name`
#[clap(
short,
Expand Down Expand Up @@ -229,3 +228,11 @@ fn open_db() -> Result<Db> {
let db_path = hd.join(".ask-bayes").join("hypotheses.db");
Ok(sled::open(db_path)?)
}

fn validate_probability(value: &str) -> Result<()> {
let value = value.parse::<f64>()?;
if value < 0.0_f64 || value > 1.0_f64 {
return Err(anyhow!("Probability must be between 0 and 1"));
}
Ok(())
}

0 comments on commit 410a0a9

Please sign in to comment.