chmm-lsim 1.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) hide show
  1. chmm_lsim-1.0.0/.gitignore +234 -0
  2. chmm_lsim-1.0.0/CHANGELOG.md +24 -0
  3. chmm_lsim-1.0.0/LICENSE +29 -0
  4. chmm_lsim-1.0.0/PKG-INFO +235 -0
  5. chmm_lsim-1.0.0/README.md +190 -0
  6. chmm_lsim-1.0.0/pyproject.toml +75 -0
  7. chmm_lsim-1.0.0/src/chmm_lsim/__init__.py +255 -0
  8. chmm_lsim-1.0.0/src/chmm_lsim/_deprecation.py +133 -0
  9. chmm_lsim-1.0.0/src/chmm_lsim/backends/__init__.py +3 -0
  10. chmm_lsim-1.0.0/src/chmm_lsim/backends/base.py +385 -0
  11. chmm_lsim-1.0.0/src/chmm_lsim/backends/numba_.py +860 -0
  12. chmm_lsim-1.0.0/src/chmm_lsim/backends/numpy_.py +110 -0
  13. chmm_lsim-1.0.0/src/chmm_lsim/backends/torch_batched.py +1985 -0
  14. chmm_lsim-1.0.0/src/chmm_lsim/chmm.py +12 -0
  15. chmm_lsim-1.0.0/src/chmm_lsim/compat.py +9 -0
  16. chmm_lsim-1.0.0/src/chmm_lsim/core/__init__.py +4 -0
  17. chmm_lsim-1.0.0/src/chmm_lsim/core/constants.py +56 -0
  18. chmm_lsim-1.0.0/src/chmm_lsim/core/diagnostics.py +98 -0
  19. chmm_lsim-1.0.0/src/chmm_lsim/core/joint_state.py +134 -0
  20. chmm_lsim-1.0.0/src/chmm_lsim/core/sampling.py +21 -0
  21. chmm_lsim-1.0.0/src/chmm_lsim/core/serialization.py +124 -0
  22. chmm_lsim-1.0.0/src/chmm_lsim/core/structure.py +94 -0
  23. chmm_lsim-1.0.0/src/chmm_lsim/core/validate.py +219 -0
  24. chmm_lsim-1.0.0/src/chmm_lsim/core/viterbi.py +14 -0
  25. chmm_lsim-1.0.0/src/chmm_lsim/diagnostics.py +9 -0
  26. chmm_lsim-1.0.0/src/chmm_lsim/discriminative/__init__.py +24 -0
  27. chmm_lsim-1.0.0/src/chmm_lsim/discriminative/counts.py +9 -0
  28. chmm_lsim-1.0.0/src/chmm_lsim/discriminative/domain.py +9 -0
  29. chmm_lsim-1.0.0/src/chmm_lsim/discriminative/engines.py +9 -0
  30. chmm_lsim-1.0.0/src/chmm_lsim/discriminative/kernels.py +9 -0
  31. chmm_lsim-1.0.0/src/chmm_lsim/discriminative/matlab_io.py +9 -0
  32. chmm_lsim-1.0.0/src/chmm_lsim/discriminative/metrics.py +9 -0
  33. chmm_lsim-1.0.0/src/chmm_lsim/discriminative/objective.py +9 -0
  34. chmm_lsim-1.0.0/src/chmm_lsim/discriminative/options.py +9 -0
  35. chmm_lsim-1.0.0/src/chmm_lsim/discriminative/trainer.py +9 -0
  36. chmm_lsim-1.0.0/src/chmm_lsim/discriminative/updates.py +9 -0
  37. chmm_lsim-1.0.0/src/chmm_lsim/em.py +10 -0
  38. chmm_lsim-1.0.0/src/chmm_lsim/emissions/__init__.py +53 -0
  39. chmm_lsim-1.0.0/src/chmm_lsim/emissions/base.py +293 -0
  40. chmm_lsim-1.0.0/src/chmm_lsim/emissions/gmm.py +904 -0
  41. chmm_lsim-1.0.0/src/chmm_lsim/emissions/gmm_init.py +643 -0
  42. chmm_lsim-1.0.0/src/chmm_lsim/equivalent.py +9 -0
  43. chmm_lsim-1.0.0/src/chmm_lsim/inference/__init__.py +11 -0
  44. chmm_lsim-1.0.0/src/chmm_lsim/inference/base.py +9 -0
  45. chmm_lsim-1.0.0/src/chmm_lsim/inference/fast.py +9 -0
  46. chmm_lsim-1.0.0/src/chmm_lsim/inference/poise.py +9 -0
  47. chmm_lsim-1.0.0/src/chmm_lsim/inference/registry.py +9 -0
  48. chmm_lsim-1.0.0/src/chmm_lsim/inference/sed.py +9 -0
  49. chmm_lsim-1.0.0/src/chmm_lsim/init.py +9 -0
  50. chmm_lsim-1.0.0/src/chmm_lsim/learning/__init__.py +4 -0
  51. chmm_lsim-1.0.0/src/chmm_lsim/learning/discriminative/__init__.py +36 -0
  52. chmm_lsim-1.0.0/src/chmm_lsim/learning/discriminative/counts.py +149 -0
  53. chmm_lsim-1.0.0/src/chmm_lsim/learning/discriminative/domain.py +173 -0
  54. chmm_lsim-1.0.0/src/chmm_lsim/learning/discriminative/engines.py +605 -0
  55. chmm_lsim-1.0.0/src/chmm_lsim/learning/discriminative/kernels.py +92 -0
  56. chmm_lsim-1.0.0/src/chmm_lsim/learning/discriminative/matlab_io.py +35 -0
  57. chmm_lsim-1.0.0/src/chmm_lsim/learning/discriminative/metrics.py +180 -0
  58. chmm_lsim-1.0.0/src/chmm_lsim/learning/discriminative/objective.py +107 -0
  59. chmm_lsim-1.0.0/src/chmm_lsim/learning/discriminative/options.py +187 -0
  60. chmm_lsim-1.0.0/src/chmm_lsim/learning/discriminative/trainer.py +467 -0
  61. chmm_lsim-1.0.0/src/chmm_lsim/learning/discriminative/updates.py +294 -0
  62. chmm_lsim-1.0.0/src/chmm_lsim/learning/em_loop.py +282 -0
  63. chmm_lsim-1.0.0/src/chmm_lsim/learning/minibatch.py +524 -0
  64. chmm_lsim-1.0.0/src/chmm_lsim/models/__init__.py +11 -0
  65. chmm_lsim-1.0.0/src/chmm_lsim/models/chmm/__init__.py +29 -0
  66. chmm_lsim-1.0.0/src/chmm_lsim/models/chmm/em.py +181 -0
  67. chmm_lsim-1.0.0/src/chmm_lsim/models/chmm/inference.py +276 -0
  68. chmm_lsim-1.0.0/src/chmm_lsim/models/chmm/init.py +40 -0
  69. chmm_lsim-1.0.0/src/chmm_lsim/models/chmm/params.py +127 -0
  70. chmm_lsim-1.0.0/src/chmm_lsim/models/chmm/simulate.py +48 -0
  71. chmm_lsim-1.0.0/src/chmm_lsim/models/chmm/viterbi.py +39 -0
  72. chmm_lsim-1.0.0/src/chmm_lsim/models/lsim/__init__.py +7 -0
  73. chmm_lsim-1.0.0/src/chmm_lsim/models/lsim/diagnostics.py +89 -0
  74. chmm_lsim-1.0.0/src/chmm_lsim/models/lsim/em.py +1295 -0
  75. chmm_lsim-1.0.0/src/chmm_lsim/models/lsim/equivalent.py +248 -0
  76. chmm_lsim-1.0.0/src/chmm_lsim/models/lsim/inference/__init__.py +33 -0
  77. chmm_lsim-1.0.0/src/chmm_lsim/models/lsim/inference/base.py +93 -0
  78. chmm_lsim-1.0.0/src/chmm_lsim/models/lsim/inference/fast.py +158 -0
  79. chmm_lsim-1.0.0/src/chmm_lsim/models/lsim/inference/from_emissions.py +80 -0
  80. chmm_lsim-1.0.0/src/chmm_lsim/models/lsim/inference/poise.py +277 -0
  81. chmm_lsim-1.0.0/src/chmm_lsim/models/lsim/inference/registry.py +146 -0
  82. chmm_lsim-1.0.0/src/chmm_lsim/models/lsim/inference/sed.py +295 -0
  83. chmm_lsim-1.0.0/src/chmm_lsim/models/lsim/init.py +67 -0
  84. chmm_lsim-1.0.0/src/chmm_lsim/models/lsim/params.py +198 -0
  85. chmm_lsim-1.0.0/src/chmm_lsim/models/lsim/serialization.py +181 -0
  86. chmm_lsim-1.0.0/src/chmm_lsim/models/lsim/simulate.py +112 -0
  87. chmm_lsim-1.0.0/src/chmm_lsim/models/lsim/viterbi.py +241 -0
  88. chmm_lsim-1.0.0/src/chmm_lsim/params.py +9 -0
  89. chmm_lsim-1.0.0/src/chmm_lsim/serialization.py +9 -0
  90. chmm_lsim-1.0.0/src/chmm_lsim/simulate.py +9 -0
  91. chmm_lsim-1.0.0/src/chmm_lsim/structure.py +9 -0
  92. chmm_lsim-1.0.0/src/chmm_lsim/validate.py +9 -0
  93. chmm_lsim-1.0.0/src/chmm_lsim/viterbi.py +9 -0
