Back to Skills

fit-hidden-markov-model

pjt222
Updated 6 days ago
17 views
17
2
17
View on GitHub
Metageneral

About

This skill fits Hidden Markov Models (HMMs) using the Baum-Welch algorithm for scenarios with unobservable latent states, like segmenting time series into regimes. It provides Viterbi decoding for the most likely state sequence and forward-backward probabilities. Use it for tasks such as market regime identification, speech phoneme segmentation, or comparing models with different numbers of hidden states.

Quick Install

Claude Code

Recommended
Primary
npx skills add pjt222/agent-almanac -a claude-code
Plugin CommandAlternative
/plugin add https://github.com/pjt222/agent-almanac
Git CloneAlternative
git clone https://github.com/pjt222/agent-almanac.git ~/.claude/skills/fit-hidden-markov-model

Copy and paste this command in Claude Code to install this skill

Documentation

擬隱 Markov 模

以 Baum-Welch(EM)擬 HMM,以信準擇模、以 Viterbi 解態序、算前後概。

  • 察射序而底生態不可察
  • 疑數源於有限制間切之系
  • 須分時序為潛段(如市制、語音素、生序注)
  • 欲算察序於生模下之概
  • 須予察下最可能隱態序(解)
  • 較異隱態數之模以擇最繁-擬衡

InputTypeDescription
observationssequence/matrixObserved data sequence (univariate or multivariate)
n_hidden_statesintegerNumber of hidden states to fit (or a range for model selection)
emission_typestringDistribution family for emissions: "gaussian", "discrete", "poisson", "multinomial"

InputTypeDefaultDescription
initial_paramsdictrandom/heuristicInitial transition matrix, emission parameters, and start probabilities
n_restartsinteger10Number of random restarts to mitigate local optima
max_iterationsinteger500Maximum EM iterations per restart
convergence_tolfloat1e-6Log-likelihood convergence threshold for EM
state_rangelist of ints[n_hidden_states]Range of state counts for model selection
covariance_typestring"full"For Gaussian emissions: "full", "diagonal", "spherical"
regularizationfloat1e-6Small constant added to diagonal of covariance matrices to prevent singularity

一:定隱態與察模

1.1. 定隱態數 K(或五步模擇候域)

1.2. 依數類擇射分佈族:

  • 續數:Gaussian(單或多)
  • 計數:Poisson 或負二項
  • 類數:離/多項

1.3. 定模件:

  • 轉矩 AK x KA[i,j] = P(z_t = j | z_{t-1} = i)
  • 射參 theta_k 各態 k:分佈特(如 Gaussian 之均與協)
  • 初態分 pipi[k] = P(z_1 = k)

1.4. 驗察數正格:序無缺、維一致、長對參足 T >> K^2

得:明設 HMM 架含 K 態、所擇射族、清察數長 T >> K^2

敗:數含缺→填或去影段。TK 過小→減 K 或增數。

二:初參

2.1. 各 n_restarts 生初參:

  • 轉矩:隨機機矩(各行自 Dirichlet 取)或微擾均矩
  • 射參:於察 K-means 聚以初均;算聚變供 Gaussian 射
  • 初分:均或比 K-means 聚大

2.2. 首啟用 K-means 啟(常最強始)。後啟用隨擾

2.3. 驗諸初參有效:

  • 轉矩諸行和為 1,諸項正
  • 射參於有效域(如協矩正定)
  • 初分和為 1

得:n_restarts 組有效初參,至少一數驅。

敗:K-means 不收→用純隨啟加啟數。協矩奇→加正則於對角。

三:行 Baum-Welch EM 以估參

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] = 1
    • beta[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])
  • 以權足統更射參:
    • Gaussian 均:mu_k = sum_t(gamma[t,k] * o_t) / sum_t(gamma[t,k])
    • Gaussian 協:權散矩加正則
    • 離: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。協奇→增正則。

四:以 Viterbi 解最可能態序

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)。

五:模擇(諸模階之 BIC/AIC)

5.1. 各 state_range 中候 K 擬全 HMM(二至四步)

