-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_binormal_sampling.py
More file actions
72 lines (55 loc) · 1.58 KB
/
test_binormal_sampling.py
File metadata and controls
72 lines (55 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import numpy as np
import matplotlib.pyplot as plt
# inputs (mean is zero)
mean = [0,0]
sig1 = 1.0
sig2 = 2.0
rho = -0.75
Npoints = 5000
#covariance matrix
cov = np.array( [[sig1*sig1,rho*sig1*sig2],[rho*sig1*sig2,sig2*sig2]] )
print("\n\n input covariance matrix: ",cov)
#
# method 1: using np routine
#
x,y = np.random.multivariate_normal(mean,cov,Npoints).T
plt.plot(x,y,'x'); plt.axis('equal'); plt.title('method 1') ; plt.show()
#
# method 2: using Chalesky decomposition
#
from numpy import linalg as LA
U = LA.cholesky(cov)
XX = np.random.normal(size=2*Npoints)
XX.shape = (2,Npoints)
x,y = np.dot(U,XX)
plt.plot(x,y,'x'); plt.axis('equal'); plt.title('method 2') ; plt.show()
#
# method 3: using eigenvvalue decomposition
#
#http://homepages.inf.ed.ac.uk/imurray2/code/matlab_octave_missing/mvnrnd.m
Lambda,E = LA.eig(cov)
LambdaM = [ [Lambda[0],0],[0,Lambda[1]]]
U = np.dot(np.sqrt(LambdaM),E.T)
U = U.T
XX = np.random.normal(size=2*Npoints)
XX.shape = (2,Npoints)
x,y = np.dot(U,XX)
plt.plot(x,y,'x'); plt.axis('equal'); plt.title('method 3') ; plt.show()
# get statistics using np
a=np.cov( np.vstack((x,y)) )
print("\n\n reasult covariance matrix: ")
print(a)
#another way
X1 = np.array(x, ndmin=2, dtype=float)
Y1 = np.array(y, copy=False, ndmin=2, dtype=float)
axis = 0
XX = np.concatenate((X1, Y1), axis)
N = X1.shape[1]
cc = (np.dot(XX, XX.T.conj()) / float(N-1) ).squeeze()
print(cc)
print("\n\n result covariance matrix (by hand): %",(cc))
# <x y>
mm = np.dot(x,y)/float(N)
print("\n\n cross term <x y> %f: "%(mm))
print("\n\n rho: %f "%(mm/np.sqrt(cc[0,0]*cc[1,1])))
print(mm)