1

I'm sampling values from a distribution. Here it is shown how to create a confidence interval for given data.

I want to continue sampling until the confidence interval is smaller than a given interval max_error. Is there a way to estimate how many more samples I will need?

sample_list = []
max_error = 10

while True:
  list.append(get_sample())
  // See https://stackoverflow.com/a/34474255
  interval = scipy.stats.t.interval(0.95, len(sample_list) - 1, loc=np.mean(sample_list), scale=scipy.stats.sem(sample_list))
  estimated_error = abs(interval[1]-interval[0])/2
  estimated_required_samples = ??? // How to calculate this?
  print(f"{len(sample_list)}/{estimated_required_samples} measurements, mean: {mean(sample_list)} +/- {estimated_error}")

  if estimated_error <= max_error:
    return mean(sample_list)

Some formula is given on Wikipedia, but it requires knowledge of the variance, which is still be estimated while sampling.

Jan Rüegg
  • 9,587
  • 8
  • 63
  • 105

0 Answers0