-
Notifications
You must be signed in to change notification settings - Fork 6
/
mo_bootstrapping_sensitivity_analysis.f90
426 lines (315 loc) · 15.7 KB
/
mo_bootstrapping_sensitivity_analysis.f90
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
!> \file mo_bootstrapping_sensitivity_analysis.f90
!> \brief Bootstrapping for sensitivity analysis.
!> \details This module generates new data sets using the bootstrapping method
!> and calculates the sobol index for each data set.
!> \authors Leonie Bruckert
!> \date Oct 2014
MODULE mo_bootstrapping_sensitivity_analysis
! Written Leonie Bruckert, Oct 2014
! License
! -------
! This file is part of the JAMS Fortran package, distributed under the MIT License.
!
! Copyright (c) 2014 Leonie Bruckert
!
! Permission is hereby granted, free of charge, to any person obtaining a copy
! of this software and associated documentation files (the "Software"), to deal
! in the Software without restriction, including without limitation the rights
! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
! copies of the Software, and to permit persons to whom the Software is
! furnished to do so, subject to the following conditions:
!
! The above copyright notice and this permission notice shall be included in all
! copies or substantial portions of the Software.
!
! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
! SOFTWARE.
USE mo_kind, ONLY: i4, i8, sp, dp
USE mo_xor4096, ONLY: get_timeseed, xor4096, n_save_state
USE mo_xor4096_apps, ONLY: xor4096_range
USE mo_sobol_index, ONLY: sobol_index
IMPLICIT NONE
PRIVATE
PUBLIC :: bootstrap_si ! generates new data sets and calculates the Sobol index for each set
! ------------------------------------------------------------------
! NAME
!> bootstrap_si
! PURPOSE
!> Artificially generates new data sets by randomly sampling with replacement and
!> then calculates the Sobol index for each new data set to estimate parameter sensitivity.
!
! INTENT(IN)
!> \param[in] "real(sp/dp) :: ya(:)/ya(:,:)" Model output for parameter sets A \n
!> dim_1 = number of parameter sets \n
!> dim_2 = number of model outputs, e.g. time steps
!>
!> \param[in] "real(sp/dp) :: yb(:)/yb(:,:)" Model output for parameter sets B \n
!> dim_1 = number of parameter sets \n
!> dim_2 = number of model outputs, e.g. time steps
!>
!> \param[in] "real(sp/dp) :: yc(:,:)/yc(:,:,:)" Model output for parameter sets C(i), i=1,npara \n
!> dim_1 = number of parameter sets \n
!> dim_2 = number of parameters \n
!> dim_3 = number of model outputs, e.g. time steps
!>
!> \param[in] "integer(i4) :: n" Number of new data sets
! INTENT(INOUT)
! None
! INTENT(OUT)
!> \param[out] "real(sp/dp) :: si(:,:)/si(:,:,:)" Sobol index - Main effect \n
!> dim_1 = number of new data sets
!> dim_2 = number of parameters \n
!> dim_3 = number of model outputs, e.g. time steps \n
!>
!> \param[out] "real(sp/dp) :: sti(:,:)/sti(:,:,:)" Sobol index - Main effect \n
!> dim_1 = number of new data sets
!> dim_2 = number of parameters \n
!> dim_3 = number of model outputs, e.g. time steps \n
! INTENT(IN), OPTIONAL
!> \param[in] "integer(i4), optional :: method" Method for calculating the Sobol index \n
!> Default = 9
!> \param[in] "integer(i4/i8), optional :: seed" Seed != 0
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RETURN
!> None
!
! RESTRICTIONS
!> \note Input values must be floating points.
!
! EXAMPLE
!> Model with 3 parameters:
!> ya = (/ 1., 2., 3., 4. /)
!> yb = (/ 3., 1., 3., 6. /)
!> yc(:,1)= (/ 1., 2.5, 2., 3. /)
!> yc(:,2)= (/ 4., 1., 4. 7. /)
!> yc(:,3)= (/ 2., 2., 3., 5. /)
!> n = 5
!> call bootstrap_si(ya, yb, yc, n, si, sti)
!> -> see also example in test directory
! LITERATURE
!
! HISTORY
!> \author Leonie Bruckert
!> \date Oct 2014
INTERFACE bootstrap_si
MODULE PROCEDURE bootstrap_si_0d_dp, bootstrap_si_0d_sp, bootstrap_si_1d_dp, bootstrap_si_1d_sp
END INTERFACE bootstrap_si
! ------------------------------------------------------------------
CONTAINS
SUBROUTINE bootstrap_si_0d_dp(ya,yb,yc,n,si,sti,method,seed)
IMPLICIT NONE
real(dp), dimension(:), intent(in) :: ya ! model output for parameter set A
real(dp), dimension(:), intent(in) :: yb ! model output for parameter set B
real(dp), dimension(:,:), intent(in) :: yc ! model output for parameter set C
integer(i4), intent(in) :: n ! number of new data sets
real(dp), dimension(n,size(yc,2)), intent(out) :: si ! Sobol index (main effect)
real(dp), dimension(n,size(yc,2)), intent(out) :: sti ! Sobol index (total effect)
integer(i4), intent(in), optional :: method ! method for calculating the sobol index
integer(i8), intent(in), optional :: seed ! seed
! local variables
real(dp), dimension(n) :: r
integer(i8), dimension(size(ya),n) :: rn
integer(i8), dimension(n) :: seed_tmp
integer(i8), dimension(:,:), allocatable :: save_state
real(dp), dimension(size(ya)) :: ya_new, yb_new
real(dp), dimension(size(ya),size(yc,2)) :: yc_new
integer(i4) :: meth
integer(i4) :: i, j, k
allocate(save_state(n,n_save_state))
if (present(seed)) then
seed_tmp(1)=seed
do i=2,n
seed_tmp(i)=seed_tmp(i-1)+1000_i8
end do
else
call get_timeseed(seed_tmp)
end if
call xor4096(seed_tmp, r, save_state=save_state)
call xor4096_range((/1_i8,int(size(ya),i8)/),rn, save_state=save_state) ! the numbers of a column represent the indices
! of the chosen parameter sets
if (present(method)) then
meth=method
else
meth=9
end if
loop1: do k=1,n
! generate new data set
do j=1,size(ya)
ya_new(j)=ya(rn(j,k))
yb_new(j)=yb(rn(j,k))
end do
do i=1,size(yc,2)
do j=1,size(ya)
yc_new(j,i)=yc(rn(j,k),i)
end do
end do
call sobol_index(ya_new, yb_new, yc_new, si(k,:), sti(k,:), method=meth)
end do loop1
deallocate(save_state)
END SUBROUTINE bootstrap_si_0d_dp
! ------------------------------------------------------------------
SUBROUTINE bootstrap_si_0d_sp(ya,yb,yc,n,si,sti,method,seed)
IMPLICIT NONE
real(sp), dimension(:), intent(in) :: ya ! model output for parameter set A
real(sp), dimension(:), intent(in) :: yb ! model output for parameter set B
real(sp), dimension(:,:), intent(in) :: yc ! model output for parameter set C
integer(i4), intent(in) :: n ! number of new data sets
real(sp), dimension(n,size(yc,2)), intent(out) :: si ! Sobol index (main effect)
real(sp), dimension(n,size(yc,2)), intent(out) :: sti ! Sobol index (total effect)
integer(i4), intent(in), optional :: method ! method for calculating the sobol index
integer(i4), intent(in), optional :: seed ! seed
! local variables
real(sp), dimension(n) :: r
integer(i4), dimension(size(ya),n) :: rn
integer(i4), dimension(n) :: seed_tmp
integer(i4), dimension(:,:), allocatable :: save_state
real(sp), dimension(size(ya)) :: ya_new, yb_new
real(sp), dimension(size(ya),size(yc,2)) :: yc_new
integer(i4) :: meth
integer(i4) :: i, j, k
allocate(save_state(n,n_save_state))
if (present(seed)) then
seed_tmp(1)=seed
do i=2,n
seed_tmp(i)=seed_tmp(i-1)+1000_i4
end do
else
call get_timeseed(seed_tmp)
end if
call xor4096(seed_tmp, r, save_state=save_state)
call xor4096_range((/1_i4,int(size(ya),i4)/),rn, save_state=save_state) ! the numbers of a column represent the indices
! of the chosen parameter sets
if (present(method)) then
meth=method
else
meth=9
end if
loop1: do k=1,n
! generate new data set
do j=1,size(ya)
ya_new(j)=ya(rn(j,k))
yb_new(j)=yb(rn(j,k))
end do
do i=1,size(yc,2)
do j=1,size(ya)
yc_new(j,i)=yc(rn(j,k),i)
end do
end do
call sobol_index(ya_new, yb_new, yc_new, si(k,:), sti(k,:), method=meth)
end do loop1
deallocate(save_state)
END SUBROUTINE bootstrap_si_0d_sp
! ------------------------------------------------------------------
SUBROUTINE bootstrap_si_1d_dp(ya,yb,yc,n,si,sti,method,seed)
IMPLICIT NONE
real(dp), dimension(:,:), intent(in) :: ya ! model output for parameter set A
real(dp), dimension(:,:), intent(in) :: yb ! model output for parameter set B
real(dp), dimension(:,:,:), intent(in) :: yc ! model output for parameter set C
integer(i4), intent(in) :: n ! number of new data sets
real(dp), dimension(n,size(yc,2),size(ya,2)), intent(out) :: si ! Sobol index (main effect)
real(dp), dimension(n,size(yc,2),size(ya,2)), intent(out) :: sti ! Sobol index (total effect)
integer(i4), intent(in), optional :: method ! method for calculating the sobol index
integer(i8), intent(in), optional :: seed ! seed
! local variables
real(dp), dimension(n) :: r
integer(i8), dimension(size(ya,1),n) :: rn
integer(i8), dimension(n) :: seed_tmp
integer(i8), dimension(:,:), allocatable :: save_state
real(dp), dimension(size(ya,1),size(ya,2)) :: ya_new, yb_new
real(dp), dimension(size(ya,1),size(yc,2),size(ya,2)) :: yc_new
integer(i4) :: meth
integer(i4) :: i, j, k
allocate(save_state(n,n_save_state))
if (present(seed)) then
seed_tmp(1)=seed
do i=2,n
seed_tmp(i)=seed_tmp(i-1)+1000_i8
end do
else
call get_timeseed(seed_tmp)
end if
call xor4096(seed_tmp, r, save_state=save_state)
call xor4096_range((/1_i8,int(size(ya,1),i8)/),rn, save_state=save_state) ! the numbers of a column represent the indices
! of the chosen parameter sets
if (present(method)) then
meth=method
else
meth=9
end if
loop1: do k=1,n
! generate new data set
do j=1,size(ya,1)
ya_new(j,:)=ya(rn(j,k),:)
yb_new(j,:)=yb(rn(j,k),:)
end do
do i=1,size(yc,2)
do j=1,size(ya,1)
yc_new(j,i,:)=yc(rn(j,k),i,:)
end do
end do
call sobol_index(ya_new, yb_new, yc_new, si(k,:,:), sti(k,:,:), method=meth)
end do loop1
deallocate(save_state)
END SUBROUTINE bootstrap_si_1d_dp
! ------------------------------------------------------------------
SUBROUTINE bootstrap_si_1d_sp(ya,yb,yc,n,si,sti,method,seed)
IMPLICIT NONE
real(sp), dimension(:,:), intent(in) :: ya ! model output for parameter set A
real(sp), dimension(:,:), intent(in) :: yb ! model output for parameter set B
real(sp), dimension(:,:,:), intent(in) :: yc ! model output for parameter set C
integer(i4), intent(in) :: n ! number of new data sets
real(sp), dimension(n,size(yc,2),size(ya,2)), intent(out) :: si ! Sobol index (main effect)
real(sp), dimension(n,size(yc,2),size(ya,2)), intent(out) :: sti ! Sobol index (total effect)
integer(i4), intent(in), optional :: method ! method for calculating the sobol index
integer(i4), intent(in), optional :: seed ! seed
! local variables
real(sp), dimension(n) :: r
integer(i4), dimension(size(ya,1),n) :: rn
integer(i4), dimension(n) :: seed_tmp
integer(i4), dimension(:,:), allocatable :: save_state
real(sp), dimension(size(ya,1),size(ya,2)) :: ya_new, yb_new
real(sp), dimension(size(ya,1),size(yc,2),size(ya,2)) :: yc_new
integer(i4) :: meth
integer(i4) :: i, j, k
allocate(save_state(n,n_save_state))
if (present(seed)) then
seed_tmp(1)=seed
do i=2,n
seed_tmp(i)=seed_tmp(i-1)+1000_i4
end do
else
call get_timeseed(seed_tmp)
end if
call xor4096(seed_tmp, r, save_state=save_state)
call xor4096_range((/1_i4,int(size(ya,1),i4)/),rn, save_state=save_state) ! the numbers of a column represent the indices
! of the chosen parameter sets
if (present(method)) then
meth=method
else
meth=9
end if
loop1: do k=1,n
! generate new data set
do j=1,size(ya,1)
ya_new(j,:)=ya(rn(j,k),:)
yb_new(j,:)=yb(rn(j,k),:)
end do
do i=1,size(yc,2)
do j=1,size(ya,1)
yc_new(j,i,:)=yc(rn(j,k),i,:)
end do
end do
call sobol_index(ya_new, yb_new, yc_new, si(k,:,:), sti(k,:,:), method=meth)
end do loop1
deallocate(save_state)
END SUBROUTINE bootstrap_si_1d_sp
! ------------------------------------------------------------------
END MODULE MO_BOOTSTRAPPING_SENSITIVITY_ANALYSIS