In [1]:
%pylab inline
import sklearn.decomposition
In [2]:
n_dims = 10000
n_steps = 1000
In [3]:
walk = np.zeros((n_steps, n_dims))
for i in range(1, n_steps):
walk[i] = walk[i-1] + np.random.normal(scale=1/np.sqrt(n_dims), size=n_dims)
In [4]:
pca = sklearn.decomposition.PCA()
In [5]:
pca_walk = pca.fit_transform(walk)
In [6]:
# The observed variance in the first PCA component
pca.explained_variance_ratio_[0]
Out[6]:
In [7]:
# Predicted value
6 / np.pi**2
Out[7]:
In [8]:
# Plot the projection of the trajectory onto the first few PCA components
for i in range(5):
plot(pca_walk[:, i], label='PCA %d' % i)
xlabel('Step')
ylabel('Distance along component')
legend(loc='upper right', fontsize=10);