hkayabilisim commited on
Commit
25273f7
·
1 Parent(s): 254ff22

Added flexible range.

Browse files
Files changed (1) hide show
  1. app.py +36 -25
app.py CHANGED
@@ -17,7 +17,7 @@ def Pn(m, x):
17
  def L(a,b,m,x):
18
  return np.sqrt((2*m+1)/(b-a))*Pn(m, 2*(x-b)/(b-a)+1)
19
 
20
- def sobol(x, y, m):
21
  print(x.shape, y.shape)
22
  N, n = x.shape
23
  f0 = np.mean(y)
@@ -26,9 +26,9 @@ def sobol(x, y, m):
26
 
27
  for r in range(m):
28
  for i in range(n):
29
- alpha[r, i] = np.mean((y-f0) * L(0, 1, r+1, np.array(x[:, [i]])))
30
 
31
- global_D = np.mean(y ** 2) - np.mean(y) ** 2
32
  D_first_order = np.zeros((n))
33
  S_first_order = np.zeros((n))
34
  for k in range(n):
@@ -37,33 +37,33 @@ def sobol(x, y, m):
37
 
38
  return S_first_order, f0, alpha
39
 
40
- def evalute_hdmr(x, f0, alpha):
41
  N, n = x.shape
42
  m, _ = alpha.shape
43
  y = f0 * np.ones((N,1))
44
  for r in range(m):
45
  for i in range(n):
46
- y = y + alpha[r, i] * L(0, 1, r+1, np.array(x[:, [i]]))
47
  return y
48
 
49
  def f(x):
50
  return np.sum((x-0.5)**2,axis=1,keepdims=True)
51
 
52
- def optimize(N,n,m,func_code,trn_ratio):
53
  global df_trn, df_hat_trn, df_tst, df_hat_tst
54
  print(func_code)
55
  print(N)
56
  N_trn = round(N*trn_ratio)
57
  N_tst = N - N_trn
58
  print(f'N:{N} N_trn:{N_trn} N_tst:{N_tst}')
59
- x_trn = np.random.uniform(0,1,size=(N_trn,n))
60
  y_trn = eval(func_code, {'x': x_trn,'np':np})
61
- x_tst = np.random.uniform(0,1,size=(N_tst,n))
62
  y_tst = eval(func_code, {'x': x_tst,'np':np})
63
 
64
- si, f0, alpha = sobol(x_trn, y_trn, m)
65
- yhat_trn = evalute_hdmr(x_trn, f0, alpha)
66
- yhat_tst = evalute_hdmr(x_tst, f0, alpha)
67
  out = ''
68
  out += f'trn x:{x_trn.shape} y:{y_trn.shape}'
69
  out += f'tst x:{x_tst.shape} y:{y_tst.shape}'
@@ -132,6 +132,16 @@ with gd.Blocks() as demo:
132
  to construct the meta-model.
