You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ENH: bernstein polynomials for cdf, ppf and pdf, univariate and multivariate #7314
evaluated on a grid, similar to histogram, provides histogram smoothing
scipy.Bpoly works also if we only have one interval [0, 1] and don't use the piecewise feature.
univariate polynomial only
Note, bernstein polynomial basis functions are the same as binom.pdf in scipy.stats
e.g. approximation to beta distribution
K = 501
k = np.arange(K).astype(float)
n = len(k) - 1.
x = 0.75
a, b = 2, 5
print(x, stats.beta.cdf(x, a, b), (stats.beta.cdf(np.linspace(0,1,K), a, b) * stats.binom.pmf(k, n, x)).sum())
print(x, stats.beta.pdf(x, a, b), (stats.beta.pdf(np.linspace(0,1,K), a, b) * stats.binom.pmf(k, n, x)).sum())
print(x, stats.beta.pdf(x, a, b), (stats.beta.pdf(np.linspace(0,1,K), a, b) * special.binom(n, k) * x**k * (1 - x)**(n - k)).sum())
bpb = BPoly(stats.beta.cdf(np.linspace(0,1,K), a, b)[:, None], [0., 1])
print(x, bpb(x))
0.75 0.995361328125 0.995117628660943
0.75 0.08789062500000004 0.09035257646249084
0.75 0.08789062500000004 0.0903525764624922
0.75 0.9951176286610438
Convergence rate of approximation by Bernstein polynomials is slow, but good shape properties, uniform approximation and restrintion to non-negative or positive values.
(in contrast to orthogonal polynomials that might have negative pdf segments)
Advantage compared to just looking at binned values or histogram is that the approximation is a bit better and the approximating functions are smooth.
I guess when we don't need smoothness, then linear interpolation might do as well or better, e.g. in simulation case using approximation to ppf.
Cheaper than kernel density/distribution methods and extends more easily to multivariate case.
We don't have boundary kernels yet, e.g. for distributions on [0, 1] as we need in copulas
I found several articles that use beta pdf for the polynomial instead of binom as I did.
That also works for random sampling, rvs from the Bernstein distribution or copula.
one problem with empirical histogram bernstein copula: margins may not be exactly uniform. (I'm not sure about the details for that yet.)
aside: avoid numpy meshgrid, it has a weird ordering
(happy debugging when using it, and the print representation of 3-d arrays is difficult to interpret)
Activity
josef-pkt commentedon Feb 11, 2021
I found several articles that use beta pdf for the polynomial instead of binom as I did.
That also works for random sampling, rvs from the Bernstein distribution or copula.
one problem with empirical histogram bernstein copula: margins may not be exactly uniform. (I'm not sure about the details for that yet.)
josef-pkt commentedon Feb 12, 2021
aside: avoid numpy meshgrid, it has a weird ordering
(happy debugging when using it, and the print representation of 3-d arrays is difficult to interpret)
ENH: distribution tools, grid of pdf, cdf, and bernstein poly functio…
ENH: distribution tools, grid of pdf, cdf, and bernstein poly functio…