Created
March 14, 2019 14:16
-
-
Save ven-kyoshiro/785a89d803ceb5d7135689264d9a6403 to your computer and use it in GitHub Desktop.
able to plot sigma area of multivariable normal distribution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
import matplotlib.patches as pat | |
rot = lambda th:np.array([[np.cos(th),-np.sin(th)],[np.sin(th),np.cos(th)]]) | |
mu = np.array([0.,0.]) | |
S = np.array([[1,0],[0,6]]) @ rot(np.pi/3) | |
x = np.random.multivariate_normal(mu, S,10000) | |
fig = plt.figure(figsize=(10, 10)) | |
ax = fig.add_subplot(111) | |
ax.scatter(x[:,0],x[:,1],s = 1) | |
# draw sigma1 | |
U, s, V = np.linalg.svd(S, full_matrices=True) | |
rot_mat = U@V | |
pn = 1. if kaiten[0][1] == 0 else abs(kaiten[0][1])/(kaiten[0][1]) | |
rad = np.arccos(kaiten[0][0])*pn | |
e2 = pat.Ellipse(xy = mu, width = 2*np.sqrt(s[1]), height = 2*np.sqrt(s[0]), alpha = 0.4,fc = None,ec = "red", | |
angle = np.rad2deg(rad), label = str(int(np.rad2deg(rad)))) | |
ax.add_patch(e2) | |
ax.legend() | |
plt.xlim(-10,10) | |
plt.ylim(-10,10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
like this