/usr/lib/python2.7/dist-packages/csb/statistics/samplers/mc/__init__.py is in python-csb 1.2.3+dfsg-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 | """
Abstract Monte Carlo samplers.
"""
import numpy.random
import csb.numeric
import csb.core
from abc import ABCMeta, abstractmethod, abstractproperty
from csb.statistics.samplers import AbstractSampler, AbstractState, State, EnsembleState
class AbstractMC(AbstractSampler):
"""
Abstract Monte Carlo sampler class. Subclasses implement various
Monte carlo equilibrium sampling schemes.
@param state: Initial state
@type state: L{AbstractState}
"""
__metaclass__ = ABCMeta
def __init__(self, state):
self._state = None
self.state = state
def _checkstate(self, state):
if not isinstance(state, AbstractState):
raise TypeError(state)
@abstractproperty
def energy(self):
"""
Energy of the current state.
"""
pass
@property
def state(self):
"""
Current state.
"""
return self._state
@state.setter
def state(self, value):
self._checkstate(value)
self._state = value
@abstractmethod
def sample(self):
"""
Draw a sample.
@rtype: L{AbstractState}
"""
pass
class AbstractPropagationResult(object):
"""
Abstract class providing the interface for the result
of a deterministic or stochastic propagation of a state.
"""
__metaclass__ = ABCMeta
@abstractproperty
def initial(self):
"""
Initial state
"""
pass
@abstractproperty
def final(self):
"""
Final state
"""
pass
@abstractproperty
def heat(self):
"""
Heat produced during propagation
@rtype: float
"""
pass
class PropagationResult(AbstractPropagationResult):
"""
Describes the result of a deterministic or stochastic
propagation of a state.
@param initial: Initial state from which the
propagation started
@type initial: L{State}
@param final: Final state in which the propagation
resulted
@type final: L{State}
@param heat: Heat produced during propagation
@type heat: float
"""
def __init__(self, initial, final, heat=0.0):
if not isinstance(initial, AbstractState):
raise TypeError(initial)
if not isinstance(final, AbstractState):
raise TypeError(final)
self._initial = initial
self._final = final
self._heat = None
self.heat = heat
def __iter__(self):
return iter([self._initial, self.final])
@property
def initial(self):
return self._initial
@property
def final(self):
return self._final
@property
def heat(self):
return self._heat
@heat.setter
def heat(self, value):
self._heat = float(value)
class Trajectory(csb.core.CollectionContainer, AbstractPropagationResult):
"""
Ordered collection of states, representing a phase-space trajectory.
@param items: list of states defining a phase-space trajectory
@type items: list of L{AbstractState}
@param heat: heat produced during the trajectory
@type heat: float
@param work: work produced during the trajectory
@type work: float
"""
def __init__(self, items, heat=0.0, work=0.0):
super(Trajectory, self).__init__(items, type=AbstractState)
self._heat = heat
self._work = work
@property
def initial(self):
return self[0]
@property
def final(self):
return self[self.last_index]
@property
def heat(self):
return self._heat
@heat.setter
def heat(self, value):
self._heat = float(value)
@property
def work(self):
return self._work
@work.setter
def work(self, value):
self._work = float(value)
class TrajectoryBuilder(object):
"""
Allows to build a Trajectory object step by step.
@param heat: heat produced over the trajectory
@type heat: float
@param work: work produced during the trajectory
@type work: float
"""
def __init__(self, heat=0.0, work=0.0):
self._heat = heat
self._work = work
self._states = []
@staticmethod
def create(full=True):
"""
Trajectory builder factory.
@param full: if True, a TrajectoryBuilder instance designed
to build a full trajectory with initial state,
intermediate states and a final state. If False,
a ShortTrajectoryBuilder instance designed to
hold only the initial and the final state is
returned
@type full: boolean
"""
if full:
return TrajectoryBuilder()
else:
return ShortTrajectoryBuilder()
@property
def product(self):
"""
The L{Trajectory} instance build by a specific instance of
this class
"""
return Trajectory(self._states, heat=self._heat, work=self._work)
def add_initial_state(self, state):
"""
Inserts a state at the beginning of the trajectory
@param state: state to be added
@type state: L{State}
"""
self._states.insert(0, state.clone())
def add_intermediate_state(self, state):
"""
Adds a state to the end of the trajectory
@param state: state to be added
@type state: L{State}
"""
self._states.append(state.clone())
def add_final_state(self, state):
"""
Adds a state to the end of the trajectory
@param state: state to be added
@type state: L{State}
"""
self._states.append(state.clone())
class ShortTrajectoryBuilder(TrajectoryBuilder):
def add_intermediate_state(self, state):
pass
@property
def product(self):
"""
The L{PropagationResult} instance built by a specific instance of
this class
"""
if len(self._states) != 2:
raise ValueError("Can't create a product, two states required")
initial, final = self._states
return PropagationResult(initial, final, heat=self._heat)
class MCCollection(csb.core.BaseCollectionContainer):
"""
Collection of single-chain samplers.
@param items: samplers
@type items: list of L{AbstractSingleChainMC}
"""
def __init__(self, items):
from csb.statistics.samplers.mc.singlechain import AbstractSingleChainMC
super(MCCollection, self).__init__(items, type=AbstractSingleChainMC)
def augment_state(state, temperature=1.0, mass_matrix=None):
"""
Augments a state with only positions given by momenta drawn
from the Maxwell-Boltzmann distribution.
@param state: State to be augmented
@type state: L{State}
@param temperature: Temperature of the desired Maxwell-Boltzmann
distribution
@type temperature: float
@param mass_matrix: Mass matrix to be used in the Maxwell-Boltzmann
distribution; None defaults to a unity matrix
@type mass_matrix: L{InvertibleMatrix}
@return: The initial state augmented with momenta
@rtype: L{State}
"""
d = len(state.position)
mm_unity = None
if mass_matrix is None:
mm_unity = True
if mm_unity == None:
mm_unity = mass_matrix.is_unity_multiple
if mm_unity == True:
momentum = numpy.random.normal(scale=numpy.sqrt(temperature),
size=d)
else:
covariance_matrix = temperature * mass_matrix
momentum = numpy.random.multivariate_normal(mean=numpy.zeros(d),
cov=covariance_matrix)
state.momentum = momentum
return state
|