2
$\begingroup$

I want to define a Gaussian distribution function and plot it in python using the mode and inflection points parameter values instead of using the mean and standard deviation.

For example, I have mode=110 and two points : (40, 160) for asymmetrical points.

Or mode=100 and two points = (50,150) for symmetrical points.

I tried with this code below, but I don't how I can add the second point to the formula which is returned by the function? Since I have two points, and I don't know how to define a function that depends on two points and mode. Thanks in advance for your help!

import numpy as np
matplotlib.pyplot as plt   
def gaussian(x, mode, inf_point):
    return 1/(np.sqrt(2*np.pi)*inf_point)*np.exp(-np.power((x - mode)/inf_point, 2)/2)
x = np.linspace(0,256)
plt.plot(x, gaussian(x, mode, inf_point))
$\endgroup$
14
  • $\begingroup$ According to this blog post a Gaussian normal has two symmetric inflection points, at the mean +/- the sdev. So, your question probably needs to be reformulated. $\endgroup$
    – JohanC
    Jul 20 '20 at 22:46
  • $\begingroup$ @JohanC yes i know but i need to use the mode and inflection points instead the mean and sdev $\endgroup$
    – khadoudj
    Jul 20 '20 at 23:00
  • $\begingroup$ A previous comment beat me to it: the second point is symmetric about 0 from the first. No, I don't know how to use it; my stats class was too long ago. However, you should be able to (a) look up the answer, or (2) get help from the algorithms group. You might also look for an appropriate stats tag to add. $\endgroup$
    – Prune
    Jul 21 '20 at 0:01
  • $\begingroup$ @khadoudj what is the difference between symmetric and asymmetric infliction points? Can you link me to an article/video about them? I can't seem to find them... $\endgroup$ Jul 21 '20 at 0:04
  • 1
    $\begingroup$ If the inflection points are not symmetric about the mode, it's not Gaussian. You'll need to clarify what you actually mean. $\endgroup$
    – Glen_b
    Jul 21 '20 at 11:28
1
$\begingroup$

The following might work for the case explained in the comments.

Given some data, skewnorm.fit will try to find parameters for skewnorm that fit the data. Such a fit needs good initial parameters. Some experimenting suggests that when the skewness parameter is initialized with zero, the resulting fit also has a skewness close to zero. Setting the initial skewness parameter rather high, e.g. 10, seems to generate a fit much closer to the real skewness used for the test data.

The following code first generates some dummy data and draws its histogram and kde. Then a skewnorm is fitted to the data, and the pdf of that fit is drawn on the same plot.

from matplotlib import pyplot as plt
import seaborn as sns
import numpy as np
from scipy.stats import skewnorm

# create some random data from a skewnorm
data = skewnorm.rvs(3, loc=90, scale=50, size=1000).astype(np.int)

# draw a histogram and kde of the given data
ax = sns.distplot(data, kde_kws={'label':'kde of given data'}, label='histogram')

# find parameters to fit a skewnorm to the data
params = skewnorm.fit(data, 10, loc=80, scale=40)

# draw the pdf of the fitted skewnorm
x = np.linspace(0, 255, 500)
ax.plot(x, skewnorm.pdf(x, *params), label='approximated skewnorm')
plt.legend()
plt.show()

resulting plot

$\endgroup$
4
  • $\begingroup$ @JohancC why you used loc=80 and scale=40 ? and how you find the right loc and scale? $\endgroup$
    – khadoudj
    Jul 22 '20 at 19:29
  • $\begingroup$ I just used some test values. You could use half the distance between your special values as guess for the scale (sigma). And the point in the middle as initial guess for loc. E.g. left 40 and right 160 would give (160-40)/2=60 as initial scale and (160+40)/2=100 as initial loc. It are just educated guesses, which might work (or not) in your particular case. (In case the kde is sufficient for your needs, you could leave out the skewnorm.) $\endgroup$
    – JohanC
    Jul 22 '20 at 19:46
  • $\begingroup$ thank you and what does mean 10 in parameters of skewnorm.fit ? $\endgroup$
    – khadoudj
    Jul 22 '20 at 19:58
  • $\begingroup$ It is the initial guess for the skewness. 0 means no skew. See wikipedia where it is called 'shape' (alpha). $\endgroup$
    – JohanC
    Jul 22 '20 at 19:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.