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/bootstrap/lecture/permutecorrelation.py
2015-11-26 23:40:12 +01:00

61 lines
1.9 KiB
Python

import numpy as np
import matplotlib.pyplot as plt
plt.xkcd()
fig = plt.figure( figsize=(6,3.5) )
rng = np.random.RandomState(637281)
# generate correlated data:
n = 200
a = 0.2
x = rng.randn(n);
y = rng.randn(n) + a*x;
rd = np.corrcoef(x, y)[0, 1]
print rd
# permutation:
nperm = 1000
rs = []
for i in xrange(nperm) :
xr=rng.permutation(x)
yr=rng.permutation(y)
rs.append( np.corrcoef(xr, yr)[0, 1] )
# pdf of the correlation coefficients:
h, b = np.histogram(rs, 20, density=True)
# significance:
rq = np.percentile(rs, 95.0);
ax = fig.add_subplot(1, 1, 1)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticks_position('bottom')
ax.annotate('Measured\ncorrelation\nis significant!',
xy=(rd, 1.1), xycoords='data',
xytext=(rd, 2.2), textcoords='data', ha='left',
arrowprops=dict(arrowstyle="->", relpos=(0.2,0.0),
connectionstyle="angle3,angleA=10,angleB=80") )
ax.annotate('95% percentile',
xy=(0.14, 0.9), xycoords='data',
xytext=(0.2, 4.0), textcoords='data', ha='left',
arrowprops=dict(arrowstyle="->", relpos=(0.1,0.0),
connectionstyle="angle3,angleA=30,angleB=70") )
ax.annotate('Distribution of\nuncorrelated\nsamples',
xy=(-0.08, 3.6), xycoords='data',
xytext=(-0.22, 5.0), textcoords='data', ha='left',
arrowprops=dict(arrowstyle="->", relpos=(0.5,0.0),
connectionstyle="angle3,angleA=150,angleB=100") )
ax.bar(b[:-1], h, width=b[1]-b[0], color='#ffff66')
ax.bar(b[:-1][b[:-1]>=rq], h[b[:-1]>=rq], width=b[1]-b[0], color='#ff9900')
ax.plot( [rd, rd], [0, 1], 'b', linewidth=4 )
ax.set_xlim(-0.25, 0.35)
ax.set_xlabel('Correlation coefficient')
ax.set_ylabel('Probability density of H0')
plt.tight_layout()
plt.savefig('permutecorrelation.pdf')
#plt.show();