Skip to content

Instantly share code, notes, and snippets.

@thoppe
Created August 27, 2022 17:10
Show Gist options
  • Save thoppe/09ddf2533c2ed5e9af6d329b64f64765 to your computer and use it in GitHub Desktop.
Save thoppe/09ddf2533c2ed5e9af6d329b64f64765 to your computer and use it in GitHub Desktop.
'''
Code releated for stack exchange question:
https://math.stackexchange.com/questions/4519928/how-do-i-stop-my-normals-from-spreading-out-under-perturbation
'''
import numpy as np
from tqdm import tqdm
import pylab as plt
import pandas as pd
import seaborn as sns
from scipy.stats import skew, kurtosis
N = 500
dx = 0.01
n_iters = 100_000
X = np.random.normal(size=N)
data = []
for i in tqdm(range(n_iters)):
X += dx * np.random.normal(size=N)
data.append(
{
"m1": X.mean(),
"m2": X.std(),
"m3": skew(X),
"m4": kurtosis(X),
}
)
df = pd.DataFrame(data)
plt.title(f"{N}-dim indep. norm. vectors perturbated by {dx:0.2f}*N(0,1)")
plt.plot(df.m1, label="mu")
plt.plot(df.m2, label="std")
plt.plot(df.m3, label="skew")
plt.plot(df.m4, label="kurtosis")
plt.xlabel(f"# of perturbations")
plt.legend()
sns.despine()
plt.savefig("drift_example.png")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment