brainstorming
brainstorming
You MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).
fit-hidden-markov-modelFit hidden Markov models using the Baum-Welch (EM) algorithm with model selection, Viterbi decoding for state sequences, and forward-backward probabilities. Use when observations are generated by unobservable latent states, you need to segment a time series into latent regimes (market regimes, speech phonemes, biological sequences), compute sequence probabi…
Independent GitHub snapshot · Automated classification · Original documentation governs
以 Baum-Welch(EM)擬 HMM,以信準擇模、以 Viterbi 解態序、算前後概。
| Input | Type | Description |
|---|---|---|
observations | sequence/matrix | Observed data sequence (univariate or multivariate) |
n_hidden_states | integer | Number of hidden states to fit (or a range for model selection) |
emission_type | string | Distribution family for emissions: "gaussian", "discrete", "poisson", "multinomial" |
| Input | Type | Default | Description |
|---|---|---|---|
initial_params | dict | random/heuristic | Initial transition matrix, emission parameters, and start probabilities |
n_restarts | integer | 10 | Number of random restarts to mitigate local optima |
max_iterations | integer | 500 | Maximum EM iterations per restart |
convergence_tol | float | 1e-6 | Log-likelihood convergence threshold for EM |
state_range | list of ints | [n_hidden_states] | Range of state counts for model selection |
covariance_type | string | "full" | For Gaussian emissions: "full", "diagonal", "spherical" |
regularization | float | 1e-6 | Small constant added to diagonal of covariance matrices to prevent singularity |
1.1. 定隱態數 K(或五步模擇候域)
1.2. 依數類擇射分佈族:
1.3. 定模件:
A 大 K x K:A[i,j] = P(z_t = j | z_{t-1} = i)theta_k 各態 k:分佈特(如 Gaussian 之均與協)pi:pi[k] = P(z_1 = k)1.4. 驗察數正格:序無缺、維一致、長對參足 T >> K^2
得:明設 HMM 架含 K 態、所擇射族、清察數長 T >> K^2。
敗:數含缺→填或去影段。T 對 K 過小→減 K 或增數。
2.1. 各 n_restarts 生初參:
2.2. 首啟用 K-means 啟(常最強始)。後啟用隨擾
2.3. 驗諸初參有效:
得:n_restarts 組有效初參,至少一數驅。
敗:K-means 不收→用純隨啟加啟數。協矩奇→加正則於對角。
3.1. E 步(前後算):
alpha[t,k] = P(o_1,...,o_t, z_t=k | model) 遞:
alpha[1,k] = pi[k] * b_k(o_1)alpha[t,k] = sum_j(alpha[t-1,j] * A[j,k]) * b_k(o_t)beta[t,k] = P(o_{t+1},...,o_T | z_t=k, model):
beta[T,k] = 1beta[t,k] = sum_j(A[k,j] * b_j(o_{t+1}) * beta[t+1,j])gamma[t,k] = P(z_t=k | O, model):
gamma[t,k] = alpha[t,k] * beta[t,k] / P(O | model)xi[t,i,j] = P(z_t=i, z_{t+1}=j | O, model)3.2. M 步(再估參):
A[i,j] = sum_t(xi[t,i,j]) / sum_t(gamma[t,i])mu_k = sum_t(gamma[t,k] * o_t) / sum_t(gamma[t,k])b_k(v) = sum_t(gamma[t,k] * I(o_t=v)) / sum_t(gamma[t,k])pi[k] = gamma[1,k]3.3. 算對數似然:log P(O | model) = log sum_k(alpha[T,k])。用 log-sum-exp 避下溢
3.4. 尺:於長序用尺之前後變避數下溢。各時步規 alpha 並累對數尺因子
3.5. 復 E 與 M 至對數似然變 < convergence_tol 或達 max_iterations
3.6. 跨諸啟,留末對數似然最高之參組
得:對數似然於迭間單調非減,於 max_iterations 內收。末參有效(機矩、正定協)。
敗:對數似然減→E 或 M 有蟲——驗式。收極慢→試善啟或增 max_iterations。協奇→增正則。
4.1. 初 Viterbi 變:
delta[1,k] = log(pi[k]) + log(b_k(o_1))psi[1,k] = 0(無先)4.2. t = 2,...,T 前遞:
delta[t,k] = max_j(delta[t-1,j] + log(A[j,k])) + log(b_k(o_t))psi[t,k] = argmax_j(delta[t-1,j] + log(A[j,k]))4.3. 終:
z*_T = argmax_k(delta[T,k])max_k(delta[T,k])4.4. t = T-1,...,1 反追:
z*_t = psi[t+1, z*_{t+1}]4.5. 出所解態序 z* = (z*_1, ..., z*_T) 與其對數概
4.6. 較 Viterbi 路概於前算總序概以估最佳路主度
得:長 T 之單最可能態序,各項於 {1,...,K}。Viterbi 對數概宜 <= 總對數似然。
敗:Viterbi 路對數概為負無窮→某轉或射概於不應處為零。加底值避 log(0)。
5.1. 各 state_range 中候 K 擬全 HMM(二至四步)
5.2. 算自參數 p:
K * (K - 1)(各行為單純)d 維全協 Gaussian:K * (d + d*(d+1)/2))K - 15.3. 算信準:
BIC = -2 * log_likelihood + p * log(T)AIC = -2 * log_likelihood + 2 * pAICc = AIC + 2*p*(p+1) / (T - p - 1)(小樣正)5.4. 擇最低 BIC(偏一致)或 AIC(偏預)之模。報兩者
5.5. 列果:各 K 示對數似然、參數、BIC、AIC、收態
5.6. 最優 K 於 state_range 邊→擴域重擬
得:BIC/AIC 明最低識最優隱態數。所擇模已收且有可釋之態義。
敗:無明最低(BIC 單減)→模或誤設——考異射族。諸模對數似然劣→數或不循 HMM 構。
6.1. 分數為訓與驗(如 80/20 或用多序若有)
6.2. 訓擬模。以前算於留算對數似然(勿重擬參)
6.3. 後解(Viterbi 之替):
z^_t = argmax_k(gamma[t,k])6.4. 較 Viterbi 與後解:
6.5. 估態可釋:
A 對角隱)合理6.6. 算每察之留對數似然並跨模階較以確訓集模擇
得:留對數似然近訓對數似然(無重過擬)。Viterbi 與後解於 90%+ 時步同意。態有明可釋之射分。
敗:留似然遠劣於訓→模過擬——減 K 或增正則。態不可釋→試異啟或異射族。
P(O) = sum_k(alpha[T,k]) = sum_k(pi[k] * b_k(o_1) * beta[1,k])O(K + d^2) 參。模擇用 BIC(非僅似然)並於留數驗