Skip to content

ENH: tools, integrating, probability of hypercubes in multivariate distributions #7302

Open

Description

@josef-pkt

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

Activity

  1. josef-pkt commented on Feb 2, 2021

    @josef-pkt
    MemberAuthor

    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)

    cdfu[j-d:j, j-d:j, j-d:j].shape
    (2, 2, 2)
    
    d = 2
    cdf2prob_grid(cdfu[j-d:j, j-d:j, j-d:j])[-d+1:, -d+1:, -d+1:]
    array([[[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

  2. josef-pkt commented on Feb 4, 2021

    @josef-pkt
    MemberAuthor

    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.

  3. josef-pkt commented on Feb 12, 2021

    @josef-pkt
    MemberAuthor

    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)
  4. josef-pkt commented on Feb 12, 2021

    @josef-pkt
    MemberAuthor

    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

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