apollon.hmm.utilities module¶
- Functions:
assert_poisson_input Raise if array does not conform restrictions. assert_st_matrix Raise if array is not a stochastic matrix. assert_st_vector Raise if array is not a stochastic vector.
init_lambda_linear Init linearly between min and max. init_lambda_quantile Init regarding data quantiles. init_lambda_random Init with random samples from data range.
init_gamma_dirichlet Init using Dirichlet distribution. init_gamma_softmax Init with softmax of random floats. init_gamma_uniform Init with uniform distr over the main diagonal.
init_delta_dirichlet Init using Dirichlet distribution. init_delta_softmax Init with softmax of random floats. init_delta_stationary Init with stationary distribution. init_delta_uniform Init with uniform distribution.
stationary_distr Compute stationary distribution of tpm.
get_off_diag Return off-diagonal elements of square array. set_off_diag Set off-diagonal elements of square array. logit_gamma Transform tpm to logit space. expit_gamma Transform tpm back from logit space. sort_param Sort messed up gamma.
-
class
apollon.hmm.utilities.
StartDistributionInitializer
¶ Bases:
object
Initializes the start distribution of HMM.
-
static
dirichlet
(m: int, alpha: tuple) → numpy.ndarray¶ Initialize the initial distribution with a Dirichlet random sample.
- Parameters
m (int) –
alpha (iterable) –
- Returns
(np.ndarray) Stochastic vector of shape (m, ).
-
methods
= ('dirichlet', 'softmax', 'stationary', 'uniform')¶
-
static
softmax
(m: int) → numpy.ndarray¶ Initialize the initial distribution by applying softmax to a sample of random floats.
- Parameters
m (int) –
- Returns
(np.ndarray) Stochastic vector of shape (m, ).
-
static
stationary
(gamma_: numpy.ndarray) → numpy.ndarray¶ Initialize the initial distribution with the stationary distribution of init_gamma.
- Parameters
gamma (np.ndarray) –
- Returns
(np.ndarray) Stochastic vector of shape (m, ).
-
static
uniform
(m: int) → numpy.ndarray¶ Initialize the initial distribution uniformly. The initial values are set to the inverse of the number of states.
- Parameters
m (int) –
- Returns
(np.ndarray) Stochastic vector of shape (m, ).
-
static
-
class
apollon.hmm.utilities.
StateDependentMeansInitializer
¶ Bases:
object
Initializer methods for state-dependent vector of means.
-
static
hist
(data: numpy.ndarray, m_states: int) → numpy.ndarray¶ Initialize state-dependent means based on a histogram of
data
.The histogram is calculated with ten bins. The centers of the
m_states
most frequent bins are returned as estimates of lambda.- Parameters
data – Input data.
m_states – Number of states.
- Returns
Lambda estimates.
-
static
linear
(X: numpy.ndarray, m: int) → numpy.ndarray¶ Initialize state-dependent means with m linearily spaced values from [min(data), max(data)].
- Args:
X (np.ndarray) Input data. m (int) Number of states.
- Returns:
(np.ndarray) Initial state-dependent means of shape (m, ).
-
methods
= ('hist', 'linear', 'quantile', 'random')¶
-
static
quantile
(X: numpy.ndarray, m: int) → numpy.ndarray¶ Initialize state-dependent means with m equally spaced percentiles from data.
- Parameters
X (np.ndarray) –
m (int) –
- Returns
(np.ndarray) Initial state-dependent means of shape (m, ).
-
static
random
(X: numpy.ndarray, m: int) → numpy.ndarray¶ Initialize state-dependent means with random integers from [min(x), max(x)[.
- Parameters
X (np.ndarray) –
m (int) –
- Retruns:
(np.ndarray) Initial state-dependent means of shape (m, ).
-
static
-
class
apollon.hmm.utilities.
TpmInitializer
¶ Bases:
object
Initializes transition probability matrix.
-
static
dirichlet
(m: int, alpha: tuple) → numpy.ndarray¶ - Parameters
m (int) –
alpha (iterable) – Iterable of size m. Each entry controls the probability mass that is put on the respective transition.
- Returns
(np.ndarray) Transition probability matrix of shape (m, m).
-
methods
= ('dirichlet', 'softmax', 'uniform')¶
-
static
softmax
(m: int) → numpy.ndarray¶ Initialize init_gamma by applying softmax to a sample of random floats.
- Parameters
m (int) –
- Returns
(np.ndarray) Transition probability matrix of shape (m, m).
-
static
uniform
(m: int, diag: float) → numpy.ndarray¶ Fill the main diagonal of init_gamma with diag. Set the off-diagoanl elements to the proportion of the remaining probability mass and the remaining number of elements per row.
- Args:
m (int) Number of states. diag (float) Value on main diagonal in [0, 1].
- Returns:
(np.ndarray) Transition probability matrix of shape (m, m).
-
static
-
apollon.hmm.utilities.
assert_poisson_input
(X: numpy.ndarray)¶ Check wether data is a one-dimensional array of integer values. Otherwise raise an exception.
- Parameters
X (np.ndarray) –
- Raises
TypeError –
ValueError –
-
apollon.hmm.utilities.
assert_st_matrix
(arr: numpy.ndarray)¶ Raise if arr is not a valid two-dimensional stochastic matrix.
A stochastic matrix is a (1) two-dimensional, (2) quadratic matrix, with (3) elements from [0.0, 1.0] and (4) rows sums of exactly exactly 1.0.
- Parameters
arr (np.ndarray) –
- Raises
ValueError –
-
apollon.hmm.utilities.
assert_st_val
(val: float)¶ Check wheter val is suitable as element of stochastic matrix.
- Parameters
val (float) –
- Raises
TypeError –
ValueError –
-
apollon.hmm.utilities.
assert_st_vector
(vect: numpy.ndarray)¶ Raise if vect is not a valid one-dimensional stochastic vector.
- Parameters
vect (np.ndarray) –
- Raises
ValueError –
-
apollon.hmm.utilities.
expit_gamma
(lg_tpm: numpy.ndarray, m: int) → numpy.ndarray¶ Transform lg_tpm back from logit space.
- Parameters
lg_tpm (np.ndarray) –
m (int) –
- Returns
(np.ndarray) Transition probability matrix.
-
apollon.hmm.utilities.
get_off_diag
(mat: numpy.ndarray) → numpy.ndarray¶ Return the off-diagonal elements of square array.
- Parameters
mat (np.ndarray) –
- Returns
(np.ndarray) mat filled with values
- Raises
ValueError –
-
apollon.hmm.utilities.
logit_tpm
(tpm: numpy.ndarray) → numpy.ndarray¶ Transform tpm to logit space for unconstrained optimization.
Note: There must be no zeros on the main diagonal.
- Parameters
tpm (np.ndarray) –
- Returns
(np.nadarray) lg_tpm of shape (1, m**2-m).
-
apollon.hmm.utilities.
set_offdiag
(mat: numpy.ndarray, vals: numpy.ndarray)¶ Set all off-diagonal elements of square array to elements of values.
- Parameters
mat (np.ndarray) –
- Returns
vals (np.ndarray) values
- Raises
ValueError –
-
apollon.hmm.utilities.
sort_param
(m_key: numpy.ndarray, m_param: numpy.ndarray)¶ Sort one- or two-dimensional parameter array according to a unsorted 1-d array of distribution parameters.
In some cases the estimated distribution parameters are not in order. The transition probability matrix and the distribution parameters have then to be reorganized according to the array of sorted values.
- Parameters
m_key (np.ndarray) –
m_parma (np.ndarray) –
- Returns
(np.ndarray) Reordered parameter.
-
apollon.hmm.utilities.
stationary_distr
(tpm: numpy.ndarray) → numpy.ndarray¶ Calculate the stationary distribution of the transition probability matrix tpm.
- Parameters
tpm (np.ndarray) –
- Returns
(np.ndarray) Stationary distribution of shape (m, ).