-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcovariance.py
59 lines (55 loc) · 1.86 KB
/
covariance.py
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
"""Covariance"""
import numpy as np
class Covariance():
'''
The Covariance class implements a covariance matrix as a diagonal matrix of positive values
'''
def __init__(self,
size=3,
sigma1=2.50,
sigma2=1.5,
sigma3=0.5,
rflag=0,
msg = '',
verbose=True):
'''
Constructor for Covariance class. Declare object as follows:
myObject = covariance.Covariance()
If you imported covariance as an alias, say, as cov, then an object declaration might be
myObject = cov.Covariance()
Parameters
----------
size: size of the matrix, default=3
sigma1: parameters for three diagonal elements of default covariance
sigma2
sigma3
The data member for this class is called Cov
'''
if size==1:
self.Cov=np.array([[sigma1**2]],float)
elif size==3:
self.Cov = np.diag([sigma1**2,sigma2**2,sigma3**2])
if verbose==True:
print('Covariance Init: ')
print(msg)
print('sigma1: ',sigma1)
print('sigma2: ',sigma2)
print('sigma3: ',sigma3)
else:
d=sigma1*np.ones(size)
self.Cov = np.diag(d)
self.size = size
# print(self.__doc__)
# print('\t\tImplementing: ',msg)
def test(self):
print(self.Cov)
print(np.linalg.eig(self.Cov))
def randomize(self):
n=self.size
X=np.random.rand(n,n)
print(X)
Ignore,U = np.linalg.eig((X + np.transpose(X))/2)
print(U)
self.Cov = U * np.diag(abs(np.random.randn(3, 1))) * np.transpose(U)
#myobj = Covariance()
#%print(myobj.__doc__)