/
infrastructure.py
201 lines (155 loc) · 4.71 KB
/
infrastructure.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env python
# -*- coding:utf-8 -*-
'''
There is the foundation of the whole alg in the statistic.
In this file, there exist two algebra estimation alg.
one is least square related, including OLS, WLS.
when change weight in WLS, get many different LS variants.
the other is distance estimation alg.
Actually, there exist three alg. LS, Distance estimation, Maxim Likelihood estimation
In this file, do not implement MLE alg since when using MLE, must know the distribution density function in first.
unfortunately, do not know the DDF in advance.
'''
from builtins import object, isinstance, len
import numpy as np
class LSEstimation(object):
'''
This class is for least square estimation operation,
Y = A*W*X
input:
X:
Y:
W: weight for A
output:
A: Y/X
SSE: sum of (Yi - Yi(estimator))
SST: SSE + SSR = sum of (Yi - Yi(Mean))
SSR: sum of (Yi(estimator) -Yi(Mean))
coefVar: var of each of A's coefficiency
'''
def __init__(self, X, Y, W):
if not isinstance(X, np.ndarray) or not isinstance(Y, np.ndarray) or not isinstance(W,np.ndarray):
self.X = np.asarray(X)
self.Y = np.asarray(Y)
self.W = np.asarray(W)
else:
self.X = X
self.Y = Y
self.W = W
iLen = Y.shape[0]
self.A = np.ndarray((iLen,1))
self.AVar = np.ndarray((iLen,1))
self.AStd = np.ndarray((iLen,1))
self.SSE = None
self.SST = None
self.SSR = None
self.__calEstimationVariable__()
def __calEstimationVariable__(self):
xDim = self.X.shape
yDim = self.Y.shape
if xDim[0] != yDim[0]:
print("please make use of the dimension of X and Y is same!")
return False
iX = self.X
iY = self.Y
iXT = iX.transpose()
iXTmp = iXT.dot(iX)
iYTmp = iXT.dot(iY)
if len(iX) == len(iY): # when this thing take place, that's to say, only one parameter need to be estimated.
self.A = iYTmp/iXTmp
yReg = iX*self.A
else:
iXInv = np.linalg.inv(iXTmp)
self.A = np.array(iXInv.dot(iYTmp))
yReg = iX.dot(self.A)
iErrTmp = iY - np.mean(iY)
self.SST = iErrTmp.T.dot(iErrTmp)
iErrTmp = yReg - np.mean(iY)
self.SSE = iErrTmp.T.dot(iErrTmp)
iErrTmp = iY - yReg
self.SSR = iErrTmp.T.dot(iErrTmp)
iVar = iErrTmp.T.dot(iErrTmp)/yDim[0]
if len(iX) == len(iY):
self.AVar = np.array(iVar/iXTmp)
else:
iEye = np.eye(yDim[0], k=0)
iXInv = np.linalg.inv(iXTmp)
self.AVar = iVar*np.diagonal(iEye.dot(iXInv).dot(iEye.T))
self.AStd = np.sqrt(self.AVar)
def getEstimatorVar(self):
return self.AVar
def getEstimatorSTD(self):
return self.AStd
def getEstimator(self):
return self.A
def getEstimatorVar(self):
return self.AVar
def getXYValue(self):
return self.X, self.Y, self.W
def getSSE(self):
return self.SSE
def getSSR(self):
return self.SSR
def getSST(self):
return self.SST
class DEstimation(object):
'''
This class is for distance estimation operation,
Y = A*X
input:
X:
Y:
output:
A: Y/X
SSE: sum of (Yi - Yi(estimator))
SST: SSE + SSR = sum of (Yi - Yi(Mean))
SSR: sum of (Yi(estimator) -Yi(Mean))
coefVar: var of each of A's coefficiency
'''
def __init__(self, X, Y) :
if not isinstance(X, np.ndarray) or not isinstance(Y, np.ndarray) :
self.X = np.asarray(X)
self.Y = np.asarray(Y)
else :
self.X = X
self.Y = Y
self.A = [Y.shape[0]]
self.AVar = [Y.shape[0]]
self.SSE = None
self.SST = None
self.SSR = None
def __calEstimationA__(self) :
pass
def __calEstimationAVar__(self) :
pass
def getEstimator(self) :
return self.A
def getEstimatorVar(self) :
return self.AVar
def getXYValue(self) :
return self.X, self.Y
def getSSE(self) :
return self.SSE
def getSSR(self) :
return self.SSR
def getSST(self) :
return self.SST
class MLEstimation(object):
def __init__(self):
pass
def __calEstimationA__(self) :
pass
def __calEstimationAVar__(self) :
pass
def getEstimator(self) :
return self.A
def getEstimatorVar(self) :
return self.AVar
def getXYValue(self) :
return self.X, self.Y
def getSSE(self) :
return self.SSE
def getSSR(self) :
return self.SSR
def getSST(self) :
return self.SST