pub struct EvolutionModel<P, L>{ /* private fields */ }Expand description
A probabilistic model of an evolutionary population: a genome prior
program, an observation program (likelihood), and an inverse temperature
β.
Implementations§
Source§impl<P, F> EvolutionModel<P, FactorFitness<F>>
impl<P, F> EvolutionModel<P, FactorFitness<F>>
Sourcepub fn new(prior: P, fitness: F) -> Self
pub fn new(prior: P, fitness: F) -> Self
Create a model with a black-box scalar fitness entering as
factor(β·f(x)) (the classical Gibbs-posterior mode), at β = 1.
Sourcepub fn fitness_value(&self, genome: &P::Genome) -> f64
pub fn fitness_value(&self, genome: &P::Genome) -> f64
Raw fitness f(x) (higher is better).
Sourcepub fn log_weight(&self, genome: &P::Genome) -> f64
pub fn log_weight(&self, genome: &P::Genome) -> f64
The tempered fitness log-factor β · f(x).
Sourcepub fn to_weighted_trace(
&self,
genome: &P::Genome,
) -> Result<Trace, GenomeError>
pub fn to_weighted_trace( &self, genome: &P::Genome, ) -> Result<Trace, GenomeError>
Create a fugue trace whose choices equal the genome’s encoding under
this model’s prior and whose total_log_weight() equals β · f(x) —
the “fitness as likelihood” weighted-trace contract (EV-52): a genuine
factor(β·f) model run through
TraceScoringHandler,
so the mass lands in log_factors. Fails with the prior’s
GenomePrior::validate error (e.g. DimensionMismatch) for a genome
of the wrong shape, before the fitness is evaluated.
Source§impl<P, L> EvolutionModel<P, L>
impl<P, L> EvolutionModel<P, L>
Sourcepub fn from_likelihood(prior: P, likelihood: L) -> Self
pub fn from_likelihood(prior: P, likelihood: L) -> Self
Create a model from an arbitrary observation program (observe
statements, latent nuisance parameters, factors), at β = 1.
Sourcepub fn with_beta(self, beta: f64) -> Self
pub fn with_beta(self, beta: f64) -> Self
Set the inverse temperature β directly (β ≥ 0; negative values
clamp to 0, the prior).
§Panics
If beta is not finite. β = ∞ would make the fitness factor
∞ · f(x) — NaN wherever f = 0, ±∞ elsewhere — a target on
which no sampler can move (EV-N5). Optimizer mode is
EvolutionSMC::anneal with a
large finite beta_max.
Sourcepub fn with_temperature(self, temperature: f64) -> Self
pub fn with_temperature(self, temperature: f64) -> Self
Set the temperature T > 0; equivalent to β = 1/T.
§Panics
If temperature is not a finite, strictly positive number: T = 0
is β = ∞, see Self::with_beta.
Sourcepub fn temperature(&self) -> f64
pub fn temperature(&self) -> f64
Current temperature T = 1/β.
Sourcepub fn likelihood(&self) -> &L
pub fn likelihood(&self) -> &L
The observation program.
Sourcepub fn target_model(&self) -> impl Fn() -> Model<P::Genome> + Clone + '_
pub fn target_model(&self) -> impl Fn() -> Model<P::Genome> + Clone + '_
The fixed-β target as a program:
log π_β(x) = log p(x) + β·log p(data|x). This is the model MH runs
against.
Sourcepub fn smc_model(&self) -> impl Fn() -> Model<P::Genome> + Clone + '_
pub fn smc_model(&self) -> impl Fn() -> Model<P::Genome> + Clone + '_
The untempered (β = 1) joint program for tempered SMC: fugue’s
adaptive_smc supplies β by tempering
log_likelihood + log_factors, applying it exactly once.
Sourcepub fn sample_prior<R: Rng>(&self, rng: &mut R) -> P::Genome
pub fn sample_prior<R: Rng>(&self, rng: &mut R) -> P::Genome
Draw a genome from the prior p(x) by running the prior program.
Sourcepub fn score(
&self,
genome: &P::Genome,
) -> Result<(P::Genome, Trace), GenomeError>
pub fn score( &self, genome: &P::Genome, ) -> Result<(P::Genome, Trace), GenomeError>
Score a genome under the fixed-β target by replaying its encoding
under this model’s prior (GenomePrior::trace_of) through the
target program — so this works for every prior, including generative
grammars over trees.
The returned trace satisfies log π_β(g) = trace.total_log_weight()
and log p(g) = trace.log_prior. A genome outside the prior’s support
scores log_prior = −∞ (that is a valid score, not an error).
§Errors
Never panics on a structural mismatch (EV-N3). Returns the prior’s
GenomePrior::validate error for a genome of the wrong shape
(GenomeError::DimensionMismatch for the vector priors);
GenomeError::MissingAddress when the program visits a site the
encoding lacks — in particular a latent nuisance site of the
likelihood (NoiseSpec::Infer’s sigma, a Pareto weight), which is
not part of trace_of(g): use Self::score_with_latents to draw it
from its prior, or the SMC/MH drivers, which sample it; and
GenomeError::InvalidStructure when the encoding carries sites the
program never visits.
Sourcepub fn score_with_latents<R: Rng>(
&self,
rng: &mut R,
genome: &P::Genome,
) -> Result<(P::Genome, Trace), GenomeError>
pub fn score_with_latents<R: Rng>( &self, rng: &mut R, genome: &P::Genome, ) -> Result<(P::Genome, Trace), GenomeError>
Like Self::score, but a site the program visits that the genome’s
encoding lacks — a latent nuisance parameter of the likelihood — is
drawn from its prior with rng and kept in the returned trace,
which is then a complete, fully scored state of the target (the shape
EvolutionChain::step needs).
Sites of the encoding the program never visits are still an error.
Sourcepub fn log_boltzmann_target(
&self,
genome: &P::Genome,
) -> Result<f64, GenomeError>
pub fn log_boltzmann_target( &self, genome: &P::Genome, ) -> Result<f64, GenomeError>
Unnormalised log target log π_β(x) = log p(x) + β·log p(data|x);
−∞ outside the prior’s support. Errors as Self::score.
Trait Implementations§
Source§impl<P, L> Clone for EvolutionModel<P, L>
impl<P, L> Clone for EvolutionModel<P, L>
Source§fn clone(&self) -> EvolutionModel<P, L>
fn clone(&self) -> EvolutionModel<P, L>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl<P, L> Freeze for EvolutionModel<P, L>
impl<P, L> RefUnwindSafe for EvolutionModel<P, L>where
P: RefUnwindSafe,
L: RefUnwindSafe,
impl<P, L> Send for EvolutionModel<P, L>
impl<P, L> Sync for EvolutionModel<P, L>
impl<P, L> Unpin for EvolutionModel<P, L>
impl<P, L> UnsafeUnpin for EvolutionModel<P, L>where
P: UnsafeUnpin,
L: UnsafeUnpin,
impl<P, L> UnwindSafe for EvolutionModel<P, L>where
P: UnwindSafe,
L: UnwindSafe,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<T> Pointable for T
impl<T> Pointable for 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.