Struct net_ensembles::sampling::Metropolis
source · [−]pub struct Metropolis<E, R, S, Res, T> { /* private fields */ }
Expand description
Create a metropolis simulation
Citation see, e.g,
M. E. J. Newman and G. T. Barkema, “Monte Carlo Methods in Statistical Physics” Clarendon Press, 1999, ISBN: 978-0-19-8517979
Explanation
- used for large-deviation simulations
- Performes markov chain using the markov chain trait
All self.metropolis*
functions do the following
- Let the current state of the system (i.e., the ensemble) be S(i) with corresponding energy
E(i) = energy_fn(S(i))
. - Now perform a markov step, such that the new system is S_new with energy E_new.
- The new state will be accepted (meaning S(i+1) = S_new) with probability:
min[1.0, exp{m_beta * (E_new - E(i))}]
- otherwise the new state will be rejected, meaning S(i + 1) = S(i).
- the
measure
function is called:measure(S(i + 1))
Implementations
sourceimpl<R, E, S, Res, T> Metropolis<E, R, S, Res, T>where
T: Copy + AsPrimitive<f64>,
impl<R, E, S, Res, T> Metropolis<E, R, S, Res, T>where
T: Copy + AsPrimitive<f64>,
sourcepub fn set_m_beta(&mut self, m_beta: f64)
pub fn set_m_beta(&mut self, m_beta: f64)
sets m_beta (minus beta). Is related to the temperature: m_beta = -1 / temperature
sourcepub fn set_temperature(&mut self, temperature: f64)
pub fn set_temperature(&mut self, temperature: f64)
sets m_beta according to m_beta = -1 / temperature
sourcepub unsafe fn set_energy(&mut self, energy: T) -> Result<(), ()>
pub unsafe fn set_energy(&mut self, energy: T) -> Result<(), ()>
set stored value for current_energy
- Will return Err() if you try to set the energy to nan
- otherwise it will set the stored
energy
and return Ok(())
Important
- It is very unlikely that you need this function - Only use it, if you know what you are doing
Safety
This is not unsafe in the programming sense, but I chose to make it unsafe anyway to make the user aknowledge that this will result in a logical error for the algorithms if set to the incorrect energy
sourcepub unsafe fn ensemble_mut(&mut self) -> &mut E
pub unsafe fn ensemble_mut(&mut self) -> &mut E
returns mutable reference to ensemble
- use with care!
Safety
- if you change your ensemble, this might invalidate the simulation!
- The metropolis functions do not calculate the energy of the current state
- Unsafe purely for logical reasons, in the programming sense this function didn’t need to be unsafe
sourcepub fn counter(&self) -> usize
pub fn counter(&self) -> usize
returns stored value for the counter
, i.e., where to resume iteration
- note:
counter
is a wrapping counter - counter is increase each time, a markov step is performed, i.e,
each time
ensemble.m_steps(step_size)
is called, the counter will increase by 1 (not by step_size)
sourcepub fn reset_counter(&mut self)
pub fn reset_counter(&mut self)
resets the counter
to 0
- note:
counter
is a wrapping counter
sourceimpl<E, R, S, Res, T> Metropolis<E, R, S, Res, T>where
R: Rng,
E: MarkovChain<S, Res>,
T: Copy + AsPrimitive<f64>,
impl<E, R, S, Res, T> Metropolis<E, R, S, Res, T>where
R: Rng,
E: MarkovChain<S, Res>,
T: Copy + AsPrimitive<f64>,
sourcepub fn new_from_m_beta(
rng: R,
ensemble: E,
energy: T,
m_beta: f64,
step_size: usize
) -> Result<Metropolis<E, R, S, Res, T>, MetropolisError>
pub fn new_from_m_beta(
rng: R,
ensemble: E,
energy: T,
m_beta: f64,
step_size: usize
) -> Result<Metropolis<E, R, S, Res, T>, MetropolisError>
Create a new Metropolis struct - used for Metropolis simulations
meaning | |
---|---|
rng | the Rng used to decide, if a state should be accepted or rejected |
ensemble | the ensemble that is explored with the markov chain |
energy | current energy of the ensemble - cannot be NAN, should match energy_fn(ensemble) (see metropolis* functions) |
m_beta | minus beta, has to be finite - used for acceptance, i.e., probability to accept a markov step from Energy E to Energy E_new is min[1.0, exp{m_beta * (E_new - E)}] |
step_size | is used for each markov step, i.e., ensemble.m_steps(stepsize) is called |
- will return Err if
energy
is nan orm_beta
is not finite
sourcepub fn new_from_temperature(
rng: R,
ensemble: E,
energy: T,
temperature: f64,
step_size: usize
) -> Result<Metropolis<E, R, S, Res, T>, MetropolisError>
pub fn new_from_temperature(
rng: R,
ensemble: E,
energy: T,
temperature: f64,
step_size: usize
) -> Result<Metropolis<E, R, S, Res, T>, MetropolisError>
Create a new Metropolis struct - used for Metropolis simulations
meaning | |
---|---|
rng | the Rng used to decide, if a state should be accepted or rejected |
ensemble | the ensemble that is explored with the markov chain |
energy | current energy of the ensemble - cannot be NAN, should match energy_fn(ensemble) (see metropolis* functions) |
temperature | m_beta = -1.0/temperature. Used for acceptance, i.e., probability to accept a markov step from Energy E to Energy E_new is min[1.0, exp{m_beta * (E_new - E)}] |
step_size | is used for each markov step, i.e., ensemble.m_steps(stepsize) is called |
- will return Err if
energy
is nan orm_beta
is not finite
sourcepub fn change_markov_chain<S2, Res2>(self) -> Metropolis<E, R, S2, Res2, T>where
E: MarkovChain<S2, Res2>,
pub fn change_markov_chain<S2, Res2>(self) -> Metropolis<E, R, S2, Res2, T>where
E: MarkovChain<S2, Res2>,
Change, which markov chain is used for the metropolis simulations
- Use this if there are different ways to perform a markov chain for your problem and you want to switch between them
sourcepub fn metropolis<Energy, Mes>(
&mut self,
step_target: usize,
energy_fn: Energy,
measure: Mes
)where
Energy: FnMut(&E) -> Option<T>,
Mes: FnMut(&Metropolis<E, R, S, Res, T>),
pub fn metropolis<Energy, Mes>(
&mut self,
step_target: usize,
energy_fn: Energy,
measure: Mes
)where
Energy: FnMut(&E) -> Option<T>,
Mes: FnMut(&Metropolis<E, R, S, Res, T>),
Metropolis Simulation
- see
- performs
self.counter..=step_target
markov steps energy_fn(self.ensemble)
is assumed to equalself.energy
at the beginning!- if
energy_fn
returns None, the step will always be rejected - after each acceptance/rejection,
measure
is called
Note
- I assume, that the energy_fn never returns
nan
(when cast as f64) If nan is possible, please check for that beforhand and returnNone
in that case - Maybe do the same for infinity, it is unlikely, that infinit energys make sense
sourcepub unsafe fn metropolis_unsafe<Energy, Mes>(
&mut self,
step_target: usize,
energy_fn: Energy,
measure: Mes
)where
Energy: FnMut(&mut E) -> Option<T>,
Mes: FnMut(&mut Metropolis<E, R, S, Res, T>),
pub unsafe fn metropolis_unsafe<Energy, Mes>(
&mut self,
step_target: usize,
energy_fn: Energy,
measure: Mes
)where
Energy: FnMut(&mut E) -> Option<T>,
Mes: FnMut(&mut Metropolis<E, R, S, Res, T>),
Metropolis Simulation
- see
- performs
self.counter..=step_target
markov steps energy_fn(self.ensemble)
is assumed to equalself.energy
at the beginning!- if
energy_fn
returns None, the step will always be rejected - after each acceptance/rejection,
measure
is called
Important
- if possible, prefere
self.metropolis
as it is safer - use this, if your energy function needs mutable access, or
measure
needs mutable access. Be careful though, this can invalidate the results of your simulation
Safety
- I assume, that the energy_fn never returns
nan
(when cast as f64) If nan is possible, please check for that beforhand and returnNone
in that case - Maybe do the same for infinity, it is unlikely, that infinit energys make sense
- Note: I chose to make this function unsafe to force users to aknowledge the (purely logical) limitations regarding the usage of the mutable ensemble. From a programming point of view this will not lead to any undefined behavior or such regardless of if the user fullfills the requirements
sourcepub fn metropolis_efficient<Energy, Mes>(
&mut self,
step_target: usize,
energy_fn: Energy,
measure: Mes
)where
Energy: FnMut(&E, T, &[S]) -> Option<T>,
Mes: FnMut(&Metropolis<E, R, S, Res, T>),
pub fn metropolis_efficient<Energy, Mes>(
&mut self,
step_target: usize,
energy_fn: Energy,
measure: Mes
)where
Energy: FnMut(&E, T, &[S]) -> Option<T>,
Mes: FnMut(&Metropolis<E, R, S, Res, T>),
Metropolis Simulation
- see
- performs
self.counter..=step_target
markov steps energy_fn(self.ensemble)
is assumed to equalself.energy
at the beginning!- if
energy_fn
returns None, the step will always be rejected - after each acceptance/rejection,
measure
is called
Difference to self.metropolis
- Function parameter of energy_fn: &ensemble, old_energy, &[steps] - that means, you should prefere this, if you can calculate the new energy more efficient by accessing the old energy and the information about what the markov step changed
Note
- I assume, that the energy_fn never returns
nan
(when cast as f64) If nan is possible, please check for that beforhand and returnNone
in that case - Maybe do the same for infinity, it is unlikely, that infinit energys make sense
sourcepub unsafe fn metropolis_efficient_unsafe<Energy, Mes>(
&mut self,
step_target: usize,
energy_fn: Energy,
measure: Mes
)where
Energy: Fn(&mut E, T, &[S]) -> Option<T>,
Mes: FnMut(&mut Metropolis<E, R, S, Res, T>),
pub unsafe fn metropolis_efficient_unsafe<Energy, Mes>(
&mut self,
step_target: usize,
energy_fn: Energy,
measure: Mes
)where
Energy: Fn(&mut E, T, &[S]) -> Option<T>,
Mes: FnMut(&mut Metropolis<E, R, S, Res, T>),
Metropolis Simulation
- see
- performs
self.counter..=step_target
markov steps energy_fn(self.ensemble)
is assumed to equalself.energy
at the beginning!- if
energy_fn
returns None, the step will always be rejected - after each acceptance/rejection,
measure
is called
Difference to self.metropolis
- Function parameter of energy_fn: &ensemble, old_energy, &[steps] - that means, you should prefere this, if you can calculate the new energy more efficient by accessing the old energy and the information about what the markov step changed
Safety
- I assume, that the energy_fn never returns
nan
(when cast as f64) If nan is possible, please check for that beforhand and returnNone
in that case - Maybe do the same for infinity, it is unlikely, that infinite energys make sense
sourcepub fn metropolis_while<Energy, Mes, Cond>(
&mut self,
energy_fn: Energy,
measure: Mes,
condition: Cond
)where
Energy: FnMut(&E) -> Option<T>,
Mes: FnMut(&Metropolis<E, R, S, Res, T>),
Cond: FnMut(&Metropolis<E, R, S, Res, T>) -> bool,
pub fn metropolis_while<Energy, Mes, Cond>(
&mut self,
energy_fn: Energy,
measure: Mes,
condition: Cond
)where
Energy: FnMut(&E) -> Option<T>,
Mes: FnMut(&Metropolis<E, R, S, Res, T>),
Cond: FnMut(&Metropolis<E, R, S, Res, T>) -> bool,
Metropolis Simulation
- see
- checks
condition(self)
after eachmetropolis_step(&mut energy_fn)
and returns whenfalse
is returned by the condition energy_fn(self.ensemble)
is assumed to equalself.energy
at the beginning!- if
energy_fn
returns None, the step will always be rejected - after each acceptance/rejection,
measure
is called
Note
- I assume, that the energy_fn never returns
nan
(when cast as f64) If nan is possible, please check for that beforhand and returnNone
in that case - Maybe do the same for infinity, it is unlikely, that infinit energys make sense
sourcepub unsafe fn metropolis_while_unsafe<Energy, Mes, Cond>(
&mut self,
energy_fn: Energy,
measure: Mes,
condition: Cond
)where
Energy: FnMut(&mut E) -> Option<T>,
Mes: FnMut(&mut Metropolis<E, R, S, Res, T>),
Cond: FnMut(&mut Metropolis<E, R, S, Res, T>) -> bool,
pub unsafe fn metropolis_while_unsafe<Energy, Mes, Cond>(
&mut self,
energy_fn: Energy,
measure: Mes,
condition: Cond
)where
Energy: FnMut(&mut E) -> Option<T>,
Mes: FnMut(&mut Metropolis<E, R, S, Res, T>),
Cond: FnMut(&mut Metropolis<E, R, S, Res, T>) -> bool,
Metropolis simulation
- almost the same as
metropolis_while
Difference
energy_fn
now works with a mutable reference ofE
(the ensemble).
Note
- prefere
metropolis_while
as it is safer. - the changeing of the Ensemble must not affect subsequent Energy calculations - otherwise the logic of the algorithm breaks down
Safety
- Note: I chose to make this function unsafe to force users to aknowledge the (purely logical) limitations regarding the usage of the mutable ensemble. From a programming point of view this will not lead to any undefined behavior or such regardless of if the user fullfills the requirements
sourcepub fn metropolis_efficient_while<Energy, Mes, Cond>(
&mut self,
energy_fn: Energy,
measure: Mes,
condition: Cond
)where
Energy: FnMut(&E, T, &[S]) -> Option<T>,
Mes: FnMut(&Metropolis<E, R, S, Res, T>),
Cond: FnMut(&Metropolis<E, R, S, Res, T>) -> bool,
pub fn metropolis_efficient_while<Energy, Mes, Cond>(
&mut self,
energy_fn: Energy,
measure: Mes,
condition: Cond
)where
Energy: FnMut(&E, T, &[S]) -> Option<T>,
Mes: FnMut(&Metropolis<E, R, S, Res, T>),
Cond: FnMut(&Metropolis<E, R, S, Res, T>) -> bool,
Metropolis simulation
- similar to
metropolis_while
Difference
- energy fn can use the old energy and the performed markov steps to more efficiently calculate the current Energy
sourcepub unsafe fn metropolis_efficient_while_unsafe<Energy, Mes, Cond>(
&mut self,
energy_fn: Energy,
measure: Mes,
condition: Cond
)where
Energy: FnMut(&mut E, T, &[S]) -> Option<T>,
Mes: FnMut(&mut Metropolis<E, R, S, Res, T>),
Cond: FnMut(&mut Metropolis<E, R, S, Res, T>) -> bool,
pub unsafe fn metropolis_efficient_while_unsafe<Energy, Mes, Cond>(
&mut self,
energy_fn: Energy,
measure: Mes,
condition: Cond
)where
Energy: FnMut(&mut E, T, &[S]) -> Option<T>,
Mes: FnMut(&mut Metropolis<E, R, S, Res, T>),
Cond: FnMut(&mut Metropolis<E, R, S, Res, T>) -> bool,
Metropolis simulation
- similar to
metropolis_efficient_while
Difference
- now
energy_fn
works with a mutable reference of the ensemble instead - This is intended for usages in which the energy can be calculated much more efficiently using a mutable reference than an immutable one
Safety
- Only use this, if it is absolutly nessessary. The ensemble must not be changed in a way, which affects successive energy calculations (or the markov steps)
Trait Implementations
sourceimpl<E, R, S, Res, T> Clone for Metropolis<E, R, S, Res, T>where
E: Clone,
R: Clone,
S: Clone,
Res: Clone,
T: Clone,
impl<E, R, S, Res, T> Clone for Metropolis<E, R, S, Res, T>where
E: Clone,
R: Clone,
S: Clone,
Res: Clone,
T: Clone,
sourcefn clone(&self) -> Metropolis<E, R, S, Res, T>
fn clone(&self) -> Metropolis<E, R, S, Res, T>
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moresourceimpl<'de, E, R, S, Res, T> Deserialize<'de> for Metropolis<E, R, S, Res, T>where
E: Deserialize<'de>,
R: Deserialize<'de>,
S: Deserialize<'de>,
T: Deserialize<'de>,
impl<'de, E, R, S, Res, T> Deserialize<'de> for Metropolis<E, R, S, Res, T>where
E: Deserialize<'de>,
R: Deserialize<'de>,
S: Deserialize<'de>,
T: Deserialize<'de>,
sourcefn deserialize<__D>(
__deserializer: __D
) -> Result<Metropolis<E, R, S, Res, T>, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D
) -> Result<Metropolis<E, R, S, Res, T>, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
sourceimpl<E, R, S, Res, T> HasRng<R> for Metropolis<E, R, S, Res, T>where
R: Rng,
impl<E, R, S, Res, T> HasRng<R> for Metropolis<E, R, S, Res, T>where
R: Rng,
sourceimpl<E, R, S, Res, T> Serialize for Metropolis<E, R, S, Res, T>where
E: Serialize,
R: Serialize,
S: Serialize,
T: Serialize,
impl<E, R, S, Res, T> Serialize for Metropolis<E, R, S, Res, T>where
E: Serialize,
R: Serialize,
S: Serialize,
T: Serialize,
sourcefn serialize<__S>(
&self,
__serializer: __S
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
Serialize this value into the given Serde serializer. Read more
Auto Trait Implementations
impl<E, R, S, Res, T> RefUnwindSafe for Metropolis<E, R, S, Res, T>where
E: RefUnwindSafe,
R: RefUnwindSafe,
Res: RefUnwindSafe,
S: RefUnwindSafe,
T: RefUnwindSafe,
impl<E, R, S, Res, T> Send for Metropolis<E, R, S, Res, T>where
E: Send,
R: Send,
Res: Send,
S: Send,
T: Send,
impl<E, R, S, Res, T> Sync for Metropolis<E, R, S, Res, T>where
E: Sync,
R: Sync,
Res: Sync,
S: Sync,
T: Sync,
impl<E, R, S, Res, T> Unpin for Metropolis<E, R, S, Res, T>where
E: Unpin,
R: Unpin,
Res: Unpin,
S: Unpin,
T: Unpin,
impl<E, R, S, Res, T> UnwindSafe for Metropolis<E, R, S, Res, T>where
E: UnwindSafe,
R: UnwindSafe,
Res: UnwindSafe,
S: UnwindSafe,
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<S, T> CastFloat<T> for Swhere
T: ConvFloat<S>,
impl<S, T> CastFloat<T> for Swhere
T: ConvFloat<S>,
fn cast_trunc(self) -> T
fn cast_trunc(self) -> T
Cast to integer, truncating Read more
fn cast_nearest(self) -> T
fn cast_nearest(self) -> T
Cast to the nearest integer Read more
fn cast_floor(self) -> T
fn cast_floor(self) -> T
Cast the floor to an integer Read more
fn try_cast_trunc(self) -> Result<T, Error>
fn try_cast_trunc(self) -> Result<T, Error>
Try converting to integer with truncation Read more
fn try_cast_nearest(self) -> Result<T, Error>
fn try_cast_nearest(self) -> Result<T, Error>
Try converting to the nearest integer Read more
fn try_cast_floor(self) -> Result<T, Error>
fn try_cast_floor(self) -> Result<T, Error>
Try converting the floor to an integer Read more
fn try_cast_ceil(self) -> Result<T, Error>
fn try_cast_ceil(self) -> Result<T, Error>
Try convert the ceiling to an integer Read more