Skip to content

ENH: bernstein polynomials for cdf, ppf and pdf, univariate and multivariate #7314

Open

Description

@josef-pkt

see also #7296 (comment)

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

Activity

  1. josef-pkt commented on Feb 11, 2021

    @josef-pkt
    MemberAuthor

    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.)

  2. josef-pkt commented on Feb 12, 2021

    @josef-pkt
    MemberAuthor

    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)

    [i.shape for i in np.meshgrid(np.arange(2), np.arange(3), np.arange(4))]
    [(3, 2, 4), (3, 2, 4), (3, 2, 4)]
    
    [i.shape for i in np.meshgrid(np.arange(2), np.arange(3), np.arange(4), sparse=True)]
    [(1, 2, 1), (3, 1, 1), (1, 1, 4)]
    
    np.__version__
    '1.16.4'
    
    np.column_stack([i.ravel() for i in np.meshgrid(np.arange(2), np.arange(3), np.arange(4))])
    array([[0, 0, 0],
           [0, 0, 1],
           [0, 0, 2],
           [0, 0, 3],
           [1, 0, 0],
           [1, 0, 1],
           [1, 0, 2],
           [1, 0, 3],
           [0, 1, 0],
           [0, 1, 1],
           [0, 1, 2],
           [0, 1, 3],
           [1, 1, 0],
           [1, 1, 1],
           [1, 1, 2],
           [1, 1, 3],
           [0, 2, 0],
           [0, 2, 1],
           [0, 2, 2],
           [0, 2, 3],
           [1, 2, 0],
           [1, 2, 1],
           [1, 2, 2],
           [1, 2, 3]])
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions