0

I am trying to calculate the exact median of a simple standard normal PDF in Python 36. The code looks like this:

from scipy.stats import norm
from pynverse import inversefunc

mean = 'some_number'
standard_deviation = 1

inverse_normal_pdf = inversefunc(lambda x: norm.pdf(x, mean, standard_deviation))
median = inverse_normal_pdf(norm.pdf(float('-inf'), mean, standard_deviation)+.5)

I use the pynverse library to get the inverse of the normal PDF and use the solver for upper limit of integration from here to arrive to the solution for the median. But this method works for only means in the range [-8.6:11.2], and any other mean outside this range gives me exactly the number 2.6180339603380443 for some reason. I can't figure out what's happening here? What is this number?

Yudeg
  • 21
  • 5
  • Why aren't you using the median function in numpy ? `x=np.random.normal(loc=0,scale=1,size=100000)` `np.median(x)` – Gaurav Taneja Mar 01 '18 at 08:51
  • gboffi: changing the mean only shifts the function along the x-axis right? why is its monotonicity affected with shifting the mean? – Yudeg Mar 01 '18 at 09:30
  • gaurav: I'm trying to solve for a continuous function. Thanks for the tip, I will use the numpy method when I can get away with it. – Yudeg Mar 01 '18 at 09:32
  • @gboffi: using: `inversefunc(lambda x: norm.cdf(x, mean, standard_deviation))` now, isn't this CDF monotonic for any value for the mean? Very confused, sorry if I'm pushing a really simple problem. – Yudeg Mar 01 '18 at 09:43

1 Answers1

0

If your distribution is symmetrical (which is the case of the normal distribution), then the theoretical median, has the same value as the average.

Otherwise, the median probability, is the one corresponding with 0.5 in CDF distribution.

Camion
  • 1,264
  • 9
  • 22
  • Can you give me a hint for a pythonic way to find the CDF value for 0.5 probability? I tried my method with norm.cdf() instead of norm.pdf(), but for values out of bounds pynverse can't get the inverse... – Yudeg Mar 01 '18 at 09:08
  • The reverse function of norm.cdf in norm.ppf https://stackoverflow.com/questions/20626994/how-to-calculate-the-inverse-of-the-normal-cumulative-distribution-function-in-p – Camion Mar 01 '18 at 22:58