@@ -0,0 +1,234 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[codz]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py.cover
50
+ *.lcov
51
+ .hypothesis/
52
+ .pytest_cache/
53
+ cover/
54
+
55
+ # Translations
56
+ *.mo
57
+ *.pot
58
+
59
+ # Django stuff:
60
+ *.log
61
+ local_settings.py
62
+ db.sqlite3
63
+ db.sqlite3-journal
64
+
65
+ # Flask stuff:
66
+ instance/
67
+ .webassets-cache
68
+
69
+ # Scrapy stuff:
70
+ .scrapy
71
+
72
+ # Sphinx documentation
73
+ docs/_build/
74
+
75
+ # PyBuilder
76
+ .pybuilder/
77
+ target/
78
+
79
+ # Jupyter Notebook
80
+ .ipynb_checkpoints
81
+
82
+ # IPython
83
+ profile_default/
84
+ ipython_config.py
85
+
86
+ # pyenv
87
+ # For a library or package, you might want to ignore these files since the code is
88
+ # intended to run in multiple environments; otherwise, check them in:
89
+ # .python-version
90
+
91
+ # pipenv
92
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
93
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
94
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
95
+ # install all needed dependencies.
96
+ # Pipfile.lock
97
+
98
+ # uv
99
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
100
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
101
+ # commonly ignored for libraries.
102
+ # uv.lock
103
+
104
+ # poetry
105
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
106
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
107
+ # commonly ignored for libraries.
108
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
109
+ # poetry.lock
110
+ # poetry.toml
111
+
112
+ # pdm
113
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
114
+ # pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
115
+ # https://pdm-project.org/en/latest/usage/project/#working-with-version-control
116
+ # pdm.lock
117
+ # pdm.toml
118
+ .pdm-python
119
+ .pdm-build/
120
+
121
+ # pixi
122
+ # Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
123
+ # pixi.lock
124
+ # Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
125
+ # in the .venv directory. It is recommended not to include this directory in version control.
126
+ .pixi/*
127
+ !.pixi/config.toml
128
+
129
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
130
+ __pypackages__/
131
+
132
+ # Celery stuff
133
+ celerybeat-schedule*
134
+ celerybeat.pid
135
+
136
+ # Redis
137
+ *.rdb
138
+ *.aof
139
+ *.pid
140
+
141
+ # RabbitMQ
142
+ mnesia/
143
+ rabbitmq/
144
+ rabbitmq-data/
145
+
146
+ # ActiveMQ
147
+ activemq-data/
148
+
149
+ # SageMath parsed files
150
+ *.sage.py
151
+
152
+ # Environments
153
+ .env
154
+ .envrc
155
+ .venv
156
+ env/
157
+ venv/
158
+ ENV/
159
+ env.bak/
160
+ venv.bak/
161
+
162
+ # Spyder project settings
163
+ .spyderproject
164
+ .spyproject
165
+
166
+ # Rope project settings
167
+ .ropeproject
168
+
169
+ # mkdocs/Zensical documentation
170
+ /site
171
+
172
+ # mypy
173
+ .mypy_cache/
174
+ .dmypy.json
175
+ dmypy.json
176
+
177
+ # Pyre type checker
178
+ .pyre/
179
+
180
+ # pytype static type analyzer
181
+ .pytype/
182
+
183
+ # Cython debug symbols
184
+ cython_debug/
185
+
186
+ # PyCharm
187
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
188
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
189
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
190
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
191
+ # .idea/
192
+
193
+ # Abstra
194
+ # Abstra is an AI-powered process automation framework.
195
+ # Ignore directories containing user credentials, local state, and settings.
196
+ # Learn more at https://abstra.io/docs
197
+ .abstra/
198
+
199
+ # Visual Studio Code
200
+ # Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore that
201
+ # can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
202
+ # and can be added to the global gitignore or merged into this file. However, if you prefer, you
203
+ # could uncomment the following to ignore the entire vscode folder
204
+ # .vscode/
205
+ # Temporary file for partial code execution
206
+ tempCodeRunnerFile.py
207
+
208
+ # Ruff stuff:
209
+ .ruff_cache/
210
+
211
+ # PyPI configuration file
212
+ .pypirc
213
+
214
+ # Marimo
215
+ marimo/_static/
216
+ marimo/_lsp/
217
+ __marimo__/
218
+
219
+ # Streamlit
220
+ .streamlit/secrets.toml
221
+
222
+ # MATLAB
223
+ *.asv
224
+ *.m~
225
+ *.mex*
226
+ slprj/
227
+ octave-workspace
228
+
229
+ # benchmark outputs
230
+ benchmarks/*.csv
231
+
232
+ # patch backups
233
+ *.orig
234
+ *.rej
@@ -0,0 +1,24 @@
1
+ # Changelog
2
+
3
+ All notable changes to chmm-lsim-x are recorded here. The format follows
4
+ [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and versions follow
5
+ [Semantic Versioning](https://semver.org/spec/v2.0.0.html):
6
+
7
+ - **patch** (1.0.x): bug fixes only; no change to the public API;
8
+ - **minor** (1.x.0): new models, functions, options or backends; existing code keeps working unchanged;
9
+ - **major** (x.0.0): the only releases that may remove or change public API, and only after at least one minor release in which the old form still works and issues a `DeprecationWarning` (MATLAB: a `warning`).
10
+
11
+ The public API is every name exported by `chmm_lsim` (its `__all__`) and by `chmm_lsim.discriminative`, and every MATLAB function in `matlab/` (outside `tests/`), with its documented arguments and outputs. Everything else (Python submodules such as `chmm_lsim.models.lsim.em`) is internal and may be reorganized in any release. Models saved with `save_lsim_params` stay loadable by later versions.
12
+
13
+ ## [Unreleased]
14
+
15
+ ## [1.0.0]
16
+
17
+ First public release, accompanying the POISE paper.
18
+
19
+ ### Added
20
+ - **LSIM inference** with three backward recursions: POISE (the default), SED and fast/SVI, in MATLAB (`forward_backward_lsim`, `lsim_fb_constants`) and Python (`forward_backward`, `FbConstants`); NumPy, numba and PyTorch (CPU/CUDA, batched) backends in Python.
21
+ - **LSIM learning:** maximum-likelihood EM (`em_lsim`), mini-batch training for large datasets (`em_lsim_minibatch_train`), parameter initialization, simulation, approximate Viterbi decoding (`viterbi_lsim`), exact inference for small models, model save/load.
22
+ - **Full CHMM:** EM (`em_chmm`), exact forward-backward (`forward_backward_chmm`), exact Viterbi (`viterbi_chmm`) and simulation, in MATLAB and Python, with outputs compatible with `em_lsim` (MATLAB: the same output order without the coupling matrix; Python: the same `EmResult`).
23
+ - **Discriminative (MMI) learning** of class-conditional LSIMs: `em_lsim_discriminative`, `lsim_discriminative_predict` (MATLAB and Python).
24
+ - Runtime benchmark of the backward recursions (`benchmarks/`), demos in MATLAB and Python, tests checked against MATLAB reference outputs.
@@ -0,0 +1,29 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2026, Sajjad Karimi
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without
7
+ modification, are permitted provided that the following conditions are met:
8
+
9
+ 1. Redistributions of source code must retain the above copyright notice, this
10
+ list of conditions and the following disclaimer.
11
+
12
+ 2. Redistributions in binary form must reproduce the above copyright notice,
13
+ this list of conditions and the following disclaimer in the documentation
14
+ and/or other materials provided with the distribution.
15
+
16
+ 3. Neither the name of the copyright holder nor the names of its
17
+ contributors may be used to endorse or promote products derived from
18
+ this software without specific prior written permission.
19
+
20
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,235 @@
1
+ Metadata-Version: 2.5
2
+ Name: chmm-lsim
3
+ Version: 1.0.0
4
+ Summary: Inference and learning for coupled hidden Markov models and Latent Structure Influence Models (LSIMs): POISE, SED and SVI forward-backward, EM, and discriminative (MMI) training
5
+ Project-URL: Homepage, https://github.com/sajjadkarimi91/chmm-lsim-x
6
+ Project-URL: Repository, https://github.com/sajjadkarimi91/chmm-lsim-x
7
+ Project-URL: Issues, https://github.com/sajjadkarimi91/chmm-lsim-x/issues
8
+ Project-URL: Changelog, https://github.com/sajjadkarimi91/chmm-lsim-x/blob/main/CHANGELOG.md
9
+ Author: Sajjad Karimi
10
+ License-Expression: BSD-3-Clause
11
+ License-File: LICENSE
12
+ Keywords: chmm,coupled-hmm,em-algorithm,forward-backward,hidden-markov-model,influence-model,lsim,multichannel,time-series
13
+ Classifier: Development Status :: 4 - Beta
14
+ Classifier: Intended Audience :: Science/Research
15
+ Classifier: Operating System :: OS Independent
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3.10
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Programming Language :: Python :: 3.13
21
+ Classifier: Topic :: Scientific/Engineering
22
+ Classifier: Topic :: Scientific/Engineering :: Information Analysis
23
+ Requires-Python: >=3.10
24
+ Requires-Dist: numpy>=1.26
25
+ Requires-Dist: scipy>=1.11
26
+ Provides-Extra: all
27
+ Requires-Dist: numba>=0.60; extra == 'all'
28
+ Requires-Dist: scikit-learn>=1.3; extra == 'all'
29
+ Requires-Dist: threadpoolctl>=3; extra == 'all'
30
+ Requires-Dist: torch>=2.4; extra == 'all'
31
+ Provides-Extra: dev
32
+ Requires-Dist: numba>=0.60; extra == 'dev'
33
+ Requires-Dist: pytest>=8; extra == 'dev'
34
+ Requires-Dist: scikit-learn>=1.3; extra == 'dev'
35
+ Requires-Dist: threadpoolctl>=3; extra == 'dev'
36
+ Requires-Dist: torch>=2.4; extra == 'dev'
37
+ Provides-Extra: init
38
+ Requires-Dist: scikit-learn>=1.3; extra == 'init'
39
+ Requires-Dist: threadpoolctl>=3; extra == 'init'
40
+ Provides-Extra: numba
41
+ Requires-Dist: numba>=0.60; extra == 'numba'
42
+ Provides-Extra: torch
43
+ Requires-Dist: torch>=2.4; extra == 'torch'
44
+ Description-Content-Type: text/markdown
45
+
46
+ # chmm-lsim-x
47
+
48
+ Inference and learning for **coupled hidden Markov models (CHMMs)** and **Latent Structure Influence Models (LSIMs)**, in **MATLAB** and **Python**.
49
+
50
+ An LSIM models C interacting channels (for example EEG electrodes or sensors). Each channel has its own hidden Markov chain, and the next state of channel ζ is drawn from a mixture of pairwise transitions from every channel c, weighted by coupling strengths θ(c, ζ):
51
+
52
+ P(x<sub>t</sub><sup>ζ</sup> | x<sub>t−1</sub>) = Σ<sub>c</sub> θ(c, ζ) · A<sup>c,ζ</sup>(x<sub>t−1</sub><sup>c</sup>, x<sub>t</sub><sup>ζ</sup>)
53
+
54
+ This keeps the model size at O((Σ M)²) instead of the O(Π M) joint state space of a full CHMM.
55
+
56
+ ## What is included
57
+
58
+ | | MATLAB (`matlab/`) | Python (`chmm_lsim`) |
59
+ |---|---|---|
60
+ | Forward-backward inference: **POISE** (default), **SED**, **fast/SVI** | `forward_backward_lsim` + `lsim_fb_constants` | `forward_backward`, `FbConstants` |
61
+ | Exact inference for small models (equivalent joint HMM) | `forward_backward_chmm`, `viterbi_chmm`, `em_lsim_exact` | `forward_backward_chmm`, `viterbi_chmm`, `exact_posterior`, `viterbi_exact` |
62
+ | Maximum-likelihood EM learning | `em_lsim`, `em_lsim_poise`, `em_lsim_fast` | `em_lsim`, `em_lsim_minibatch_train` |
63
+ | Full CHMM (joint-state transitions): EM, exact inference, exact Viterbi, simulation | `em_chmm`, `forward_backward_chmm`, `viterbi_chmm`, `generate_chmm_time_series` | `em_chmm`, `forward_backward_chmm`, `viterbi_chmm`, `simulate_chmm`, `initialize_chmm_params`, `lsim_to_chmm` |
64
+ | Discriminative learning (**MMI**) of class-conditional LSIMs | `em_lsim_discriminative`, `lsim_discriminative_predict` | `em_lsim_discriminative`, `lsim_discriminative_predict` |
65
+ | LSIM simulation, approximate Viterbi decoding, model save/load | `generate_chmm_time_series`, `viterbi_lsim` | `simulate_lsim`, `viterbi_lsim`, `save_lsim_params` |
66
+ | Execution | CPU | NumPy, numba, PyTorch (CPU or CUDA, batched) |
67
+
68
+ All observations may contain missing samples (`NaN`). Emissions are Gaussian mixtures with diagonal or full covariance.
69
+
70
+ `em_lsim` and `em_chmm` share the same options, initialization, emission update, convergence rule and outputs: in MATLAB, `em_chmm` returns `em_lsim`'s outputs in the same order without the coupling matrix (`[pi_0, transitions, gmm, AIC, log_likelihood, BIC, pi_steady]`), and in Python both return the same `EmResult`. `viterbi_lsim` is a fast approximate per-channel decoder for LSIMs; `viterbi_chmm` decodes a CHMM exactly on the joint state space (in MATLAB, `viterbi_chmm` called with LSIM parameters still runs `viterbi_lsim`).
71
+
72
+ ### Choosing an inference recursion
73
+
74
+ All three recursions share the same forward pass, so they give the same approximate log-likelihood at fixed parameters. They differ in the backward pass:
75
+
76
+ - **POISE** (default): weighs each channel's outgoing influence by its total outgoing coupling strength. It needs no pairwise (S×S) message, is the fastest, and gives the most accurate smoothed marginals in our simulations, especially for strongly coupled channels.
77
+ - **SED**: builds a pairwise conditional message and solves a small linear system per channel at every step. Slower, and uses more memory.
78
+ - **fast/SVI**: fixed coupling weights raised to a fixed power. As fast as POISE.
79
+
80
+ Use exact inference (Cartesian product of channel states) only for small models; its cost grows with the product of the state counts.
81
+
82
+ ## Installation
83
+
84
+ **MATLAB** (tested with R2024b; the GMM initialization in `em_lsim` uses the Statistics and Machine Learning Toolbox):
85
+
86
+ ```matlab
87
+ run('<path to chmm-lsim-x>/matlab/setup_chmm_lsim.m') % or: addpath(genpath('<path to chmm-lsim-x>/matlab'))
88
+ ```
89
+
90
+ **Python** (3.10 or later):
91
+
92
+ ```bash
93
+ pip install "chmm-lsim[all] @ git+https://github.com/sajjadkarimi91/chmm-lsim-x"
94
+ ```
95
+
96
+ Optional extras: `init` (scikit-learn, for `initialize_lsim_params`), `numba` (numba CPU backend), `torch` (PyTorch CPU/CUDA backend), `all` (all three). The core needs only NumPy and SciPy. For CUDA, install a CUDA build of PyTorch first (see pytorch.org).
97
+
98
+ ## Quick start: MATLAB
99
+
100
+ Observations are a C×1 cell array; `observations{c}` is a d(c)×T matrix (`NaN` = missing). For several sequences, use a C×N cell array.
101
+
102
+ ```matlab
103
+ % EM learning: 4 hidden states and 1 Gaussian per channel, 100 iterations, POISE E-step
104
+ extra = struct('plot', 0, 'sigma_diag', 1);
105
+ [pi_0, coupling, transitions, gmm_para, AIC, log_lik, BIC] = ...
106
+ em_lsim(observations, 4*ones(1,C), ones(1,C), 100, extra);
107
+
108
+ % Smoothed marginals P(x_t^c | all data); marginals{c} is M(c)xT
109
+ [log_lik, ~, ~, marginals] = forward_backward_lsim(pi_0, coupling, transitions, gmm_para, observations);
110
+
111
+ % Choose the recursion explicitly
112
+ fbc = lsim_fb_constants('sed'); % 'poise' (default), 'sed' or 'fast'
113
+ [~, ~, ~, marginals_sed] = forward_backward_lsim(pi_0, coupling, transitions, gmm_para, observations, fbc);
114
+ extra.fb_constants = fbc; % the same option selects the E-step of em_lsim
115
+ ```
116
+
117
+ **MMI training** of one LSIM per class, starting from EM models:
118
+
119
+ ```matlab
120
+ % class_obs{k}: C x N_k cell of class-k training sequences; val_obs: same format
121
+ for k = 1:numel(class_obs) % generative start: EM per class
122
+ [pi_0, theta, A, gmm] = em_lsim(class_obs{k}, M, K, 30, extra);
123
+ init_models{k} = struct('pi_0_lsim', {pi_0}, 'coupling_tetha_IM', theta, ...
124
+ 'transition_matrices_IM_cell', {A}, 'gmm_para_lsim', {gmm});
125
+ end
126
+ [models, history] = em_lsim_discriminative(class_obs, M, K, [], ...
127
+ struct('objective', 'mmi', 'validation_observations', {val_obs}), init_models);
128
+ [scores, predicted] = lsim_discriminative_predict(models, test_obs); % test_obs: C x N cell
129
+ ```
130
+
131
+ **Full CHMM** (small models; parameters have the same cell format as `em_lsim`'s, with one prod(M) x M(c) transition table per channel):
132
+
133
+ ```matlab
134
+ [pi_0, transitions, gmm, AIC, log_lik, BIC] = em_chmm(observations, M, K, 100, extra);
135
+ [log_lik, ~, ~, marginals] = forward_backward_chmm(pi_0, transitions, gmm, observations); % exact
136
+ [log_prob, states] = viterbi_chmm(pi_0, transitions, gmm, observations); % exact
137
+ ```
138
+
139
+ Demos: `matlab/demos/demo_run_lsim.m` (POISE, SED and fast vs exact inference, then EM), `demo_run_chmm.m` (CHMM vs LSIM) and `demo_discriminative_lsim.m` (EM vs MMI classification).
140
+
141
+ ## Quick start: Python
142
+
143
+ Observations of one sequence are a (D, T) array with the channels stacked along D (`NaN` = missing); a dataset is a list of such arrays.
144
+
145
+ ```python
146
+ from chmm_lsim import (LSIMStructure, FbConstants, initialize_lsim_params, em_lsim,
147
+ forward_backward, viterbi_lsim)
148
+
149
+ structure = LSIMStructure(channel_state_num=(4, 4, 4), channel_obs_dim=(1, 2, 1),
150
+ num_gmm_component=(1, 1, 1))
151
+ params0 = initialize_lsim_params(structure, Y_list, sigma_diag=True, seed=0) # needs [init]
152
+ result = em_lsim(structure, params0, Y_list, max_iterations=100) # POISE E-step
153
+ post = forward_backward(structure, result.params, Y_list[0]) # post.gamma: (S, T)
154
+ post_sed = forward_backward(structure, result.params, Y_list[0], FbConstants(variant="sed"))
155
+ states = viterbi_lsim(structure, result.params, Y_list[0]).states
156
+ ```
157
+
158
+ Large datasets and GPUs:
159
+
160
+ ```python
161
+ from chmm_lsim import em_lsim_minibatch_train, forward_backward_batch
162
+
163
+ loglik = forward_backward_batch(structure, result.params, Y_list, mode="loglik",
164
+ device="cuda", dtype="float32") # batched scoring
165
+ fit = em_lsim_minibatch_train(structure, params0, Y_list, max_iterations=20, num_minibatches=10,
166
+ backend="torch", backend_options={"device": "cuda", "dtype": "float64"})
167
+ ```
168
+
169
+ MMI training:
170
+
171
+ ```python
172
+ from chmm_lsim import em_lsim_discriminative, lsim_discriminative_predict
173
+
174
+ res = em_lsim_discriminative((Y_train, y_train), init_models, # one LSIMParams per class
175
+ validation=(Y_val, y_val), objective="mmi")
176
+ scores, predicted, normalized = lsim_discriminative_predict(res.models, Y_test)
177
+ ```
178
+
179
+ Full CHMM (exact, for small models; `em_chmm` returns the same `EmResult` as `em_lsim`):
180
+
181
+ ```python
182
+ from chmm_lsim import initialize_chmm_params, em_chmm, forward_backward_chmm, viterbi_chmm
183
+
184
+ fit = em_chmm(structure, initialize_chmm_params(structure, Y_list, seed=0), Y_list, max_iterations=100)
185
+ post = forward_backward_chmm(structure, fit.params, Y_list[0]) # post.gamma: (S, T)
186
+ states = viterbi_chmm(structure, fit.params, Y_list[0]).states # exact joint decoding
187
+ ```
188
+
189
+ Demos: `examples/demo_run_lsim.py` and `examples/demo_run_chmm.py`.
190
+
191
+ ## Benchmarks
192
+
193
+ `benchmarks/` reproduces the runtime and memory comparison of the POISE, SED and fast/SVI backward recursions on CPU and GPU, and the full E-step on 100,000 sequences. See `benchmarks/README.md`.
194
+
195
+ ## Repository layout
196
+
197
+ ```
198
+ matlab/
199
+ setup_chmm_lsim.m adds the toolbox folders to the MATLAB path
200
+ lsim/ LSIM inference (POISE, SED, fast/SVI), EM, exact EM, constants, approximate Viterbi
201
+ lsim/backward/ one file per LSIM backward recursion
202
+ chmm/ full CHMM: EM, exact forward-backward, exact Viterbi, simulation
203
+ emissions/ Gaussian-mixture densities and sampling
204
+ common/ parameter helpers
205
+ discriminative/ MMI training, prediction, metrics (+ tests/)
206
+ demos/
207
+ src/chmm_lsim/ Python package
208
+ core/ structure, validation, constants, serialization, joint-state utilities
209
+ emissions/ emission models (Gaussian mixtures) and their registry
210
+ models/lsim/ LSIM: inference (POISE, SED, fast), EM, initialization, simulation, Viterbi
211
+ models/chmm/ full CHMM
212
+ learning/ shared EM loop, mini-batch training, discriminative (MMI)
213
+ backends/ NumPy, numba, PyTorch
214
+ tests/ Python tests (pytest), with MATLAB reference fixtures
215
+ examples/ Python demos
216
+ benchmarks/ runtime benchmark
217
+ docs/EXTENDING.md how to add inference recursions, emission models and model families
218
+ ```
219
+
220
+ ## Versions and compatibility
221
+
222
+ Releases follow [semantic versioning](https://semver.org): new models and features arrive in minor releases without breaking existing code, and anything public that is ever renamed or removed keeps working with a deprecation warning for at least one minor release first. The public API is everything importable from `chmm_lsim` (and `chmm_lsim.discriminative`) and every MATLAB function; Python submodules are internal, so import from `chmm_lsim` directly. Changes are listed in [`CHANGELOG.md`](CHANGELOG.md); [`docs/EXTENDING.md`](docs/EXTENDING.md) explains how new models are added.
223
+
224
+ ## Tests
225
+
226
+ ```bash
227
+ pip install -e ".[dev]"
228
+ pytest
229
+ ```
230
+
231
+ The Python implementation is checked against MATLAB reference outputs (`tests/fixtures/`). MATLAB tests of the MMI trainer: `runtests('matlab/discriminative/tests/TestMMI.m')`.
232
+
233
+ ## License
234
+
235
+ BSD 3-Clause. See `LICENSE`.