dutt
dutt

Reputation: 8209

Plotting exponential with mean

I'm working on a validation script where I'm parsing out some distributions from source code and wanting to create plots. So far most of it is working, but I can't figure out how I'm supposed to supply the mean.

In java we use org.apache.commons.math3.distribution.ExponentialDistribution which takes a mean so we use it like this new ExponentialDistribution(m)

I'm using matplotlib.pyplot for plotting and numpy + scikit.stats for the data. But looking at https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.expon.html#scipy.stats.expon I can't figure out how to supply the mean variable I parse from the code.

In short, if I parse out new ExponentialDistrubution(5)...how do I plot that?

If I remove all the parsing code and other irrelevant stuff my code does this:

import numpy as np
import scipy.stats as stats
import matplotlib.pyplot as plt

xvalues = np.linspace(stats.expon.ppf(0.01), stats.expon.ppf(0.99), 100)
cdf = stats.expon.cdf(xvalues)
plt.plot(xvalues, cdf)
plt.savefig("graph.png")

Upvotes: 2

Views: 5173

Answers (1)

Asgeer
Asgeer

Reputation: 176

From https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.expon.html you find that to shift and scale you can use expon.pdf(x, loc, scale), with loc being the location and scale the inverse of lambda. scale also corresponds to the mean, since the mean of an exponential distribution is 1/lambda.

If you want a distribution with a mean of 5 you would write:

mean = 5
loc = 0

xvalues = np.linspace(stats.expon.ppf(0.01, loc, mean), stats.expon.ppf(0.99, loc, mean), 100)
cdf = stats.expon.cdf(xvalues, loc, mean)
plt.plot(xvalues, cdf)
plt.savefig("graph.png")

Upvotes: 2

Related Questions