bs-python-utils 0.4__tar.gz → 0.4.2__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.
- {bs_python_utils-0.4 → bs_python_utils-0.4.2}/PKG-INFO +6 -2
- {bs_python_utils-0.4 → bs_python_utils-0.4.2}/README.md +4 -1
- {bs_python_utils-0.4 → bs_python_utils-0.4.2}/bs_python_utils/bivariate_quantiles.py +10 -5
- {bs_python_utils-0.4 → bs_python_utils-0.4.2}/bs_python_utils/bs_sparse_gaussian.py +19 -4
- {bs_python_utils-0.4 → bs_python_utils-0.4.2}/bs_python_utils/bsstats.py +0 -35
- {bs_python_utils-0.4 → bs_python_utils-0.4.2}/pyproject.toml +2 -1
- {bs_python_utils-0.4 → bs_python_utils-0.4.2}/LICENSE +0 -0
- {bs_python_utils-0.4 → bs_python_utils-0.4.2}/bs_python_utils/Timer.py +0 -0
- {bs_python_utils-0.4 → bs_python_utils-0.4.2}/bs_python_utils/__init__.py +0 -0
- {bs_python_utils-0.4 → bs_python_utils-0.4.2}/bs_python_utils/bs_altair.py +0 -0
- {bs_python_utils-0.4 → bs_python_utils-0.4.2}/bs_python_utils/bs_logging.py +0 -0
- {bs_python_utils-0.4 → bs_python_utils-0.4.2}/bs_python_utils/bs_mathstr.py +0 -0
- {bs_python_utils-0.4 → bs_python_utils-0.4.2}/bs_python_utils/bs_mem.py +0 -0
- {bs_python_utils-0.4 → bs_python_utils-0.4.2}/bs_python_utils/bs_opt.py +0 -0
- {bs_python_utils-0.4 → bs_python_utils-0.4.2}/bs_python_utils/bs_plots.py +0 -0
- {bs_python_utils-0.4 → bs_python_utils-0.4.2}/bs_python_utils/bs_seaborn.py +0 -0
- {bs_python_utils-0.4 → bs_python_utils-0.4.2}/bs_python_utils/bsmplutils.py +0 -0
- {bs_python_utils-0.4 → bs_python_utils-0.4.2}/bs_python_utils/bsnputils.py +0 -0
- {bs_python_utils-0.4 → bs_python_utils-0.4.2}/bs_python_utils/bssputils.py +0 -0
- {bs_python_utils-0.4 → bs_python_utils-0.4.2}/bs_python_utils/bsutils.py +0 -0
- {bs_python_utils-0.4 → bs_python_utils-0.4.2}/bs_python_utils/chebyshev.py +0 -0
- {bs_python_utils-0.4 → bs_python_utils-0.4.2}/bs_python_utils/distance_covariances.py +0 -0
- {bs_python_utils-0.4 → bs_python_utils-0.4.2}/bs_python_utils/example_opt.py +0 -0
- {bs_python_utils-0.4 → bs_python_utils-0.4.2}/bs_python_utils/examples_altair.py +0 -0
- {bs_python_utils-0.4 → bs_python_utils-0.4.2}/bs_python_utils/examples_distance_covariances.py +0 -0
- {bs_python_utils-0.4 → bs_python_utils-0.4.2}/bs_python_utils/examples_mem.py +0 -0
- {bs_python_utils-0.4 → bs_python_utils-0.4.2}/bs_python_utils/examples_seaborn.py +0 -0
- {bs_python_utils-0.4 → bs_python_utils-0.4.2}/bs_python_utils/examples_sklearn.py +0 -0
- {bs_python_utils-0.4 → bs_python_utils-0.4.2}/bs_python_utils/pandas_utils.py +0 -0
- {bs_python_utils-0.4 → bs_python_utils-0.4.2}/bs_python_utils/sklearn_utils.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: bs-python-utils
|
|
3
|
-
Version: 0.4
|
|
3
|
+
Version: 0.4.2
|
|
4
4
|
Summary: my Python utilities
|
|
5
5
|
Home-page: https://github.com/bsalanie/bs-python-utils
|
|
6
6
|
Author: Bernard Salanie
|
|
@@ -13,6 +13,7 @@ Classifier: Programming Language :: Python :: 3.10
|
|
|
13
13
|
Classifier: Programming Language :: Python :: 3.11
|
|
14
14
|
Requires-Dist: altair (>=4.2.2,<5.0.0)
|
|
15
15
|
Requires-Dist: altair-saver (>=0.5.0,<0.6.0)
|
|
16
|
+
Requires-Dist: griffe (>=0.32.3,<0.33.0)
|
|
16
17
|
Requires-Dist: matplotlib (>=3.7.1,<4.0.0)
|
|
17
18
|
Requires-Dist: scikit-learn (>=1.2.2,<2.0.0)
|
|
18
19
|
Requires-Dist: seaborn (>=0.12.2,<0.13.0)
|
|
@@ -36,7 +37,10 @@ My Python utilities.
|
|
|
36
37
|
- **Documentation** <https://bsalanie.github.io/bs-python-utils/>
|
|
37
38
|
|
|
38
39
|
### Release notes
|
|
39
|
-
|
|
40
|
+
#### 0.4.2 (August 2, 2023)
|
|
41
|
+
Updated the docs.
|
|
42
|
+
#### 0.4.1 (July 23, 2023)
|
|
43
|
+
Only print if verbose in bivariate quantiles.
|
|
40
44
|
#### 0.4 (July 22, 2023)
|
|
41
45
|
Added bivariate quantiles and ranks à la optimal transportation.
|
|
42
46
|
#### 0.3 (July 21, 2023)
|
|
@@ -12,7 +12,10 @@ My Python utilities.
|
|
|
12
12
|
- **Documentation** <https://bsalanie.github.io/bs-python-utils/>
|
|
13
13
|
|
|
14
14
|
### Release notes
|
|
15
|
-
|
|
15
|
+
#### 0.4.2 (August 2, 2023)
|
|
16
|
+
Updated the docs.
|
|
17
|
+
#### 0.4.1 (July 23, 2023)
|
|
18
|
+
Only print if verbose in bivariate quantiles.
|
|
16
19
|
#### 0.4 (July 22, 2023)
|
|
17
20
|
Added bivariate quantiles and ranks à la optimal transportation.
|
|
18
21
|
#### 0.3 (July 21, 2023)
|
|
@@ -122,6 +122,7 @@ def bivariate_ranks_v(
|
|
|
122
122
|
Args:
|
|
123
123
|
y: the observations, an `(n,2)` matrix
|
|
124
124
|
v: the converged values of the weights, an `n`-vector
|
|
125
|
+
n_nodes: the number of nodes for Chebyshev integration
|
|
125
126
|
presorted: if `True`, then `y` and `v` are sorted by increasing `y[:, 1]`.
|
|
126
127
|
|
|
127
128
|
Returns:
|
|
@@ -234,12 +235,14 @@ def solve_for_v_(y: np.ndarray, n_nodes: int = 32, verbose: bool = False) -> np.
|
|
|
234
235
|
argsog = [y_sorted, u1_nodes, u1_weights, verbose]
|
|
235
236
|
|
|
236
237
|
res = minimize_free(obj_, grad_, v0, args=argsog)
|
|
237
|
-
|
|
238
|
+
if verbose:
|
|
239
|
+
print_optimization_results(res, "Minimizing over v")
|
|
238
240
|
|
|
239
241
|
if not res.success:
|
|
240
242
|
bs_error_abort("Problem! the optimization failed.")
|
|
241
243
|
vstar = res.x
|
|
242
|
-
|
|
244
|
+
if verbose:
|
|
245
|
+
print(f"The final gradient over v is close to 0: error {npmaxabs(res.jac)}")
|
|
243
246
|
vstar1_sorted = np.append(vstar, -np.sum(vstar))
|
|
244
247
|
|
|
245
248
|
# revert to original order
|
|
@@ -251,12 +254,12 @@ def solve_for_v_(y: np.ndarray, n_nodes: int = 32, verbose: bool = False) -> np.
|
|
|
251
254
|
|
|
252
255
|
def bivariate_quantiles(
|
|
253
256
|
y: np.ndarray, u: np.ndarray, n_nodes: int = 32, verbose: bool = False
|
|
254
|
-
):
|
|
257
|
+
) -> np.ndarray:
|
|
255
258
|
"""computes the bivariate quantiles of `y` at the quantiles `u`
|
|
256
259
|
|
|
257
260
|
Args:
|
|
258
261
|
y: the observations, an `(n, 2)` matrix
|
|
259
|
-
|
|
262
|
+
u: the quantiles at which to compute the bivariate quantiles,
|
|
260
263
|
an `(m, 2)` matrix
|
|
261
264
|
n_nodes: the number of nodes to use for the quadrature
|
|
262
265
|
verbose: if `True`, print some information
|
|
@@ -268,7 +271,9 @@ def bivariate_quantiles(
|
|
|
268
271
|
return bivariate_quantiles_v(y, u, v)
|
|
269
272
|
|
|
270
273
|
|
|
271
|
-
def bivariate_ranks(
|
|
274
|
+
def bivariate_ranks(
|
|
275
|
+
y: np.ndarray, n_nodes: int = 32, verbose: bool = False
|
|
276
|
+
) -> np.ndarray:
|
|
272
277
|
"""computes the bivariate ranks of `y`
|
|
273
278
|
|
|
274
279
|
Args:
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
"""
|
|
2
|
-
|
|
2
|
+
Sets up sparse integration over a Gaussian, given text files that contain rescaled Gauss-Hermite nodes and weights.
|
|
3
|
+
|
|
4
|
+
These files must be named `GHsparseGrid{ndims}prec{iprec}.txt`, where `ndims` is the number of dimensions of integration
|
|
5
|
+
and `iprec` is a precision level that must be 9, 13, or (most precise) 17. The file must have `(ndims+1) columns,
|
|
6
|
+
with the weights in the first column.
|
|
7
|
+
|
|
8
|
+
The nodes and weights are rescaled so that `f(nodes) @ weights` approximates `Ef(X)` for `X` an `N(0,I)` variable.
|
|
3
9
|
"""
|
|
4
10
|
from pathlib import Path
|
|
5
11
|
|
|
@@ -15,11 +21,12 @@ def setup_sparse_gaussian(
|
|
|
15
21
|
"""
|
|
16
22
|
get nodes and weights for sparse integration Ef(X) with X = N(0,1) in `ndims` dimensions
|
|
17
23
|
|
|
18
|
-
usage: nodes, weights = setup_sparse_gaussian(mdims, iprec);
|
|
24
|
+
usage: nodes, weights = setup_sparse_gaussian(mdims, iprec); integral_f = f(nodes) @ weights
|
|
19
25
|
|
|
20
26
|
Args:
|
|
21
27
|
ndims: number of dimensions (1 to 5)
|
|
22
28
|
iprec: precision (must be 9, 13, or 17)
|
|
29
|
+
GHsparsedir: the name of a directory that contains nodes and weights
|
|
23
30
|
|
|
24
31
|
Returns:
|
|
25
32
|
a pair of arrays `nodes` and `weights`;
|
|
@@ -36,9 +43,17 @@ def setup_sparse_gaussian(
|
|
|
36
43
|
)
|
|
37
44
|
|
|
38
45
|
if ndims in [1, 2, 3, 4, 5]:
|
|
46
|
+
if not GHdir.exists():
|
|
47
|
+
bs_error_abort("I did not find the directory with the nodes/weights files.")
|
|
39
48
|
grid = np.loadtxt(GHdir / f"GHsparseGrid{ndims}prec{iprec}.txt")
|
|
40
|
-
|
|
41
|
-
|
|
49
|
+
|
|
50
|
+
print(f"{grid.shape=}")
|
|
51
|
+
if ndims == 1:
|
|
52
|
+
weights = grid[:, 0]
|
|
53
|
+
nodes = grid[:, 1]
|
|
54
|
+
else:
|
|
55
|
+
weights = grid[:, 0]
|
|
56
|
+
nodes = grid[:, 1:]
|
|
42
57
|
return nodes, weights
|
|
43
58
|
else:
|
|
44
59
|
bs_error_abort(
|
|
@@ -16,7 +16,6 @@ from bs_python_utils.bsnputils import (
|
|
|
16
16
|
check_matrix,
|
|
17
17
|
check_vector,
|
|
18
18
|
check_vector_or_matrix,
|
|
19
|
-
make_lexico_grid,
|
|
20
19
|
)
|
|
21
20
|
from bs_python_utils.bssputils import spline_reg
|
|
22
21
|
from bs_python_utils.bsutils import bs_error_abort
|
|
@@ -416,37 +415,3 @@ def estimate_pdf(
|
|
|
416
415
|
if weights is not None:
|
|
417
416
|
f_x *= weights / np.mean(weights)
|
|
418
417
|
return cast(np.ndarray, f_x)
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
def estimate_densities_at_quantiles(
|
|
422
|
-
X: np.ndarray, qtiles: np.ndarray
|
|
423
|
-
) -> tuple[np.ndarray, np.ndarray] | tuple[np.ndarray, np.ndarray, np.ndarray]:
|
|
424
|
-
"""estimate densities of margins at prespecified quantiles (Silverman rule)
|
|
425
|
-
and the joint density at each vector of these quantiles
|
|
426
|
-
|
|
427
|
-
Args:
|
|
428
|
-
X: `n`-vector or `(n, nx)`-matrix
|
|
429
|
-
qtiles: vector of `nq` numbers between 0 and 1
|
|
430
|
-
|
|
431
|
-
Returns:
|
|
432
|
-
if `X` is a matrix, the `({nq}^{nx}, {nx})` matrices of estimated margin densities \
|
|
433
|
-
and the `{nq}^{nx}` vector of the joint density on the lexicographic grid of quantiles;
|
|
434
|
-
if `X` is a vector, the `nq`-vector of the density at the quantiles, twice
|
|
435
|
-
"""
|
|
436
|
-
ndims_X = check_vector_or_matrix(X, "estimate_densities_")
|
|
437
|
-
if ndims_X == 1:
|
|
438
|
-
f_X = estimate_pdf(X, np.quantile(X, qtiles))
|
|
439
|
-
return f_X, f_X
|
|
440
|
-
else:
|
|
441
|
-
nx = X.shape[1]
|
|
442
|
-
nq = qtiles.size
|
|
443
|
-
f_X_k = np.zeros((nq, nx))
|
|
444
|
-
nodes_mat = np.zeros((nq, nx))
|
|
445
|
-
for i_x in range(nx):
|
|
446
|
-
X_ix = X[:, i_x]
|
|
447
|
-
nodes_mat[:, i_x] = np.quantile(X_ix, qtiles)
|
|
448
|
-
f_X_k[:, i_x] = estimate_pdf(X_ix, nodes_mat[:, i_x])
|
|
449
|
-
f_margins = make_lexico_grid(f_X_k)
|
|
450
|
-
values_X = make_lexico_grid(nodes_mat)
|
|
451
|
-
f_X = estimate_pdf(X, values_X) # joint density
|
|
452
|
-
return f_margins, f_X, values_X
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[tool.poetry]
|
|
2
2
|
name = "bs_python_utils"
|
|
3
|
-
version = "0.4"
|
|
3
|
+
version = "0.4.2"
|
|
4
4
|
description = "my Python utilities"
|
|
5
5
|
authors = ["Bernard Salanie <bsalanie@columbia.edu>"]
|
|
6
6
|
repository = "https://github.com/bsalanie/bs-python-utils"
|
|
@@ -19,6 +19,7 @@ seaborn = "^0.12.2"
|
|
|
19
19
|
scikit-learn = "^1.2.2"
|
|
20
20
|
statsmodels = "^0.13.5"
|
|
21
21
|
vega-datasets = "^0.9.0"
|
|
22
|
+
griffe = "^0.32.3"
|
|
22
23
|
|
|
23
24
|
[tool.poetry.group.dev.dependencies]
|
|
24
25
|
pytest = "^7.2.0"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{bs_python_utils-0.4 → bs_python_utils-0.4.2}/bs_python_utils/examples_distance_covariances.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|