Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the mode of distribution in scipy.stats

The scipy.stats library has functions to find the mean and median of a fitted distribution but not mode.

If I have the parameters of a distribution after fitting to data, how can I find the mode of the fitted distribution?

like image 660
Adnan Tamimi Avatar asked Jan 09 '20 11:01

Adnan Tamimi


People also ask

How do you find the mode using Scipy?

stats. mode(array, axis=0) function calculates the mode of the array elements along the specified axis of the array (list in python).

What is PPF in Scipy stats?

Percent point function (inverse of cdf ) at q of the given RV.


1 Answers

If I don't get your wrong, you want to find the mode of fitted distributions instead of mode of a given data. Basically, we can do it with following 3 steps.

Step 1: generate a dataset from a distribution

from scipy import stats
from scipy.optimize import minimize
# generate a norm data with 0 mean and 1 variance
data = stats.norm.rvs(loc= 0,scale = 1,size = 100)
data[0:5]

Output:

array([1.76405235, 0.40015721, 0.97873798, 2.2408932 , 1.86755799])

Step 2: fit the parameters

# fit the parameters of norm distribution
params = stats.norm.fit(data)
params

Output:

(0.059808015534485, 1.0078822447165796)

Note that there are 2 parameters for stats.norm, i.e. loc and scale. For different dist in scipy.stats, the parameters are different. I think it's convenient to store parameter in a tuple and then unpack it in the next step.

Step 3: get the mode(maximum of your density function) of fitted distribution

# continuous case
def your_density(x):
    return -stats.norm.pdf(x,*paras)
minimize(your_density,0).x

Output:

0.05980794

Note that a norm distribution has mode equals to mean. It's a coincidence in this example.

One more thing is that scipy treats continuous dist and discrete dist different(they have different father classes), you can do the same thing with following code on discrete dists.

## discrete dist, example for poisson
x = np.arange(0,100) # the range of x should be specificied
x[stats.poisson.pmf(x,mu = 2).argmax()] # find the x value to maximize pmf

Out:

1

You can it try with your own data and distributions!

like image 151
Travis Avatar answered Sep 18 '22 15:09

Travis