Search code examples
pythonsamplingnormal-distribution

Sequential Sampling


To sample from N(1,2) with sample size 100 and calculating the mean of this sample we can do this:

import numpy as np

s = np.random.normal(1, 2, 100)
mean = np.mean(s)

Now if we want to produce 10000 samples and save mean of each of them we can do:

sample_means = []
for x in range(10000):
    sample = np.random.normal(1, 2, 100)
    sample_means.append (sample.mean())

How can I do it when we want to sample sequentially from N(1,2) and estimate the distribution mean sequentially?


Solution

  • IIUC you meant accumulative

    sample = np.random.normal(1,2,(10000, 100))
    sample_mean = []
    for i,_ in enumerate(sample):
        sample_mean.append(sample[:i+1,:].ravel().mean())
    

    Then sample_mean contains the accumulative samples mean

    sample_mean[:10]
    [1.1185342714036368,
     1.3270808654923423,
     1.3266440422140355,
     1.2542028664103761,
     1.179358517854582,
     1.1224645540064788,
     1.1416887857272255,
     1.1156887336750463,
     1.0894328800573165,
     1.0878896099712452]