randcraft 0.1.4__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.
- randcraft-0.1.4/.github/workflows/publish.yml +76 -0
- randcraft-0.1.4/.github/workflows/tests.yml +41 -0
- randcraft-0.1.4/.gitignore +179 -0
- randcraft-0.1.4/.python-version +1 -0
- randcraft-0.1.4/.vscode/settings.json +34 -0
- randcraft-0.1.4/LICENSE +21 -0
- randcraft-0.1.4/PKG-INFO +159 -0
- randcraft-0.1.4/README.md +147 -0
- randcraft-0.1.4/images/double_normal.png +0 -0
- randcraft-0.1.4/images/mixture.png +0 -0
- randcraft-0.1.4/main.py +6 -0
- randcraft-0.1.4/pyproject.toml +73 -0
- randcraft-0.1.4/randcraft/__init__.py +23 -0
- randcraft-0.1.4/randcraft/anonymous.py +26 -0
- randcraft-0.1.4/randcraft/constructors.py +77 -0
- randcraft-0.1.4/randcraft/misc.py +16 -0
- randcraft-0.1.4/randcraft/models.py +272 -0
- randcraft-0.1.4/randcraft/observations.py +67 -0
- randcraft-0.1.4/randcraft/pdf_convolver.py +122 -0
- randcraft-0.1.4/randcraft/random_variable.py +112 -0
- randcraft-0.1.4/randcraft/rvs/__init__.py +22 -0
- randcraft-0.1.4/randcraft/rvs/anonymous.py +44 -0
- randcraft-0.1.4/randcraft/rvs/base.py +214 -0
- randcraft-0.1.4/randcraft/rvs/continuous.py +82 -0
- randcraft-0.1.4/randcraft/rvs/discrete.py +114 -0
- randcraft-0.1.4/randcraft/rvs/gaussian_kde.py +55 -0
- randcraft-0.1.4/randcraft/rvs/mixture.py +133 -0
- randcraft-0.1.4/randcraft/rvs/multi.py +131 -0
- randcraft-0.1.4/randcraft/rvs/scipy_pdf.py +103 -0
- randcraft-0.1.4/randcraft/utils.py +20 -0
- randcraft-0.1.4/tests/base_test_case.py +12 -0
- randcraft-0.1.4/tests/test_anon.py +26 -0
- randcraft-0.1.4/tests/test_combining_rvs.py +148 -0
- randcraft-0.1.4/tests/test_core_rvs.py +118 -0
- randcraft-0.1.4/tests/test_misc.py +26 -0
- randcraft-0.1.4/tests/test_observations.py +20 -0
- randcraft-0.1.4/tests/test_plotting.py +34 -0
- randcraft-0.1.4/tests/test_scipy_rvs.py +24 -0
- randcraft-0.1.4/uv.lock +1180 -0
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# This workflow will upload a Python Package to PyPI when a release is created
|
|
2
|
+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python#publishing-to-package-registries
|
|
3
|
+
|
|
4
|
+
# This workflow uses actions that are not certified by GitHub.
|
|
5
|
+
# They are provided by a third-party and are governed by
|
|
6
|
+
# separate terms of service, privacy policy, and support
|
|
7
|
+
# documentation.
|
|
8
|
+
|
|
9
|
+
name: Upload Python Package
|
|
10
|
+
|
|
11
|
+
on:
|
|
12
|
+
release:
|
|
13
|
+
types: [published]
|
|
14
|
+
|
|
15
|
+
permissions:
|
|
16
|
+
contents: read
|
|
17
|
+
|
|
18
|
+
jobs:
|
|
19
|
+
release-build:
|
|
20
|
+
runs-on: ubuntu-latest
|
|
21
|
+
|
|
22
|
+
steps:
|
|
23
|
+
- uses: actions/checkout@v4
|
|
24
|
+
|
|
25
|
+
- uses: actions/setup-python@v5
|
|
26
|
+
with:
|
|
27
|
+
python-version: "3.x"
|
|
28
|
+
|
|
29
|
+
- name: Install uv
|
|
30
|
+
uses: astral-sh/setup-uv@v6
|
|
31
|
+
with:
|
|
32
|
+
# Install a specific version of uv.
|
|
33
|
+
version: "0.8.16"
|
|
34
|
+
|
|
35
|
+
- name: Install the project
|
|
36
|
+
run: uv sync --locked --all-extras --dev
|
|
37
|
+
|
|
38
|
+
- name: Build release distributions
|
|
39
|
+
run: |
|
|
40
|
+
uv build
|
|
41
|
+
|
|
42
|
+
- name: Upload distributions
|
|
43
|
+
uses: actions/upload-artifact@v4
|
|
44
|
+
with:
|
|
45
|
+
name: release-dists
|
|
46
|
+
path: dist/
|
|
47
|
+
|
|
48
|
+
pypi-publish:
|
|
49
|
+
runs-on: ubuntu-latest
|
|
50
|
+
needs:
|
|
51
|
+
- release-build
|
|
52
|
+
permissions:
|
|
53
|
+
# IMPORTANT: this permission is mandatory for trusted publishing
|
|
54
|
+
id-token: write
|
|
55
|
+
|
|
56
|
+
# Dedicated environments with protections for publishing are strongly recommended.
|
|
57
|
+
# For more information, see: https://docs.github.com/en/actions/deployment/targeting-different-environments/using-environments-for-deployment#deployment-protection-rules
|
|
58
|
+
environment:
|
|
59
|
+
name: pypi
|
|
60
|
+
url: https://pypi.org/project/randcraft
|
|
61
|
+
#
|
|
62
|
+
# ALTERNATIVE: if your GitHub Release name is the PyPI project version string
|
|
63
|
+
# ALTERNATIVE: exactly, uncomment the following line instead:
|
|
64
|
+
# url: https://pypi.org/project/YOURPROJECT/${{ github.event.release.name }}
|
|
65
|
+
|
|
66
|
+
steps:
|
|
67
|
+
- name: Retrieve release distributions
|
|
68
|
+
uses: actions/download-artifact@v4
|
|
69
|
+
with:
|
|
70
|
+
name: release-dists
|
|
71
|
+
path: dist/
|
|
72
|
+
|
|
73
|
+
- name: Publish release distributions to PyPI
|
|
74
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
75
|
+
with:
|
|
76
|
+
packages-dir: dist/
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# This workflow will install Python dependencies, run tests and lint with a single version of Python
|
|
2
|
+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python
|
|
3
|
+
|
|
4
|
+
name: Python application
|
|
5
|
+
|
|
6
|
+
on:
|
|
7
|
+
push:
|
|
8
|
+
branches: [ "main" ]
|
|
9
|
+
pull_request:
|
|
10
|
+
branches: [ "main" ]
|
|
11
|
+
|
|
12
|
+
permissions:
|
|
13
|
+
contents: read
|
|
14
|
+
|
|
15
|
+
jobs:
|
|
16
|
+
build:
|
|
17
|
+
runs-on: ubuntu-latest
|
|
18
|
+
|
|
19
|
+
steps:
|
|
20
|
+
- uses: actions/checkout@v4
|
|
21
|
+
|
|
22
|
+
- name: Set up Python 3.12
|
|
23
|
+
uses: actions/setup-python@v3
|
|
24
|
+
with:
|
|
25
|
+
python-version: "3.12"
|
|
26
|
+
|
|
27
|
+
- name: Install uv
|
|
28
|
+
uses: astral-sh/setup-uv@v6
|
|
29
|
+
with:
|
|
30
|
+
# Install a specific version of uv.
|
|
31
|
+
version: "0.8.16"
|
|
32
|
+
|
|
33
|
+
- name: Install the project
|
|
34
|
+
run: uv sync --locked --all-extras --dev
|
|
35
|
+
|
|
36
|
+
- name: Lint with ruff
|
|
37
|
+
run: |
|
|
38
|
+
uv run ruff check randcraft --output-format=full
|
|
39
|
+
|
|
40
|
+
- name: Test with pytest
|
|
41
|
+
run: uv run pytest tests
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
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
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
#Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# UV
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
#uv.lock
|
|
102
|
+
|
|
103
|
+
# poetry
|
|
104
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
105
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
106
|
+
# commonly ignored for libraries.
|
|
107
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
108
|
+
#poetry.lock
|
|
109
|
+
|
|
110
|
+
# pdm
|
|
111
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
112
|
+
#pdm.lock
|
|
113
|
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
|
114
|
+
# in version control.
|
|
115
|
+
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
|
|
116
|
+
.pdm.toml
|
|
117
|
+
.pdm-python
|
|
118
|
+
.pdm-build/
|
|
119
|
+
|
|
120
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
121
|
+
__pypackages__/
|
|
122
|
+
|
|
123
|
+
# Celery stuff
|
|
124
|
+
celerybeat-schedule
|
|
125
|
+
celerybeat.pid
|
|
126
|
+
|
|
127
|
+
# SageMath parsed files
|
|
128
|
+
*.sage.py
|
|
129
|
+
|
|
130
|
+
# Environments
|
|
131
|
+
.env
|
|
132
|
+
.venv
|
|
133
|
+
env/
|
|
134
|
+
venv/
|
|
135
|
+
ENV/
|
|
136
|
+
env.bak/
|
|
137
|
+
venv.bak/
|
|
138
|
+
|
|
139
|
+
# Spyder project settings
|
|
140
|
+
.spyderproject
|
|
141
|
+
.spyproject
|
|
142
|
+
|
|
143
|
+
# Rope project settings
|
|
144
|
+
.ropeproject
|
|
145
|
+
|
|
146
|
+
# mkdocs documentation
|
|
147
|
+
/site
|
|
148
|
+
|
|
149
|
+
# mypy
|
|
150
|
+
.mypy_cache/
|
|
151
|
+
.dmypy.json
|
|
152
|
+
dmypy.json
|
|
153
|
+
|
|
154
|
+
# Pyre type checker
|
|
155
|
+
.pyre/
|
|
156
|
+
|
|
157
|
+
# pytype static type analyzer
|
|
158
|
+
.pytype/
|
|
159
|
+
|
|
160
|
+
# Cython debug symbols
|
|
161
|
+
cython_debug/
|
|
162
|
+
|
|
163
|
+
# PyCharm
|
|
164
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
165
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
166
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
167
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
168
|
+
#.idea/
|
|
169
|
+
|
|
170
|
+
# Ruff stuff:
|
|
171
|
+
.ruff_cache/
|
|
172
|
+
|
|
173
|
+
# PyPI configuration file
|
|
174
|
+
.pypirc
|
|
175
|
+
|
|
176
|
+
# IDE
|
|
177
|
+
.idea/
|
|
178
|
+
src/game_cache/*.json
|
|
179
|
+
src/game_cache/*.html
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
3.12
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"python.testing.unittestArgs": [
|
|
3
|
+
"-v",
|
|
4
|
+
"-s",
|
|
5
|
+
"./tests",
|
|
6
|
+
"-p",
|
|
7
|
+
"test*.py"
|
|
8
|
+
],
|
|
9
|
+
"python.testing.pytestEnabled": false,
|
|
10
|
+
"python.testing.unittestEnabled": true,
|
|
11
|
+
"python.terminal.activateEnvironment": true,
|
|
12
|
+
"python.analysis.typeCheckingMode": "basic",
|
|
13
|
+
"python.analysis.diagnosticMode": "workspace",
|
|
14
|
+
"[python]": {
|
|
15
|
+
"editor.defaultFormatter": "charliermarsh.ruff",
|
|
16
|
+
"editor.codeActionsOnSave": {
|
|
17
|
+
"source.organizeImports": "always",
|
|
18
|
+
"source.sortImports": "always",
|
|
19
|
+
"source.unusedImports": "always"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"autoDocstring.generateDocstringOnEnter": true,
|
|
23
|
+
"autoDocstring.docstringFormat": "google",
|
|
24
|
+
"terminal.integrated.env.windows": {
|
|
25
|
+
"PYTHONPATH": "${workspaceFolder}"
|
|
26
|
+
},
|
|
27
|
+
"workbench.tree.indent": 20,
|
|
28
|
+
"editor.semanticTokenColorCustomizations": {
|
|
29
|
+
"rules": {
|
|
30
|
+
"selfParameter": "#e181ff",
|
|
31
|
+
"clsParameter": "#e181ff",
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
randcraft-0.1.4/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Robbie Muir
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
randcraft-0.1.4/PKG-INFO
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: randcraft
|
|
3
|
+
Version: 0.1.4
|
|
4
|
+
Summary: An object-oriented approach to defining and combining random variables
|
|
5
|
+
License-File: LICENSE
|
|
6
|
+
Requires-Python: >=3.12
|
|
7
|
+
Requires-Dist: matplotlib>=3.10.6
|
|
8
|
+
Requires-Dist: numpy>=2.3.3
|
|
9
|
+
Requires-Dist: pandas>=2.3.0
|
|
10
|
+
Requires-Dist: scipy>=1.16.1
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
|
|
13
|
+
# RandCraft
|
|
14
|
+
|
|
15
|
+
RandCraft is a Python library for object-oriented combination and manipulation of univariate random variables, built on top of the [scipy.stats](https://docs.scipy.org/doc/scipy/reference/stats.html) module.
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
## Usage Example
|
|
19
|
+
Have you ever wanted to add together random variables but can't be bothered working out an analytical solution?
|
|
20
|
+
Randcraft makes it simple.
|
|
21
|
+
|
|
22
|
+
```python
|
|
23
|
+
from randcraft import make_normal, make_coin_flip
|
|
24
|
+
|
|
25
|
+
coin_flip = make_coin_flip()
|
|
26
|
+
# <RandomVariable(discrete): mean=0.5, var=0.25>
|
|
27
|
+
norm = make_normal(mean=0, std_dev=0.2)
|
|
28
|
+
# <RandomVariable(normal): mean=0.0, var=0.04>
|
|
29
|
+
combined = coin_flip + norm
|
|
30
|
+
# <RandomVariable(mixture): mean=0.5, var=0.29>
|
|
31
|
+
combined.sample_one()
|
|
32
|
+
# 0.8678903828104276
|
|
33
|
+
combined.plot()
|
|
34
|
+
```
|
|
35
|
+

|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
## Features
|
|
39
|
+
|
|
40
|
+
- **Object-oriented random variables:** Wrap and combine distributions as Python objects.
|
|
41
|
+
- **Distribution composition:** Add, multiply, and transform random variables.
|
|
42
|
+
- **Sampling and statistics:** Easily sample from composed distributions and compute statistics.
|
|
43
|
+
- **Extensible:** Supports custom distributions via subclassing.
|
|
44
|
+
- **Integration with scipy.stats:** Use any frozen continuous distribution from scipy stats
|
|
45
|
+
|
|
46
|
+
## Supported Distributions
|
|
47
|
+
|
|
48
|
+
RandCraft currently supports the following distributions:
|
|
49
|
+
|
|
50
|
+
- Normal, Uniform, Beta, Gamma + any other parametric continuous distribution from scipy.stats
|
|
51
|
+
- Discrete
|
|
52
|
+
- DiracDelta
|
|
53
|
+
- Gaussian kde distribution from provided observations
|
|
54
|
+
- Mixture distributions
|
|
55
|
+
- Anonymous distribution functions based on a provided sampler function
|
|
56
|
+
|
|
57
|
+
Distributions can all be combined arbitrarily with addition and subtraction.
|
|
58
|
+
The library will simplify the new distribution analytically where possible, and use numerical approaches otherwise.
|
|
59
|
+
|
|
60
|
+
You can also extend RandCraft with your own custom distributions.
|
|
61
|
+
|
|
62
|
+
## Installation
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
pip install randcraft
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## API Overview
|
|
69
|
+
|
|
70
|
+
- `make_normal`, `make_uniform` ...etc: Create a random variable
|
|
71
|
+
- Addition subtraction with constants or other RVs: `+`, `-`
|
|
72
|
+
- `.sample_numpy(size)`: Draw samples
|
|
73
|
+
- `.get_mean()`, `.get_variance()`: Get statistics
|
|
74
|
+
- `.cdf(x)`: Evaluate cdf at points
|
|
75
|
+
- `.ppf(x)`: Evaluate inverse of cdf at points
|
|
76
|
+
- `.plot()`: Take a look at your distribution
|
|
77
|
+
|
|
78
|
+
## More Examples
|
|
79
|
+
### Combining dice rolls
|
|
80
|
+
```python
|
|
81
|
+
from randcraft.constructors import make_die_roll
|
|
82
|
+
|
|
83
|
+
die = make_die_roll(sides=6)
|
|
84
|
+
# <RandomVariable(discrete): mean=3.5, var=2.92>
|
|
85
|
+
three_dice = die.multi_sample(3)
|
|
86
|
+
# <RandomVariable(discrete): mean=10.5, var=8.75>
|
|
87
|
+
three_dice.cdf(10.0)
|
|
88
|
+
# 0.5
|
|
89
|
+
three_dice.ppf(0.5)
|
|
90
|
+
# 10.0
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Using arbitrary parametric continuous distribution from scipy.stats
|
|
94
|
+
```python
|
|
95
|
+
from scipy.stats import uniform
|
|
96
|
+
from randcraft.constructors import make_scipy
|
|
97
|
+
|
|
98
|
+
rv = make_scipy(uniform, loc=1, scale=2)
|
|
99
|
+
# <RandomVariable(scipy-uniform): mean=2.0, var=0.333>
|
|
100
|
+
b = rv.scale(2.0)
|
|
101
|
+
# <RandomVariable(scipy-uniform): mean=4.0, var=1.33>
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Kernel density estimation and combination
|
|
105
|
+
You have observations of two independent random variables. You want to use kernal density estimation to create continuous random variables for each and then add them together.
|
|
106
|
+
```python
|
|
107
|
+
import numpy as np
|
|
108
|
+
from randcraft.observations import make_gaussian_kde
|
|
109
|
+
|
|
110
|
+
observations_a = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
|
|
111
|
+
observations_b = np.array([1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0])
|
|
112
|
+
rv_a = make_gaussian_kde(observations=observations_a, bw_method=0.1)
|
|
113
|
+
# <RandomVariable(multi): mean=3.0, var=2.42>
|
|
114
|
+
rv_b = make_gaussian_kde(observations=observations_b)
|
|
115
|
+
# <RandomVariable(multi): mean=0.5, var=0.676>
|
|
116
|
+
rv_joined = rv_a + rv_b
|
|
117
|
+
# <RandomVariable(multi): mean=3.5, var=3.1>
|
|
118
|
+
```
|
|
119
|
+
Uses `gaussian_kde` by `scipy.stats` under the hood. You also have the option to pass arguments for `gaussian_kde`, or provide your own kernel as a `RandomVariable`.
|
|
120
|
+
|
|
121
|
+
### Mixing continuous and discrete variables
|
|
122
|
+
You have observations of two independent random variables. You want to use kernal density estimation to create continuous random variables for each and then add them together.
|
|
123
|
+
```python
|
|
124
|
+
from randcraft.constructors import make_normal, make_uniform, make_discrete
|
|
125
|
+
from randcraft.misc import mix_rvs
|
|
126
|
+
|
|
127
|
+
rv1 = make_normal(mean=0, std_dev=1)
|
|
128
|
+
# <RandomVariable(scipy-norm): mean=0.0, var=1.0>
|
|
129
|
+
rv2 = make_uniform(low=-1, high=1)
|
|
130
|
+
# <RandomVariable(scipy-uniform): mean=-0.0, var=0.333>
|
|
131
|
+
combined = rv1 + rv2
|
|
132
|
+
# <RandomVariable(multi): mean=0.0, var=1.33>
|
|
133
|
+
discrete = make_discrete(values=[1, 2, 3])
|
|
134
|
+
# <RandomVariable(discrete): mean=2.0, var=0.667>
|
|
135
|
+
|
|
136
|
+
# Make a new rv which has a random chance of drawing from one of the other 4 rvs
|
|
137
|
+
mixture = mix_rvs([rv1, rv2, combined, discrete])
|
|
138
|
+
# <RandomVariable(mixture): mean=0.5, var=1.58>
|
|
139
|
+
mixture.plot()
|
|
140
|
+
```
|
|
141
|
+

|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
## Extending RandCraft
|
|
146
|
+
|
|
147
|
+
You can create custom random variable classes by subclassing the base RV class and implementing required methods.
|
|
148
|
+
|
|
149
|
+
## Known limitations
|
|
150
|
+
|
|
151
|
+
The library is designed to work with univariate random variables only. Multi-dimensional rvs or correlations etc are not supported.
|
|
152
|
+
|
|
153
|
+
## License
|
|
154
|
+
|
|
155
|
+
MIT License
|
|
156
|
+
|
|
157
|
+
## Acknowledgements
|
|
158
|
+
|
|
159
|
+
Built on [scipy.stats](https://docs.scipy.org/doc/scipy/reference/stats.html).
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# RandCraft
|
|
2
|
+
|
|
3
|
+
RandCraft is a Python library for object-oriented combination and manipulation of univariate random variables, built on top of the [scipy.stats](https://docs.scipy.org/doc/scipy/reference/stats.html) module.
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
## Usage Example
|
|
7
|
+
Have you ever wanted to add together random variables but can't be bothered working out an analytical solution?
|
|
8
|
+
Randcraft makes it simple.
|
|
9
|
+
|
|
10
|
+
```python
|
|
11
|
+
from randcraft import make_normal, make_coin_flip
|
|
12
|
+
|
|
13
|
+
coin_flip = make_coin_flip()
|
|
14
|
+
# <RandomVariable(discrete): mean=0.5, var=0.25>
|
|
15
|
+
norm = make_normal(mean=0, std_dev=0.2)
|
|
16
|
+
# <RandomVariable(normal): mean=0.0, var=0.04>
|
|
17
|
+
combined = coin_flip + norm
|
|
18
|
+
# <RandomVariable(mixture): mean=0.5, var=0.29>
|
|
19
|
+
combined.sample_one()
|
|
20
|
+
# 0.8678903828104276
|
|
21
|
+
combined.plot()
|
|
22
|
+
```
|
|
23
|
+

|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
## Features
|
|
27
|
+
|
|
28
|
+
- **Object-oriented random variables:** Wrap and combine distributions as Python objects.
|
|
29
|
+
- **Distribution composition:** Add, multiply, and transform random variables.
|
|
30
|
+
- **Sampling and statistics:** Easily sample from composed distributions and compute statistics.
|
|
31
|
+
- **Extensible:** Supports custom distributions via subclassing.
|
|
32
|
+
- **Integration with scipy.stats:** Use any frozen continuous distribution from scipy stats
|
|
33
|
+
|
|
34
|
+
## Supported Distributions
|
|
35
|
+
|
|
36
|
+
RandCraft currently supports the following distributions:
|
|
37
|
+
|
|
38
|
+
- Normal, Uniform, Beta, Gamma + any other parametric continuous distribution from scipy.stats
|
|
39
|
+
- Discrete
|
|
40
|
+
- DiracDelta
|
|
41
|
+
- Gaussian kde distribution from provided observations
|
|
42
|
+
- Mixture distributions
|
|
43
|
+
- Anonymous distribution functions based on a provided sampler function
|
|
44
|
+
|
|
45
|
+
Distributions can all be combined arbitrarily with addition and subtraction.
|
|
46
|
+
The library will simplify the new distribution analytically where possible, and use numerical approaches otherwise.
|
|
47
|
+
|
|
48
|
+
You can also extend RandCraft with your own custom distributions.
|
|
49
|
+
|
|
50
|
+
## Installation
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
pip install randcraft
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## API Overview
|
|
57
|
+
|
|
58
|
+
- `make_normal`, `make_uniform` ...etc: Create a random variable
|
|
59
|
+
- Addition subtraction with constants or other RVs: `+`, `-`
|
|
60
|
+
- `.sample_numpy(size)`: Draw samples
|
|
61
|
+
- `.get_mean()`, `.get_variance()`: Get statistics
|
|
62
|
+
- `.cdf(x)`: Evaluate cdf at points
|
|
63
|
+
- `.ppf(x)`: Evaluate inverse of cdf at points
|
|
64
|
+
- `.plot()`: Take a look at your distribution
|
|
65
|
+
|
|
66
|
+
## More Examples
|
|
67
|
+
### Combining dice rolls
|
|
68
|
+
```python
|
|
69
|
+
from randcraft.constructors import make_die_roll
|
|
70
|
+
|
|
71
|
+
die = make_die_roll(sides=6)
|
|
72
|
+
# <RandomVariable(discrete): mean=3.5, var=2.92>
|
|
73
|
+
three_dice = die.multi_sample(3)
|
|
74
|
+
# <RandomVariable(discrete): mean=10.5, var=8.75>
|
|
75
|
+
three_dice.cdf(10.0)
|
|
76
|
+
# 0.5
|
|
77
|
+
three_dice.ppf(0.5)
|
|
78
|
+
# 10.0
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Using arbitrary parametric continuous distribution from scipy.stats
|
|
82
|
+
```python
|
|
83
|
+
from scipy.stats import uniform
|
|
84
|
+
from randcraft.constructors import make_scipy
|
|
85
|
+
|
|
86
|
+
rv = make_scipy(uniform, loc=1, scale=2)
|
|
87
|
+
# <RandomVariable(scipy-uniform): mean=2.0, var=0.333>
|
|
88
|
+
b = rv.scale(2.0)
|
|
89
|
+
# <RandomVariable(scipy-uniform): mean=4.0, var=1.33>
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Kernel density estimation and combination
|
|
93
|
+
You have observations of two independent random variables. You want to use kernal density estimation to create continuous random variables for each and then add them together.
|
|
94
|
+
```python
|
|
95
|
+
import numpy as np
|
|
96
|
+
from randcraft.observations import make_gaussian_kde
|
|
97
|
+
|
|
98
|
+
observations_a = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
|
|
99
|
+
observations_b = np.array([1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0])
|
|
100
|
+
rv_a = make_gaussian_kde(observations=observations_a, bw_method=0.1)
|
|
101
|
+
# <RandomVariable(multi): mean=3.0, var=2.42>
|
|
102
|
+
rv_b = make_gaussian_kde(observations=observations_b)
|
|
103
|
+
# <RandomVariable(multi): mean=0.5, var=0.676>
|
|
104
|
+
rv_joined = rv_a + rv_b
|
|
105
|
+
# <RandomVariable(multi): mean=3.5, var=3.1>
|
|
106
|
+
```
|
|
107
|
+
Uses `gaussian_kde` by `scipy.stats` under the hood. You also have the option to pass arguments for `gaussian_kde`, or provide your own kernel as a `RandomVariable`.
|
|
108
|
+
|
|
109
|
+
### Mixing continuous and discrete variables
|
|
110
|
+
You have observations of two independent random variables. You want to use kernal density estimation to create continuous random variables for each and then add them together.
|
|
111
|
+
```python
|
|
112
|
+
from randcraft.constructors import make_normal, make_uniform, make_discrete
|
|
113
|
+
from randcraft.misc import mix_rvs
|
|
114
|
+
|
|
115
|
+
rv1 = make_normal(mean=0, std_dev=1)
|
|
116
|
+
# <RandomVariable(scipy-norm): mean=0.0, var=1.0>
|
|
117
|
+
rv2 = make_uniform(low=-1, high=1)
|
|
118
|
+
# <RandomVariable(scipy-uniform): mean=-0.0, var=0.333>
|
|
119
|
+
combined = rv1 + rv2
|
|
120
|
+
# <RandomVariable(multi): mean=0.0, var=1.33>
|
|
121
|
+
discrete = make_discrete(values=[1, 2, 3])
|
|
122
|
+
# <RandomVariable(discrete): mean=2.0, var=0.667>
|
|
123
|
+
|
|
124
|
+
# Make a new rv which has a random chance of drawing from one of the other 4 rvs
|
|
125
|
+
mixture = mix_rvs([rv1, rv2, combined, discrete])
|
|
126
|
+
# <RandomVariable(mixture): mean=0.5, var=1.58>
|
|
127
|
+
mixture.plot()
|
|
128
|
+
```
|
|
129
|
+

|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
## Extending RandCraft
|
|
134
|
+
|
|
135
|
+
You can create custom random variable classes by subclassing the base RV class and implementing required methods.
|
|
136
|
+
|
|
137
|
+
## Known limitations
|
|
138
|
+
|
|
139
|
+
The library is designed to work with univariate random variables only. Multi-dimensional rvs or correlations etc are not supported.
|
|
140
|
+
|
|
141
|
+
## License
|
|
142
|
+
|
|
143
|
+
MIT License
|
|
144
|
+
|
|
145
|
+
## Acknowledgements
|
|
146
|
+
|
|
147
|
+
Built on [scipy.stats](https://docs.scipy.org/doc/scipy/reference/stats.html).
|
|
Binary file
|
|
Binary file
|