Reputation: 31
Unless I've gone completely insane, the documentation for numpy.random.exponential()
http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.exponential.html
suggests that setting the scale parameter to 1.0 should guarantee that function calls return a number between 0.0 and 1.0, since it is only defined for x > 0.
I've graphed it on wolfram alpha to verify I'm not going insane and it does range from 0 to 1.
https://www.wolframalpha.com/input/?i=graph+exp(-x)+from+-1+to+10
I also tried both numpy.random.exponential() since the default argument is 1.0 and numpy.random.standard_exponential(), but both sometimes give me values greater than 1.
I feel like I've made some silly mistake in my understanding but cannot find it. Any help would be greatly appreciated.
Example code that I ran:
import numpy as np
print np.random.exponential(scale=1.0)
Example returned value:
1.56783951494
Upvotes: 0
Views: 4844
Reputation: 31
I think I've realized my stupid mistake. It's not returning f(x, beta) it's returning x. Thank you!
Upvotes: 0
Reputation: 353019
The exponential function has pdf = 1/beta * exp(-x/beta). You're passing scale = beta = 1.0, so you have pdf = 1/1 * exp(-x/1) = exp(-x).
This isn't zero above 1. In fact, you have a 63% chance of getting a number between 0 and 1, and a 37% chance of getting a value above 1:
>>> quad(lambda x: np.exp(-x), 0, 1)
(0.6321205588285578, 7.017947987503856e-15)
>>> quad(lambda x: np.exp(-x), 1, np.inf)
(0.3678794411714423, 2.149374899076157e-11)
Check:
>>> ns = np.random.exponential(scale=1.0, size=10**6)
>>> (ns < 1).mean()
0.63164799999999
Now it's true that the PDF exp(-x)
stays between 0 and 1, but that doesn't mean that all the random numbers drawn from the distribution will be between 0 and 1.
Think of a die: the chance of any given outcome is 1/6, but every single numeric outcome is >= 1.
Upvotes: 2