markovgpu-rane 0.2.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.
- markovgpu_rane-0.2.0/.github/workflows/publish.yml +28 -0
- markovgpu_rane-0.2.0/.github/workflows/test.yml +37 -0
- markovgpu_rane-0.2.0/.gitignore +76 -0
- markovgpu_rane-0.2.0/.python-version +1 -0
- markovgpu_rane-0.2.0/PKG-INFO +205 -0
- markovgpu_rane-0.2.0/README.md +182 -0
- markovgpu_rane-0.2.0/pyproject.toml +60 -0
- markovgpu_rane-0.2.0/src/markovgpu/__init__.py +5 -0
- markovgpu_rane-0.2.0/src/markovgpu/backend.py +345 -0
- markovgpu_rane-0.2.0/src/markovgpu/kernels.cl +170 -0
- markovgpu_rane-0.2.0/src/markovgpu/py.typed +0 -0
- markovgpu_rane-0.2.0/src/markovgpu/sklearn.py +94 -0
- markovgpu_rane-0.2.0/tests/test_basic.py +17 -0
- markovgpu_rane-0.2.0/uv.lock +1152 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
name: Publish to PyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags:
|
|
6
|
+
- "v*"
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
pypi-publish:
|
|
10
|
+
name: Publish to PyPI
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
environment: pypi # Matches what you typed in PyPI website
|
|
13
|
+
permissions:
|
|
14
|
+
id-token: write # REQUIRED for Trusted Publishing
|
|
15
|
+
|
|
16
|
+
steps:
|
|
17
|
+
- name: Checkout Code
|
|
18
|
+
uses: actions/checkout@v4
|
|
19
|
+
|
|
20
|
+
- name: Install uv
|
|
21
|
+
uses: astral-sh/setup-uv@v5
|
|
22
|
+
|
|
23
|
+
- name: Build Package
|
|
24
|
+
run: uv build
|
|
25
|
+
|
|
26
|
+
- name: Publish to PyPI
|
|
27
|
+
# This action uses the "Trusted" connection automatically
|
|
28
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
name: CI (Test & Lint)
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [main]
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
test:
|
|
11
|
+
name: Test on Python ${{ matrix.python-version }}
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
strategy:
|
|
14
|
+
matrix:
|
|
15
|
+
python-version: ["3.10", "3.11", "3.12"] # Test multiple versions
|
|
16
|
+
|
|
17
|
+
steps:
|
|
18
|
+
- name: Checkout Code
|
|
19
|
+
uses: actions/checkout@v4
|
|
20
|
+
|
|
21
|
+
- name: Install uv
|
|
22
|
+
uses: astral-sh/setup-uv@v5 # The official uv action
|
|
23
|
+
|
|
24
|
+
- name: Set up Python ${{ matrix.python-version }}
|
|
25
|
+
run: uv python install ${{ matrix.python-version }}
|
|
26
|
+
|
|
27
|
+
- name: Install Project
|
|
28
|
+
run: uv sync --all-extras --dev
|
|
29
|
+
|
|
30
|
+
- name: Lint Code (Ruff)
|
|
31
|
+
run: |
|
|
32
|
+
uv run ruff check .
|
|
33
|
+
uv run ruff format --check .
|
|
34
|
+
|
|
35
|
+
- name: Run Tests
|
|
36
|
+
# We assume CPU fallback works (GitHub runners don't have GPUs)
|
|
37
|
+
run: uv run pytest
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# --- Python Basics ---
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# --- C Extensions & Shared Libraries ---
|
|
7
|
+
*.so
|
|
8
|
+
*.pyd
|
|
9
|
+
*.dylib
|
|
10
|
+
|
|
11
|
+
# --- Distribution / Packaging ---
|
|
12
|
+
.Python
|
|
13
|
+
build/
|
|
14
|
+
develop-eggs/
|
|
15
|
+
dist/
|
|
16
|
+
downloads/
|
|
17
|
+
eggs/
|
|
18
|
+
.eggs/
|
|
19
|
+
lib/
|
|
20
|
+
lib64/
|
|
21
|
+
parts/
|
|
22
|
+
sdist/
|
|
23
|
+
var/
|
|
24
|
+
wheels/
|
|
25
|
+
share/python-wheels/
|
|
26
|
+
*.egg-info/
|
|
27
|
+
.installed.cfg
|
|
28
|
+
*.egg
|
|
29
|
+
MANIFEST
|
|
30
|
+
|
|
31
|
+
# --- Virtual Environments ---
|
|
32
|
+
# Common names for virtual envs
|
|
33
|
+
.venv
|
|
34
|
+
venv/
|
|
35
|
+
ENV/
|
|
36
|
+
env/
|
|
37
|
+
|
|
38
|
+
# --- Unit Test / Coverage ---
|
|
39
|
+
htmlcov/
|
|
40
|
+
.tox/
|
|
41
|
+
.nox/
|
|
42
|
+
.coverage
|
|
43
|
+
.coverage.*
|
|
44
|
+
.cache
|
|
45
|
+
nosetests.xml
|
|
46
|
+
coverage.xml
|
|
47
|
+
*.cover
|
|
48
|
+
*.py.cover
|
|
49
|
+
.hypothesis/
|
|
50
|
+
.pytest_cache/
|
|
51
|
+
|
|
52
|
+
# --- IDEs & Editors (Optional but Recommended) ---
|
|
53
|
+
# VS Code
|
|
54
|
+
.vscode/
|
|
55
|
+
!.vscode/settings.json
|
|
56
|
+
!.vscode/tasks.json
|
|
57
|
+
!.vscode/launch.json
|
|
58
|
+
!.vscode/extensions.json
|
|
59
|
+
*.code-workspace
|
|
60
|
+
|
|
61
|
+
# PyCharm / IntelliJ
|
|
62
|
+
.idea/
|
|
63
|
+
|
|
64
|
+
# Mac / Windows System Files
|
|
65
|
+
.DS_Store
|
|
66
|
+
Thumbs.db
|
|
67
|
+
|
|
68
|
+
# --- Environment Variables (Security) ---
|
|
69
|
+
# NEVER commit your secrets
|
|
70
|
+
.env
|
|
71
|
+
.env.local
|
|
72
|
+
.env.*.local
|
|
73
|
+
|
|
74
|
+
# --- Project Specific ---
|
|
75
|
+
# If your OpenCL kernels generate binary caches
|
|
76
|
+
*.cl.bin
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
3.12
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: markovgpu-rane
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: High-performance Markov Chains & HMMs using OpenCL
|
|
5
|
+
Author-email: Sahil Rane <sahilrane249@gmail.com>
|
|
6
|
+
Classifier: Development Status :: 4 - Beta
|
|
7
|
+
Classifier: Intended Audience :: Developers
|
|
8
|
+
Classifier: Intended Audience :: Financial and Insurance Industry
|
|
9
|
+
Classifier: Intended Audience :: Science/Research
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
14
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
15
|
+
Requires-Python: >=3.12
|
|
16
|
+
Requires-Dist: matplotlib>=3.8.0
|
|
17
|
+
Requires-Dist: numpy>=1.26.0
|
|
18
|
+
Requires-Dist: pyopencl>=2024.1
|
|
19
|
+
Requires-Dist: scikit-learn>=1.8.0
|
|
20
|
+
Requires-Dist: scipy>=1.11.0
|
|
21
|
+
Requires-Dist: yfinance>=1.1.0
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
|
|
24
|
+
<div align="center">
|
|
25
|
+
|
|
26
|
+
# β‘ **MarkovGPU**
|
|
27
|
+
|
|
28
|
+
### *Massive Scale Markov Models on Consumer Hardware*
|
|
29
|
+
<img width="100%" alt="MarkovGPU Hero" src="https://i.imgur.com/gK9J6hD.p" /
|
|
30
|
+
|
|
31
|
+
> **Run million-state HMMs on your laptop GPU.**
|
|
32
|
+
> **No CUDA required β’ Hybrid CPU/GPU Backend β’ Production Ready**
|
|
33
|
+
|
|
34
|
+
[](https://pypi.org/project/markovgpu-rane/)
|
|
35
|
+
[](https://www.python.org/downloads/)
|
|
36
|
+
[](https://opensource.org/licenses/MIT)
|
|
37
|
+
[](https://github.com/wizardwithcodehazard/markov/actions)
|
|
38
|
+
|
|
39
|
+
</div>
|
|
40
|
+
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
## π **The Engine for Stochastic Intelligence**
|
|
44
|
+
|
|
45
|
+
**MarkovGPU** is a high-performance probabilistic modeling library built for speed. It breaks the "NVIDIA Monopoly" by using **OpenCL** to accelerate **Hidden Markov Models (HMM)** and **Markov Chains** on *any* GPUβincluding AMD Radeon, Intel Arc, and Apple Silicon.
|
|
46
|
+
|
|
47
|
+
It doesn't just run; it *thinks*. The **Smart Hybrid Backend** automatically routes small tasks to the CPU (NumPy) and massive workloads to the GPU, giving you optimal performance at every scale.
|
|
48
|
+
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
## π **Core Superpowers**
|
|
52
|
+
|
|
53
|
+
| Feature | Magic Behind It |
|
|
54
|
+
|-------|----------------|
|
|
55
|
+
| β‘ **Hardware Agnostic** | Built on **OpenCL** β runs on AMD, Intel, NVIDIA, and Apple M1/M2/M3 chips. |
|
|
56
|
+
| π§ **Smart Hybrid Backend** | Auto-detects problem size ($N$). Uses **NumPy** for speed on small data, **GPU** for massive throughput. |
|
|
57
|
+
| π **Log-Space Stability** | Implements **Log-Sum-Exp** kernels to prevent underflow on long time-series (1M+ steps). |
|
|
58
|
+
| π΅οΈ **Viterbi Decoding** | Finds the "Hidden Truth" in noisy data (e.g., market regimes, DNA sequences) in milliseconds. |
|
|
59
|
+
| π **Unsupervised Learning** | **Baum-Welch (EM)** algorithm trains models directly on the GPU, learning rules from raw data. |
|
|
60
|
+
| π¦ **Zero-Config Install** | `pip install markovgpu-rane`. No driver hell. No CUDA toolkit nightmares. |
|
|
61
|
+
|
|
62
|
+
---
|
|
63
|
+
|
|
64
|
+
## ποΈ **Architecture: The Hybrid Pipeline**
|
|
65
|
+
|
|
66
|
+
```mermaid
|
|
67
|
+
graph LR
|
|
68
|
+
A[User Code] -->|Request Fit/Predict| B{Smart Dispatcher}
|
|
69
|
+
B -->|Small N < 64| C["CPU Engine
|
|
70
|
+
(NumPy AVX2)"]
|
|
71
|
+
B -->|Large N >= 64| D["GPU Engine
|
|
72
|
+
(OpenCL Kernels)"]
|
|
73
|
+
C --> E[Result]
|
|
74
|
+
D --> E
|
|
75
|
+
subgraph GPU_Acceleration[GPU Acceleration]
|
|
76
|
+
D --> F[Matrix Multiply]
|
|
77
|
+
D --> G[Log-Sum-Exp]
|
|
78
|
+
D --> H[Parallel Viterbi]
|
|
79
|
+
end
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
The library handles the hardware. You handle the math.
|
|
83
|
+
|
|
84
|
+
## β‘ Performance: Benchmarks
|
|
85
|
+
|
|
86
|
+
**Task**: Viterbi Decoding (64 Hidden States, 5000 Days of Data).
|
|
87
|
+
**Hardware**: AMD Ryzen 680M (Integrated Graphics).
|
|
88
|
+
|
|
89
|
+
| Engine | Execution Time | Speedup |
|
|
90
|
+
|--------|---------------|---------|
|
|
91
|
+
| π’ CPU (NumPy Optimized) | 5.06s | 1x |
|
|
92
|
+
| π GPU (MarkovGPU) | 0.82s | **6.2x** |
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
## βοΈ Quick Start in 30 Seconds
|
|
97
|
+
|
|
98
|
+
### Installation
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
# Production
|
|
102
|
+
pip install markovgpu-rane
|
|
103
|
+
|
|
104
|
+
# Or for local development
|
|
105
|
+
uv pip install markovgpu-rane
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### 1. Market Regime Detection (Viterbi)
|
|
109
|
+
Identify hidden "Bull" vs. "Bear" markets from noisy stock returns.
|
|
110
|
+
|
|
111
|
+
```python
|
|
112
|
+
import numpy as np
|
|
113
|
+
from markovgpu import MarkovEngine
|
|
114
|
+
|
|
115
|
+
# 1. Setup the Rules (Transition Matrix)
|
|
116
|
+
# "Bull markets tend to stay Bullish (95%)"
|
|
117
|
+
trans_mat = np.array([[0.95, 0.05],
|
|
118
|
+
[0.10, 0.90]], dtype=np.float32)
|
|
119
|
+
|
|
120
|
+
# 2. Feed the Data (Observation Likelihoods)
|
|
121
|
+
# Shape: (1000 Days, 2 States)
|
|
122
|
+
obs_probs = np.random.rand(1000, 2).astype(np.float32)
|
|
123
|
+
|
|
124
|
+
# 3. Ignite the Engine
|
|
125
|
+
engine = MarkovEngine()
|
|
126
|
+
predicted_states = engine.decode_regime(trans_mat, obs_probs)
|
|
127
|
+
|
|
128
|
+
print("Detected Regimes:", predicted_states)
|
|
129
|
+
# Output: [0, 0, 0, 1, 1, 1, 0 ...]
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### 2. Unsupervised Learning (Baum-Welch)
|
|
133
|
+
Train the AI to discover the hidden rules from raw data.
|
|
134
|
+
|
|
135
|
+
```python
|
|
136
|
+
# The engine learns the Transition Matrix automatically
|
|
137
|
+
learned_matrix = engine.fit(
|
|
138
|
+
obs_probs,
|
|
139
|
+
n_states=2,
|
|
140
|
+
n_iters=100,
|
|
141
|
+
tolerance=1e-4
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
print("Discovered Rules:")
|
|
145
|
+
print(learned_matrix)
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
---
|
|
149
|
+
|
|
150
|
+
## π¬ Technical Brilliance
|
|
151
|
+
|
|
152
|
+
### 1. The Log-Sum-Exp Kernel
|
|
153
|
+
Standard HMMs crash on long sequences because probabilities like $0.9^{1000}$ vanish to zero.
|
|
154
|
+
We solved this by rewriting the entire GPU kernel in Log-Space:
|
|
155
|
+
|
|
156
|
+
```c
|
|
157
|
+
// Actual OpenCL Kernel snippet
|
|
158
|
+
float log_add(float log_a, float log_b) {
|
|
159
|
+
float max_val = max(log_a, log_b);
|
|
160
|
+
return max_val + log1p(exp(min(log_a, log_b) - max_val));
|
|
161
|
+
}
|
|
162
|
+
```
|
|
163
|
+
β **Result**: You can process sequences of infinite length without numerical collapse.
|
|
164
|
+
|
|
165
|
+
### 2. Parallel Viterbi
|
|
166
|
+
Instead of a slow Python loop, we launch $N$ threads (one per state) for every time step on the GPU, calculating the optimal path in parallel.
|
|
167
|
+
|
|
168
|
+
---
|
|
169
|
+
|
|
170
|
+
## π οΈ Project Structure
|
|
171
|
+
|
|
172
|
+
```
|
|
173
|
+
markovgpu/
|
|
174
|
+
βββ src/markovgpu/
|
|
175
|
+
β βββ backend.py # The Brain (Smart Dispatcher)
|
|
176
|
+
β βββ kernels.cl # The Muscle (OpenCL C Code)
|
|
177
|
+
β βββ __init__.py
|
|
178
|
+
βββ tests/ # Unit Tests
|
|
179
|
+
βββ pyproject.toml # Modern Packaging Config
|
|
180
|
+
βββ README.md
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## π± Contributing
|
|
184
|
+
|
|
185
|
+
We welcome forks, issues, and PRs!
|
|
186
|
+
|
|
187
|
+
```bash
|
|
188
|
+
git clone https://github.com/wizardwithcodehazard/markov.git
|
|
189
|
+
cd markov
|
|
190
|
+
uv sync --dev
|
|
191
|
+
uv run pytest
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
## π License
|
|
195
|
+
|
|
196
|
+
**MIT License** β Free to use, modify, and ship in commercial products.
|
|
197
|
+
|
|
198
|
+
<div align="center">
|
|
199
|
+
|
|
200
|
+
MarkovGPU doesnβt just crunch numbers.
|
|
201
|
+
### It discovers the hidden structure of reality.
|
|
202
|
+
|
|
203
|
+
Made with π§‘ by Sahil Rane
|
|
204
|
+
|
|
205
|
+
</div>
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
# β‘ **MarkovGPU**
|
|
4
|
+
|
|
5
|
+
### *Massive Scale Markov Models on Consumer Hardware*
|
|
6
|
+
<img width="100%" alt="MarkovGPU Hero" src="https://i.imgur.com/gK9J6hD.p" /
|
|
7
|
+
|
|
8
|
+
> **Run million-state HMMs on your laptop GPU.**
|
|
9
|
+
> **No CUDA required β’ Hybrid CPU/GPU Backend β’ Production Ready**
|
|
10
|
+
|
|
11
|
+
[](https://pypi.org/project/markovgpu-rane/)
|
|
12
|
+
[](https://www.python.org/downloads/)
|
|
13
|
+
[](https://opensource.org/licenses/MIT)
|
|
14
|
+
[](https://github.com/wizardwithcodehazard/markov/actions)
|
|
15
|
+
|
|
16
|
+
</div>
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## π **The Engine for Stochastic Intelligence**
|
|
21
|
+
|
|
22
|
+
**MarkovGPU** is a high-performance probabilistic modeling library built for speed. It breaks the "NVIDIA Monopoly" by using **OpenCL** to accelerate **Hidden Markov Models (HMM)** and **Markov Chains** on *any* GPUβincluding AMD Radeon, Intel Arc, and Apple Silicon.
|
|
23
|
+
|
|
24
|
+
It doesn't just run; it *thinks*. The **Smart Hybrid Backend** automatically routes small tasks to the CPU (NumPy) and massive workloads to the GPU, giving you optimal performance at every scale.
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
## π **Core Superpowers**
|
|
29
|
+
|
|
30
|
+
| Feature | Magic Behind It |
|
|
31
|
+
|-------|----------------|
|
|
32
|
+
| β‘ **Hardware Agnostic** | Built on **OpenCL** β runs on AMD, Intel, NVIDIA, and Apple M1/M2/M3 chips. |
|
|
33
|
+
| π§ **Smart Hybrid Backend** | Auto-detects problem size ($N$). Uses **NumPy** for speed on small data, **GPU** for massive throughput. |
|
|
34
|
+
| π **Log-Space Stability** | Implements **Log-Sum-Exp** kernels to prevent underflow on long time-series (1M+ steps). |
|
|
35
|
+
| π΅οΈ **Viterbi Decoding** | Finds the "Hidden Truth" in noisy data (e.g., market regimes, DNA sequences) in milliseconds. |
|
|
36
|
+
| π **Unsupervised Learning** | **Baum-Welch (EM)** algorithm trains models directly on the GPU, learning rules from raw data. |
|
|
37
|
+
| π¦ **Zero-Config Install** | `pip install markovgpu-rane`. No driver hell. No CUDA toolkit nightmares. |
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
## ποΈ **Architecture: The Hybrid Pipeline**
|
|
42
|
+
|
|
43
|
+
```mermaid
|
|
44
|
+
graph LR
|
|
45
|
+
A[User Code] -->|Request Fit/Predict| B{Smart Dispatcher}
|
|
46
|
+
B -->|Small N < 64| C["CPU Engine
|
|
47
|
+
(NumPy AVX2)"]
|
|
48
|
+
B -->|Large N >= 64| D["GPU Engine
|
|
49
|
+
(OpenCL Kernels)"]
|
|
50
|
+
C --> E[Result]
|
|
51
|
+
D --> E
|
|
52
|
+
subgraph GPU_Acceleration[GPU Acceleration]
|
|
53
|
+
D --> F[Matrix Multiply]
|
|
54
|
+
D --> G[Log-Sum-Exp]
|
|
55
|
+
D --> H[Parallel Viterbi]
|
|
56
|
+
end
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
The library handles the hardware. You handle the math.
|
|
60
|
+
|
|
61
|
+
## β‘ Performance: Benchmarks
|
|
62
|
+
|
|
63
|
+
**Task**: Viterbi Decoding (64 Hidden States, 5000 Days of Data).
|
|
64
|
+
**Hardware**: AMD Ryzen 680M (Integrated Graphics).
|
|
65
|
+
|
|
66
|
+
| Engine | Execution Time | Speedup |
|
|
67
|
+
|--------|---------------|---------|
|
|
68
|
+
| π’ CPU (NumPy Optimized) | 5.06s | 1x |
|
|
69
|
+
| π GPU (MarkovGPU) | 0.82s | **6.2x** |
|
|
70
|
+
|
|
71
|
+
---
|
|
72
|
+
|
|
73
|
+
## βοΈ Quick Start in 30 Seconds
|
|
74
|
+
|
|
75
|
+
### Installation
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
# Production
|
|
79
|
+
pip install markovgpu-rane
|
|
80
|
+
|
|
81
|
+
# Or for local development
|
|
82
|
+
uv pip install markovgpu-rane
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### 1. Market Regime Detection (Viterbi)
|
|
86
|
+
Identify hidden "Bull" vs. "Bear" markets from noisy stock returns.
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
import numpy as np
|
|
90
|
+
from markovgpu import MarkovEngine
|
|
91
|
+
|
|
92
|
+
# 1. Setup the Rules (Transition Matrix)
|
|
93
|
+
# "Bull markets tend to stay Bullish (95%)"
|
|
94
|
+
trans_mat = np.array([[0.95, 0.05],
|
|
95
|
+
[0.10, 0.90]], dtype=np.float32)
|
|
96
|
+
|
|
97
|
+
# 2. Feed the Data (Observation Likelihoods)
|
|
98
|
+
# Shape: (1000 Days, 2 States)
|
|
99
|
+
obs_probs = np.random.rand(1000, 2).astype(np.float32)
|
|
100
|
+
|
|
101
|
+
# 3. Ignite the Engine
|
|
102
|
+
engine = MarkovEngine()
|
|
103
|
+
predicted_states = engine.decode_regime(trans_mat, obs_probs)
|
|
104
|
+
|
|
105
|
+
print("Detected Regimes:", predicted_states)
|
|
106
|
+
# Output: [0, 0, 0, 1, 1, 1, 0 ...]
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### 2. Unsupervised Learning (Baum-Welch)
|
|
110
|
+
Train the AI to discover the hidden rules from raw data.
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
# The engine learns the Transition Matrix automatically
|
|
114
|
+
learned_matrix = engine.fit(
|
|
115
|
+
obs_probs,
|
|
116
|
+
n_states=2,
|
|
117
|
+
n_iters=100,
|
|
118
|
+
tolerance=1e-4
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
print("Discovered Rules:")
|
|
122
|
+
print(learned_matrix)
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
---
|
|
126
|
+
|
|
127
|
+
## π¬ Technical Brilliance
|
|
128
|
+
|
|
129
|
+
### 1. The Log-Sum-Exp Kernel
|
|
130
|
+
Standard HMMs crash on long sequences because probabilities like $0.9^{1000}$ vanish to zero.
|
|
131
|
+
We solved this by rewriting the entire GPU kernel in Log-Space:
|
|
132
|
+
|
|
133
|
+
```c
|
|
134
|
+
// Actual OpenCL Kernel snippet
|
|
135
|
+
float log_add(float log_a, float log_b) {
|
|
136
|
+
float max_val = max(log_a, log_b);
|
|
137
|
+
return max_val + log1p(exp(min(log_a, log_b) - max_val));
|
|
138
|
+
}
|
|
139
|
+
```
|
|
140
|
+
β **Result**: You can process sequences of infinite length without numerical collapse.
|
|
141
|
+
|
|
142
|
+
### 2. Parallel Viterbi
|
|
143
|
+
Instead of a slow Python loop, we launch $N$ threads (one per state) for every time step on the GPU, calculating the optimal path in parallel.
|
|
144
|
+
|
|
145
|
+
---
|
|
146
|
+
|
|
147
|
+
## π οΈ Project Structure
|
|
148
|
+
|
|
149
|
+
```
|
|
150
|
+
markovgpu/
|
|
151
|
+
βββ src/markovgpu/
|
|
152
|
+
β βββ backend.py # The Brain (Smart Dispatcher)
|
|
153
|
+
β βββ kernels.cl # The Muscle (OpenCL C Code)
|
|
154
|
+
β βββ __init__.py
|
|
155
|
+
βββ tests/ # Unit Tests
|
|
156
|
+
βββ pyproject.toml # Modern Packaging Config
|
|
157
|
+
βββ README.md
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## π± Contributing
|
|
161
|
+
|
|
162
|
+
We welcome forks, issues, and PRs!
|
|
163
|
+
|
|
164
|
+
```bash
|
|
165
|
+
git clone https://github.com/wizardwithcodehazard/markov.git
|
|
166
|
+
cd markov
|
|
167
|
+
uv sync --dev
|
|
168
|
+
uv run pytest
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
## π License
|
|
172
|
+
|
|
173
|
+
**MIT License** β Free to use, modify, and ship in commercial products.
|
|
174
|
+
|
|
175
|
+
<div align="center">
|
|
176
|
+
|
|
177
|
+
MarkovGPU doesnβt just crunch numbers.
|
|
178
|
+
### It discovers the hidden structure of reality.
|
|
179
|
+
|
|
180
|
+
Made with π§‘ by Sahil Rane
|
|
181
|
+
|
|
182
|
+
</div>
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "markovgpu-rane"
|
|
3
|
+
version = "0.2.0"
|
|
4
|
+
description = "High-performance Markov Chains & HMMs using OpenCL"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
authors = [
|
|
7
|
+
{ name = "Sahil Rane", email = "sahilrane249@gmail.com" }
|
|
8
|
+
]
|
|
9
|
+
requires-python = ">=3.12"
|
|
10
|
+
|
|
11
|
+
# Your core libraries + visualization/stats tools you used in the demos
|
|
12
|
+
dependencies = [
|
|
13
|
+
"numpy>=1.26.0", # Relaxed version constraint slightly for better compatibility
|
|
14
|
+
"pyopencl>=2024.1",
|
|
15
|
+
"matplotlib>=3.8.0",
|
|
16
|
+
"scipy>=1.11.0",
|
|
17
|
+
"yfinance>=1.1.0",
|
|
18
|
+
"scikit-learn>=1.8.0",
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
# Metadata tags to help people find your library on PyPI
|
|
22
|
+
classifiers = [
|
|
23
|
+
"Development Status :: 4 - Beta",
|
|
24
|
+
"Intended Audience :: Developers",
|
|
25
|
+
"Intended Audience :: Science/Research",
|
|
26
|
+
"Intended Audience :: Financial and Insurance Industry",
|
|
27
|
+
"License :: OSI Approved :: MIT License",
|
|
28
|
+
"Operating System :: OS Independent",
|
|
29
|
+
"Programming Language :: Python :: 3",
|
|
30
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
31
|
+
"Topic :: Scientific/Engineering :: Mathematics",
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
# (Optional) Add your GitHub URL if you have pushed the code
|
|
35
|
+
# [project.urls]
|
|
36
|
+
# Repository = "https://github.com/yourusername/markovgpu"
|
|
37
|
+
# Issues = "https://github.com/yourusername/markovgpu/issues"
|
|
38
|
+
|
|
39
|
+
# ---------------------------------------------------------
|
|
40
|
+
# BUILD SYSTEM: Hatchling (Standard for uv)
|
|
41
|
+
# ---------------------------------------------------------
|
|
42
|
+
[build-system]
|
|
43
|
+
requires = ["hatchling"]
|
|
44
|
+
build-backend = "hatchling.build"
|
|
45
|
+
|
|
46
|
+
# ---------------------------------------------------------
|
|
47
|
+
# PACKAGING: Include the .cl Kernel file
|
|
48
|
+
# ---------------------------------------------------------
|
|
49
|
+
[tool.hatch.build.targets.wheel]
|
|
50
|
+
packages = ["src/markovgpu"]
|
|
51
|
+
|
|
52
|
+
[tool.hatch.build.targets.wheel.force-include]
|
|
53
|
+
# Map source path (left) to package destination (right)
|
|
54
|
+
"src/markovgpu/kernels.cl" = "markovgpu/kernels.cl"
|
|
55
|
+
|
|
56
|
+
[dependency-groups]
|
|
57
|
+
dev = [
|
|
58
|
+
"pytest>=9.0.2",
|
|
59
|
+
"ruff>=0.15.0",
|
|
60
|
+
]
|