Instance Methods
- class pydtmc.HiddenMarkovModel[source]
- decode(symbols, initial_status=None, use_scaling=True)[source]
The method calculates the log probability, the posterior probabilities, the backward probabilities and the forward probabilities of an observed sequence of symbols.
Notes:If the observed sequence of symbols cannot be decoded, then
None
is returned.
- Parameters:
symbols (
Union
[List
[int
],List
[str
]]) – the observed sequence of symbols.initial_status (
Optional
[Union
[int
,str
,ndarray
,spmatrix
]]) – the initial state or the initial distribution of the states (if omitted, the states are assumed to be uniformly distributed).use_scaling (
bool
) – a boolean indicating whether to return scaled backward and forward probabilities together with their scaling factors.
- Raises:
ValidationError
– if any input argument is not compliant.- Return Type:
Optional
[Tuple
[float
,ndarray
,ndarray
,ndarray
,Optional
[ndarray
]]]
- emission_probability(symbol, state)[source]
The method computes the probability of a given symbol, conditioned on the process being at a given state.
- next(initial_state, target='both', output_index=False, seed=None)[source]
The method simulates a single step in a random walk.
- Parameters:
- Raises:
ValidationError
– if any input argument is not compliant.- Return Type:
- predict(prediction_type, symbols, initial_status=None, output_indices=False)[source]
The method calculates the log probability and the most probable states path of an observed sequence of symbols.
Notes:If the maximum a posteriori prediction is used and the observed sequence of symbols cannot be decoded, then
None
is returned.If the maximum likelihood prediction is used and the observed sequence of symbols produces null transition probabilities, then
None
is returned.
- Parameters:
prediction_type (
str
)map for the maximum a posteriori prediction;
mle or viterbi for the maximum likelihood prediction.
symbols (
Union
[List
[int
],List
[str
]]) – the observed sequence of symbols.initial_status (
Optional
[Union
[int
,str
,ndarray
,spmatrix
]]) – the initial state or the initial distribution of the states (if omitted, the states are assumed to be uniformly distributed).output_indices (
bool
) – a boolean indicating whether to output the state indices.
- Raises:
ValidationError
– if any input argument is not compliant.- Return Type:
- restrict(states=None, symbols=None)[source]
The method returns a submodel restricted to the given states and symbols.
Notes:Submodel transition and emission matrices are normalized so that their rows sum to 1.0.
Submodel transition and emission matrices whose rows sum to 0.0 are replaced by uniformly distributed probabilities.
- Parameters:
- Raises:
ValidationError
– if any input argument is not compliant.- Return Type:
- simulate(steps, initial_state=None, final_state=None, final_symbol=None, output_indices=False, seed=None)[source]
The method simulates a random sequence of states and symbols of the given number of steps.
- Parameters:
steps (
int
) – the number of steps.initial_state (
Optional
[Union
[int
,str
]]) – the initial state (if omitted, it is chosen uniformly at random).final_state (
Optional
[Union
[int
,str
]]) – the final state of the simulation (if specified, the simulation stops as soon as it is reached even if not all the steps have been performed).final_symbol (
Optional
[Union
[int
,str
]]) – the final state of the simulation (if specified, the simulation stops as soon as it is reached even if not all the steps have been performed).output_indices (
bool
) – a boolean indicating whether to output the state indices.seed (
Optional
[int
]) – a seed to be used as RNG initializer for reproducibility purposes.
- Raises:
ValidationError
– if any input argument is not compliant.- Return Type:
Union
[Tuple
[List
[int
],List
[int
]],Tuple
[List
[str
],List
[str
]]]
- to_dictionary()[source]
The method returns a dictionary representing the hidden Markov model.
- to_file(file_path)[source]
The method writes a hidden Markov model to the given file.
Only csv, json, txt and xml files are supported; data format is inferred from the file extension.- Parameters:
file_path (
Union
[str
,Path
]) – the location of the file in which the hidden Markov model must be written.- Raises:
OSError
– if the file cannot be written.ValidationError
– if any input argument is not compliant.
- to_graph()[source]
The method returns a directed graph representing the hidden Markov model.
- Return Type:
- to_matrices()[source]
The method returns a tuple of two items representing the underlying matrices of the hidden Markov model.
The first item is the transition matrix and the second item is the emission matrix.
- transition_probability(state_target, state_origin)[source]
The method computes the probability of a given state, conditioned on the process being at a given state.