Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to view the source code of numpy.random.exponential?

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.

like image 604
Lei Hao Avatar asked Oct 07 '16 05:10

Lei Hao


People also ask

How do you generate a sample from an exponential distribution in Python?

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.

How do you select a random element from a 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)

What is random rand () function in NumPy?

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.


2 Answers

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);
}
like image 91
sascha Avatar answered Oct 21 '22 12:10

sascha


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.

like image 22
Wassily Minkow Avatar answered Oct 21 '22 13:10

Wassily Minkow