Dániel Terbe
Dániel Terbe

Reputation: 105

Create N-Dimensional normal distribution from single ones (in python)

Is there a general solution for this? You have to multiply them, but it is hard to implement.

For the 2 Dimensional case you can use the outer product of the two vector representing single normal distributions.

Upvotes: 2

Views: 572

Answers (2)

pjs
pjs

Reputation: 19855

The general solution involves Cholesky decomposition of the variance/covariance matrix. Cholesky decomposition is available in Python via numpy.

Upvotes: 3

Dániel Terbe
Dániel Terbe

Reputation: 105

I found a possible solution and tested in 2d case. It seems good, but I'll test it on more cases:

def normal_nd(*priors):
    # Trivial case
    if len(priors) == 1:
        return priors

    # General case
    shape = []
    for item in priors:
        shape.append(len(item))
    n = np.ones(shape)

    for idx, _ in np.ndenumerate(n):
        for ax, element in enumerate(idx):
            n[idx] *= priors[ax][element]

    return n

2d case plot

Edit: I tested it in general cases too, it seems it is a correct solution! :)

Upvotes: 0

Related Questions