-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptimalBatch4_repeated.py
66 lines (51 loc) · 1.93 KB
/
optimalBatch4_repeated.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
import numpy as np
import optimalSampling
class LinearFunction2(optimalSampling.FittingFunctionLSMAP):
# y = b0+b1*x
def getPriorMean(self):
return [None, None, 1]
def getPriorVar(self):
return [None, None, 0.01*0.01]
def getFunction(self, xn, beta):
return beta[0]+beta[1]*xn+beta[2]*xn*xn
def getPartialDerivative1(self, xn, yn, beta, i):
priormu=self.getPriorMean()
priorvar=self.getPriorVar()
if i==0:
return 1
elif i==1:
return xn
elif i==2:
return xn*xn-(beta[2]-priormu[2])/priorvar[2]
def getPartialDerivative2(self, xn, yn, beta, i, j):
priorvar=self.getPriorVar()
if i==2:
return -1/priorvar[2]
else:
return 0.0
h=LinearFunction2()
h.sigma2=0.1
trueBeta=np.asarray([-0.*0.,-2*0.,1])
X=np.asarray([[0],[0.5],[1]])
y=h.simulateFunctionAtMultiplePoints(X, trueBeta, True)
stepx=0.01
def evaluateDiff(h,y,beta,xrange):
yp=h.simulateFunctionAtMultiplePoints(xrange, beta, False)
return np.var(y-yp)
evaluator0=optimalSampling.FIMEvaluator()
evaluator2=optimalSampling.VarEvaluator()
Xrange=[]
for x in np.arange(0,1,stepx):
Xrange.append([x])
Xrange=np.asarray(Xrange)
ytrue=h.simulateFunctionAtMultiplePoints(Xrange, trueBeta, False)
X,y,beta=optimalSampling.simulateProcess(h,trueBeta,X,y,np.asarray([0,0,0]),30,np.mgrid[0:1+stepx:stepx],evaluator2, verbose=False)
Nreplicates=1000
d=np.zeros((Nreplicates,2))
for n in range(Nreplicates):
_,_,beta=optimalSampling.simulateProcess(h,trueBeta,X,y,np.asarray([0,0,0]),30,np.mgrid[0:1+stepx:stepx],evaluator0, verbose=False)
d[n,0]=evaluateDiff(h,ytrue,beta,Xrange)
_,_,beta=optimalSampling.simulateProcess(h,trueBeta,X,y,np.asarray([0,0,0]),30,np.mgrid[0:1+stepx:stepx],evaluator2, verbose=False)
d[n,1]=evaluateDiff(h,ytrue,beta,Xrange)
print("Diff: %d %f %f"%(n, d[n,0],d[n,1]))
np.savetxt("diff02.txt",d)