/
smoothedMC.py
837 lines (723 loc) · 26.8 KB
/
smoothedMC.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
from math import erf
import numpy as np
import stochpy
from scipy.stats import norm
from sklearn.gaussian_process.kernels import RBF
from pycheck.semantics.STL.BooleanSemantics import BooleanSemantics
from pycheck.semantics.STL.STLLexer import STLLexer, CommonTokenStream, InputStream
from pycheck.semantics.STL.STLParser import STLParser
from pycheck.series.TimeSeries import TimeSeries
# CORRECTION_FACTOR = 1
# CORRECTION = 1E-4
# eps_damp = 0.5
invC = []
mu_tilde = []
sigma_tilde = []
# trainSetX=[]
# trainSetY=[]
kernel = lambda x: 0
# scale = 1
def getProbability(mean, variance):
return norm.cdf(mean / np.sqrt(1 + variance))
def getBounds(mean, variance, beta, trainSetX):
return norm.cdf(np.tile(1 / np.sqrt(1 + variance), (trainSetX.shape[1], 1)) * [mean - beta * np.sqrt(variance),
mean + beta * np.sqrt(variance)])
def doTraining(trainSetX, trainSetY, scale, CORRECTION, CORRECTION_FACTOR, eps_damp):
gauss = expectationPropagation(1e-6, trainSetX, trainSetY, scale, CORRECTION, CORRECTION_FACTOR, eps_damp)
v_tilde = gauss.Term[:, 0]
tau_tilde = gauss.Term[:, 1]
diag_sigma_tilde = 1 / tau_tilde
global mu_tilde
mu_tilde = v_tilde * diag_sigma_tilde
sigma_tilde = np.diag(diag_sigma_tilde)
global invC
invC = np.linalg.solve(gauss.C + sigma_tilde, np.eye(len(mu_tilde)))
return invC, mu_tilde, sigma_tilde
def expectationPropagation(tolerance, trainSetX, trainSetY, scale, CORRECTION, CORRECTION_FACTOR, eps_damp):
gauss = Gauss()
p = kernel(trainSetX)
gauss.C = p
gauss.C = gauss.C + CORRECTION * np.eye(len(gauss.C))
gauss.LC = np.linalg.cholesky(gauss.C)
gauss.LC_t = gauss.LC.transpose()
gauss_LC_diag = np.diag(gauss.LC)
logdet_LC = 2 * np.sum(np.log(gauss_LC_diag))
logZprior = 0.5 * logdet_LC
n = len(trainSetX)
logZterms = np.zeros(shape=(n, 1))
logZloo = np.zeros(shape=(n, 1))
Term = np.zeros(shape=(n, 2))
appo, gauss = computeMarginalMoments(gauss, Term, logdet_LC, CORRECTION_FACTOR)
# Stuff related to the likelihood
gauss.LikPar_p = trainSetY * scale
gauss.LikPar_q = np.ones(shape=(n, 1)) * scale - gauss.LikPar_p
NODES = 96
gauss.xGauss = np.zeros(shape=(NODES, 1))
gauss.wGauss = np.zeros(shape=(NODES, 1))
gauss.xGauss, gauss.wGauss = gausshermite(NODES, gauss.xGauss, gauss.wGauss)
gauss.logwGauss = np.log(gauss.wGauss)
# for (int i = 0; i < gauss.gauss.logwGauss.getLength(); i++)
# gauss.gauss.logwGauss.put(i, Math.log(gauss.gauss.wGauss.get(i)));
# initialize cycle control
MaxIter = 1000
tol = tolerance
logZold = 0
logZ = 2 * tol
steps = 0
logZappx = 0
while ((np.abs(logZ - logZold) > tol) & (steps < MaxIter)):
# cycle control
steps = steps + 1
logZold = logZ
cavGauss = computeCavities(gauss, -Term)
#
# // [Term, logZterms, logZloo] = EPupdate(cavGauss, gauss.LikFunc, y,
# // Term, eps_damp);
update = ep_update(cavGauss, Term, eps_damp, gauss.LikPar_p, gauss.LikPar_q, gauss.xGauss, gauss.logwGauss)
Term = update.TermNew
logZterms = update.logZterms
logZloo = update.logZ
logZappx, gauss = computeMarginalMoments(gauss, Term, logdet_LC, CORRECTION_FACTOR)
logZ = logZterms.sum() + logZappx
# finishing
logZ = logZ - logZprior
gauss.logZloo = np.sum(logZloo)
gauss.logZappx = logZappx
gauss.logZterms = logZterms
gauss.logZ = logZ
gauss.Term = Term
return gauss
def computeMarginalMoments(gauss, Term, logdet_LC, CORRECTION_FACTOR):
# // (repmat(Term(:,2),1,N).*Gauss.LC)
N = len(Term)
tmp = np.tile(Term[:, 1], (N, 1)).T * gauss.LC
A = np.matrix.dot(gauss.LC_t, tmp) + np.eye(N) * CORRECTION_FACTOR
# // Serious numerical stability issue with the calculation
# // of A (i.e. A = LC' * tmp + I)
# // as it does not appear to be PD for large amplitudes
gauss.L = np.linalg.cholesky(A)
# // Gauss.W = Gauss.L\(Gauss.LC');
gauss.W = np.linalg.solve(gauss.L, gauss.LC_t)
# // Gauss.diagV = sum(Gauss.W.*Gauss.W,1)';
tmp = gauss.W * gauss.W
# gauss.diagV = np.zeros(shape=(N, 1))
# for (int i = 0; i < N; i++)
# gauss.diagV.put(i, tmp.getColumn(i).sum());
gauss.diagV = np.array([np.sum(tmp, 0)]).T
# // or
# // gauss.diagV = gauss.W.transpose().mmul(gauss.W).diag();
#
# // Gauss.m = Gauss.W'*(Gauss.W*Term(:,1));
tmp = np.dot(gauss.W, Term[:, 0])
gauss.m = np.array([np.dot(gauss.W.T, tmp)]).T
# // logdet = -2*sum(log(diag(Gauss.L))) + 2*sum(log(diag(Gauss.LC)));
logdet = 0
sum = 0
tmp = np.diag(gauss.L)
# for (int i = 0; i < tmp.getLength(); i++)
# sum += Math.log(tmp.get(i));
# logdet += -2 * sum;
logdet += -2.0 * np.sum(np.log(tmp))
# // sum = 0;
# // tmp = gauss.LC.diag();
# // for (int i = 0; i < tmp.getLength(); i++)
# // sum += Math.log(tmp.get(i));
logdet += logdet_LC
# // logZappx = 0.5*(Gauss.m'*Term(:,1) + logdet);
logZappx = 0.5 * (np.dot(gauss.m.transpose(), Term[:, 0]) + logdet)
return logZappx, gauss
def gausshermite(n, x, w):
x0 = np.zeros(shape=(len(x), 1))
w0 = np.zeros(shape=(len(w), 1))
m = int((n + 1) / 2)
z = 0
pp = 0
p1 = 0
p2 = 0
p3 = 0
for i in range(0, m):
if (i == 0):
z = np.sqrt(2 * n + 1) - 1.85575 * (2 * n + 1) ** (-0.16667)
elif (i == 1):
z = z - 1.14 * n ** 0.426 / z
elif (i == 2):
z = 1.86 * z - 0.86 * x0[0]
elif (i == 3):
z = 1.91 * z - 0.91 * x0[1]
else:
z = 2.0 * z - x0[i - 2]
for its in range(0, 10):
p1 = 1 / np.sqrt(np.sqrt(np.pi))
p2 = 0
for j in range(1, n + 1):
p3 = p2
p2 = p1
a = z * np.sqrt(2 / j) * p2
b = np.sqrt((j - 1) / j) * p3
p1 = a - b
pp = np.sqrt(2 * n) * p2
z1 = z
z = z1 - p1 / pp
if (np.abs(z - z1) < 2.2204e-16):
break
x0[i] = z
x0[n - 1 - i] = -z
w0[i] = 2 / (pp * pp)
w0[n - 1 - i] = w0[i]
w0 = w0 / np.sqrt(np.pi)
x0 = x0 * np.sqrt(2)
x0 = np.sort(x0)[::-1]
x = x0
w = w0
return x, w
def computeCavities(gauss, Term):
cavGauss = CavGauss()
# // C = Gauss.diagV;
C = gauss.diagV
# // s = 1./(1 + Term(:,2).*C)
appo = np.array([a * b for a, b in zip(Term[:, 1], C)])
s = np.ones(shape=(len(C), 1)) / (appo + 1)
# // CavGauss.diagV = s. * C;
cavGauss.diagV = s * C
# // CavGauss.m = s. * (Gauss.m + Term(:, 1).*C);
appo = np.array([a * b for a, b in zip(Term[:, 0], C)])
cavGauss.m = s * (gauss.m + appo)
return cavGauss
def ep_update(cavGauss, Term, eps_damp, LikPar_p, LikPar_q, xGauss, wGauss):
update = EPupdate()
Cumul = np.zeros(shape=(len(LikPar_p), 2))
logZ, Cumul = GaussHermiteNQ(LikPar_p, LikPar_q, cavGauss.m, cavGauss.diagV, xGauss, wGauss, Cumul)
update.logZ = np.array([logZ]).T
m2 = cavGauss.m * cavGauss.m
logV = np.log(cavGauss.diagV)
cumul1 = np.array([Cumul[:, 0] * Cumul[:, 0]]).T
cumul2 = np.log(np.array([Cumul[:, 1]]).T)
tmp = m2 / (cavGauss.diagV) + logV - (cumul1 / np.array([Cumul[:, 1]]).T + cumul2)
update.logZterms = update.logZ + tmp * 0.5
ones = np.ones(shape=(len(LikPar_p), 1))
TermNew = np.zeros(shape=(len(LikPar_p), 2))
c1 = np.array([Cumul[:, 0] / Cumul[:, 1]]).T - cavGauss.m / cavGauss.diagV
c2 = ones / np.array([Cumul[:, 1]]).T - ones / cavGauss.diagV
TermNew[:, 0] = c1[:, 0]
TermNew[:, 1] = c2[:, 0]
TermNew = (1 - eps_damp) * Term + eps_damp * TermNew
update.TermNew = TermNew
return update
def GaussHermiteNQ(FuncPar_p, FuncPar_q, m, v, xGH, logwGH, Cumul):
stdv = np.sqrt(v)
Nnodes = len(xGH)
tmp = np.dot(stdv, xGH.transpose())
Y = tmp + np.tile(m, (1, Nnodes))
tmp = logprobitpow(Y, FuncPar_p, FuncPar_q)
G = tmp + np.tile(logwGH.transpose(), (len(tmp), 1))
maxG = np.max(G, 1)
G = G - np.tile(maxG, (Nnodes, 1)).T
expG = np.exp(G)
denominator = np.sum(expG, 1)
logZ = maxG + np.log(denominator)
deltam = stdv * (np.dot(expG, xGH)) / np.array([denominator]).T
appo = m + deltam
Cumul[:, 0] = appo[:, 0]
appo = v * np.dot(expG, xGH ** 2) / np.array([denominator]).T - deltam ** 2
Cumul[:, 1] = appo[:, 0]
return logZ, Cumul
def logprobitpow(X, LikPar_p, LikPar_q):
n = X.shape[0]
m = X.shape[1]
Y = np.zeros(shape=(n, m))
for i in range(0, n):
for j in range(0, m):
Y[i][j] = ncdflogbc(X[i][j])
Za = Y * np.tile(LikPar_p, (1, m))
Y = np.zeros(shape=(n, m))
for i in range(0, n):
for j in range(0, m):
Y[i][j] = ncdflogbc(-X[i][j])
Zb = Y * np.tile(LikPar_q, (1, m))
return Za + Zb
def ncdflogbc(x):
sqrt2 = np.sqrt(2)
invSqrt2 = 1 / sqrt2
log2 = np.log(2)
treshold = -sqrt2 * 5
z = -x
if (x >= 0):
return np.log(1 + erf(x * invSqrt2)) - log2
if (treshold < x):
return np.log(1 - erf(-x * invSqrt2)) - log2
return -0.5 * np.log(np.pi) - log2 - 0.5 * z * z - np.log(z) + np.log(
1 - 1 / z + 3 / z ** 4 - 15 / z ** 6 + 105 / z ** 8 - 945 / z ** 10)
def latentPrediction(Xs, trainSetX):
kss = np.diag(kernel(Xs))
ks = kernel(Xs, trainSetX)
# if (invC == null | | mu_tilde == null | | trainingSet.isModified())
# doTraining();
tmp = np.dot(ks, invC)
fs = np.dot(tmp, mu_tilde)
vfs = kss - (np.diag(np.dot(tmp, ks.transpose())))
return fs, vfs
def getMarginalLikelihood(trainSetX, trainSetY, scale, CORRECTION, CORRECTION_FACTOR, eps_damp):
gauss = expectationPropagation(1e-3, trainSetX, trainSetY, scale, CORRECTION, CORRECTION_FACTOR, eps_damp)
return gauss.logZ
def objectivefunction(l, trainSetX, trainSetY, scale, CORRECTION, CORRECTION_FACTOR, eps_damp):
global kernel
r = RBF(l)
kernel = r
return getMarginalLikelihood(trainSetX, trainSetY, scale, CORRECTION, CORRECTION_FACTOR, eps_damp)
def getDefaultHyperarametersRBF(X, Y):
signal = 0.5 * (np.max(Y) - np.min(Y))
sum = 0
n, dim = X.shape
for d in range(0, dim):
max = -float('inf')
min = float('inf')
for i in range(0, n):
curr = X[i][d]
if (curr > max):
max = curr
if (curr < min):
min = curr
sum += (max - min) / 10.0
lengthScale = sum / dim
return signal, lengthScale
class EPupdate():
def __init__(self):
self.TermNew = None
self.logZterms = None
self.logZ = None
class CavGauss():
def __init__(self):
self.diagV = None
self.m = None
class Gauss():
def __init__(self):
self.LikPar_p = None
self.xGauss = None
self.wGauss = None
self.logwGauss = None
self.C = []
self.LC = None
self.LC_t = None
self.L = None
self.W = None
self.diagV = None
self.m = None
self.logZloo = None
self.logZappx = None
self.logZterms = None
self.logZ = None
self.Term = None
def calculateProbability(value, modelName, paramterName, timeEnd, trajectoriesNumber, mitlFormula):
smod = stochpy.SSA()
smod.Model(modelName)
smod.ChangeParameter(paramterName, value[0])
smod.DoStochSim(end=timeEnd, mode='time', trajectories=trajectoriesNumber, quiet=True)
input_stream = InputStream(mitlFormula)
lexer = STLLexer(input=input_stream)
token_stream = CommonTokenStream(lexer)
parser = STLParser(token_stream)
tree = parser.prog()
count = 0
for i in range(1, trajectoriesNumber + 1):
smod.GetTrajectoryData(i)
# if(max(smod.data_stochsim.time)<130):
# print("CAZZO")
visitor = BooleanSemantics(
timeSeries=TimeSeries(smod.data_stochsim.species_labels, smod.data_stochsim.time,
smod.data_stochsim.species.T))
count += 1 if visitor.visit(tree) else 0
return count / trajectoriesNumber
#
#
#
# global trainSetX
# trainSetX = np.array([np.linspace(0.005, 0.3, num=20)]).T
# global trainSetY
# trainSetY =np.array([np.array([function(x) for x in trainSetX])]).T
# # value =np.array([[0.2],[0.25],[0.26],[0.5],[0.9]])
# global scale
# scale =500
# aa,bb =getDefaultHyperarametersRBF(trainSetX, trainSetY)
#
# res = minimize(objectivefunction, bb, method='L-BFGS-B', bounds=((0.5*bb,2*bb),))
# global kernel
# r = RBF(res.x)
# print(r)
# kernel = r
# invC,mu_tilde,sigma_tilde = doTraining()
# xs = [np.linspace(0.005, 0.3, num=200)]
# a,b=latentPrediction(np.array(xs).T)
# prob = getProbability(a,b)
# bounds = getBounds(a,b,2)
# print(prob)
# print(bounds)
#
# plt.scatter(trainSetX, trainSetY)
# plt.plot(xs[0], prob)
# plt.plot(xs[0], bounds[0,:])
# plt.plot(xs[0], bounds[1,:])
#
# plt.show(block=True)
def fit(trainSetX, trainSetY, scale, CORRECTION, CORRECTION_FACTOR, eps_damp):
aa, bb = getDefaultHyperarametersRBF(trainSetX, trainSetY)
objectivefunctionWrap = lambda x: objectivefunction(x, trainSetX, trainSetY, scale, CORRECTION, CORRECTION_FACTOR,
eps_damp)
res = minimize(objectivefunctionWrap, bb, method='L-BFGS-B', bounds=((0.5 * bb, 2 * bb),))
global kernel
r = RBF(res.x)
print(r)
kernel = r
global invC, mu_tilde, sigma_tilde
invC, mu_tilde, sigma_tilde = doTraining(trainSetX, trainSetY, scale, CORRECTION, CORRECTION_FACTOR, eps_damp)
from scipy.optimize import minimize
from pyDOE import *
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
def modelMin(x, trainSetX):
a, b = latentPrediction(x, trainSetX)
# prob = getProbability(a, b)
bounds = getBounds(a, b, 3, trainSetX)
# y_pred, sigma = gp.predict(x[0], return_std=True)
return bounds[0, :]
def modelMax(x, trainSetX):
a, b = latentPrediction(x, trainSetX)
# prob = getProbability(a, b)
bounds = getBounds(a, b, 3, trainSetX)
# y_pred, sigma = gp.predict(x[0], return_std=True)
return -bounds[1, :]
def model(x, trainSetX):
a, b = latentPrediction(x, trainSetX)
prob = getProbability(a, b)
return prob
def modelMinMax(x, trainSetX):
a, b = latentPrediction(x, trainSetX)
bounds = getBounds(a, b, 3, trainSetX)
return bounds[0, :], bounds[1, :]
def minimizeGP(interval, trainSetX):
global bestRes
bnds = ((interval[0], interval[1]),)
# points = interval[0] + lhs(1, samples=10, criterion='maximin') * (interval[1] - interval[0])
points = np.linspace(interval[0], interval[1], num=5)
best = float('Inf')
for i in range(0, len(points)):
modelMinWrapper = lambda x: modelMin(x, trainSetX)
res = minimize(modelMinWrapper, points[i], method='L-BFGS-B', bounds=bnds)
if (res.fun < best):
best = res.fun
bestRes = res
min = bestRes.x
eMin = bestRes.fun
return min[0], eMin[0]
def maximizeGp(interval, trainSetX):
global bestRes
bnds = ((interval[0], interval[1]),)
points = np.linspace(interval[0], interval[1], num=5)
# points = interval[0] + lhs(1, samples=10, criterion='maximin')* (interval[1] - interval[0])
best = float('Inf')
for i in range(0, len(points)):
modelMaxWrapper = lambda x: modelMax(x, trainSetX)
res = minimize(modelMaxWrapper, points[i], method='L-BFGS-B', bounds=bnds)
if (res.fun < best):
best = res.fun
bestRes = res
min = bestRes.x
eMax = -bestRes.fun
return min[0], eMax[0]
def findMax(matrix):
values = [a[1] - a[0] for a in matrix]
return np.argmax(values), np.max(values)
def adjustAll(Yminmax, Xminmax, sets, trainSetX):
for i in range(len(sets)):
lb = sets[i][0]
ub = sets[i][1]
min, eMin = minimizeGP([lb, ub], trainSetX)
max, eMax = maximizeGp([lb, ub], trainSetX)
Xminmax[i] = [min, max]
Yminmax[i] = [eMin, eMax]
return Yminmax
def dinstanceFromtS(x, trainSetX):
b = [np.linalg.norm(a - x) for a in trainSetX]
return np.min(b)
def smoothedMC(modelName, paramterName, timeEnd, trajectoriesNumber, mitlFormula, precision, interval):
CORRECTION_FACTOR = 1
CORRECTION = 1E-4
eps_damp = 0.5
glb = interval[0]
gub = interval[1]
# global trainSetX
# global trainSetY
# trainSetX = np.array([[glb], [gub]])
trainSetX = np.array([np.linspace(glb, gub, 5)]).T
function = lambda x: calculateProbability(x, modelName, paramterName, timeEnd, trajectoriesNumber, mitlFormula)
trainSetY = np.array([np.array([function(x) for x in trainSetX])]).T
scale = trajectoriesNumber
fit(trainSetX, trainSetY, scale, CORRECTION, CORRECTION_FACTOR, eps_damp)
# gp = GaussianProcessRegressor(n_restarts_optimizer=20)
#
# gp.fit(trainSetX, trainSetY)
# initialization########################
sets = list()
Xminmax = list()
Yminmax = list()
sets.append([glb, gub])
i = 0
lb = sets[i][0]
ub = sets[i][1]
min, eMin = minimizeGP([lb, ub], trainSetX)
max, eMax = maximizeGp([lb, ub], trainSetX)
Xminmax.insert(i, [min, max])
Yminmax.insert(i, [eMin, eMax])
val = 1
while (val > precision):
while (val > precision):
# print(val)
i, val = findMax(Yminmax)
print(str(len(trainSetX)) + '-->' + str(val))
lb = sets[i][0]
ub = sets[i][1]
xmin = Xminmax[i][0]
xmax = Xminmax[i][1]
c = 0
if dinstanceFromtS(xmin, trainSetX) > 0:
trainSetX = np.vstack([trainSetX, [xmin]])
rmin = function([xmin])
trainSetY = np.vstack([trainSetY, rmin])
c = 1
if dinstanceFromtS(xmax, trainSetX) > 0:
trainSetX = np.vstack([trainSetX, [xmax]])
rmax = function([xmax])
trainSetY = np.vstack([trainSetY, rmax])
c = 1
if c == 1:
fit(trainSetX, trainSetY, scale, CORRECTION, CORRECTION_FACTOR, eps_damp)
sets.pop(i)
Xminmax.pop(i)
Yminmax.pop(i)
minL, eMinL = minimizeGP([lb, ub], trainSetX)
maxR, eMaxR = maximizeGp([lb, ub], trainSetX)
newPoint = (minL + maxR) / 2
if minL < maxR:
leftSet = [lb, newPoint]
rightSet = [newPoint, ub]
else:
rightSet = [lb, newPoint]
leftSet = [newPoint, ub]
minR, eMinR = minimizeGP(rightSet, trainSetX)
maxL, eMaxL = maximizeGp(leftSet, trainSetX)
# Yminmax=adjustAll(Yminmax)
sets.append(leftSet)
Xminmax.append([minL, maxL])
Yminmax.append([eMinL, eMaxL])
sets.append(rightSet)
Xminmax.append([minR, maxR])
Yminmax.append([eMinR, eMaxR])
if (minL == maxL or minR == maxR):
print('=')
if (eMaxR < eMinR or eMaxL < eMinL):
print('=')
Yminmax = adjustAll(Yminmax, Xminmax, sets, trainSetX)
Yminmax = adjustAll(Yminmax, Xminmax, sets, trainSetX)
print(sets)
print(Xminmax)
print(Yminmax)
print(sets)
plt.figure()
plt.xlim([glb, gub])
plt.ylim([0, 1])
currentAxis = plt.gca()
plt.scatter(trainSetX, trainSetY)
for i in range(len(sets)):
currentAxis.add_patch(
Rectangle((sets[i][0], Yminmax[i][0]), sets[i][1] - sets[i][0], Yminmax[i][1] - Yminmax[i][0], fill=None,
alpha=1))
x = np.linspace(glb, gub, num=150)
y = np.array([model(e, trainSetX) for e in x])
mmax = np.array([-modelMax(t, trainSetX) for t in x])
mmin = np.array([modelMin(t, trainSetX) for t in x])
plt.plot(x, y[:, 0])
plt.plot(x, mmax[:, 0])
plt.plot(x, mmin[:, 0])
plt.show(block=True)
# print(trainSetX)
# fun = lambda x: np.sin(3*x)
def smoothedMCNaive(modelName, paramterName, timeEnd, trajectoriesNumber, mitlFormula, precision, interval, points):
CORRECTION_FACTOR = 1
CORRECTION = 1E-4
eps_damp = 0.5
glb = interval[0]
gub = interval[1]
# global trainSetX
# global trainSetY
# trainSetX = np.array([[glb], [gub]])
trainSetX = np.array([np.linspace(glb, gub, points)]).T
function = lambda x: calculateProbability(x, modelName, paramterName, timeEnd, trajectoriesNumber, mitlFormula)
trainSetY = np.array([np.array([function(x) for x in trainSetX])]).T
scale = trajectoriesNumber
fit(trainSetX, trainSetY, scale, CORRECTION, CORRECTION_FACTOR, eps_damp)
plt.figure()
plt.xlim([glb, gub])
plt.ylim([0, 1])
plt.scatter(trainSetX, trainSetY)
x = np.linspace(glb, gub, num=150)
y = np.array([model(e, trainSetX) for e in x])
mmax = np.array([-modelMax(t, trainSetX) for t in x])
mmin = np.array([modelMin(t, trainSetX) for t in x])
plt.plot(x, y[:, 0])
plt.plot(x, mmax[:, 0])
plt.plot(x, mmin[:, 0])
plt.show(block=True)
def findInterval(xs, start, t, trainSetX):
c = start
lb, ub = modelMinMax(xs[start], trainSetX)
maxInit = ub
minInit = lb
maxValue = maxInit
minValue = minInit
c = c + 1
while True:
maxOld = maxValue
minOld = minValue
# ys, sigma = latentPrediction(xs[c], trainSetX)
lb, ub = modelMinMax(xs[c], trainSetX)
maxValue = max(maxValue, ub)
minValue = min(minValue, lb)
if (maxValue - minValue > t or c == len(xs) - 1):
break
# maxInit = ys[0] + 3 * sigma[0]
# minInit = ys[0] - 3 * sigma[0]
c = c + 1
if c == len(xs) - 1 and (maxValue - minValue <= t):
maxOld = maxValue
minOld = minValue
end = c
else:
end = c - 1
return end, minOld, maxOld, c == len(xs) - 1
def drowSquares(interval, n, t, modelName, paramterName, timeEnd, trajectoriesNumber, mitlFormula):
global M, condition, trainSetX, trainSetY
k = n
xs = np.linspace(interval[0], interval[1], num=k)
CORRECTION_FACTOR = 1
CORRECTION = 1E-4
eps_damp = 0.5
glb = interval[0]
gub = interval[1]
# global trainSetX
# global trainSetY
trainSetX = np.array([[glb], [gub]])
# trainSetX = np.array([np.linspace(glb, gub, 5)]).T
function = lambda x: calculateProbability(x, modelName, paramterName, timeEnd, trajectoriesNumber, mitlFormula)
trainSetY = np.array([np.array([function(x) for x in trainSetX])]).T
# trainSetY = np.array([function([interval[0]]), function([interval[1]])])[:,None]
scale = trajectoriesNumber
fit(trainSetX, trainSetY, scale, CORRECTION, CORRECTION_FACTOR, eps_damp)
# gp = GaussianProcessRegressor(n_restarts_optimizer=20)
# trainSetX = np.array([[interval[0]], [interval[1]]])
# gp.fit(trainSetX, trainSetY)
stopCondition = 0
addnumber = False
while (True):
if (addnumber):
k = 2 * k
xs = np.linspace(interval[0], interval[1], num=k)
M = [0, 0, 0, 0]
c = 0
while c < len(xs):
end, minOld, maxOld, condition = findInterval(xs, c, t, trainSetX)
if (c == end):
if (stopCondition == c):
addnumber = True
break
else:
addnumber = False
j = findMaxSlope(xs, c)
trainSetX = np.vstack([trainSetX, [xs[c]]])
trainSetY = np.vstack([trainSetY, function([xs[c]])])
fit(trainSetX, trainSetY, scale, CORRECTION, CORRECTION_FACTOR, eps_damp)
stopCondition = c
break
M = np.vstack([M, [xs[c], xs[end], minOld, maxOld]])
c = end
if condition:
break
if condition:
break
else:
print(str(c) + '/' + str(len(xs)))
continue
plt.figure()
currentAxis = plt.gca()
for i in range(len(M)):
currentAxis.add_patch(
Rectangle((M[i][0], M[i][2]), M[i][1] - M[i][0], M[i][3] - M[i][2], fill=None,
alpha=1))
plt.xlim(interval)
# xs = np.linspace(interval[0], interval[1], num=40)
# ys, sigma = gp.predict(xs[:, None], return_std=True)
# ys, sigma=latentPrediction(xs[:,None], trainSetX)
lb, ub = modelMinMax(xs[:, None], trainSetX)
ys = model(xs[:, None], trainSetX)
plt.plot(xs, ub)
plt.plot(xs, ys, 'r')
plt.plot(xs, lb)
plt.scatter(trainSetX, trainSetY)
plt.show(block=True)
def findMaxSlope(xs):
index = 0
maxValue = -float('Inf')
for i in range(len(xs) - 1):
mini, maxi = modelMinMax(xs[i], trainSetX)
minj, maxj = modelMinMax(xs[i + 1], trainSetX)
value = max(maxi, maxj) - min(mini, minj)
if (value > maxValue):
maxValue = value
index = i
return index, maxValue
def drowSquares2(interval, n, t, modelName, paramterName, timeEnd, trajectoriesNumber, mitlFormula):
global M, condition, trainSetX, trainSetY, delta
k = n
xs = np.linspace(interval[0], interval[1], num=k)
CORRECTION_FACTOR = 1
CORRECTION = 1E-4
eps_damp = 0.5
glb = interval[0]
gub = interval[1]
# trainSetX = np.array([[glb], [gub]])
trainSetX = np.array([np.linspace(glb, gub, 5)]).T
function = lambda x: calculateProbability(x, modelName, paramterName, timeEnd, trajectoriesNumber, mitlFormula)
trainSetY = np.array([np.array([function(x) for x in trainSetX])]).T
scale = trajectoriesNumber
fit(trainSetX, trainSetY, scale, CORRECTION, CORRECTION_FACTOR, eps_damp)
slope = float('Inf')
while (slope > t):
c, slope = findMaxSlope(xs)
print(str(c) + ':' + str(slope))
trainSetX = np.vstack([trainSetX, [xs[c]]])
trainSetY = np.vstack([trainSetY, function([xs[c]])])
fit(trainSetX, trainSetY, scale, CORRECTION, CORRECTION_FACTOR, eps_damp)
print('FINISH')
c = 0
M = [0, 0, 0, 0]
while c < len(xs):
end, minOld, maxOld, condition = findInterval(xs, c, t, trainSetX)
M = np.vstack([M, [xs[c], xs[end], minOld, maxOld]])
c = end
if condition:
break
plt.figure()
currentAxis = plt.gca()
for i in range(len(M)):
currentAxis.add_patch(
Rectangle((M[i][0], M[i][2]), M[i][1] - M[i][0], M[i][3] - M[i][2], fill=None,
alpha=1))
plt.xlim(interval)
# xs = np.linspace(interval[0], interval[1], num=40)
# ys, sigma = gp.predict(xs[:, None], return_std=True)
# ys, sigma=latentPrediction(xs[:,None], trainSetX)
lb, ub = modelMinMax(xs[:, None], trainSetX)
ys = model(xs[:, None], trainSetX)
plt.plot(xs, ub)
plt.plot(xs, ys, 'r')
plt.plot(xs, lb)
plt.scatter(trainSetX, trainSetY)
plt.show(block=True)