Sunday, February 15, 2015

py20. qqnorm in Python

The qqnorm function from statsmodels.api is used for the qqnorm plots.


The percentile of x is plotted against the normal distribution values for same percentiles or quantiles.


The calculated, vs. that from the statsmodels.api, are plotted. Ofcourse, there is no difference, as we will see in the R program.

# ex20.py
from __future__ import print_function, division
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import norm
from statsmodels.api import qqplot
N = 100
nm = ['normal','beta','uniform']
ld = 0
if ld == 0:
    y = np.random.normal(size = N)
elif ld == 1:
    y = np.random.beta(2,5,N)
elif ld == 2:
    y = np.random.uniform(-.1,3,N)

z = (y-y.mean())/y.std()
z.sort()
prob1 = np.linspace(0,100, num = N)
prob2 = np.arange(1/(2*N), 1, step = 1/N)
zs = np.percentile(z,prob1)
pz = norm.ppf(prob2)
plt.plot(pz,zs,'go',pz,pz,'k')
plt.title('Calculated - ' + nm[ld],)
plt.xlabel('pz')
plt.ylabel(' zs')
qqplot(z, fit = True, line='45')
plt.title('qqnorm - ' + nm[ld])

Output:

No comments:

Post a Comment