5.2. 算自參數 p

  • 轉矩:K * (K - 1)(各行為單純)
  • 射參:依族(如 d 維全協 Gaussian:K * (d + d*(d+1)/2)
  • 初分:K - 1

5.3. 算信準:

  • BIC = -2 * log_likelihood + p * log(T)
  • AIC = -2 * log_likelihood + 2 * p
  • AICc = AIC + 2*p*(p+1) / (T - p - 1)(小樣正)

5.4. 擇最低 BIC(偏一致)或 AIC(偏預)之模。報兩者

5.5. 列果:各 K 示對數似然、參數、BIC、AIC、收態

5.6. 最優 Kstate_range 邊→擴域重擬

得:BIC/AIC 明最低識最優隱態數。所擇模已收且有可釋之態義。

敗:無明最低(BIC 單減)→模或誤設——考異射族。諸模對數似然劣→數或不循 HMM 構。

六:以留數與後解驗

6.1. 分數為訓與驗(如 80/20 或用多序若有)

6.2. 訓擬模。以前算於留算對數似然(勿重擬參)

6.3. 後解(Viterbi 之替):

  • 各時步賦最高後概之態:z^_t = argmax_k(gamma[t,k])
  • 此最大期正解態數(對 Viterbi 之最大聯路概)

6.4. 較 Viterbi 與後解:

  • 算兩解序間同意率
  • 不同意區示態賦歧

6.5. 估態可釋:

  • 察各態射參(均、變、離分)
  • 驗態應域境有意之制
  • 察態居時(A 對角隱)合理

6.6. 算每察之留對數似然並跨模階較以確訓集模擇

得:留對數似然近訓對數似然(無重過擬)。Viterbi 與後解於 90%+ 時步同意。態有明可釋之射分。

敗:留似然遠劣於訓→模過擬——減 K 或增正則。態不可釋→試異啟或異射族。

  • Baum-Welch 各啟之對數似然於迭間單調非減
  • 轉矩為行機(行和為 1,諸項非負)
  • 射參於有效域(正定協、有效概分)
  • Viterbi 路對數概不逾總序對數概
  • BIC/AIC 曲於所擇模階有明最低
  • 留對數似然確模通外訓集
  • 前後概算同:P(O) = sum_k(alpha[T,k]) = sum_k(pi[k] * b_k(o_1) * beta[1,k])

  • EM 局最優:Baum-Welch 收於局最大非必全。用多隨啟擇最佳
  • 數下溢:前後概隨序長指減。用對數空算或尺變避下溢至零
  • 態過多過擬:各增隱態加 O(K + d^2) 參。模擇用 BIC(非僅似然)並於留數驗
  • 標換:隱態唯至置換可識。跨啟較模→以射參匹態,非以索
  • 退化態:態或塌以釋單察(Gaussian 近零變)。協矩正則避此
  • 混 Viterbi 與後解:Viterbi 予單最佳聯路;後解予各時步最佳邊態。彼答異問且或大異
  • 忽態居時:標 HMM 隱之幾何居時分或於長制數劣擬。居時非幾何→考隱半 Markov 模

GitHub Repository

pjt222/agent-almanac
Path: i18n/wenyan-ultra/skills/fit-hidden-markov-model
0
agentsagentskillsai-assisted-developmentclaude-codeskillsteams

Related Skills

content-collections

Meta

This skill provides a production-tested setup for Content Collections, a TypeScript-first tool that transforms Markdown/MDX files into type-safe data collections with Zod validation. Use it when building blogs, documentation sites, or content-heavy Vite + React applications to ensure type safety and automatic content validation. It covers everything from Vite plugin configuration and MDX compilation to deployment optimization and schema validation.

View skill

polymarket

Meta

This skill enables developers to build applications with the Polymarket prediction markets platform, including API integration for trading and market data. It also provides real-time data streaming via WebSocket to monitor live trades and market activity. Use it for implementing trading strategies or creating tools that process live market updates.

View skill

creating-opencode-plugins

Meta

This skill helps developers create OpenCode plugins that hook into 25+ event types like commands, files, and LSP operations. It provides the plugin structure, event API specifications, and implementation patterns for JavaScript/TypeScript modules. Use it when you need to intercept, monitor, or extend the OpenCode AI assistant's lifecycle with custom event-driven logic.

View skill

sglang

Meta

SGLang is a high-performance LLM serving framework that specializes in fast, structured generation for JSON, regex, and agentic workflows using its RadixAttention prefix caching. It delivers significantly faster inference, especially for tasks with repeated prefixes, making it ideal for complex, structured outputs and multi-turn conversations. Choose SGLang over alternatives like vLLM when you need constrained decoding or are building applications with extensive prefix sharing.

View skill