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: tools, integrating, probability of hypercubes in multivariate distributions #7302
we need tools function with the formulas, because I don't find them when I need them
1-dim : prob is just interval cdf(ub) - cdf(lb)
2-dim: prob of rectangle: cdf(b) - cdf(a) - cdf(d) + cdf(c) What's the best way to get probs of all cells in a grid? (vectorized) #7106 (comment)
k-dim: reference joe 2014 copula book p. 27 equ (2.2) #7454 (comment)
application
binning in higher dimensions, e.g. for gof
prob of multivariate discrete with latent variables #7301 copula with discrete margins
actually this looks easy on a grid because np.diff is the inverse operation of cumsum
this is limited by max ndim in numpy
def prob2cdf_grid(x):
"""cumulative counts from cell provabilites on a grid
"""
cdf = np.asarray(x).copy()
k = cdf.ndim
for i in range(k):
cdf = cdf.cumsum(axis=i)
return cdf
def cdf2prob_grid(x):
"""cell provabilites from cumulative counts on a grid
"""
pdf = np.asarray(x).copy()
k = pdf.ndim
for i in range(k):
pdf = np.diff(pdf, prepend=0, axis=i)
return pdf
this works also for a single hypercube, with shape (2, 2,...2)
np.unravel_index raises an exception if dimension is larger than 32. (triggered by bug in my development code, not intentional)
We have the same restriction with flattened grid as when using k-dim array grid, if we use numpy ravel and unravel functions for indexing.
the two functions in the previous comment are now included in PR #7296 in distributions.tools together with helper functions for bernstein polynomials
two possible versions, using prepend option in np.diff
full grid containing support of distribution, e.g. copula as distribution on [0,1]^d, first edge point is lower support bound, last edge point is upper support bound. That's my current use case. cdf at first edge point is zero, cdf(edges[0]) = 0.
This also works for distributions in R+.
left open bin, in the normal case the lower support bound is -inf. Instead of including it we can drop it and use first bin edge as the first finite bin edge and include all left out probability cdf(edges[0]) > 0, pdf from diff of first bin is pdf(first bin) = cdf(edges[0]). Case is not yet properly checked in current function.
I didn't try open upper bin, i.e. fill in the remainder with 1 - sum(probs), or 1 - cdf(upper interior edge)
aside: The Bernstein polynomial distribution and Bernstein copula literature makes heavy use of multivariate forward differencing as here with finite number of grid points. It's used for the derivation of pdf from cdf of multivariate Bernstein polynomial distribution. #7314
Activity
josef-pkt commentedon Feb 2, 2021
actually this looks easy on a grid because np.
diffis the inverse operation ofcumsumthis is limited by max
ndimin numpythis works also for a single hypercube, with shape (2, 2,...2)
A version that doesn't use k-dim numpy array, needs to use multi_index
something including
cdfg[np.ravel_multi_index(([j, j] - comb).T, (3, 3))]but I still need to convert to proper function
josef-pkt commentedon Feb 4, 2021
np.unravel_indexraises an exception if dimension is larger than 32. (triggered by bug in my development code, not intentional)We have the same restriction with flattened grid as when using k-dim array grid, if we use numpy ravel and unravel functions for indexing.
josef-pkt commentedon Feb 12, 2021
the two functions in the previous comment are now included in PR #7296 in
distributions.toolstogether with helper functions for bernstein polynomialstwo possible versions, using prepend option in np.diff
cdf(edges[0]) = 0.This also works for distributions in R+.
pdf(first bin) = cdf(edges[0]). Case is not yet properly checked in current function.1 - sum(probs), or1 - cdf(upper interior edge)josef-pkt commentedon Feb 12, 2021
aside: The Bernstein polynomial distribution and Bernstein copula literature makes heavy use of multivariate forward differencing as here with finite number of grid points. It's used for the derivation of pdf from cdf of multivariate Bernstein polynomial distribution.
#7314