Lei Hao
Lei Hao

Reputation: 799

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.

Upvotes: 9

Views: 8752

Answers (2)

sascha
sascha

Reputation: 33532

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);
}

Upvotes: 10

Wassily Minkow
Wassily Minkow

Reputation: 53

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.

Upvotes: 3

Related Questions