From 1be7effea73c5b9343c605cc39537ec0e49898f2 Mon Sep 17 00:00:00 2001 From: "Jessica B. Hamrick" Date: Thu, 14 Jul 2016 14:54:03 -0500 Subject: [PATCH] Add confidence intervals exercise --- README.html | 17 +++++++++++++++-- README.rst | 12 ++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/README.html b/README.html index fb8d155..387e1a3 100644 --- a/README.html +++ b/README.html @@ -4,12 +4,12 @@ -100 numpy exercises +100 numpy exercises
-

100 numpy exercises

+

100 numpy exercises

A joint effort of the numpy community

The goal is both to offer a quick reference for new and old users and to @@ -949,7 +949,20 @@ the rows which only contain integers and which sum to n. (★★★)

print(X[M]) +
  • Compute bootstrapped 95% confidence intervals for the mean of a 1D array X (i.e., resample the elements of an array with replacement N times, compute the mean of each sample, and then compute percentiles over the means).

    +
  • +
    +
    +# Author: Jessica B. Hamrick
    +
    +N = 1000 # number of bootstrap samples
    +idx = np.random.randint(0, X.size, (N, X.size))
    +means = X[idx].mean(axis=1)
    +confint = np.percentile(means, [2.5, 97.5])
    +print(confint)
    +
    +
    diff --git a/README.rst b/README.rst index f32cf4c..02fa815 100644 --- a/README.rst +++ b/README.rst @@ -1106,3 +1106,15 @@ Thanks to Michiaki Ariga, there is now a M = np.logical_and.reduce(np.mod(X, 1) == 0, axis=-1) M &= (X.sum(axis=-1) == n) print(X[M]) + +#. Compute bootstrapped 95% confidence intervals for the mean of a 1D array X (i.e., resample the elements of an array with replacement N times, compute the mean of each sample, and then compute percentiles over the means). + + .. code-block:: python + + # Author: Jessica B. Hamrick + + N = 1000 # number of bootstrap samples + idx = np.random.randint(0, X.size, (N, X.size)) + means = X[idx].mean(axis=1) + confint = np.percentile(means, [2.5, 97.5]) + print(confint)