How to sample M times from N different normal distributions in python? Is there a "faster" way in terms of processing time?

Viewed 95

I need to sample multiple (M) times from N different normal distributions. This repeated sampling will happen in turn several thousand times. I want to do this in the most efficient way, because I would like to not die of old age before this process ends. The code would look something like this:

import numpy as np

# bunch of stuff that is unrelated to the problem

number_of_repeated_processes = 5000
number_of_samples_per_process = 20

# the normal distributions I'm sampling from are described by 2 vectors:
#
# myMEANS <- an numpy array of length 10 containing the means for the distributions
# myVAR <- an numpy array of length 10 containing the variance for the distributions


for i in range(number_of_repeated_processes):
  # myRESULT is a list of arrays containing the results for the sampling
  #
  myRESULT = [np.random.normal(loc=myMEANS[j], scale=myVAR[j], size = number_of_samples_per_process) for j in range(10)]
  #
  # here do something with myRESULT

# end for loop

The questions is... is there a better way to obtain the myRESULT matrix

1 Answers

np.random.normal accepts means-var as an array directly and you can choose a size that covers all the sampling in one run without loops:

myRESULT = np.random.normal(loc=myMEANS, scale=myVAR, size = (number_of_samples_per_process, number_of_repeated_processes,myMEANS.size))

This will return a number_of_samples_per_process by number_of_repeated_processes column for each mean-var pair in your myMEANS-myVAR array. For example, to access your samples of myMEANS[i]-myVAR[i], use myRESULT[...,i]. This should boost your performance somewhat.

Related