Static Methods

class pydtmc.HiddenMarkovModel[source]
estimate(possible_states, possible_symbols, sequence_states, sequence_symbols)[source]

The method performs the maximum likelihood estimation of transition and emission probabilities from an observed sequence of states and symbols.

Parameters:
  • possible_states (List[str]) – the possible states of the model.

  • possible_symbols (List[str]) – the possible symbols of the model.

  • sequence_states (Union[List[int], List[str]]) – the observed sequence of states.

  • sequence_symbols (Union[List[int], List[str]]) – the observed sequence of symbols.

Raises:

ValidationError – if any input argument is not compliant.

Return Type:

HiddenMarkovModel

fit(fitting_type, possible_states, possible_symbols, p_guess, e_guess, symbols, initial_status=None)[source]

The method fits a hidden Markov model from an initial guess and one or more observed sequences of symbols.

Parameters:
  • fitting_type (str)

    • baum-welch for the Baum-Welch fitting;

    • map for the maximum a posteriori fitting;

    • mle or viterbi for the maximum likelihood fitting.

  • possible_states (List[str]) – the possible states of the model.

  • possible_symbols (List[str]) – the possible symbols of the model.

  • p_guess (ndarray) – the initial transition matrix guess.

  • e_guess (ndarray) – the initial emission matrix guess.

  • symbols (Union[List[int], List[str], List[List[int]], List[List[str]]]) – the observed sequence(s) 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).

Raises:
Return Type:

HiddenMarkovModel

from_dictionary(d)[source]

The method generates a hidden Markov model from the given dictionary, whose keys represent state pairs and whose values represent transition probabilities.

Parameters:

d (Dict[Tuple[str, str, str], Union[float, int]]) – the dictionary to transform into the transition matrix.

Raises:
  • ValidationError – if any input argument is not compliant.

  • ValueError – if the transition matrix defined by the dictionary is not valid.

Return Type:

HiddenMarkovModel

from_file(file_path)[source]

The method reads a hidden Markov model from the given file.

Only csv, json, txt and xml files are supported; data format is inferred from the file extension.
Transition probabilities are associated to reference attribute “P”, emission probabilities are associated to reference attribute “E”.
In csv files, data must be structured as follows:
  • Delimiter: comma

  • Quoting: minimal

  • Quote Character: double quote

  • Header Row: state names (prefixed with “P_”) and symbol names (prefixed with “E_”)

  • Data Rows: probabilities

In json files, data must be structured as an array of objects with the following properties:
  • reference (string)

  • element_from (string)

  • element_to (string)

  • probability (float or int)

In txt files, every line of the file must have the following format:
  • <reference> <element_from> <element_to> <probability>

In xml files, the structure must be defined as follows:
  • Root Element: HiddenMarkovModel

  • Child Elements: Item, with attributes:

    • reference (string)

    • element_from (string)

    • element_to (string)

    • probability (float or int)

Parameters:

file_path (Union[str, Path]) – the location of the file that defines the hidden Markov model.

Raises:
Return Type:

HiddenMarkovModel

from_graph(graph)[source]

The method generates a hidden Markov model from the given directed graph, whose transition and emission matrices are obtained through the normalization of edge weights.

Raises:

ValidationError – if any input argument is not compliant.

Return Type:

HiddenMarkovModel

from_matrices(mp, me, states=None, symbols=None)[source]

The method generates a hidden Markov model whose transition and emission matrices are obtained through the normalization of the given matrices.

Parameters:
  • mp (Union[ndarray, spmatrix]) – the matrix to transform into the transition matrix.

  • me (Union[ndarray, spmatrix]) – the matrix to transform into the emission matrix.

  • states (Optional[List[str]]) – the name of each state (if omitted, an increasing sequence of integers starting at 1 with prefix P).

  • symbols (Optional[List[str]]) – the name of each symbol (if omitted, an increasing sequence of integers starting at 1 with prefix E).

Raises:

ValidationError – if any input argument is not compliant.

Return Type:

HiddenMarkovModel

random(n, k, states=None, p_zeros=0, p_mask=None, symbols=None, e_zeros=0, e_mask=None, seed=None)[source]

The method generates a Markov chain of given size with random transition probabilities.

Notes:
  • In the mask parameter, undefined transition probabilities are represented by NaN values.

Parameters:
  • n (int) – the number of states.

  • k (int) – the number of symbols.

  • states (Optional[List[str]]) – the name of each state (if omitted, an increasing sequence of integers starting at 1 with prefix P).

  • p_zeros (int) – the number of null transition probabilities.

  • p_mask (Optional[Union [ndarray, spmatrix]]) – a matrix representing locations and values of fixed transition probabilities.

  • symbols (Optional[List[str]]) – the name of each symbol (if omitted, an increasing sequence of integers starting at 1 with prefix E).

  • e_zeros (int) – the number of null emission probabilities.

  • e_mask (Optional[Union [ndarray, spmatrix]]) – a matrix representing locations and values of fixed emission probabilities.

  • 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:

HiddenMarkovModel