Markov Chains and Hidden Markov Models (lib601.markov)¶
Tools for working with Markov Chains and Hidden Markov Models
-
class
lib601.markov.HMM(initial_distribution, transition_model, observation_model)¶ Class for specifying a hidden Markov model
Parameters: - initial_distribution – \(\Pr(S_0)\), the initial distribution
over states, represented as a
lib601.dist.DDist - transition_model – \(\Pr(S_{t+1}~|~S_t)\) represented as a
procedure that takes an old state and
returns a
lib601.dist.DDistover new states. - observation_model – \(\Pr(O_t~|~S_t)\) represented as
a procedure that takes a state and
returns a
lib601.dist.DDistover observations that can be made if the system is in that state.
Variables: - initial_distribution – The value passed in as
initial_distribution - transition_model – The value passed in as
transition_model - observation_model – The value passed in as
observation_model
-
make_simulator()¶ Returns: An instance of HMMSimulatorfor simulating the behavior of this HMM
-
make_state_estimator()¶ Returns: An instance of StateEstimatorfor performing Bayesian State Estimation using the models described by this HMM
- initial_distribution – \(\Pr(S_0)\), the initial distribution
over states, represented as a
-
class
lib601.markov.HMMSimulator(hmm)¶ Class for simulating the behavior of some system whose behavior is described by a Hidden Markov Model.
-
initialize()¶ Randomly selects and element from C{initial_distribution} and stores the result in the instance variable L{state}. Returns C{None}.
-
make_observation()¶ Returns an observation randomly drawn from P(O_t | S_t), where S_t is the internal state of the system (stored in the instance variable L{state}).
-
transition()¶ Updates the instance variable L{state} by randomly selecting an element from P(S_(t+1) | S_(t)). Returns C{None}.
-
state= None¶ Holds the “current” state of the system (will have value C{None} until L{initialize} is called, and will be updated by L{transition}).
-
-
class
lib601.markov.MarkovChain(initial_distribution, transition_model)¶ Class for describing a Markov chain and computing some useful properties of it.
Parameters: - initial_distribution – \(\Pr(S_0)\), the initial distribution
over states, represented as an instance of
lib601.dist.DDist - transition_model – \(\Pr(S_{t+1}~|~S_t)\) represented
as a procedure that takes an old state
and returns a
lib601.dist.DDistover new states.
Variables: - initial_distribution – The value passed in as
initial_distribution - transition_model – The value passed in as
transition_model
-
make_simulator()¶ Returns: An instance of MarkovChainSimulatorfor simulating the behavior of this Markov Chain
-
occupation_dist(T)¶ Returns a
lib601.dist.DDistover states at timeTParameters: T – time step
-
sample_sequence(T)¶ Returns a sequence of length
T, drawn from the distribution over sequences
-
state_sequence_prob(seq)¶ Returns the probability of a sequence of states in this Markov Chain
Parameters: seq – list of states \([s_0, s_1, \ldots, s_T]\)
- initial_distribution – \(\Pr(S_0)\), the initial distribution
over states, represented as an instance of
-
class
lib601.markov.MarkovChainSimulator(mc)¶ Class for simulating the evolution of a system described by a Markov chain
Parameters: mc – The instance of
MarkovChainto be simulated.Variables: - mc – The instance of
MarkovChainthat was passed in asmc. - state – Holds the “current” state of the system (will have value
Noneuntilinitialize()is called, and will be updated bytransition()).
-
initialize()¶ Randomly selects and element from
self.mc.initial_distributionand stores the result in the instance variablestate. ReturnsNone.
-
transition()¶ Updates the instance variable
stateby randomly selecting an element from \(\Pr(S_{t+1}~|~S_t=\mathtt{self.state})\). Also returns the new state.
- mc – The instance of
-
class
lib601.markov.StateEstimator(hmm)¶ Class for estimating the state of some underlying system whose behavior is described by a Hidden Markov Model.
Parameters: hmm – An instance of
HMMdescribing the underlying systemVariables: - hmm – The value passed in as
hmm - belief – The “current” belief, as a
lib601.dist.DDistover states. This variable is initialized tohmm.initial_distributionand is updated by callingreset(),transition(),observe(), orupdate().
-
observe(obs)¶ Update the belief based on making an observation. Changes
beliefbut does not return anything.Parameters: obs – The observation that was made Returns: None
-
reset()¶ Reset the estimator so that the instance variable
beliefis equal to the initial belief.
-
transition()¶ Update the belief following a transition. Changes
beliefbut does not return anything.Returns: None
-
update(obs)¶ Update the belief based on making an observation and then transitioning. Changes
beliefbut does not return anything. Same effect astransition()followed byobserve(), but more efficient.Parameters: obs – The observation that was made Returns: None
- hmm – The value passed in as