title: Bootstrap t method Python implementation
body: <p>I have this function to perform a <a href="https://en.wikipedia.org/wiki/Welch&#39;s_t-test" rel="nofollow noreferrer">t-test</a>, with samples and populations using the same key in dictionaries. It works well, as intended.</p>
  
  <pre><code>def ttest_(d):
      result = {}
      for k, (l, t) in d.items():
          mean_sample = np.mean(t) 
          mean_population = np.mean(l)
          sd_sample = np.std(t, ddof=1)
          sd_population = np.std(l, ddof=1)
          sample_size = len(t)
          population_size = len(l)
          result[k] = round(((mean_sample - mean_population) /
                                  np.sqrt(((sd_sample/np.sqrt(sample_size))**2) +
                                           ((sd_population/np.sqrt(population_size))**2))), 2)
  </code></pre>
  
  <p>How can I modify this function in order to:</p>
  
  <p>--> Instead of doing the final calculation once, do it it in a bootstrap x times along the lines of something like:</p>
  
  <pre><code>for _ in range(1000)
  </code></pre>
  
  <p>--> The previous step, would create a distribution of T-tests per key, then the result[k] would be the nth percentile value.... that you could specify with a parameter, and give a value say 0.05.</p>
  
  <p><strong>Edit #1:</strong>
  For clarity, the way I use the function is the following:</p>
  
  <pre><code>pairs = {}
  for (k, v), (k2, v2) in product(population.items(), samples.items()):
      if k == k2:
          pairs.update({k: (v, v2)})
  </code></pre>
  
  <p>Then applied the formula on this dict:</p>
  
  <pre><code>ttest_ = ttest_(pairs)
  </code></pre>
  
  <p><strong>Edit #2:</strong> 
  Is important to preserve this structure of applying the function on a dictionary of dictionaries in order to make the associations between the different samples and keys, and get the associated result[k]. The only difference is adding a bootstrap and the percentile selection.</p>
  
  <p><strong>Edit #3:</strong> 
  Thanks to Norman question. To clarify that in the new formula, you compare the same sample[k] with a random sub sample drawn from the population[k], x times, that's how you get the distribution. Those sub-samples are of the size of the original sample[k]. </p>