9

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.

2 Answers 2

10

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);
}
Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.