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
sourceimpl 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
) -> LogProb where
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
) -> LogProb where
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
) -> LogProb where
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
) -> LogProb where
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]
) -> LogProb where
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]
) -> LogProb where
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.
Trait Implementations
sourceimpl AddAssign<LogProb> for LogProb
impl AddAssign<LogProb> for LogProb
sourcefn add_assign(&mut self, other: LogProb)
fn add_assign(&mut self, other: LogProb)
Performs the +=
operation. Read more
sourceimpl<'de> Deserialize<'de> for LogProb
impl<'de> Deserialize<'de> for LogProb
sourcefn 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>,
Deserialize this value from the given Serde deserializer. Read more
sourceimpl PartialOrd<LogProb> for LogProb
impl PartialOrd<LogProb> for LogProb
sourcefn partial_cmp(&self, other: &LogProb) -> Option<Ordering>
fn partial_cmp(&self, other: &LogProb) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
sourceimpl SubAssign<LogProb> for LogProb
impl SubAssign<LogProb> for LogProb
sourcefn sub_assign(&mut self, other: LogProb)
fn sub_assign(&mut self, other: LogProb)
Performs the -=
operation. Read more
impl 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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<SS, SP> SupersetOf<SS> for SP where
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SP where
SS: SubsetOf<SP>,
pub fn to_subset(&self) -> Option<SS>
pub fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct self
from the equivalent element of its
superset. Read more
pub fn is_in_subset(&self) -> bool
pub fn is_in_subset(&self) -> bool
Checks if self
is actually part of its subset T
(and can be converted to it).
pub fn to_subset_unchecked(&self) -> SS
pub fn to_subset_unchecked(&self) -> SS
Use with care! Same as self.to_subset
but without any property checks. Always succeeds.
pub fn from_subset(element: &SS) -> SP
pub fn from_subset(element: &SS) -> SP
The inclusion map: converts self
to the equivalent element of its superset.
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more