133
  """)
134
  trn_ratio = gd.Slider(0.1, 0.9, value=0.2, step=0.1, label="Training ratio")
 
 
 
 
 
 
 
 
 
 
135
 
136
  with gd.Column():
137
  func_disp = gd.Markdown()
@@ -144,22 +154,23 @@ with gd.Blocks() as demo:
144
  )
145
  btn = gd.Button('Create FEOM')
146
  gd.Examples([["np.sum((10*x-5)**2 - 10*np.cos(2*3.14*(10*x-5)),axis=1,keepdims=True) +10*x.shape[1]",
147
- "$$\mathbf{y} = 10n+\sum_{i=1}^n[\mathbf{x}_i^2-10 \cos(2 \pi x_i)] \;\; \mathbf{x}_i \in R^{N}$$"],
148
- ["np.sum((x-0.5)**2,axis=1,keepdims=True)","$$\mathbf{y} = \sum_i^n(\mathbf{x}_i-0.5)^2,\;\; \mathbf{x}_i \in R^{N}$$"],
149
- ["np.sum(x,axis=1,keepdims=True)","$$\mathbf{y} = \sum_i^n \mathbf{x}_i \;\; \mathbf{x}_i \in R^{N}$$"],
150
- ["np.sum((x-0.5)**3,axis=1,keepdims=True)","$$\mathbf{y} = \sum_i^n (\mathbf{x}_i-0.5)^3 \;\; \mathbf{x}_i \in R^{N}$$"]],
151
- inputs=[func_code,func_disp])
 
152
  with gd.Row():
153
- gd.Markdown('Training Data')
154
- gd.Markdown('FEOM applied to Training')
155
  r2_out_trn = gd.Markdown('R2 Score on Training')
156
  with gd.Row():
157
  pdp_trn = gd.ScatterPlot(label='Scatter plot of input data').style(container=True)
158
  feom_trn = gd.ScatterPlot(label='Scatter plot of FEOM').style(container=True)
159
  corr_trn = gd.ScatterPlot(label='y versus feom')
160
  with gd.Row():
161
- gd.Markdown('Testing')
162
- gd.Markdown('FEOM applied to Testing')
163
  r2_out_tst = gd.Markdown('R2 Score on Testing')
164
  with gd.Row():
165
  pdp_tst = gd.ScatterPlot(label='Scatter plot of input data').style(container=True)
@@ -168,19 +179,19 @@ with gd.Blocks() as demo:
168
 
169
  pdp_x = gd.Dropdown(['x1','x2'], label="Choose input variable to show in scatter plots")
170
  out = gd.TextArea()
171
- btn.click(optimize, inputs=[N,n,m,func_code, trn_ratio],
172
  outputs=[out, pdp_x, pdp_trn, feom_trn,corr_trn,r2_out_trn,
173
  pdp_tst, feom_tst,corr_tst,r2_out_tst])
174
- n.change(optimize, inputs=[N,n,m,func_code,trn_ratio],
175
  outputs=[out, pdp_x, pdp_trn, feom_trn,corr_trn,r2_out_trn,
176
  pdp_tst, feom_tst,corr_tst,r2_out_tst])
177
- N.change(optimize, inputs=[N,n,m,func_code,trn_ratio],
178
  outputs=[out, pdp_x, pdp_trn, feom_trn,corr_trn,r2_out_trn,
179
  pdp_tst, feom_tst,corr_tst,r2_out_tst])
180
- m.change(optimize, inputs=[N,n,m,func_code,trn_ratio],
181
  outputs=[out, pdp_x, pdp_trn, feom_trn,corr_trn,r2_out_trn,
182
  pdp_tst, feom_tst,corr_tst,r2_out_tst])
183
- trn_ratio.change(optimize, inputs=[N,n,m,func_code,trn_ratio],
184
  outputs=[out, pdp_x, pdp_trn, feom_trn,corr_trn,r2_out_trn,
185
  pdp_tst, feom_tst,corr_tst,r2_out_tst])
186
  pdp_x.change(change_pdp, inputs=pdp_x, outputs=[pdp_trn, feom_trn, pdp_tst, feom_tst])
 
17
  def L(a,b,m,x):
18
  return np.sqrt((2*m+1)/(b-a))*Pn(m, 2*(x-b)/(b-a)+1)
19
 
20
+ def sobol(x, y, m, range_min, range_max):
21
  print(x.shape, y.shape)
22
  N, n = x.shape
23
  f0 = np.mean(y)
 
26
 
27
  for r in range(m):
28
  for i in range(n):
29
+ alpha[r, i] = (range_max-range_min) * np.mean((y-f0) * L(range_min, range_max, r+1, np.array(x[:, [i]])))
30
 
31
+ global_D = (range_max-range_min)*np.mean(y ** 2) - ((range_max-range_min)*np.mean(y)) ** 2
32
  D_first_order = np.zeros((n))
33
  S_first_order = np.zeros((n))
34
  for k in range(n):
 
37
 
38
  return S_first_order, f0, alpha
39
 
40
+ def evalute_hdmr(x, f0, alpha, range_min, range_max):
41
  N, n = x.shape
42
  m, _ = alpha.shape
43
  y = f0 * np.ones((N,1))
44
  for r in range(m):
45
  for i in range(n):
46
+ y = y + alpha[r, i] * L(range_min, range_max, r+1, np.array(x[:, [i]]))
47
  return y
48
 
49
  def f(x):
50
  return np.sum((x-0.5)**2,axis=1,keepdims=True)
51
 
52
+ def optimize(N,n,m,func_code,trn_ratio,range_min,range_max):
53
  global df_trn, df_hat_trn, df_tst, df_hat_tst
54
  print(func_code)
55
  print(N)
56
  N_trn = round(N*trn_ratio)
57
  N_tst = N - N_trn
58
  print(f'N:{N} N_trn:{N_trn} N_tst:{N_tst}')
59
+ x_trn = np.random.uniform(range_min,range_max,size=(N_trn,n))
60
  y_trn = eval(func_code, {'x': x_trn,'np':np})
61
+ x_tst = np.random.uniform(range_min,range_max,size=(N_tst,n))
62
  y_tst = eval(func_code, {'x': x_tst,'np':np})
63
 
64
+ si, f0, alpha = sobol(x_trn, y_trn, m, range_min, range_max)
65
+ yhat_trn = evalute_hdmr(x_trn, f0, alpha, range_min, range_max)
66
+ yhat_tst = evalute_hdmr(x_tst, f0, alpha, range_min, range_max)
67
  out = ''
68
  out += f'trn x:{x_trn.shape} y:{y_trn.shape}'
69
  out += f'tst x:{x_tst.shape} y:{y_tst.shape}'
 
132
  to construct the meta-model.
133
  """)
