Search code examples
pythonnumpymultidimensional-arrayrandomprobability

Generate an N-dimensional matrix using Numpy


For a certain assignment, I have to create a multivariate discrete probability mass function over N random variables. I want to do this by creating an array A filled with random numbers where each element denotes the joint probability over the random variables. In case of 2 random variables, having i and j possible values respectively, this can be done by creating an (i*j) Numpy array filled with random numbers where the total sum = 1.

It becomes more difficult however, when an additional random variable with k possible values is introduced. In this case, I need to have an i*j*k Numpy array, again filled with random numbers where the total sum equals 1.

Say I am given the structure (number of possible values for each random variable) as a list [n1,n2,...,nN], how can I from here create such an N dimensional Numpy array?


Solution

  • If l is your list of dimensions, you could let

    a = np.random.random(size=l)
    a = a/a.sum()