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

returns stored m_beta value (-β for metropolis)

sets m_beta (minus beta). Is related to the temperature: m_beta = -1 / temperature

sets m_beta according to m_beta = -1 / temperature

returns stored value for current_energy

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

returns reference to ensemble

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
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)
resets the counter to 0
  • note: counter is a wrapping counter

return current stepsize

  • change the stepsize
  • returns err if you try to set stepsize to 0, because that would be invalid
Create a new Metropolis struct - used for Metropolis simulations
meaning
rngthe Rng used to decide, if a state should be accepted or rejected
ensemblethe ensemble that is explored with the markov chain
energycurrent energy of the ensemble - cannot be NAN, should match energy_fn(ensemble) (see metropolis* functions)
m_betaminus 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_sizeis used for each markov step, i.e., ensemble.m_steps(stepsize) is called
  • will return Err if energy is nan or m_beta is not finite
Create a new Metropolis struct - used for Metropolis simulations
meaning
rngthe Rng used to decide, if a state should be accepted or rejected
ensemblethe ensemble that is explored with the markov chain
energycurrent energy of the ensemble - cannot be NAN, should match energy_fn(ensemble) (see metropolis* functions)
temperaturem_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_sizeis used for each markov step, i.e., ensemble.m_steps(stepsize) is called
  • will return Err if energy is nan or m_beta is not finite
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
Metropolis Simulation
  • see
  • performs self.counter..=step_target markov steps
  • energy_fn(self.ensemble) is assumed to equal self.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 return None in that case
  • Maybe do the same for infinity, it is unlikely, that infinit energys make sense
Metropolis Simulation
  • see
  • performs self.counter..=step_target markov steps
  • energy_fn(self.ensemble) is assumed to equal self.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 measureneeds 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 return None 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
Metropolis Simulation
  • see
  • performs self.counter..=step_target markov steps
  • energy_fn(self.ensemble) is assumed to equal self.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 return None in that case
  • Maybe do the same for infinity, it is unlikely, that infinit energys make sense
Metropolis Simulation
  • see
  • performs self.counter..=step_target markov steps
  • energy_fn(self.ensemble) is assumed to equal self.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 return None in that case
  • Maybe do the same for infinity, it is unlikely, that infinite energys make sense
Metropolis Simulation
  • see
  • checks condition(self) after each metropolis_step(&mut energy_fn) and returns when false is returned by the condition
  • energy_fn(self.ensemble) is assumed to equal self.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 return None in that case
  • Maybe do the same for infinity, it is unlikely, that infinit energys make sense
Metropolis simulation
Difference
  • energy_fn now works with a mutable reference of E (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
Metropolis simulation
Difference
  • energy fn can use the old energy and the performed markov steps to more efficiently calculate the current Energy
Metropolis simulation
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

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Deserialize this value from the given Serde deserializer. Read more
Access RNG Read more
If you need to exchange the internal rng Read more
Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Cast from Self to T
Try converting from Self to T
Cast to integer, truncating Read more
Cast to the nearest integer Read more
Cast the floor to an integer Read more
Cast the ceiling to an integer Read more
Try converting to integer with truncation Read more
Try converting to the nearest integer Read more
Try converting the floor to an integer Read more
Try convert the ceiling to an integer Read more
Convert from T to Self
Try converting from T to Self

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The alignment of pointer.
The type for initializers.
Initializes a with the given initializer. Read more
Dereferences the given pointer. Read more
Mutably dereferences the given pointer. Read more
Drops the object pointed to by the given pointer. Read more
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.