Skip to content

Instantly share code, notes, and snippets.

@mattjj
Created April 11, 2012 01:31
Show Gist options
  • Save mattjj/2356182 to your computer and use it in GitHub Desktop.
Save mattjj/2356182 to your computer and use it in GitHub Desktop.
Q-Q plots
from __future__ import division
import numpy as np
import scipy.stats as stats
from matplotlib import pyplot as plt
def plot(data,cdf=stats.norm.cdf):
plt.figure()
plt.plot(np.cumsum(1./len(data) * np.ones(data.shape)),cdf(np.sort(data)),'bx')
plt.plot((0,1),(0,1),'r-')
plt.ylim((0,1))
plt.xlim((0,1))
plt.xlabel('empirical percentiles')
plt.ylabel('fit percentiles')
function qqplot(data,cdf)
if ~exist('cdf','var'), cdf=@normcdf; end
figure();
hold on
plot(cumsum(1/length(data)*ones(size(data))),cdf(sort(data)),'bx');
plot([0,1],[0,1],'r-');
ylim([0,1]);
xlim([0,1]);
xlabel('empirical percentiles');
ylabel('fit percentiles');
hold off
end
@mattjj
Copy link
Author

mattjj commented Apr 11, 2012

In Matlab, let's generate some data from an exponential

data = exprnd(16,[100,1]);

Now how well does the maximum-likelihood fit exponential distribution fit the data?

qqplot(data,@(x) expcdf(x,mean(data)));

What if we had a lot more data?

data = exprnd(mu,[10000,1]);
qqplot(data,@(x) expcdf(x,mean(data)));

What if we fit some totally bogus distributions?

qqplot(data,@(x) normcdf(x,mean(data)));
qqplot(data,@(x) normcdf(x,mean(data),std(data)));

@mattjj
Copy link
Author

mattjj commented May 8, 2013

OKAY technically these are P-P plots

@tommycarstensen
Copy link

This is good. I don't understand, why SciPy doesn't do QQ plots.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment