pythonmultidimensional-arraydistributionnormal-distributiongauss

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.


Solution

  • 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! :)