coniii.samplers module¶
-
class
coniii.samplers.
HamiltonianMC
(n, theta, calc_e, random_sample, grad_e=None, dt=0.01, leapfrogN=20, nCpus=0)¶ Bases:
coniii.samplers.Sampler
-
generate_samples
(nSamples, nBurn=100, fast=True, x0=None)¶ Generate nSamples from this Hamiltonian starting from random initial conditions from each sample.
-
sample
(x0, nBurn, saveHistory=False)¶ Get a single sample by MC sampling from this Hamiltonian. Slow method
-
-
class
coniii.samplers.
Heisenberg3DSampler
(J, calc_e, random_sample)¶ Bases:
coniii.samplers.Sampler
Simple MC Sampling from Heisenberg model with a lot of helpful functions.
generate_samples() equilibrate_samples() sample_metropolis() sample_energy_min()
-
equilibrate_samples
(samples, n_iters, method='mc', nCpus=0)¶
-
generate_samples
(nSamples, n_iters=100, **kwargs)¶ sample_size : int
-
grad_E
(X)¶ Gradient wrt theta and phi.
- X : ndarray
- with dims (nSpins,2) with angles theta and phi
-
sample_energy_min
(nFixed=0, rng=RandomState(MT19937) at 0x7F956598E160, initialState=None, method='powell', **kwargs)¶ Find local energy minimum given state in angular form. Angular representation makes it easy to be explicit about constraints on the vectors.
- initialState : ndarray,None
- n_samples x n_features x 2
- nFixed : int,0
- Number of vectors that are fixed.
-
sample_metropolis
(oldState, E0)¶ - s : ndarray
- State to perturb randomly.
- energy : float
- Energy of configuration.
-
sample_nearby_sample
(X, **kwargs)¶ Randomly move given state around for new metropolis sample. Question is whether it is more efficient to push only one of the many vectors around or all of them simultaneously.
-
sample_nearby_vector
(v, nSamples=1, otheta=None, ophi=None, sigma=0.1)¶ Sample random vector that is nearby. It is important how you choose the width sigma. NOTE: code might be simplified by using arctan2 instead of arctan
- v : ndarray
- xyz vector about which to sample random vectors
- nSamples : int,1
- number of random samples
- otheta : float,None
- polar angle for v
- ophi : float,None
- azimuthal angle for v
- sigma : float,.1
- width of Gaussian about v
-
classmethod
to_dict
(data, names)¶ Convenience function taking 3d array of of samples and arranging them into n x 3 arrays in a dictionary.
-
-
class
coniii.samplers.
Metropolis
(n, theta, calc_e=None, n_cpus=None, rng=None, boost=True, iprint=True)¶ Bases:
coniii.samplers.Sampler
-
generate_cond_samples
(sample_size, fixed_subset, burn_in=1000, n_cpus=None, initial_sample=None, systematic_iter=False, parallel=True)¶ Generate samples from conditional distribution (while a subset of the spins are held fixed). Samples are generated in parallel.
NOTE: There is a bug with multiprocess where many calls to the parallel sampling routine in a row leads to increasingly slow evaluation of the code.
sample_size : int fixed_subset : list of duples
Each duple is the index of the spin and the value to fix it at. These should be ordered by spin index.- burn_in : int
- Burn in.
- n_cpus : int
- Number of cpus to use.
- initial_sample : ndarray
- Option to set initial random sample.
- systematic_iter : bool
- Iterate through spins systematically instead of choosing them randomly.
- parallel : bool
- If True, use parallelized routine.
- ndarray
- Samples from distribution.
- ndarray
- Energy of each sample.
-
generate_samples_boost
(sample_size, n_iters=1000, burn_in=None, systematic_iter=False)¶ Generate Metropolis samples using C++ and boost.
- sample_size : int
- Number of samples.
- n_iters : int, 1000
- Number of Metropolis iterations between samples.
- burn_in : int, None
- If not set, will be the same value as n_iters.
- systematic_iter : bool, False
- If True, iterate through each element of system by increment index by one.
- ndarray, optional
- Saved array of energies at each sampling step.
-
generate_samples_parallel_boost
(sample_size, n_iters=1000, burn_in=None, systematic_iter=False)¶ Generate samples in parallel. Each replica in self._samples runs on its own thread and a sample is generated every n_iters.
In order to control the random number generator, we pass in seeds that are samples from the class instance’s rng.
- sample_size : int
- Number of samples.
- n_iters : int, 1000
- Number of iterations between taking a random sample.
- burn_in : int, None
- If None, n_iters is used.
- systematic_iter : bool, False
- If True, iterate through spins systematically instead of choosing them randomly.
-
generate_samples_parallel_py
(sample_size, n_iters=1000, burn_in=None, initial_sample=None, systematic_iter=False)¶ Generate samples in parallel. Each replica in self._samples runs on its own thread and a sample is generated every n_iters.
In order to control the random number generator, we pass in seeds that are samples from the class instance’s rng.
- sample_size : int
- Number of samples.
- n_iters : int, 1000
- Number of iterations between taking a random sample.
- burn_in : int, None
- If None, n_iters is used.
- initial_sample : ndarray, None
- Starting set of replicas otherwise self._samples is used.
- systematic_iter : bool, False
- If True, iterate through spins systematically instead of choosing them randomly.
-
generate_samples_py
(sample_size, n_iters=1000, burn_in=None, systematic_iter=False, saveHistory=False, initial_sample=None)¶ Generate Metropolis samples using a for loop.
- sample_size : int
- Number of samples.
- n_iters : int, 1000
- Number of iterations to run the sampler floor.
- burn_in : int, None
- If None, n_iters is used.
- systematic_iter : bool, False
- If True, iterate through each element of system by increment index by one.
- saveHistory : bool, False
- If True, also save the energy of each sample at each sampling step.
- initial_sample : ndarray, None
- Start with this sample (i.e. to avoid warming up). Otherwise, self._samples is the initial sample.
- ndarray, optional
- Saved array of energies at each sampling step.
-
random_sample
(n_samples)¶
-
sample_metropolis
(sample0, E0, rng=None, flip_site=None, calc_e=None)¶ Metropolis sampling given an arbitrary energy function.
- sample0 : ndarray
- Sample to start with. Passed by ref and changed.
- E0 : ndarray
- Initial energy of state.
- rng : np.random.RandomState
- Random number generator.
- flip_site : int
- Site to flip.
- calc_e : function
- If another function to calculate energy should be used
- float
- delta energy.
-
update_parameters
(theta)¶
-
-
class
coniii.samplers.
ParallelTempering
(n, theta, calc_e, n_replicas, Tbds=(1.0, 3.0), sample_size=1000, replica_burnin=None, rep_ex_burnin=None, n_cpus=None, rng=None)¶ Bases:
coniii.samplers.Sampler
-
burn_and_exchange
(pool)¶ pool : mp.multiprocess.Pool
-
burn_in_replicas
(pool=None, close_pool=True, n_iters=None)¶ Run each replica separately.
pool : multiprocess.Pool, None close_pool : bool, True
If True, call pool.close() at end.- n_iters : int, None
- Default value is self.replicaBurnin.
-
generate_samples
(sample_size, save_exchange_trajectory=False)¶ Burn in, run replica exchange simulation, then sample.
- sample_size : int
- Number of samples to take for each replica.
- save_exchange_trajectory : bool, False
- If True, keep track of the location of each replica in beta space and return the history.
- ndarray, optional
- Trajectory of each replica through beta space. Each row is tells where each index is located in beta space.
-
static
initialize_beta
(b0, b1, n_replicas)¶ Use linear interpolation of temperature range.
-
static
iterate_beta
(beta, acceptance_ratio)¶ Apply algorithm from Hukushima but reversed to maintain one replica at T=1.
- beta : ndarray
- Inverse temperature.
- acceptance_ratio : ndarray
- Estimate of acceptance ratio.
- ndarray
- New beta.
-
optimize_beta
(n_samples, n_iters, tol=0.01, max_iter=10)¶ Find suitable temperature range for replicas. Sets self.beta.
- n_samples : int
- Number of samples to use to estimate acceptance ratio. Acceptance ratio is estimated as the average of these samples.
- n_iters : int
- Number of sampling iterations for each replica.
- tol : float, .1
- Average change in beta to reach before stopping.
- max_iter : int, 10
- Number of times to iterate algorithm for beta. Each iteration involves sampling from replicas.
-
setup_replicas
()¶ Initialise a set of replicas at different temperatures using the Metropolis algorithm and optimize the temperatures. Replicas are burned in and ready to sample.
-
update_replica_parameters
()¶ Update parameters for each replica. Remember that the parameters include the factor of beta.
-
-
class
coniii.samplers.
Potts3
(n, theta, calc_e=None, n_cpus=None, rng=None, boost=True)¶ Bases:
coniii.samplers.Metropolis
-
generate_samples_parallel_boost
(sample_size, n_iters=1000, burn_in=None, systematic_iter=False)¶ Generate samples in parallel. Each replica in self._samples runs on its own thread and a sample is generated every n_iters.
In order to control the random number generator, we pass in seeds that are samples from the class instance’s rng.
- sample_size : int
- Number of samples.
- n_iters : int, 1000
- Number of iterations between taking a random sample.
- burn_in : int, None
- If None, n_iters is used.
- systematic_iter : bool, False
- If True, iterate through spins systematically instead of choosing them randomly.
-
random_sample
(n_samples)¶
-
sample_metropolis
(sample0, E0, rng=None, flip_site=None, calc_e=None)¶ Metropolis sampling given an arbitrary sampling function.
- sample0 : ndarray
- Sample to start with. Passed by ref and changed.
- E0 : ndarray
- Initial energy of state.
- rng : np.random.RandomState
- Random number generator.
- flip_site : int
- Site to flip.
- calc_e : function
- If another function to calculate energy should be used
- float
- delta energy.
-
-
class
coniii.samplers.
SWIsing
(n, theta, calc_e, nCpus=None, rng=None)¶ Bases:
coniii.samplers.Sampler
-
generate_sample
(n_samples, n_iters, initial_state=None)¶ n_samples n_iters initial_state : ndarray,None
-
generate_sample_parallel
(n_samples, n_iters, initial_state=None, n_cpus=None)¶ n_samples n_iters initial_state : ndarray,None
-
get_clusters
(state)¶ Get a random sample of clusters.
-
one_step
(state)¶
-
print_cluster_size
(n_iters)¶
-
randomly_flip_clusters
(state, clusters)¶
-
-
class
coniii.samplers.
Sampler
(n, theta, **kwargs)¶ Bases:
object
Base class for MCMC sampling.
-
generate_samples
(sample_size, **kwargs)¶ sample_size : int
-
generate_samples_parallel
(sample_size, **kwargs)¶ sample_size : int
-
sample_metropolis
(s, energy)¶ - s : ndarray
- State to perturb randomly.
- energy : float
- Energy of configuration.
-
update_parameters
(new_parameters)¶
-
-
class
coniii.samplers.
WolffIsing
(J, h)¶ Bases:
coniii.samplers.Sampler
-
build_cluster
(state, initialsite)¶ Grow cluster from initial site.
-
find_neighbors
(state, site, alreadyMarked)¶ Return neighbors of given site that need to be visited excluding sites that have already been visited. This is the implementation of the Wolff algorithm for finding neighbors such that detailed balance is satisfied. I have modified to include random fields such tha the probability of adding a neighbors depends both on its coupling with the current site and the neighbor’s magnetic field.
state site alreadyMarked
-
generate_sample
(samplesize, n_iters, initialSample=None, save_history=False)¶ Generate samples by starting from random initial states.
-
generate_sample_parallel
(samplesize, n_iters, initialSample=None)¶ Generate samples by starting from random or given initial states.
-
one_step
(state, initialsite=None)¶ Run one iteration of the Wolff algorithm that involves finding a cluster and possibly flipping it.
-
update_parameters
(J, h)¶
-
-
coniii.samplers.
calc_e
¶ Heisenberg model.
- theta : ndarray
- List of couplings Jij
- x : ndarray
- List of angles (theta_0,phi_0,theta_1,phi_1,…,theta_n,phi_n)
-
coniii.samplers.
check_e_logp
(sample, calc_e)¶ Boltzmann type model with discrete state space should have E propto -logP. Calculate these quantities for comparison.
sample calc_e
-
coniii.samplers.
cross
¶ Calculate the cross product of two 3d vectors.
-
coniii.samplers.
cross_
¶ Calculate the cross product of two 3d vectors.
-
coniii.samplers.
grad_e
¶ Derivatives wrt the angles of the spins.
-
coniii.samplers.
grad_e_theta
¶ Derivatives wrt the couplings theta.
-
coniii.samplers.
iter_cluster
(adj)¶ Cycle through all spins to get clusters.
-
coniii.samplers.
iterate_neighbors
¶ Iterate through all neighbors of a particular site and see if a bond should be formed between them.
- n : int
- System size.
- ix : ndarray of bool
- Indices of sites that have already been visited.
- expdJ : ndarray
- np.exp( -2*state[:,None]*state[None,:]*J )
- r : ndarray
- Array of random numbers.
-
coniii.samplers.
jit_sample
¶ Get a single sample by MC sampling from this Hamiltonian.
- theta : ndarray
- Parameters
- x0 : ndarray
- Sample
nBurn : int dt : float leapfrogN : int randNormal : ndarray
nBurn x ndim- randUnif : ndarray
- nBurn
-
coniii.samplers.
jit_sample_nearby_vector
¶
-
coniii.samplers.
pairwise_prod
¶
-
coniii.samplers.
sample_bonds
¶ - p : ndarray
- Probability of bond formation.
- r : ndarray
- Random numbers.
state J
-
coniii.samplers.
sample_ising
(multipliers, n_samples, n_cpus=None, seed=None, generate_samples_kw={})¶ Easy way to Metropolis sample from Ising model.
- multipliers : ndarray
- N individual fields followed by N(N-1)/2 pairwise couplings.
- n_samples : int
- Number of samples to take.
- n_cpus : int, None
- Number of cpus to use. If more than one, multiprocessing invoked.
- seed : int, None
- Random number generator seed.
- generate_samples_kw : dict, {}
- Any extra arguments to send into Metropolis sampler. Default args are
- n_iters=1000 systematic_iter=False saveHistory=False initial_sample=None
- ndarray
- Matrix of dimensions (n_samples, n).
-
coniii.samplers.
spec_cluster
(L, exact=True)¶ - L : ndarray
- Graph Laplacian