I want to see if numpy.random.exponential
was implemented using F^{-1} (U) method, where F is the c.d.f of exponential distribution and U is uniform distribution.
I tried numpy.source(random.exponential)
, but returned 'Not available for this object'. Does it mean this function is not written in Python?
I also tried inspect.getsource(random.exponential)
, but returned an error saying it's not module, function, etc.
With the help of numpy. random. exponential() method, we can get the random samples from exponential distribution and returns the numpy array of random samples by using this method. Return : Return the random samples of numpy array.
choice() function is used to get random elements from a NumPy array. It is a built-in function in the NumPy package of python. Parameters: a: a one-dimensional array/list (random sample will be generated from its elements) or an integer (random samples will be generated in the range of this integer)
The numpy.random.rand() function creates an array of specified shape and fills it with random values. Syntax : numpy.random.rand(d0, d1, ..., dn) Parameters : d0, d1, ..., dn : [int, optional]Dimension of the returned array we require, If no argument is given a single Python float is returned.
numpy's sources are at github so you can use github's source-search.
As often, these parts of the library are not implemented in pure python.
The python-parts (in regards to your question) are here:
The more relevant code-part is from distributions.c:
double rk_standard_exponential(rk_state *state)
{
/* We use -log(1-U) since U is [0, 1) */
return -log(1.0 - rk_double(state));
}
double rk_exponential(rk_state *state, double scale)
{
return scale * rk_standard_exponential(state);
}
A lot of numpy functions are written with C/C++ and Fortran. numpy.source()
returns the source code only for objects written in Python. It is written on NumPy website.
You can find all of the NumPy functions on their GitHub page. One that you need is written in C. Here is the link to the file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With