/// Integrate numerically stable over given log-space density in the interval [a, b]. Uses the trapezoidal rule with n grid points.
Integrate numerically stable over given log-space density in the interval [a, b]. Uses the trapezoidal rule with n grid points.
Integrate numerically stable over given log - space density in the interval [ a b ] . Uses the trapezoidal rule with n grid points .
pub fn ln_trapezoidal_integrate_exp<T, D>(mut 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>,
    {
        let mut probs = linspace(a, b, n)
            .enumerate()
            .dropping(1)
            .dropping_back(1)
            .map(|(i, v)| LogProb(*density(i, v) + 2.0f64.ln()))
            .collect_vec();
        probs.push(density(0, a));
        probs.push(density(n, b));
        let width = f64::from(b - a);
        LogProb(*Self::ln_sum_exp(&probs) + width.ln() - (2.0 * (n - 1) as f64).ln())
    }
pub fn ln_trapezoidal_integrate_exp < T , D > ( mut 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 > , { let mut probs = linspace ( a , b , n ) . enumerate ( ) . dropping ( 1 ) . dropping_back ( 1 ) . map ( | ( i , v ) | LogProb ( * density ( i , v ) + 2.0f64 . ln ( ) ) ) . collect_vec ( ) ; probs . push ( density ( 0 , a ) ) ; probs . push ( density ( n , b ) ) ; let width = f64 :: from ( b - a ) ; LogProb ( * Self :: ln_sum_exp ( & probs ) + width . ln ( ) - ( 2.0 * ( n - 1 ) as f64 ) . ln ( ) ) }
Integrate numerically stable over given log-space density in the interval [a, b].
Integrate numerically stable over given log - space density in the interval [ a b ] .
density
D
a
T
b
T
n
usize