bezierv 0.1.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.
- bezierv-0.1.0/.gitignore +171 -0
- bezierv-0.1.0/LICENSE +21 -0
- bezierv-0.1.0/PKG-INFO +32 -0
- bezierv-0.1.0/README.md +38 -0
- bezierv-0.1.0/bezierv/__init__.py +0 -0
- bezierv-0.1.0/bezierv/algorithms/__init__.py +0 -0
- bezierv-0.1.0/bezierv/algorithms/nelder_mead.py +157 -0
- bezierv-0.1.0/bezierv/algorithms/non_linear.py +167 -0
- bezierv-0.1.0/bezierv/algorithms/proj_grad.py +132 -0
- bezierv-0.1.0/bezierv/algorithms/proj_subgrad.py +203 -0
- bezierv-0.1.0/bezierv/algorithms/utils.py +67 -0
- bezierv-0.1.0/bezierv/classes/__init__.py +0 -0
- bezierv-0.1.0/bezierv/classes/bezierv.py +579 -0
- bezierv-0.1.0/bezierv/classes/convolver.py +74 -0
- bezierv-0.1.0/bezierv/classes/distfit.py +216 -0
- bezierv-0.1.0/bezierv.egg-info/PKG-INFO +32 -0
- bezierv-0.1.0/bezierv.egg-info/SOURCES.txt +34 -0
- bezierv-0.1.0/bezierv.egg-info/dependency_links.txt +1 -0
- bezierv-0.1.0/bezierv.egg-info/requires.txt +28 -0
- bezierv-0.1.0/bezierv.egg-info/top_level.txt +1 -0
- bezierv-0.1.0/docs/assets/logo.png +0 -0
- bezierv-0.1.0/docs/index.md +80 -0
- bezierv-0.1.0/docs/reference.md +84 -0
- bezierv-0.1.0/mkdocs.yml +17 -0
- bezierv-0.1.0/noxfile.py +77 -0
- bezierv-0.1.0/pyproject.toml +49 -0
- bezierv-0.1.0/setup.cfg +4 -0
- bezierv-0.1.0/tests/__init__.py +0 -0
- bezierv-0.1.0/tests/test_algorithms/test_nelder_mead.py +55 -0
- bezierv-0.1.0/tests/test_algorithms/test_proj_grad.py +60 -0
- bezierv-0.1.0/tests/test_algorithms/test_proj_subgrad.py +64 -0
- bezierv-0.1.0/tests/test_algorithms/test_utils.py +42 -0
- bezierv-0.1.0/tests/test_classes/conftest.py +42 -0
- bezierv-0.1.0/tests/test_classes/test_bezierv.py +59 -0
- bezierv-0.1.0/tests/test_classes/test_convolver.py +38 -0
- bezierv-0.1.0/tests/test_classes/test_distfit.py +33 -0
bezierv-0.1.0/.gitignore
ADDED
@@ -0,0 +1,171 @@
|
|
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
|
+
# PyPI configuration file
|
171
|
+
.pypirc
|
bezierv-0.1.0/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025 Esteban Leiva and Andrés L. Medaglia
|
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.
|
bezierv-0.1.0/PKG-INFO
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
Metadata-Version: 2.4
|
2
|
+
Name: bezierv
|
3
|
+
Version: 0.1.0
|
4
|
+
Author-email: Esteban Leiva <e.leivam@uniandes.edu.co>, "Andrés L. Medaglia" <amedagli@uniandes.edu.co>
|
5
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
6
|
+
Classifier: Programming Language :: Python :: 3.10
|
7
|
+
Classifier: Programming Language :: Python :: 3.11
|
8
|
+
Classifier: Programming Language :: Python :: 3.12
|
9
|
+
Classifier: Programming Language :: Python :: 3.13
|
10
|
+
Requires-Python: <3.14,>=3.10
|
11
|
+
License-File: LICENSE
|
12
|
+
Requires-Dist: numpy<2,>=1.26; python_version == "3.10"
|
13
|
+
Requires-Dist: scipy<1.16,>=1.13; python_version == "3.10"
|
14
|
+
Requires-Dist: matplotlib<3.11,>=3.9; python_version == "3.10"
|
15
|
+
Requires-Dist: statsmodels<0.15,>=0.14.2; python_version == "3.10"
|
16
|
+
Requires-Dist: pyomo<7,>=6.8; python_version == "3.10"
|
17
|
+
Requires-Dist: numpy<2.3,>=1.26; python_version == "3.11"
|
18
|
+
Requires-Dist: scipy<1.17,>=1.13; python_version == "3.11"
|
19
|
+
Requires-Dist: matplotlib<3.11,>=3.9; python_version == "3.11"
|
20
|
+
Requires-Dist: statsmodels<0.15,>=0.14.2; python_version == "3.11"
|
21
|
+
Requires-Dist: pyomo<7,>=6.8; python_version == "3.11"
|
22
|
+
Requires-Dist: numpy<2.3,>=2.1; python_version == "3.12"
|
23
|
+
Requires-Dist: scipy<1.17,>=1.14.1; python_version == "3.12"
|
24
|
+
Requires-Dist: matplotlib<3.11,>=3.9; python_version == "3.12"
|
25
|
+
Requires-Dist: statsmodels<0.15,>=0.14.2; python_version == "3.12"
|
26
|
+
Requires-Dist: pyomo<7,>=6.8; python_version == "3.12"
|
27
|
+
Requires-Dist: numpy<2.4,>=2.2; python_version == "3.13"
|
28
|
+
Requires-Dist: scipy<1.17,>=1.14.1; python_version == "3.13"
|
29
|
+
Requires-Dist: matplotlib<3.11,>=3.9; python_version == "3.13"
|
30
|
+
Requires-Dist: statsmodels<0.15,>=0.14.2; python_version == "3.13"
|
31
|
+
Requires-Dist: pyomo<7,>=6.8; python_version == "3.13"
|
32
|
+
Dynamic: license-file
|
bezierv-0.1.0/README.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
<p align="center">
|
2
|
+
<!-- If you used a different path, update the src accordingly -->
|
3
|
+
<img src="docs/assets/logo.png" alt="bezierv logo" width="260"/>
|
4
|
+
</p>
|
5
|
+
|
6
|
+
<h1 align="center">bezierv</h1>
|
7
|
+
<p align="center">
|
8
|
+
<em>Fit smooth Bézier random variables to empirical data — elegant, differentiable, and fast.</em>
|
9
|
+
</p>
|
10
|
+
|
11
|
+
<p align="center">
|
12
|
+
<!-- Add real badges once you publish to PyPI / set up CI -->
|
13
|
+
<img alt="PyPI" src="https://img.shields.io/pypi/v/bezierv?style=flat-square">
|
14
|
+
<img alt="CI" src="https://img.shields.io/github/actions/workflow/status/EstebanLeiva/bezierv/ci.yml?style=flat-square">
|
15
|
+
<img alt="License" src="https://img.shields.io/github/license/EstebanLeiva/bezierv?style=flat-square">
|
16
|
+
</p>
|
17
|
+
|
18
|
+
---
|
19
|
+
|
20
|
+
## Why Bézier random variables?
|
21
|
+
Classical parametric distributions can be too rigid; non-parametric methods can be noisy.
|
22
|
+
Bézier curves offer a sweet spot: **smooth** and **shape-controlled**.
|
23
|
+
With **bezierv** you can:
|
24
|
+
|
25
|
+
* Fit Bézier CDFs/PDFs to sample data of any shape.
|
26
|
+
* Evaluate moments and quantiles.
|
27
|
+
* Compose variables via convolution.
|
28
|
+
|
29
|
+
---
|
30
|
+
|
31
|
+
## Installation
|
32
|
+
|
33
|
+
```bash
|
34
|
+
pip install bezierv # from PyPI (coming soon)
|
35
|
+
# or, for development:
|
36
|
+
git clone https://github.com/your-org/bezierv
|
37
|
+
cd bezierv
|
38
|
+
pip install -e .
|
File without changes
|
File without changes
|
@@ -0,0 +1,157 @@
|
|
1
|
+
import numpy as np
|
2
|
+
from bezierv.classes.bezierv import Bezierv
|
3
|
+
from scipy.optimize import minimize
|
4
|
+
import bezierv.algorithms.utils as utils
|
5
|
+
|
6
|
+
def objective_function(concatenated: np.array,
|
7
|
+
n: int,
|
8
|
+
m: int,
|
9
|
+
data:np.array,
|
10
|
+
bezierv: Bezierv,
|
11
|
+
emp_cdf_data: np.array) -> float:
|
12
|
+
"""
|
13
|
+
Compute the objective function value for the given control points.
|
14
|
+
|
15
|
+
This method calculates the sum of squared errors between the Bezier random variable's CDF
|
16
|
+
and the empirical CDF data.
|
17
|
+
|
18
|
+
Parameters
|
19
|
+
----------
|
20
|
+
concatenated : np.array
|
21
|
+
A concatenated array containing the control points for z and x coordinates.
|
22
|
+
The first n+1 elements are the z control points, and the remaining elements are the x control points.
|
23
|
+
n : int
|
24
|
+
The number of control points minus one for the Bezier curve.
|
25
|
+
m : int
|
26
|
+
The number of empirical CDF data points.
|
27
|
+
data : np.array
|
28
|
+
The sorted data points used to fit the Bezier distribution.
|
29
|
+
bezierv : Bezierv
|
30
|
+
An instance of the Bezierv class representing the Bezier random variable.
|
31
|
+
emp_cdf_data : np.array
|
32
|
+
The empirical CDF data points used for fitting.
|
33
|
+
|
34
|
+
Returns
|
35
|
+
-------
|
36
|
+
float
|
37
|
+
The value of the objective function (MSE).
|
38
|
+
"""
|
39
|
+
x = concatenated[0 : n + 1]
|
40
|
+
z = concatenated[n + 1:]
|
41
|
+
t = utils.get_t(n, m, data, bezierv, x)
|
42
|
+
se = 0
|
43
|
+
for j in range(m):
|
44
|
+
se += (bezierv.poly_z(t[j], z) - emp_cdf_data[j])**2
|
45
|
+
return se / m
|
46
|
+
|
47
|
+
def objective_function_lagrangian(concatenated: np.array,
|
48
|
+
n: int,
|
49
|
+
m: int,
|
50
|
+
data: np.array,
|
51
|
+
bezierv: Bezierv,
|
52
|
+
emp_cdf_data: np.array,
|
53
|
+
penalty_weight: float=1e3) -> float:
|
54
|
+
"""
|
55
|
+
Compute the objective function value for the given control points.
|
56
|
+
|
57
|
+
This method calculates the sum of squared errors between the Bezier random variable's CDF
|
58
|
+
and the empirical CDF data.
|
59
|
+
|
60
|
+
Parameters
|
61
|
+
----------
|
62
|
+
concatenated : np.array
|
63
|
+
A concatenated array containing the control points for z and x coordinates.
|
64
|
+
The first n+1 elements are the z control points, and the remaining elements are the x control points.
|
65
|
+
n : int
|
66
|
+
The number of control points minus one for the Bezier curve.
|
67
|
+
m : int
|
68
|
+
The number of empirical CDF data points.
|
69
|
+
data : np.array
|
70
|
+
The sorted data points used to fit the Bezier distribution.
|
71
|
+
bezierv : Bezierv
|
72
|
+
An instance of the Bezierv class representing the Bezier random variable.
|
73
|
+
emp_cdf_data : np.array
|
74
|
+
The empirical CDF data points used for fitting.
|
75
|
+
penalty_weight : float, optional
|
76
|
+
The weight for the penalty term in the objective function (default is 1e3).
|
77
|
+
|
78
|
+
Returns
|
79
|
+
-------
|
80
|
+
float
|
81
|
+
The value of the objective function + penalty (MSE + penalty).
|
82
|
+
"""
|
83
|
+
|
84
|
+
x = concatenated[0 : n + 1]
|
85
|
+
z = concatenated[n + 1 : ]
|
86
|
+
|
87
|
+
try:
|
88
|
+
t = utils.get_t(n, m, data, bezierv, x)
|
89
|
+
except ValueError as e:
|
90
|
+
return np.inf
|
91
|
+
|
92
|
+
se = 0
|
93
|
+
for j in range(m):
|
94
|
+
se += (bezierv.poly_z(t[j], z) - emp_cdf_data[j])**2
|
95
|
+
mse = se / m
|
96
|
+
|
97
|
+
penalty = 0.0
|
98
|
+
penalty += abs(z[0] - 0.0)
|
99
|
+
penalty += abs(z[-1] - 1.0)
|
100
|
+
delta_zs = np.diff(z)
|
101
|
+
delta_xs = np.diff(x)
|
102
|
+
penalty += np.sum(abs(np.minimum(0, delta_zs)))
|
103
|
+
penalty += np.sum(abs(np.minimum(0, delta_xs)))
|
104
|
+
penalty += abs(x[0] - data[0])
|
105
|
+
penalty += abs(data[-1] - x[-1])
|
106
|
+
|
107
|
+
return mse + penalty_weight * penalty
|
108
|
+
|
109
|
+
def fit(n: int,
|
110
|
+
m: int,
|
111
|
+
data: np.array,
|
112
|
+
bezierv: Bezierv,
|
113
|
+
init_x: np.array,
|
114
|
+
init_z: np.array,
|
115
|
+
emp_cdf_data: np.array,
|
116
|
+
max_iter: int
|
117
|
+
) -> Bezierv:
|
118
|
+
"""
|
119
|
+
Fit the Bezier random variable to the empirical CDF data using the Nelder-Mead optimization algorithm.
|
120
|
+
|
121
|
+
Parameters
|
122
|
+
----------
|
123
|
+
n : int
|
124
|
+
The number of control points minus one for the Bezier curve.
|
125
|
+
m : int
|
126
|
+
The number of empirical CDF data points.
|
127
|
+
data : np.array
|
128
|
+
The sorted data points used to fit the Bezier distribution.
|
129
|
+
bezierv : Bezierv
|
130
|
+
An instance of the Bezierv class representing the Bezier random variable.
|
131
|
+
init_x : np.array
|
132
|
+
Initial guess for the x-coordinates of the control points.
|
133
|
+
init_z : np.array
|
134
|
+
Initial guess for the z-coordinates of the control points.
|
135
|
+
emp_cdf_data : np.array
|
136
|
+
The empirical CDF data points used for fitting.
|
137
|
+
|
138
|
+
Returns
|
139
|
+
-------
|
140
|
+
Bezierv
|
141
|
+
The fitted Bezierv object with updated control points.
|
142
|
+
float
|
143
|
+
The mean squared error (MSE) of the fit.
|
144
|
+
"""
|
145
|
+
start = np.concatenate((init_x, init_z))
|
146
|
+
result = minimize(
|
147
|
+
fun=objective_function_lagrangian,
|
148
|
+
args=(n, m, data, bezierv, emp_cdf_data),
|
149
|
+
x0=start,
|
150
|
+
method='Nelder-Mead',
|
151
|
+
options={'maxiter': max_iter, 'disp': False})
|
152
|
+
sol = result.x
|
153
|
+
controls_x = sol[0 : n + 1]
|
154
|
+
controls_z = sol[n + 1: ]
|
155
|
+
bezierv.update_bezierv(controls_x, controls_z)
|
156
|
+
mse = objective_function(sol, n, m, data, bezierv, emp_cdf_data)
|
157
|
+
return bezierv, mse
|
@@ -0,0 +1,167 @@
|
|
1
|
+
from pyexpat import model
|
2
|
+
import pyomo.environ as pyo
|
3
|
+
import numpy as np
|
4
|
+
from bezierv.classes.bezierv import Bezierv
|
5
|
+
from pyomo.opt import SolverFactory, SolverStatus, TerminationCondition
|
6
|
+
|
7
|
+
def fit(n: int,
|
8
|
+
m: int,
|
9
|
+
data: np.array,
|
10
|
+
bezierv: Bezierv,
|
11
|
+
init_x: np.array,
|
12
|
+
init_z: np.array,
|
13
|
+
init_t: np.array,
|
14
|
+
emp_cdf_data: np.array,
|
15
|
+
solver: str) -> Bezierv:
|
16
|
+
"""
|
17
|
+
Fit a Bézier random variable to the empirical CDF data using a nonlinear optimization solver.
|
18
|
+
|
19
|
+
Parameters
|
20
|
+
----------
|
21
|
+
n : int
|
22
|
+
The number of control points minus one for the Bezier curve.
|
23
|
+
m : int
|
24
|
+
The number of empirical CDF data points.
|
25
|
+
data : np.array
|
26
|
+
The sorted data points used to fit the Bézier distribution.
|
27
|
+
bezierv : Bezierv
|
28
|
+
An instance of the Bezierv class representing the Bézier random variable.
|
29
|
+
init_x : np.array
|
30
|
+
Initial guess for the x-coordinates of the control points.
|
31
|
+
init_z : np.array
|
32
|
+
Initial guess for the z-coordinates of the control points.
|
33
|
+
init_t : np.array
|
34
|
+
Initial guess for the Bézier 'time' parameters corresponding to the data points.
|
35
|
+
emp_cdf_data : np.array
|
36
|
+
The empirical CDF data points used for fitting.
|
37
|
+
solver : str, optional
|
38
|
+
The name of the solver to use for optimization.
|
39
|
+
|
40
|
+
Returns
|
41
|
+
-------
|
42
|
+
Bezierv
|
43
|
+
The fitted Bezierv object with updated control points.
|
44
|
+
float
|
45
|
+
The mean squared error (MSE) of the fit.
|
46
|
+
|
47
|
+
Raises:
|
48
|
+
Exception: If the solver fails to find an optimal solution.
|
49
|
+
|
50
|
+
Notes:
|
51
|
+
- The method uses the IPOPT solver for optimization.
|
52
|
+
- The control points are constrained to lie within the range of the data.
|
53
|
+
- The method ensures that the control points and the Bézier 'time' parameters are sorted.
|
54
|
+
- Convexity constraints are applied to the control points and the Bézier 'time' parameters.
|
55
|
+
- The first and last control points are fixed to the minimum and maximum of the data, respectively.
|
56
|
+
- The first and last Bézier 'time' parameters are fixed to 0 and 1, respectively.
|
57
|
+
"""
|
58
|
+
# Defining the optimization model
|
59
|
+
model = pyo.ConcreteModel()
|
60
|
+
|
61
|
+
# Sets
|
62
|
+
model.N = pyo.Set(initialize=list(range(n + 1))) # N = 0,...,i,...,n
|
63
|
+
model.N_n = pyo.Set(initialize=list(range(n))) # N = 0,...,i,...,n-1
|
64
|
+
model.M = pyo.Set(initialize=list(range(1, m + 1))) # M = 1,...,j,...,m
|
65
|
+
model.M_m = pyo.Set(initialize=list(range(1, m))) # M = 1,...,j,...,m-1
|
66
|
+
|
67
|
+
# Decision variables
|
68
|
+
# Control points. Box constraints.
|
69
|
+
X_min = data[0];
|
70
|
+
X_max = data[-1];
|
71
|
+
# var x{i in 0..n} >=X[1], <=X[m];
|
72
|
+
# Initialization:
|
73
|
+
def init_x_rule(model, i):
|
74
|
+
return float(init_x[i])
|
75
|
+
model.x = pyo.Var(model.N, within=pyo.Reals, bounds=(X_min, X_max), initialize=init_x_rule)
|
76
|
+
# var z{i in 0..n} >=0, <=1;
|
77
|
+
# Initialization:
|
78
|
+
def init_z_rule(model, i):
|
79
|
+
return float(init_z[i])
|
80
|
+
model.z = pyo.Var(model.N, within=pyo.NonNegativeReals, bounds=(0, 1), initialize=init_z_rule)
|
81
|
+
# Bezier 'time' parameter t for the j-th sample point.
|
82
|
+
# var t{j in 1..m} >=0, <= 1;
|
83
|
+
# Initialization:
|
84
|
+
def init_t_rule(model, j):
|
85
|
+
return float(init_t[j - 1]) # j starts from 1, so we access init_t with j-1
|
86
|
+
model.t = pyo.Var(model.M, within=pyo.NonNegativeReals, bounds=(0,1), initialize=init_t_rule )
|
87
|
+
# Estimated cdf for the j-th sample point.
|
88
|
+
# var F_hat{j in 1..m} >=0, <= 1;
|
89
|
+
model.F_hat = pyo.Var(model.M, within=pyo.NonNegativeReals, bounds=(0,1) )
|
90
|
+
|
91
|
+
# Objective function
|
92
|
+
# minimize mean_square_error:
|
93
|
+
# 1/m * sum {j in 1..m} ( ( j/m - F_hat[j] )^2);
|
94
|
+
def mse_rule(model):
|
95
|
+
return (1 / m) * sum((emp_cdf_data[j - 1] - model.F_hat[j])**2 for j in model.M)
|
96
|
+
model.mse = pyo.Objective(rule=mse_rule, sense=pyo.minimize )
|
97
|
+
|
98
|
+
# Constraints
|
99
|
+
# subject to F_hat_estimates {j in 1..m}:
|
100
|
+
# sum{i in 0..n}( comb[i]*t[j]^i*(1-t[j])^(n-i)*z[i] ) = F_hat[j];
|
101
|
+
def F_hat_rule(model, j):
|
102
|
+
return sum(bezierv.comb[i] * model.t[j]**i * (1 - model.t[j])**(n - i) * model.z[i] for i in model.N ) == model.F_hat[j]
|
103
|
+
model.ctr_F_hat = pyo.Constraint(model.M , rule=F_hat_rule)
|
104
|
+
|
105
|
+
# subject to data_sample {j in 1..m}:
|
106
|
+
# sum{i in 0..n}( comb[i]*t[j]^i*(1-t[j])^(n-i)*x[i] ) = X[j];
|
107
|
+
def data_sample_rule(model, j):
|
108
|
+
return sum(bezierv.comb[i] * model.t[j]**i * (1 - model.t[j])**(n - i) * model.x[i] for i in model.N ) == data[j-1]
|
109
|
+
model.ctr_sample = pyo.Constraint(model.M , rule=data_sample_rule)
|
110
|
+
|
111
|
+
# subject to convexity_x {i in 0..n-1}:
|
112
|
+
# x[i] <= x[i+1];
|
113
|
+
def convexity_x_rule(model, i):
|
114
|
+
return model.x[i] <= model.x[i + 1]
|
115
|
+
model.ctr_convexity_x = pyo.Constraint(model.N_n , rule=convexity_x_rule)
|
116
|
+
|
117
|
+
# subject to convexity_z {i in 0..n-1}:
|
118
|
+
# z[i] <= z[i+1];
|
119
|
+
def convexity_z_rule(model, i):
|
120
|
+
return model.z[i] <= model.z[i + 1]
|
121
|
+
model.ctr_convexity_z = pyo.Constraint(model.N_n , rule=convexity_z_rule)
|
122
|
+
|
123
|
+
# subject to first_control_x:
|
124
|
+
# x[0] = X[1];
|
125
|
+
model.first_control_x = pyo.Constraint(expr=model.x[0] <= data[0])
|
126
|
+
# subject to first_control_z:
|
127
|
+
# z[0] = 0;
|
128
|
+
model.first_control_z = pyo.Constraint(expr=model.z[0] == 0)
|
129
|
+
|
130
|
+
# subject to last_control_x:
|
131
|
+
# x[n] = X[m];
|
132
|
+
model.last_control_x = pyo.Constraint(expr=model.x[n] >= data[-1])
|
133
|
+
# subject to last_control_z:
|
134
|
+
# z[n] = 1;
|
135
|
+
model.last_control_z = pyo.Constraint(expr=model.z[n] == 1)
|
136
|
+
|
137
|
+
# subject to first_data_t:
|
138
|
+
# t[1] = 0;
|
139
|
+
model.first_t = pyo.Constraint(expr=model.t[1] == 0)
|
140
|
+
# subject to last_data_t:
|
141
|
+
# t[m] = 1;
|
142
|
+
model.last_t = pyo.Constraint(expr=model.t[m] == 1)
|
143
|
+
|
144
|
+
delta_z = 0.0001
|
145
|
+
delta_x = 0.5
|
146
|
+
# Left end: x1 ~= x0, z1 ~= z0
|
147
|
+
#model.end_close_z_left = pyo.Constraint(expr = model.z[1] - model.z[0] <= delta_z)
|
148
|
+
#model.end_close_x_left = pyo.Constraint(expr = model.x[1] - model.x[0] <= delta_x)
|
149
|
+
|
150
|
+
# Right end: xn ~= xn-1, zn ~= zn-1
|
151
|
+
#model.end_close_x_right = pyo.Constraint(expr = model.x[n] - model.x[n-1] <= delta_x)
|
152
|
+
#model.end_close_z_right = pyo.Constraint(expr = model.z[n] - model.z[n-1] <= delta_z)
|
153
|
+
|
154
|
+
# Set solver
|
155
|
+
pyo_solver = SolverFactory(solver)
|
156
|
+
|
157
|
+
try:
|
158
|
+
results = pyo_solver.solve(model, tee=False, timelimit=60)
|
159
|
+
if (results.solver.status == SolverStatus.ok) and (results.solver.termination_condition == TerminationCondition.optimal):
|
160
|
+
controls_x = np.array([model.x[i]() for i in model.N])
|
161
|
+
controls_z = np.array([model.z[i]() for i in model.N])
|
162
|
+
mse = model.mse()
|
163
|
+
bezierv.update_bezierv(controls_x, controls_z)
|
164
|
+
except Exception as e:
|
165
|
+
print("NonLinearSolver [fit]: An exception occurred during model evaluation:", e)
|
166
|
+
|
167
|
+
return bezierv, mse
|