This repository has been archived on 2021-05-17. You can view files and clone it, but cannot push or open issues or pull requests.
scientificComputing/statistics/lecture/cumulative.py

46 lines
1.2 KiB
Python

import numpy as np
import matplotlib.pyplot as plt
from plotstyle import *
# data:
rng = np.random.RandomState(981)
data = rng.randn(100)
xs = np.sort(data)
cdf = np.arange(len(xs))/float(len(xs))
# Gauss:
dx = 0.01
xx = np.arange(-4.0, 4.0, dx)
gauss = np.exp(-0.5*xx*xx)/np.sqrt(2.0*np.pi)
gausscdf = np.cumsum(gauss)*dx
# plot:
fig, ax = plt.subplots(figsize=cm_size(figure_width, 1.2*figure_height))
ax.set_xlabel('x')
ax.set_xlim(-3.2, 3.2)
ax.set_xticks(np.arange(-3.0, 3.1, 1.0))
ax.set_ylabel('F(x)')
ax.set_ylim(-0.05, 1.05)
ax.set_yticks(np.arange(0.0, 1.1, 0.2))
med = xs[cdf>=0.5][0]
ax.plot([-3.2, med, med], [0.5, 0.5, 0.0], zorder=-5, **lsSpine)
ax.text(-2.8, 0.55, 'F=0.5')
ax.text(0.15, 0.25, 'median at %.2f' % med)
q3 = xs[cdf>=0.75][0]
ax.plot([-3.2, q3, q3], [0.75, 0.75, 0.0], zorder=-5, **lsSpine)
ax.text(-2.8, 0.8, 'F=0.75')
ax.text(0.8, 0.5, '3. quartile at %.2f' % q3)
p = cdf[xs>=-1.0][0]
ax.plot([-3.2, -1.0, -1.0], [p, p, 0.0], zorder=-5, **lsSpine)
ax.text(-2.8, 0.2, 'F=%.2f' % p)
ax.text(-0.9, 0.05, '-1')
ax.plot(xx, gausscdf, zorder=-1, **lsAm)
ax.plot(xs, cdf, zorder=-1, **lsB)
ax.plot([-3.2, 3.2], [1.0, 1.0], zorder=-10, **lsGrid)
fig.savefig('cumulative.pdf')