stmath 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.
- stmath-1.0.0/PKG-INFO +47 -0
- stmath-1.0.0/README.md +32 -0
- stmath-1.0.0/pyproject.toml +24 -0
- stmath-1.0.0/setup.cfg +17 -0
- stmath-1.0.0/setup.py +9 -0
- stmath-1.0.0/stmath/__init__.py +270 -0
- stmath-1.0.0/stmath/algebra.py +23 -0
- stmath-1.0.0/stmath/aptitude_math.py +20 -0
- stmath-1.0.0/stmath/benchmark.py +18 -0
- stmath-1.0.0/stmath/combinatorics.py +54 -0
- stmath-1.0.0/stmath/core.py +50 -0
- stmath-1.0.0/stmath/crypto_math.py +17 -0
- stmath-1.0.0/stmath/distributions.py +83 -0
- stmath-1.0.0/stmath/dl.py +60 -0
- stmath-1.0.0/stmath/finance_math.py +19 -0
- stmath-1.0.0/stmath/genai.py +41 -0
- stmath-1.0.0/stmath/graph.py +66 -0
- stmath-1.0.0/stmath/interpreter.py +43 -0
- stmath-1.0.0/stmath/math_ext.py +13 -0
- stmath-1.0.0/stmath/ml.py +84 -0
- stmath-1.0.0/stmath/nlp.py +46 -0
- stmath-1.0.0/stmath/optimization.py +34 -0
- stmath-1.0.0/stmath/probability.py +39 -0
- stmath-1.0.0/stmath/quantum_math.py +23 -0
- stmath-1.0.0/stmath/scientific.py +47 -0
- stmath-1.0.0/stmath/statistics.py +83 -0
- stmath-1.0.0/stmath/timeseries.py +31 -0
- stmath-1.0.0/stmath/utils.py +5 -0
- stmath-1.0.0/stmath/vision.py +104 -0
- stmath-1.0.0/stmath.egg-info/PKG-INFO +47 -0
- stmath-1.0.0/stmath.egg-info/SOURCES.txt +33 -0
- stmath-1.0.0/stmath.egg-info/dependency_links.txt +1 -0
- stmath-1.0.0/stmath.egg-info/top_level.txt +1 -0
stmath-1.0.0/PKG-INFO
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: stmath
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: A unified Math + ML + AI + Crypto + Quantum library for Python
|
|
5
|
+
Author: Saksham Tomar
|
|
6
|
+
Author-email: Saksham Tomar <tomarsaksham55@gmail.com>
|
|
7
|
+
Keywords: math,ai,ml,deep-learning,quantum,statistics
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Requires-Python: >=3.8
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
Dynamic: author
|
|
15
|
+
|
|
16
|
+
<div align="center">
|
|
17
|
+
|
|
18
|
+
# โก STMATH
|
|
19
|
+
### The Next-Generation Unified Math + AI + ML + Quantum Engine
|
|
20
|
+
**Built for developers. Designed for performance. Powered by purity.**
|
|
21
|
+
|
|
22
|
+
</div>
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## ๐ฅ What is STMATH?
|
|
27
|
+
|
|
28
|
+
STMATH is a **next-generation computational engine** that merges:
|
|
29
|
+
|
|
30
|
+
- ๐งฎ Core Math
|
|
31
|
+
- ๐ Scientific Functions
|
|
32
|
+
- ๐ ML & DL Metrics
|
|
33
|
+
- ๐ค GEN-AI Math (Attention, LogitsโProb, Temperature Softmax)
|
|
34
|
+
- ๐งญ Graph Algorithms
|
|
35
|
+
- ๐ Vision Utils (Conv2D Output, MaxPool Shape, IoU, NMS)
|
|
36
|
+
- โ Optimizers (Adam, RMSProp, Momentum, Cosine Anneal, SGD)
|
|
37
|
+
- ๐น Finance Math
|
|
38
|
+
- ๐ Crypto Hashing
|
|
39
|
+
- โ Quantum Gates
|
|
40
|
+
- โณ Time Series Tools
|
|
41
|
+
- ๐ Aptitude & Algebra
|
|
42
|
+
|
|
43
|
+
All in **one clean import**:
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
|
|
47
|
+
import stmath as am
|
stmath-1.0.0/README.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
# โก STMATH
|
|
4
|
+
### The Next-Generation Unified Math + AI + ML + Quantum Engine
|
|
5
|
+
**Built for developers. Designed for performance. Powered by purity.**
|
|
6
|
+
|
|
7
|
+
</div>
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## ๐ฅ What is STMATH?
|
|
12
|
+
|
|
13
|
+
STMATH is a **next-generation computational engine** that merges:
|
|
14
|
+
|
|
15
|
+
- ๐งฎ Core Math
|
|
16
|
+
- ๐ Scientific Functions
|
|
17
|
+
- ๐ ML & DL Metrics
|
|
18
|
+
- ๐ค GEN-AI Math (Attention, LogitsโProb, Temperature Softmax)
|
|
19
|
+
- ๐งญ Graph Algorithms
|
|
20
|
+
- ๐ Vision Utils (Conv2D Output, MaxPool Shape, IoU, NMS)
|
|
21
|
+
- โ Optimizers (Adam, RMSProp, Momentum, Cosine Anneal, SGD)
|
|
22
|
+
- ๐น Finance Math
|
|
23
|
+
- ๐ Crypto Hashing
|
|
24
|
+
- โ Quantum Gates
|
|
25
|
+
- โณ Time Series Tools
|
|
26
|
+
- ๐ Aptitude & Algebra
|
|
27
|
+
|
|
28
|
+
All in **one clean import**:
|
|
29
|
+
|
|
30
|
+
```python
|
|
31
|
+
|
|
32
|
+
import stmath as am
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "stmath"
|
|
7
|
+
version = "1.0.0"
|
|
8
|
+
description = "A unified Math + ML + AI + Crypto + Quantum library for Python"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { file = "LICENSE" }
|
|
11
|
+
authors = [
|
|
12
|
+
{ name="Saksham Tomar", email="tomarsaksham55@gmail.com" }
|
|
13
|
+
]
|
|
14
|
+
keywords = ["math", "ai", "ml", "deep-learning", "quantum", "statistics"]
|
|
15
|
+
requires-python = ">=3.8"
|
|
16
|
+
|
|
17
|
+
classifiers = [
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Programming Language :: Python :: 3.10",
|
|
20
|
+
"License :: OSI Approved :: MIT License",
|
|
21
|
+
"Operating System :: OS Independent",
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
dependencies = []
|
stmath-1.0.0/setup.cfg
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
[metadata]
|
|
2
|
+
name = stmath
|
|
3
|
+
version = 1.0.0
|
|
4
|
+
license = MIT
|
|
5
|
+
description = A unified Math + ML + AI + Crypto + Quantum library for Python.
|
|
6
|
+
long_description = file: README.md
|
|
7
|
+
long_description_content_type = text/markdown
|
|
8
|
+
|
|
9
|
+
[options]
|
|
10
|
+
packages = find:
|
|
11
|
+
python_requires = >=3.8
|
|
12
|
+
include_package_data = True
|
|
13
|
+
|
|
14
|
+
[egg_info]
|
|
15
|
+
tag_build =
|
|
16
|
+
tag_date = 0
|
|
17
|
+
|
stmath-1.0.0/setup.py
ADDED
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
"""
|
|
2
|
+
AIMATHX โ One-line import math + ML + AI + Crypto + Quantum library.
|
|
3
|
+
|
|
4
|
+
Example:
|
|
5
|
+
import aimath as am
|
|
6
|
+
print(am.sqrt(144))
|
|
7
|
+
print(am.mean([10, 20, 30]))
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
# =====================================================
|
|
11
|
+
# CORE
|
|
12
|
+
# =====================================================
|
|
13
|
+
from .core import add, sub, mul, div, square, cube, sqrt, power, percent, percent_change
|
|
14
|
+
|
|
15
|
+
# =====================================================
|
|
16
|
+
# SCIENTIFIC
|
|
17
|
+
# =====================================================
|
|
18
|
+
from .scientific import (
|
|
19
|
+
sin,
|
|
20
|
+
cos,
|
|
21
|
+
tan,
|
|
22
|
+
log10,
|
|
23
|
+
ln,
|
|
24
|
+
exp,
|
|
25
|
+
factorial,
|
|
26
|
+
gcd,
|
|
27
|
+
lcm,
|
|
28
|
+
deg2rad,
|
|
29
|
+
rad2deg,
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
# =====================================================
|
|
33
|
+
# STATISTICS
|
|
34
|
+
# =====================================================
|
|
35
|
+
from .statistics import mean, median, mode, variance, std, data_range, iqr, z_score
|
|
36
|
+
|
|
37
|
+
# =====================================================
|
|
38
|
+
# PROBABILITY
|
|
39
|
+
# =====================================================
|
|
40
|
+
from .probability import nCr, nPr, bayes, expected_value
|
|
41
|
+
from .distributions import (
|
|
42
|
+
normal_pdf,
|
|
43
|
+
normal_cdf,
|
|
44
|
+
bernoulli_pmf,
|
|
45
|
+
binomial_pmf,
|
|
46
|
+
poisson_pmf,
|
|
47
|
+
exponential_pdf,
|
|
48
|
+
uniform_pdf,
|
|
49
|
+
t_pdf,
|
|
50
|
+
chi_square_pdf,
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
# =====================================================
|
|
54
|
+
# MACHINE LEARNING
|
|
55
|
+
# =====================================================
|
|
56
|
+
from .ml import mse, rmse, mae, accuracy, precision, recall, f1_score, r2_score
|
|
57
|
+
|
|
58
|
+
# =====================================================
|
|
59
|
+
# DEEP LEARNING
|
|
60
|
+
# =====================================================
|
|
61
|
+
from .dl import (
|
|
62
|
+
sigmoid,
|
|
63
|
+
relu,
|
|
64
|
+
tanh,
|
|
65
|
+
softmax,
|
|
66
|
+
entropy,
|
|
67
|
+
kl_divergence,
|
|
68
|
+
binary_cross_entropy,
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
# =====================================================
|
|
72
|
+
# NLP
|
|
73
|
+
# =====================================================
|
|
74
|
+
from .nlp import (
|
|
75
|
+
term_frequency,
|
|
76
|
+
inverse_document_frequency,
|
|
77
|
+
tfidf,
|
|
78
|
+
cosine_similarity,
|
|
79
|
+
perplexity,
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
# =====================================================
|
|
83
|
+
# TIME SERIES
|
|
84
|
+
# =====================================================
|
|
85
|
+
from .timeseries import sma, ema
|
|
86
|
+
|
|
87
|
+
# =====================================================
|
|
88
|
+
# FINANCE
|
|
89
|
+
# =====================================================
|
|
90
|
+
from .finance_math import simple_interest, compound_interest, loan_emi
|
|
91
|
+
|
|
92
|
+
# =====================================================
|
|
93
|
+
# CRYPTO / BLOCKCHAIN
|
|
94
|
+
# =====================================================
|
|
95
|
+
from .crypto_math import sha256, gas_fee
|
|
96
|
+
|
|
97
|
+
# =====================================================
|
|
98
|
+
# QUANTUM
|
|
99
|
+
# =====================================================
|
|
100
|
+
from .quantum_math import hadamard, pauli_x
|
|
101
|
+
|
|
102
|
+
# =====================================================
|
|
103
|
+
# APTITUDE
|
|
104
|
+
# =====================================================
|
|
105
|
+
from .aptitude_math import profit_percent, loss_percent, avg_speed
|
|
106
|
+
|
|
107
|
+
# =====================================================
|
|
108
|
+
# ALGEBRA
|
|
109
|
+
# =====================================================
|
|
110
|
+
from .algebra import solve_linear, quadratic_roots
|
|
111
|
+
|
|
112
|
+
# =====================================================
|
|
113
|
+
# GEN-AI (Phase 2)
|
|
114
|
+
# =====================================================
|
|
115
|
+
from .genai import logits_to_prob, softmax_temperature, attention_scores
|
|
116
|
+
|
|
117
|
+
# =====================================================
|
|
118
|
+
# OPTIMIZATION (Phase 2)
|
|
119
|
+
# =====================================================
|
|
120
|
+
from .optimization import (
|
|
121
|
+
sgd_update,
|
|
122
|
+
adam_update,
|
|
123
|
+
rmsprop_update,
|
|
124
|
+
lr_step_decay,
|
|
125
|
+
lr_cosine_anneal,
|
|
126
|
+
momentum_update,
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
# =====================================================
|
|
130
|
+
# GRAPH (Phase 2)
|
|
131
|
+
# =====================================================
|
|
132
|
+
from .graph import (
|
|
133
|
+
bfs_distance,
|
|
134
|
+
dijkstra_shortest_path,
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
# =====================================================
|
|
138
|
+
# VISION (Phase 2)
|
|
139
|
+
# =====================================================
|
|
140
|
+
from .vision import conv2d_output_shape, maxpool_output_shape, iou, nms
|
|
141
|
+
|
|
142
|
+
# =====================================================
|
|
143
|
+
# FULL MATH EXTENSION (Python math module exposed)
|
|
144
|
+
# =====================================================
|
|
145
|
+
from .math_ext import *
|
|
146
|
+
|
|
147
|
+
# =====================================================
|
|
148
|
+
# BENCHMARK + DOCS
|
|
149
|
+
# =====================================================
|
|
150
|
+
from .benchmark import timeit, mem_profile
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
# =====================================================
|
|
154
|
+
# EXPORTED API LIST
|
|
155
|
+
# =====================================================
|
|
156
|
+
__all__ = [
|
|
157
|
+
# core
|
|
158
|
+
"add",
|
|
159
|
+
"sub",
|
|
160
|
+
"mul",
|
|
161
|
+
"div",
|
|
162
|
+
"square",
|
|
163
|
+
"cube",
|
|
164
|
+
"sqrt",
|
|
165
|
+
"power",
|
|
166
|
+
"percent",
|
|
167
|
+
"percent_change",
|
|
168
|
+
# scientific
|
|
169
|
+
"sin",
|
|
170
|
+
"cos",
|
|
171
|
+
"tan",
|
|
172
|
+
"log10",
|
|
173
|
+
"ln",
|
|
174
|
+
"exp",
|
|
175
|
+
"factorial",
|
|
176
|
+
"gcd",
|
|
177
|
+
"lcm",
|
|
178
|
+
"deg2rad",
|
|
179
|
+
"rad2deg",
|
|
180
|
+
# statistics
|
|
181
|
+
"mean",
|
|
182
|
+
"median",
|
|
183
|
+
"mode",
|
|
184
|
+
"variance",
|
|
185
|
+
"std",
|
|
186
|
+
"data_range",
|
|
187
|
+
"iqr",
|
|
188
|
+
"z_score",
|
|
189
|
+
# probability
|
|
190
|
+
"nCr",
|
|
191
|
+
"nPr",
|
|
192
|
+
"bayes",
|
|
193
|
+
"expected_value",
|
|
194
|
+
# distributions
|
|
195
|
+
"normal_pdf",
|
|
196
|
+
"normal_cdf",
|
|
197
|
+
"bernoulli_pmf",
|
|
198
|
+
"binomial_pmf",
|
|
199
|
+
"poisson_pmf",
|
|
200
|
+
"exponential_pdf",
|
|
201
|
+
"uniform_pdf",
|
|
202
|
+
"t_pdf",
|
|
203
|
+
"chi_square_pdf",
|
|
204
|
+
# ML
|
|
205
|
+
"mse",
|
|
206
|
+
"rmse",
|
|
207
|
+
"mae",
|
|
208
|
+
"accuracy",
|
|
209
|
+
"precision",
|
|
210
|
+
"recall",
|
|
211
|
+
"f1_score",
|
|
212
|
+
"r2_score",
|
|
213
|
+
# DL
|
|
214
|
+
"sigmoid",
|
|
215
|
+
"relu",
|
|
216
|
+
"tanh",
|
|
217
|
+
"softmax",
|
|
218
|
+
"entropy",
|
|
219
|
+
"kl_divergence",
|
|
220
|
+
"binary_cross_entropy",
|
|
221
|
+
# NLP
|
|
222
|
+
"term_frequency",
|
|
223
|
+
"inverse_document_frequency",
|
|
224
|
+
"tfidf",
|
|
225
|
+
"cosine_similarity",
|
|
226
|
+
"perplexity",
|
|
227
|
+
# time series
|
|
228
|
+
"sma",
|
|
229
|
+
"ema",
|
|
230
|
+
# finance
|
|
231
|
+
"simple_interest",
|
|
232
|
+
"compound_interest",
|
|
233
|
+
"loan_emi",
|
|
234
|
+
# crypto
|
|
235
|
+
"sha256",
|
|
236
|
+
"gas_fee",
|
|
237
|
+
# quantum
|
|
238
|
+
"hadamard",
|
|
239
|
+
"pauli_x",
|
|
240
|
+
# aptitude
|
|
241
|
+
"profit_percent",
|
|
242
|
+
"loss_percent",
|
|
243
|
+
"avg_speed",
|
|
244
|
+
# algebra
|
|
245
|
+
"solve_linear",
|
|
246
|
+
"quadratic_roots",
|
|
247
|
+
# GEN-AI
|
|
248
|
+
"logits_to_prob",
|
|
249
|
+
"softmax_temperature",
|
|
250
|
+
"attention_scores",
|
|
251
|
+
# optimization
|
|
252
|
+
"sgd_update",
|
|
253
|
+
"adam_update",
|
|
254
|
+
"rmsprop_update",
|
|
255
|
+
"lr_step_decay",
|
|
256
|
+
"lr_cosine_anneal",
|
|
257
|
+
"momentum_update",
|
|
258
|
+
# graph
|
|
259
|
+
"bfs_distance",
|
|
260
|
+
"dijkstra_shortest_path",
|
|
261
|
+
# vision
|
|
262
|
+
"conv2d_output_shape",
|
|
263
|
+
"maxpool_output_shape",
|
|
264
|
+
"iou",
|
|
265
|
+
"nms",
|
|
266
|
+
# math_ext (all math.* names are included automatically)
|
|
267
|
+
# benchmark
|
|
268
|
+
"timeit",
|
|
269
|
+
"mem_profile",
|
|
270
|
+
]
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import cmath
|
|
2
|
+
from typing import Tuple
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def solve_linear(a: float, b: float) -> float:
|
|
6
|
+
"""Solve ax + b = 0"""
|
|
7
|
+
if a == 0:
|
|
8
|
+
raise ZeroDivisionError("a cannot be zero in solve_linear.")
|
|
9
|
+
return -b / a
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def quadratic_roots(a: float, b: float, c: float) -> Tuple[complex, complex]:
|
|
13
|
+
"""Solve ax^2 + bx + c = 0 (supports real + complex roots)."""
|
|
14
|
+
if a == 0:
|
|
15
|
+
raise ZeroDivisionError("a cannot be zero in quadratic_roots.")
|
|
16
|
+
|
|
17
|
+
D = b**2 - 4 * a * c
|
|
18
|
+
sqrtD = cmath.sqrt(D)
|
|
19
|
+
|
|
20
|
+
x1 = (-b + sqrtD) / (2 * a)
|
|
21
|
+
x2 = (-b - sqrtD) / (2 * a)
|
|
22
|
+
|
|
23
|
+
return x1, x2
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
from .core import percent
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def profit_percent(cp, sp):
|
|
5
|
+
if cp == 0:
|
|
6
|
+
raise ZeroDivisionError("cp cannot be zero.")
|
|
7
|
+
return (sp - cp) * 100.0 / cp
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def loss_percent(cp, sp):
|
|
11
|
+
if cp == 0:
|
|
12
|
+
raise ZeroDivisionError("cp cannot be zero.")
|
|
13
|
+
return (cp - sp) * 100.0 / cp
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def avg_speed(distance1, speed1, distance2, speed2):
|
|
17
|
+
"""Average speed over two equal distances with different speeds."""
|
|
18
|
+
time = distance1 / speed1 + distance2 / speed2
|
|
19
|
+
total_dist = distance1 + distance2
|
|
20
|
+
return total_dist / time
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# \"\"\"Simple micro-benchmark helpers\"\"\"
|
|
2
|
+
import time, tracemalloc
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def timeit(fn, *args, runs=100, **kwargs):
|
|
6
|
+
t0 = time.perf_counter()
|
|
7
|
+
for _ in range(runs):
|
|
8
|
+
fn(*args, **kwargs)
|
|
9
|
+
t1 = time.perf_counter()
|
|
10
|
+
return (t1 - t0) / runs
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def mem_profile(fn, *args, **kwargs):
|
|
14
|
+
tracemalloc.start()
|
|
15
|
+
fn(*args, **kwargs)
|
|
16
|
+
current, peak = tracemalloc.get_traced_memory()
|
|
17
|
+
tracemalloc.stop()
|
|
18
|
+
return current, peak
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# combinatorics.py
|
|
2
|
+
|
|
3
|
+
import math
|
|
4
|
+
from typing import Iterable
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def comb(n: int, r: int) -> int:
|
|
8
|
+
"""n choose r"""
|
|
9
|
+
if r < 0 or r > n:
|
|
10
|
+
return 0
|
|
11
|
+
return math.comb(n, r)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def perm(n: int, r: int) -> int:
|
|
15
|
+
"""n permute r"""
|
|
16
|
+
if r < 0 or r > n:
|
|
17
|
+
return 0
|
|
18
|
+
return math.perm(n, r)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def bayes(p_a: float, p_b: float, p_b_given_a: float) -> float:
|
|
22
|
+
"""P(A|B) = P(B|A)P(A) / P(B)"""
|
|
23
|
+
if p_b == 0:
|
|
24
|
+
raise ZeroDivisionError("P(B) cannot be zero")
|
|
25
|
+
return (p_b_given_a * p_a) / p_b
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def entropy(p: Iterable[float], base: float = 2.0) -> float:
|
|
29
|
+
"""Shannon entropy of probability list p"""
|
|
30
|
+
import math
|
|
31
|
+
|
|
32
|
+
H = 0.0
|
|
33
|
+
for pi in p:
|
|
34
|
+
if pi > 0:
|
|
35
|
+
H -= pi * math.log(pi, base)
|
|
36
|
+
return H
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def kl_divergence(p: Iterable[float], q: Iterable[float], base: float = 2.0) -> float:
|
|
40
|
+
"""KL divergence D(P||Q)"""
|
|
41
|
+
import math
|
|
42
|
+
|
|
43
|
+
p = list(p)
|
|
44
|
+
q = list(q)
|
|
45
|
+
if len(p) != len(q):
|
|
46
|
+
raise ValueError("p and q must have same length")
|
|
47
|
+
total = 0.0
|
|
48
|
+
for pi, qi in zip(p, q):
|
|
49
|
+
if pi == 0:
|
|
50
|
+
continue
|
|
51
|
+
if qi == 0:
|
|
52
|
+
return float("inf")
|
|
53
|
+
total += pi * math.log(pi / qi, base)
|
|
54
|
+
return total
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import math
|
|
2
|
+
from typing import Iterable, List
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def add(a, b):
|
|
6
|
+
return a + b
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def sub(a, b):
|
|
10
|
+
return a - b
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def mul(a, b):
|
|
14
|
+
return a * b
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def div(a, b):
|
|
18
|
+
if b == 0:
|
|
19
|
+
raise ZeroDivisionError("Division by zero is not allowed.")
|
|
20
|
+
return a / b
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def square(x):
|
|
24
|
+
return x * x
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def cube(x):
|
|
28
|
+
return x * x * x
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def sqrt(x):
|
|
32
|
+
if x < 0:
|
|
33
|
+
raise ValueError("sqrt() only defined for non-negative numbers.")
|
|
34
|
+
return math.sqrt(x)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def power(x, n):
|
|
38
|
+
return x**n
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def percent(part, whole):
|
|
42
|
+
if whole == 0:
|
|
43
|
+
raise ZeroDivisionError("whole cannot be zero in percent().")
|
|
44
|
+
return (part / whole) * 100.0
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def percent_change(old, new):
|
|
48
|
+
if old == 0:
|
|
49
|
+
raise ZeroDivisionError("old value cannot be zero in percent_change().")
|
|
50
|
+
return ((new - old) / old) * 100.0
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import hashlib
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def sha256(text: str) -> str:
|
|
5
|
+
return hashlib.sha256(text.encode("utf-8")).hexdigest()
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def gas_fee(gas_used: int, gwei: float, eth_price: float) -> float:
|
|
9
|
+
"""
|
|
10
|
+
Rough ETH gas fee in USD.
|
|
11
|
+
gas_used: e.g. 21000
|
|
12
|
+
gwei: gas price in gwei
|
|
13
|
+
eth_price: price of 1 ETH in USD
|
|
14
|
+
"""
|
|
15
|
+
# 1 gwei = 1e-9 ETH
|
|
16
|
+
eth_fee = gas_used * gwei * 1e-9
|
|
17
|
+
return eth_fee * eth_price
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import math
|
|
2
|
+
from .probability import nCr
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
# ---------- Normal ----------
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def normal_pdf(x: float, mu: float = 0.0, sigma: float = 1.0) -> float:
|
|
9
|
+
if sigma <= 0:
|
|
10
|
+
raise ValueError("sigma must be positive.")
|
|
11
|
+
coef = 1.0 / (sigma * math.sqrt(2.0 * math.pi))
|
|
12
|
+
expo = -((x - mu) ** 2) / (2.0 * sigma**2)
|
|
13
|
+
return coef * math.exp(expo)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def normal_cdf(x: float, mu: float = 0.0, sigma: float = 1.0) -> float:
|
|
17
|
+
if sigma <= 0:
|
|
18
|
+
raise ValueError("sigma must be positive.")
|
|
19
|
+
z = (x - mu) / (sigma * math.sqrt(2.0))
|
|
20
|
+
return 0.5 * (1.0 + math.erf(z))
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
# ---------- Discrete ----------
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def bernoulli_pmf(k: int, p: float) -> float:
|
|
27
|
+
if not 0 <= p <= 1:
|
|
28
|
+
raise ValueError("p must be in [0,1].")
|
|
29
|
+
if k == 1:
|
|
30
|
+
return p
|
|
31
|
+
if k == 0:
|
|
32
|
+
return 1 - p
|
|
33
|
+
return 0.0
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def binomial_pmf(k: int, n: int, p: float) -> float:
|
|
37
|
+
if not 0 <= p <= 1:
|
|
38
|
+
raise ValueError("p must be in [0,1].")
|
|
39
|
+
return nCr(n, k) * (p**k) * ((1 - p) ** (n - k))
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def poisson_pmf(k: int, lam: float) -> float:
|
|
43
|
+
if k < 0:
|
|
44
|
+
return 0.0
|
|
45
|
+
return (lam**k) * math.exp(-lam) / math.factorial(k)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
# ---------- Continuous ----------
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def exponential_pdf(x: float, lam: float) -> float:
|
|
52
|
+
if lam <= 0:
|
|
53
|
+
raise ValueError("lambda must be positive.")
|
|
54
|
+
if x < 0:
|
|
55
|
+
return 0.0
|
|
56
|
+
return lam * math.exp(-lam * x)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def uniform_pdf(x: float, a: float, b: float) -> float:
|
|
60
|
+
if b <= a:
|
|
61
|
+
raise ValueError("require b > a in uniform_pdf.")
|
|
62
|
+
if a <= x <= b:
|
|
63
|
+
return 1.0 / (b - a)
|
|
64
|
+
return 0.0
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def t_pdf(x: float, df: int) -> float:
|
|
68
|
+
"""Student t-distribution pdf."""
|
|
69
|
+
if df <= 0:
|
|
70
|
+
raise ValueError("df must be positive.")
|
|
71
|
+
num = math.gamma((df + 1) / 2)
|
|
72
|
+
den = math.sqrt(df * math.pi) * math.gamma(df / 2)
|
|
73
|
+
return num / den * (1 + (x**2) / df) ** (-(df + 1) / 2)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def chi_square_pdf(x: float, k: int) -> float:
|
|
77
|
+
if k <= 0:
|
|
78
|
+
raise ValueError("k must be positive.")
|
|
79
|
+
if x < 0:
|
|
80
|
+
return 0.0
|
|
81
|
+
num = x ** (k / 2 - 1) * math.exp(-x / 2)
|
|
82
|
+
den = (2 ** (k / 2)) * math.gamma(k / 2)
|
|
83
|
+
return num / den
|