pub struct LogProb(pub f64);
Expand description
A newtype for log-scale probabilities.
For performance reasons, we use an approximation of the exp method
implemented in bio::utils::FastExp
. This can lead to slight
errors, but should not matter given that most of the probability differences
are reflected within the integer part of the log probability.
Example
#[macro_use]
extern crate approx;
use bio::stats::{LogProb, Prob};
// convert from probability
let p = LogProb::from(Prob(0.5));
// convert manually
let q = LogProb(0.2f64.ln());
// obtain zero probability in log-space
let o = LogProb::ln_one();
assert_relative_eq!(*Prob::from(p.ln_add_exp(q) + o), *Prob(0.7), epsilon=0.000001);
Tuple Fields§
§0: f64
Implementations§
source§impl LogProb
impl LogProb
pub fn is_valid(&self) -> bool
sourcepub fn cap_numerical_overshoot(&self, epsilon: f64) -> LogProb
pub fn cap_numerical_overshoot(&self, epsilon: f64) -> LogProb
sums of LogProbs, e.g. with ln_sum_exp()
can end up
slightly above the maximum of LogProb <= 0
due to
numerical imprecisions – this function can rescue such
values before panics due to asserts in other functions
handling LogProbs, e.g. ln_1m_exp
sourcepub fn ln_one_minus_exp(&self) -> LogProb
pub fn ln_one_minus_exp(&self) -> LogProb
Numerically stable calculation of 1 - p in log-space.
sourcepub fn ln_sum_exp(probs: &[LogProb]) -> LogProb
pub fn ln_sum_exp(probs: &[LogProb]) -> LogProb
Numerically stable sum of probabilities in log-space.
sourcepub fn ln_add_exp(self, other: LogProb) -> LogProb
pub fn ln_add_exp(self, other: LogProb) -> LogProb
Numerically stable addition of probabilities in log-space.
sourcepub fn ln_sub_exp(self, other: LogProb) -> LogProb
pub fn ln_sub_exp(self, other: LogProb) -> LogProb
Numerically stable subtraction of probabilities in log-space.
sourcepub fn ln_cumsum_exp<I: IntoIterator<Item = LogProb>>(probs: I) -> ScanIter<I>
pub fn ln_cumsum_exp<I: IntoIterator<Item = LogProb>>(probs: I) -> ScanIter<I>
Calculate the cumulative sum of the given probabilities in a numerically stable way (Durbin 1998).
sourcepub fn ln_trapezoidal_integrate_exp<T, D>(
density: D,
a: T,
b: T,
n: usize
) -> LogProbwhere
T: Copy + Add<Output = T> + Sub<Output = T> + Div<Output = T> + Mul<Output = T> + Float,
D: FnMut(usize, T) -> LogProb,
f64: From<T>,
pub fn ln_trapezoidal_integrate_exp<T, D>( density: D, a: T, b: T, n: usize ) -> LogProbwhere T: Copy + Add<Output = T> + Sub<Output = T> + Div<Output = T> + Mul<Output = T> + Float, D: FnMut(usize, T) -> LogProb, f64: From<T>,
Integrate numerically stable over given log-space density in the interval [a, b]. Uses the trapezoidal rule with n grid points.
sourcepub fn ln_simpsons_integrate_exp<T, D>(
density: D,
a: T,
b: T,
n: usize
) -> LogProbwhere
T: Copy + Add<Output = T> + Sub<Output = T> + Div<Output = T> + Mul<Output = T> + Float,
D: FnMut(usize, T) -> LogProb,
f64: From<T>,
pub fn ln_simpsons_integrate_exp<T, D>( density: D, a: T, b: T, n: usize ) -> LogProbwhere T: Copy + Add<Output = T> + Sub<Output = T> + Div<Output = T> + Mul<Output = T> + Float, D: FnMut(usize, T) -> LogProb, f64: From<T>,
Integrate numerically stable over given log-space density in the interval [a, b]. Uses Simpson’s rule with n (odd) grid points.
sourcepub fn ln_trapezoidal_integrate_grid_exp<T, D>(
density: D,
grid: &[T]
) -> LogProbwhere
T: Copy + Add<Output = T> + Sub<Output = T> + Div<Output = T> + Mul<Output = T> + Float,
D: FnMut(usize, T) -> LogProb,
f64: From<T>,
pub fn ln_trapezoidal_integrate_grid_exp<T, D>( density: D, grid: &[T] ) -> LogProbwhere T: Copy + Add<Output = T> + Sub<Output = T> + Div<Output = T> + Mul<Output = T> + Float, D: FnMut(usize, T) -> LogProb, f64: From<T>,
Integrate numerically stable over given log-space density and grid points. Uses the trapezoidal rule.
Methods from Deref<Target = f64>§
pub const RADIX: u32 = 2u32
pub const MANTISSA_DIGITS: u32 = 53u32
pub const DIGITS: u32 = 15u32
pub const EPSILON: f64 = 2.2204460492503131E-16f64
pub const MIN: f64 = -1.7976931348623157E+308f64
pub const MIN_POSITIVE: f64 = 2.2250738585072014E-308f64
pub const MAX: f64 = 1.7976931348623157E+308f64
pub const MIN_EXP: i32 = -1_021i32
pub const MAX_EXP: i32 = 1_024i32
pub const MIN_10_EXP: i32 = -307i32
pub const MAX_10_EXP: i32 = 308i32
pub const NAN: f64 = NaNf64
pub const INFINITY: f64 = +Inff64
pub const NEG_INFINITY: f64 = -Inff64
1.62.0 · sourcepub fn total_cmp(&self, other: &f64) -> Ordering
pub fn total_cmp(&self, other: &f64) -> Ordering
Return the ordering between self
and other
.
Unlike the standard partial comparison between floating point numbers,
this comparison always produces an ordering in accordance to
the totalOrder
predicate as defined in the IEEE 754 (2008 revision)
floating point standard. The values are ordered in the following sequence:
- negative quiet NaN
- negative signaling NaN
- negative infinity
- negative numbers
- negative subnormal numbers
- negative zero
- positive zero
- positive subnormal numbers
- positive numbers
- positive infinity
- positive signaling NaN
- positive quiet NaN.
The ordering established by this function does not always agree with the
PartialOrd
and PartialEq
implementations of f64
. For example,
they consider negative and positive zero equal, while total_cmp
doesn’t.
The interpretation of the signaling NaN bit follows the definition in the IEEE 754 standard, which may not match the interpretation by some of the older, non-conformant (e.g. MIPS) hardware implementations.
Example
struct GoodBoy {
name: String,
weight: f64,
}
let mut bois = vec![
GoodBoy { name: "Pucci".to_owned(), weight: 0.1 },
GoodBoy { name: "Woofer".to_owned(), weight: 99.0 },
GoodBoy { name: "Yapper".to_owned(), weight: 10.0 },
GoodBoy { name: "Chonk".to_owned(), weight: f64::INFINITY },
GoodBoy { name: "Abs. Unit".to_owned(), weight: f64::NAN },
GoodBoy { name: "Floaty".to_owned(), weight: -5.0 },
];
bois.sort_by(|a, b| a.weight.total_cmp(&b.weight));
Trait Implementations§
source§impl AddAssign<LogProb> for LogProb
impl AddAssign<LogProb> for LogProb
source§fn add_assign(&mut self, other: LogProb)
fn add_assign(&mut self, other: LogProb)
+=
operation. Read moresource§impl<'de> Deserialize<'de> for LogProb
impl<'de> Deserialize<'de> for LogProb
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where __D: Deserializer<'de>,
source§impl PartialEq<LogProb> for LogProb
impl PartialEq<LogProb> for LogProb
source§impl PartialOrd<LogProb> for LogProb
impl PartialOrd<LogProb> for LogProb
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl SubAssign<LogProb> for LogProb
impl SubAssign<LogProb> for LogProb
source§fn sub_assign(&mut self, other: LogProb)
fn sub_assign(&mut self, other: LogProb)
-=
operation. Read moreimpl Copy for LogProb
impl StructuralPartialEq for LogProb
Auto Trait Implementations§
impl RefUnwindSafe for LogProb
impl Send for LogProb
impl Sync for LogProb
impl Unpin for LogProb
impl UnwindSafe for LogProb
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere SS: SubsetOf<SP>,
§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self
from the equivalent element of its
superset. Read more§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self
is actually part of its subset T
(and can be converted to it).§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset
but without any property checks. Always succeeds.§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self
to the equivalent element of its superset.