freealg 0.1.2__tar.gz → 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.
- {freealg-0.1.2/freealg.egg-info → freealg-0.1.4}/PKG-INFO +1 -1
- freealg-0.1.4/freealg/__version__.py +1 -0
- {freealg-0.1.2 → freealg-0.1.4}/freealg/_util.py +50 -1
- {freealg-0.1.2 → freealg-0.1.4}/freealg/freeform.py +33 -10
- {freealg-0.1.2 → freealg-0.1.4/freealg.egg-info}/PKG-INFO +1 -1
- freealg-0.1.2/freealg/__version__.py +0 -1
- {freealg-0.1.2 → freealg-0.1.4}/CHANGELOG.rst +0 -0
- {freealg-0.1.2 → freealg-0.1.4}/LICENSE.txt +0 -0
- {freealg-0.1.2 → freealg-0.1.4}/MANIFEST.in +0 -0
- {freealg-0.1.2 → freealg-0.1.4}/README.rst +0 -0
- {freealg-0.1.2 → freealg-0.1.4}/freealg/__init__.py +0 -0
- {freealg-0.1.2 → freealg-0.1.4}/freealg/_chebyshev.py +0 -0
- {freealg-0.1.2 → freealg-0.1.4}/freealg/_damp.py +0 -0
- {freealg-0.1.2 → freealg-0.1.4}/freealg/_decompress.py +0 -0
- {freealg-0.1.2 → freealg-0.1.4}/freealg/_jacobi.py +0 -0
- {freealg-0.1.2 → freealg-0.1.4}/freealg/_pade.py +0 -0
- {freealg-0.1.2 → freealg-0.1.4}/freealg/_plot_util.py +0 -0
- {freealg-0.1.2 → freealg-0.1.4}/freealg/_sample.py +0 -0
- {freealg-0.1.2 → freealg-0.1.4}/freealg/distributions/__init__.py +0 -0
- {freealg-0.1.2 → freealg-0.1.4}/freealg/distributions/kesten_mckay.py +0 -0
- {freealg-0.1.2 → freealg-0.1.4}/freealg/distributions/marchenko_pastur.py +0 -0
- {freealg-0.1.2 → freealg-0.1.4}/freealg/distributions/wachter.py +0 -0
- {freealg-0.1.2 → freealg-0.1.4}/freealg/distributions/wigner.py +0 -0
- {freealg-0.1.2 → freealg-0.1.4}/freealg.egg-info/SOURCES.txt +0 -0
- {freealg-0.1.2 → freealg-0.1.4}/freealg.egg-info/dependency_links.txt +0 -0
- {freealg-0.1.2 → freealg-0.1.4}/freealg.egg-info/not-zip-safe +0 -0
- {freealg-0.1.2 → freealg-0.1.4}/freealg.egg-info/requires.txt +0 -0
- {freealg-0.1.2 → freealg-0.1.4}/freealg.egg-info/top_level.txt +0 -0
- {freealg-0.1.2 → freealg-0.1.4}/pyproject.toml +0 -0
- {freealg-0.1.2 → freealg-0.1.4}/requirements.txt +0 -0
- {freealg-0.1.2 → freealg-0.1.4}/setup.cfg +0 -0
- {freealg-0.1.2 → freealg-0.1.4}/setup.py +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.1.4"
|
|
@@ -13,9 +13,10 @@
|
|
|
13
13
|
|
|
14
14
|
import numpy
|
|
15
15
|
import scipy
|
|
16
|
+
from scipy.stats import beta
|
|
16
17
|
from scipy.optimize import minimize
|
|
17
18
|
|
|
18
|
-
__all__ = ['compute_eig', 'force_density']
|
|
19
|
+
__all__ = ['compute_eig', 'beta_kde', 'force_density']
|
|
19
20
|
|
|
20
21
|
|
|
21
22
|
# ===========
|
|
@@ -32,6 +33,54 @@ def compute_eig(A, lower=False):
|
|
|
32
33
|
return eig
|
|
33
34
|
|
|
34
35
|
|
|
36
|
+
# ========
|
|
37
|
+
# beta kde
|
|
38
|
+
# ========
|
|
39
|
+
|
|
40
|
+
def beta_kde(eig, xs, lam_m, lam_p, h):
|
|
41
|
+
"""
|
|
42
|
+
Beta-kernel KDE with automatic guards against NaNs.
|
|
43
|
+
|
|
44
|
+
Parameters
|
|
45
|
+
----------
|
|
46
|
+
eig : (n,) 1-D array of samples
|
|
47
|
+
xs : evaluation grid (must lie within [lam_m, lam_p])
|
|
48
|
+
lam_m, lam_p : float, support endpoints (lam_m < lam_p)
|
|
49
|
+
h : bandwidth in rescaled units (0 < h < 1)
|
|
50
|
+
|
|
51
|
+
Returns
|
|
52
|
+
-------
|
|
53
|
+
pdf : ndarray same length as xs
|
|
54
|
+
"""
|
|
55
|
+
|
|
56
|
+
span = lam_p - lam_m
|
|
57
|
+
if span <= 0:
|
|
58
|
+
raise ValueError("lam_p must be larger than lam_m")
|
|
59
|
+
|
|
60
|
+
# map samples and grid to [0,1]
|
|
61
|
+
u = (eig - lam_m) / span
|
|
62
|
+
t = (xs - lam_m) / span
|
|
63
|
+
|
|
64
|
+
if u.min() < 0 or u.max() > 1:
|
|
65
|
+
mask = (u > 0) & (u < 1)
|
|
66
|
+
u = u[mask]
|
|
67
|
+
|
|
68
|
+
pdf = numpy.zeros_like(xs, dtype=float)
|
|
69
|
+
n = len(u)
|
|
70
|
+
|
|
71
|
+
# tiny positive number to keep shape parameters >0
|
|
72
|
+
eps = 1e-6
|
|
73
|
+
for ui in u:
|
|
74
|
+
a = max(ui / h + 1.0, eps)
|
|
75
|
+
b = max((1.0 - ui) / h + 1.0, eps)
|
|
76
|
+
pdf += beta.pdf(t, a, b)
|
|
77
|
+
|
|
78
|
+
pdf /= n * span # renormalise
|
|
79
|
+
pdf[(t < 0) | (t > 1)] = 0.0 # exact zeros outside
|
|
80
|
+
|
|
81
|
+
return pdf
|
|
82
|
+
|
|
83
|
+
|
|
35
84
|
# =============
|
|
36
85
|
# force density
|
|
37
86
|
# =============
|
|
@@ -15,7 +15,7 @@ import numpy
|
|
|
15
15
|
from scipy.stats import gaussian_kde
|
|
16
16
|
# from statsmodels.nonparametric.kde import KDEUnivariate
|
|
17
17
|
from functools import partial
|
|
18
|
-
from ._util import compute_eig, force_density
|
|
18
|
+
from ._util import compute_eig, beta_kde, force_density
|
|
19
19
|
from ._jacobi import jacobi_sample_proj, jacobi_kernel_proj, jacobi_approx, \
|
|
20
20
|
jacobi_stieltjes
|
|
21
21
|
from ._chebyshev import chebyshev_sample_proj, chebyshev_kernel_proj, \
|
|
@@ -177,7 +177,7 @@ class FreeForm(object):
|
|
|
177
177
|
# ===
|
|
178
178
|
|
|
179
179
|
def fit(self, method='jacobi', K=10, alpha=0.0, beta=0.0, reg=0.0,
|
|
180
|
-
projection='
|
|
180
|
+
projection='gaussian', kernel_bw=None, damp=None, force=False,
|
|
181
181
|
pade_p=0, pade_q=1, odd_side='left', pade_reg=0.0, optimizer='ls',
|
|
182
182
|
plot=False, latex=False, save=False):
|
|
183
183
|
"""
|
|
@@ -206,16 +206,21 @@ class FreeForm(object):
|
|
|
206
206
|
reg : float, default=0.0
|
|
207
207
|
Tikhonov regularization coefficient.
|
|
208
208
|
|
|
209
|
-
projection : {``'sample'``, ``'
|
|
209
|
+
projection : {``'sample'``, ``'gaussian'``, ``'beta'``}, \
|
|
210
|
+
default= ``'beta'``
|
|
210
211
|
The method of Galerkin projection:
|
|
211
212
|
|
|
212
213
|
* ``'sample'``: directly project samples (eigenvalues) to the
|
|
213
214
|
orthogonal polynomials. This method is highly unstable as it
|
|
214
215
|
treats each sample as a delta Dirac function.
|
|
215
|
-
* ``'
|
|
216
|
-
smooth KDE to the orthogonal polynomials. This method
|
|
217
|
-
|
|
218
|
-
|
|
216
|
+
* ``'gaussian'``: computes Gaussian-Kernel KDE from the samples and
|
|
217
|
+
project a smooth KDE to the orthogonal polynomials. This method
|
|
218
|
+
is stable.
|
|
219
|
+
* ``'beta'``: computes Beta-Kernel KDE from the samples and
|
|
220
|
+
project a smooth KDE to the orthogonal polynomials. This method
|
|
221
|
+
is stable.
|
|
222
|
+
|
|
223
|
+
kernel_bw : float, default=0.001
|
|
219
224
|
Kernel band-wdth. See scipy.stats.gaussian_kde. This argument is
|
|
220
225
|
relevant if ``projection='kernel'`` is set.
|
|
221
226
|
|
|
@@ -301,7 +306,7 @@ class FreeForm(object):
|
|
|
301
306
|
if not (method in ['jacobi', 'chebyshev']):
|
|
302
307
|
raise ValueError('"method" is invalid.')
|
|
303
308
|
|
|
304
|
-
if not (projection in ['sample', '
|
|
309
|
+
if not (projection in ['sample', 'gaussian', 'beta']):
|
|
305
310
|
raise ValueError('"projection" is invalid.')
|
|
306
311
|
|
|
307
312
|
# Project eigenvalues to Jacobi polynomials basis
|
|
@@ -313,7 +318,12 @@ class FreeForm(object):
|
|
|
313
318
|
else:
|
|
314
319
|
# smooth KDE on a fixed grid
|
|
315
320
|
xs = numpy.linspace(self.lam_m, self.lam_p, 2000)
|
|
316
|
-
|
|
321
|
+
|
|
322
|
+
if projection == 'gaussian':
|
|
323
|
+
pdf = gaussian_kde(self.eig, bw_method=kernel_bw)(xs)
|
|
324
|
+
else:
|
|
325
|
+
pdf = beta_kde(self.eig, xs, self.lam_m, self.lam_p,
|
|
326
|
+
kernel_bw)
|
|
317
327
|
|
|
318
328
|
# Adaptive KDE
|
|
319
329
|
# k = KDEUnivariate(self.eig)
|
|
@@ -321,6 +331,12 @@ class FreeForm(object):
|
|
|
321
331
|
# adaptive=True)
|
|
322
332
|
# pdf = k.evaluate(xs)
|
|
323
333
|
|
|
334
|
+
# TEST
|
|
335
|
+
# import matplotlib.pyplot as plt
|
|
336
|
+
# plt.plot(xs, pdf)
|
|
337
|
+
# plt.grid(True)
|
|
338
|
+
# plt.show()
|
|
339
|
+
|
|
324
340
|
psi = jacobi_kernel_proj(xs, pdf, support=self.support, K=K,
|
|
325
341
|
alpha=alpha, beta=beta, reg=reg)
|
|
326
342
|
|
|
@@ -332,7 +348,12 @@ class FreeForm(object):
|
|
|
332
348
|
else:
|
|
333
349
|
# smooth KDE on a fixed grid
|
|
334
350
|
xs = numpy.linspace(self.lam_m, self.lam_p, 2000)
|
|
335
|
-
|
|
351
|
+
|
|
352
|
+
if projection == 'gaussian':
|
|
353
|
+
pdf = gaussian_kde(self.eig, bw_method=kernel_bw)(xs)
|
|
354
|
+
else:
|
|
355
|
+
pdf = beta_kde(self.eig, xs, self.lam_m, self.lam_p,
|
|
356
|
+
kernel_bw)
|
|
336
357
|
|
|
337
358
|
# Adaptive KDE
|
|
338
359
|
# k = KDEUnivariate(self.eig)
|
|
@@ -893,6 +914,8 @@ class FreeForm(object):
|
|
|
893
914
|
>>> from freealg import FreeForm
|
|
894
915
|
"""
|
|
895
916
|
|
|
917
|
+
size = int(size)
|
|
918
|
+
|
|
896
919
|
rho, x, (lb, ub) = decompress(self, size, x=x, delta=delta,
|
|
897
920
|
iterations=iterations,
|
|
898
921
|
step_size=step_size, tolerance=tolerance)
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
__version__ = "0.1.2"
|
|
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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|