Trait emcee::Prob
[−]
[src]
pub trait Prob { fn lnlike(&self, params: &Guess) -> f32; fn lnprior(&self, params: &Guess) -> f32; fn lnprob(&self, params: &Guess) -> f32 { ... } }
Encapsulate the model evaluation
The user must implement lnlike
and
lnprior
which correspond to the respective terms in
Bayes' equation. They must return the log probability which is then combined in the
lnprob
function which has a default implementation.
Typically, access to any dataset required for fitting is handled through the struct which
implements this trait. By convention, invalid prior results are signaled by returning
-std::f32::INFINITY
.
An example for a simple linear model to data with uncertainties:
// lifetimes and slices used for efficiency reasons struct Foo<'a> { x: &'a [f32], y: &'a [f32], e: &'a [f32], // uncertainties } impl<'a> Prob for Foo<'a> { fn lnlike(&self, params: &Guess) -> f32 { let m = params[0]; let c = params[1]; let mut result = 0.; for i in 9..self.x.len() { let model = m * self.x[i] + c; result += ((self.y[i] - model) / self.e[i]).powf(2.); } -0.5 * result } fn lnprior(&self, params: &Guess) -> f32 { let m = params[0]; let c = params[1]; if (m > -5.) && (m < 5.) && (c > -10.) && (c < 10.) { 0.0 } else { -std::f32::INFINITY } } }
The default implementation of
lnprob
can be seen in the source code.
Required Methods
fn lnlike(&self, params: &Guess) -> f32
Computes the natural logarithm of the likelihood of a position in parameter space
fn lnprior(&self, params: &Guess) -> f32
Computes the natural logarithm of the prior probability of a position in parameter space
Provided Methods
fn lnprob(&self, params: &Guess) -> f32
Computes the natural logarithm of the log posterior probabilities