scipy: scale the probability plot

Viewed 407

I have 38400 measurement values which are normal distributed. The mean is -4.10e-11 and sigma is 0.0229. Not surprisingly, the probability plot has a slope of 0.0229. For plotting and the detection of outliers I would like to plot it with a slope of 1. The range of theoretical quantiles should then be approximately the same as for the observations, i.e. between -0.09 and +0.09. How can I do this?

(For the code example I am using random numbers instead of real measurements).

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

mean = -4.10e-11
sigma = 0.0229

np.random.seed(7654321)
e_array = np.random.normal(mean, sigma, 38400)

minvalue = np.min(e_array)
maxvalue = np.max(e_array)

qq = stats.probplot(e_array, dist="norm", plot=plt)
qq_slope, qq_intercept, qq_r = qq[1]
print('Minimum value: ', minvalue)
print('Maximum value: ', maxvalue)
print('QQ slope:', qq_slope)
print('QQ intercept:', qq_intercept)
print('QQ r:', qq_r)

plt.show()
1 Answers

I think this is what you mean.

I plot the sample cdf against the theoretical cdf and the resulting scatter plot should cluster around a line with slope one.

from scipy.stats import norm
import matplotlib.pyplot as plt

N = 100
mean = -4.10e-11
sigma = 0.0229
sample = norm.rvs(loc=mean, scale=sigma, size=N)

sample_cdf = sorted(sample)

quantile_points = [(_-0.5)/N for _ in range(1,1+N)]
theoretical_cdf = [norm.ppf(_, loc=mean, scale=sigma) for _ in quantile_points]

plt.plot([theoretical_cdf[-1], theoretical_cdf[0]], [theoretical_cdf[-1], theoretical_cdf[0]], 'r-', zorder=1)
plt.scatter(sample_cdf, theoretical_cdf, zorder=2)
plt.show()

Here's one resulting plot. No frills.

Q-Q

Related