title: numpy 1.9.0: ValueError: probabilities do not sum to 1
body: <p>I have a large code that at one point samples values from an array according to the probabilities taken from a <a href="https://en.wikipedia.org/wiki/Probability_density_function" rel="noreferrer">probability density function</a> (PDF).</p>
  
  <p>To do this I use the <a href="http://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.random.choice.html" rel="noreferrer">numpy.random.choice</a> which worked just fine until <code>numpy 1.8.0</code>. Here's a MWE (the file <code>pdf_probs.txt</code> can be downloaded <a href="http://pastebin.com/HnWZVvjd" rel="noreferrer">here</a>):</p>
  
  <pre><code>import simplejson
  import numpy as np
  
  # Read probabilities from file.
  f = open('pdf_probs.txt', 'r')
  probs = simplejson.load(f)
  f.close()
  
  print sum(probs)  # &lt;-- Not *exactly* 1. but very close: 1.00000173042
  # Define array.
  arr = np.linspace(1., 100., len(probs))
  
  # Get samples using the probabilities in probs.
  samples = np.random.choice(arr, size=1000, replace=True, p=probs)
  </code></pre>
  
  <p>The thing is that after testing it with <code>numpy 1.9.0</code> the above code fails with the error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "numpy_180_vs_190_np_random_choice.py", line 13, in &lt;module&gt;
      samples = np.random.choice(arr, size=1000, replace=True, p=probs)
    File "mtrand.pyx", line 1083, in mtrand.RandomState.choice (numpy/random/mtrand/mtrand.c:10106)
  ValueError: probabilities do not sum to 1
  </code></pre>
  
  <p>The sum of the PDF probabilities will not sum to <em>exactly</em> 1. given the small deviations that appear when using very small floats.</p>
  
  <p>From what I can gather the previous version of <code>numpy</code> (<code>1.8.0</code>) apparently had a larger tolerance than the new <code>1.9.0</code> version, but I could be wrong.</p>
  
  <p>Why does this work with <code>numpy 1.8.0</code> but not with <code>1.9.0</code>? How can I make my code work with the new <code>1.9.0</code> version?</p>