134
  trn_ratio = gd.Slider(0.1, 0.9, value=0.2, step=0.1, label="Training ratio")
135
+ range_min = gd.Slider(-10.0, 10.0, step=0.01, value=-5, label="Minimum value of the range",
136
+ info="""
137
+ Please choose the lowest value of the range. Make sure the range_min is less than
138
+ range_max.
139
+ """)
140
+ range_max = gd.Slider(-10.0, 10.0, step=0.01, value=5, label="Maximum value of the range",
141
+ info="""
142
+ Please choose the highest value of the range. Make sure the range_max is greate than
143
+ range_min.
144
+ """)
145
 
146
  with gd.Column():
147
  func_disp = gd.Markdown()
 
154
  )
155
  btn = gd.Button('Create FEOM')
156
  gd.Examples([["np.sum((10*x-5)**2 - 10*np.cos(2*3.14*(10*x-5)),axis=1,keepdims=True) +10*x.shape[1]",
157
+ "$$\mathbf{y} = 10n+\sum_{i=1}^n[\mathbf{x}_i^2-10 \cos(2 \pi x_i)] \;\; \mathbf{x}_i \in R^{N}$$",
158
+ 0,1],
159
+ ["np.sum((x-0.5)**2,axis=1,keepdims=True)","$$\mathbf{y} = \sum_i^n(\mathbf{x}_i-0.5)^2,\;\; \mathbf{x}_i \in R^{N}$$",0,1],
160
+ ["np.sum(x,axis=1,keepdims=True)","$$\mathbf{y} = \sum_i^n \mathbf{x}_i \;\; \mathbf{x}_i \in R^{N}$$",0,1],
161
+ ["np.sum((x-0.5)**3,axis=1,keepdims=True)","$$\mathbf{y} = \sum_i^n (\mathbf{x}_i-0.5)^3 \;\; \mathbf{x}_i \in R^{N}$$",0,1]],
162
+ inputs=[func_code,func_disp,range_min,range_max])
163
  with gd.Row():
164
+ gd.Markdown('Original Training Data')
165
+ gd.Markdown('HDMR Approximation applied to Training Data')
166
  r2_out_trn = gd.Markdown('R2 Score on Training')
167
  with gd.Row():
168
  pdp_trn = gd.ScatterPlot(label='Scatter plot of input data').style(container=True)
169
  feom_trn = gd.ScatterPlot(label='Scatter plot of FEOM').style(container=True)
170
  corr_trn = gd.ScatterPlot(label='y versus feom')
171
  with gd.Row():
172
+ gd.Markdown('Original Testing Data')
173
+ gd.Markdown('HDMR Approximation applied to Testing Data')
174
  r2_out_tst = gd.Markdown('R2 Score on Testing')
175
  with gd.Row():
176
  pdp_tst = gd.ScatterPlot(label='Scatter plot of input data').style(container=True)
 
179
 
180
  pdp_x = gd.Dropdown(['x1','x2'], label="Choose input variable to show in scatter plots")
181
  out = gd.TextArea()
182
+ btn.click(optimize, inputs=[N,n,m,func_code, trn_ratio,range_min,range_max],
183
  outputs=[out, pdp_x, pdp_trn, feom_trn,corr_trn,r2_out_trn,
184
  pdp_tst, feom_tst,corr_tst,r2_out_tst])
185
+ n.change(optimize, inputs=[N,n,m,func_code,trn_ratio,range_min,range_max],
186
  outputs=[out, pdp_x, pdp_trn, feom_trn,corr_trn,r2_out_trn,
187
  pdp_tst, feom_tst,corr_tst,r2_out_tst])
188
+ N.change(optimize, inputs=[N,n,m,func_code,trn_ratio,range_min,range_max],
189
  outputs=[out, pdp_x, pdp_trn, feom_trn,corr_trn,r2_out_trn,
190
  pdp_tst, feom_tst,corr_tst,r2_out_tst])
191
+ m.change(optimize, inputs=[N,n,m,func_code,trn_ratio,range_min, range_max],
192
  outputs=[out, pdp_x, pdp_trn, feom_trn,corr_trn,r2_out_trn,
193
  pdp_tst, feom_tst,corr_tst,r2_out_tst])
194
+ trn_ratio.change(optimize, inputs=[N,n,m,func_code,trn_ratio,range_min, range_max],
195
  outputs=[out, pdp_x, pdp_trn, feom_trn,corr_trn,r2_out_trn,
196
  pdp_tst, feom_tst,corr_tst,r2_out_tst])
197
  pdp_x.change(change_pdp, inputs=pdp_x, outputs=[pdp_trn, feom_trn, pdp_tst, feom_tst])