-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_alsace_meromorphic_function.py
105 lines (86 loc) · 3.13 KB
/
test_alsace_meromorphic_function.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
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 26 15:55:45 2018
@author: Dimitris Loukrezis
Test ALSACE algorithm on meromorphic function 1/(G*Y), where
G = [g1, g2, ..., g16 = ][1, 0.5, 0.1, 0.05, 0.01, ..., 5*1e-8]
Y = [Y1, Y2, ..., Y16], Y_n ~ U[-1,1]
"""
import numpy as np
import openturns as ot
from alsace_Kopt import alsace
from tools import get_ed, compute_moments, PCE_Surrogate
def qoi_mero(yvec):
"""Meromorphic function"""
gvec_tilde = np.array([1e0, 5*1e-1, 1e-1, 5*1e-2, 1e-2, 5*1e-3, 1e-3,
5*1e-4, 1e-4, 5*1e-5, 1e-5, 5*1e-6, 1e-6, 5*1e-7,
1e-7, 5*1e-8])
coeff = 1.0 / (2.0*np.linalg.norm(gvec_tilde, ord=1))
gvec = gvec_tilde * coeff
dotprod = np.dot(gvec, yvec)
return 1.0/(1 + dotprod)
# number of RVs
N = 16
# construct joint pdf
z = []
for i in range(N):
if i%2==0:
z.append(ot.TruncatedNormal(0,1,0,3))
else:
z.append(ot.TruncatedNormal(0,1,-3,0))
jpdf = ot.ComposedDistribution(z)
# generate cross-validation set
Ncv = 1000
ot.RandomGenerator.SetSeed(13)
cv_test_points, cv_values = get_ed(qoi_mero, jpdf, Ncv, 'R')
### results storage
meanz = []
varz = []
err_cv_max_m = []
err_cv_mean_m = []
err_cv_rms_m = []
fevalz = []
cond_numz = []
indicatorz = []
cardz = []
# vector of maximum function calls
max_fcalls = np.linspace(100, 1000, 10).tolist()
### APPROXIMATE
results_dict_m = {}
for mfc in max_fcalls:
# simulation budget
mfc = int(mfc)
# pce dictionary
results_dict_m = alsace(func=qoi_mero, N=N, jpdf=jpdf, max_fcalls=mfc,
limit_cond=100, sample_type='R',
pce_dict=results_dict_m, verbose=True)
# collect all multi-indices and coefficients
idx_all = results_dict_m['idx_act'] + results_dict_m['idx_adm']
pce_coeff_all = results_dict_m['pce_coeff_act'] + results_dict_m['pce_coeff_adm']
# compute pce surrogate model
sur_model = PCE_Surrogate(pce_coeff_all, idx_all, jpdf)
# compute moments
mu, sigma2 = compute_moments(pce_coeff_all)
meanz.append(mu)
varz.append(sigma2)
# evaluate PCE surrogate on cross-validation sample
Y = sur_model.evaluate(cv_test_points)
# compute cross-validation errors
errs = np.abs(cv_values-np.reshape(np.asarray(Y), np.asarray(Y).shape[0]))
err_cv_max_m.append(np.max(errs))
err_cv_mean_m.append(np.mean(errs))
err_cv_rms_m.append(np.sqrt(np.sum(errs**2)/len(errs)))
# model evaluations, basis cardinality, global error indicators
fevalz.append(len(results_dict_m['ed_fevals']))
cardz.append(len(idx_all))
indicatorz.append(np.sum(np.array(results_dict_m['pce_coeff_adm'])**2))
print('fcalls:' + str(fevalz[-1]))
print('pce terms:' + str(cardz[-1]))
print('max cv error:' + str(err_cv_max_m[-1]))
print('mean cv error:' + str(err_cv_mean_m[-1]))
print('rms cv error:' + str(err_cv_rms_m[-1]))
print('global error indicator:' + str(indicatorz[-1]))
print("")
results_all = np.array([fevalz, cardz, err_cv_max_m, err_cv_mean_m,
err_cv_rms_m]).T
#np.savetxt('alsace_meromorhic_trAinv_1.txt', results_all)