Skip to content

Instantly share code, notes, and snippets.

@rwev
Created November 14, 2019 17:44
Show Gist options
  • Save rwev/9a0c346d94c2b90af72f8aa0ca39040f to your computer and use it in GitHub Desktop.
Save rwev/9a0c346d94c2b90af72f8aa0ca39040f to your computer and use it in GitHub Desktop.
Visualize 3D correlations, inspired by Paul Graham's analysis in _Why Nerds Are Unpopular_
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pylab as pl
from mpl_toolkits.mplot3d import Axes3D
xx = np.array([0, 10]) # smart
yy = np.array([0, 10]) # nerd
zz = np.array([0, 10]) # unpopular
means = [xx.mean(), yy.mean(), zz.mean()]
stds = [xx.std() / 3, yy.std() / 3, yy.std() / 3]
xy_corr = 0.6
yz_corr = -0.8
xz_corr = -0.6
covs = [
[stds[0]**2, stds[0]*stds[1]*xy_corr, stds[0]*stds[2]*xz_corr],
[stds[1]*stds[0]*xy_corr, stds[1]**2, stds[1]*stds[2]*yz_corr],
[stds[2]*stds[0]*xz_corr, stds[2]*stds[1]*yz_corr, stds[2]**2]
]
m = np.random.multivariate_normal(means, covs, 1000).T
fig = pl.figure()
ax = Axes3D(fig)
ax.scatter(m[0], m[1], m[2], s=15, facecolor='red', edgecolors='black')
pl.title('Why Nerds Are Unpopular', fontstyle='italic')
ax.set_xlabel('Smarts')
ax.set_ylabel('Nerdiness')
ax.set_zlabel('Unpopularity')
pl.grid(True)
pl.legend()
pl.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment