forked from facebook/Ax
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsobol_measures.py
More file actions
1042 lines (970 loc) · 44 KB
/
sobol_measures.py
File metadata and controls
1042 lines (970 loc) · 44 KB
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
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
# pyre-strict
import itertools
from collections.abc import Callable
from copy import deepcopy
from typing import Any
import numpy.typing as npt
import torch
from ax.adapter.torch import TorchAdapter
from ax.core.data import MAP_KEY
from ax.core.search_space import SearchSpaceDigest
from ax.generators.torch.botorch_modular.generator import BoTorchGenerator
from ax.utils.sensitivity.derivative_measures import (
compute_derivatives_from_model_list,
sample_discrete_parameters,
)
from ax.utils.sensitivity.fixed_feature_model import (
FixedFeatureModel,
prepare_fixed_feature_inputs,
)
from botorch.models.gpytorch import GPyTorchModel
from botorch.models.model import Model
from botorch.sampling.normal import SobolQMCNormalSampler
from botorch.utils.sampling import draw_sobol_samples
from botorch.utils.transforms import unnormalize
from pyre_extensions import none_throws
from torch import Tensor
class SobolSensitivity:
def __init__(
self,
bounds: torch.Tensor,
input_function: Callable[[torch.Tensor], torch.Tensor] | None = None,
num_mc_samples: int = 10**4,
input_qmc: bool = False,
second_order: bool = False,
first_order_idcs: torch.Tensor | None = None,
num_bootstrap_samples: int = 1,
bootstrap_array: bool = False,
discrete_features: list[int] | None = None,
) -> None:
r"""Computes three types of Sobol indices:
first order indices, total indices and second order indices (if specified ).
Args:
bounds: Parameter bounds over which to evaluate model sensitivity.
input_function: The objective function.
num_mc_samples: The number of montecarlo grid samples
input_qmc: If True, a qmc Sobol grid is use instead of uniformly random.
second_order: If True, the second order indices are computed.
bootstrap: If true, the MC error is returned.
first_order_idcs: Tensor of previously computed first order indices, where
first_order_idcs.shape = torch.Size([dim]).
num_bootstrap_samples: If bootstrap is true, the number of bootstraps has
to be specified.
bootstrap_array: If true, all the num_bootstrap_samples extimated indices
are returned instead of their mean and Var.
discrete_features: If specified, the inputs associated with the indices in
this list are generated using an integer-valued uniform distribution,
rather than the default (pseudo-)random continuous uniform distribution.
"""
self.input_function = input_function
self.dim: int = bounds.shape[-1]
self.num_mc_samples = num_mc_samples
self.second_order = second_order
self.bootstrap: bool = num_bootstrap_samples > 1
self.num_bootstrap_samples: int = (
num_bootstrap_samples - 1
) # deduct 1 because the first is meant to be the full grid
self.bootstrap_array = bootstrap_array
self.device: torch.device = bounds.device
self.A: torch.Tensor
self.B: torch.Tensor
if input_qmc:
seed_A, seed_B = 1234, 5678 # to make it reproducible
self.A = draw_sobol_samples(
bounds=bounds, n=num_mc_samples, q=1, seed=seed_A
).squeeze(1)
self.B = draw_sobol_samples(
bounds=bounds, n=num_mc_samples, q=1, seed=seed_B
).squeeze(1)
else:
self.A = unnormalize(
torch.rand(num_mc_samples, self.dim, device=self.device),
bounds=bounds,
)
self.B = unnormalize(
torch.rand(num_mc_samples, self.dim, device=self.device),
bounds=bounds,
)
# uniform integral distribution for discrete features
self.A = sample_discrete_parameters(
input_mc_samples=self.A,
discrete_features=discrete_features,
bounds=bounds,
num_mc_samples=num_mc_samples,
)
self.B = sample_discrete_parameters(
input_mc_samples=self.B,
discrete_features=discrete_features,
bounds=bounds,
num_mc_samples=num_mc_samples,
)
self.A_B_ABi: torch.Tensor = self.generate_all_input_matrix().to(torch.double)
if self.bootstrap:
subset_size = 4
self.bootstrap_indices: torch.Tensor = torch.randint(
0, num_mc_samples, (self.num_bootstrap_samples, subset_size)
)
self.f_A: torch.Tensor | None = None
self.f_B: torch.Tensor | None = None
self.f_ABis: list[torch.Tensor] | None = None
self.f_total_var: torch.Tensor | None = None
self.f_A_btsp: list[torch.Tensor] | None = None
self.f_B_btsp: list[torch.Tensor] | None = None
self.f_ABis_btsp: list[list[torch.Tensor]] | None = None
self.f_total_var_btsp: list[torch.Tensor] | None = None
self.f_BAis: list[torch.Tensor] | None = None
self.f_BAis_btsp: list[list[torch.Tensor]] | None = None
self.first_order_idcs: torch.Tensor | None = first_order_idcs
self.first_order_idcs_btsp: torch.Tensor | None = None
def generate_all_input_matrix(self) -> torch.Tensor:
# NOTE Sobol samples of A are ablated with samples of B, and vice versa
# so baselies are A and B, but each one is ablated by the other for each
# dimension. First all of A, then (optionally) all of B.
A_B_ABi_list = [self.A, self.B]
for i in range(self.dim):
AB_i = deepcopy(self.A)
AB_i[:, i] = self.B[:, i]
A_B_ABi_list.append(AB_i)
if self.second_order:
for i in range(self.dim):
BA_i = deepcopy(self.B)
BA_i[:, i] = self.A[:, i]
A_B_ABi_list.append(BA_i)
A_B_ABi = torch.cat(A_B_ABi_list, dim=0)
return A_B_ABi
def evalute_function(self, f_A_B_ABi: torch.Tensor | None = None) -> None:
r"""evaluates the objective function and devides the evaluation into
torch.Tensors needed for the indices computation.
Args:
f_A_B_ABi: Function evaluations on the entire grid of size M(d+2).
"""
if f_A_B_ABi is None:
f_A_B_ABi = none_throws(self.input_function)(self.A_B_ABi)
# for multiple output models, average the outcomes
if len(f_A_B_ABi.shape) == 3:
f_A_B_ABi = f_A_B_ABi.mean(dim=0)
self.f_A = f_A_B_ABi[: self.num_mc_samples]
self.f_B = f_A_B_ABi[self.num_mc_samples : self.num_mc_samples * 2]
# first, there is simply A and B, and then there are all the ablated variants
# of each (which are retrieved here by slicing the input matrix)
# but this only retrieves the ablations of A (and not B)
self.f_ABis = [
f_A_B_ABi[self.num_mc_samples * (i + 2) : self.num_mc_samples * (i + 3)]
for i in range(self.dim)
]
# Get the variances of A and B (so simply the variance of 2 num_mc samples)
self.f_total_var = torch.var(f_A_B_ABi[: self.num_mc_samples * 2])
if self.bootstrap:
f_A = none_throws(self.f_A)
f_B = none_throws(self.f_B)
f_ABis = none_throws(self.f_ABis)
self.f_A_btsp = [
torch.index_select(f_A, 0, indices)
for indices in self.bootstrap_indices
]
self.f_B_btsp = [
torch.index_select(f_B, 0, indices)
for indices in self.bootstrap_indices
]
self.f_ABis_btsp = [
[torch.index_select(f_ABi, 0, indices) for f_ABi in f_ABis]
for indices in self.bootstrap_indices
]
f_A_btsp = self.f_A_btsp
f_B_btsp = self.f_B_btsp
self.f_total_var_btsp = [
torch.var(
torch.cat(
(f_A_btsp[i], f_B_btsp[i]),
dim=0,
)
)
for i in range(self.num_bootstrap_samples)
]
if self.second_order:
# If second order, we also need to retrieve the ablations of B
# In total, we have f_ABis and f_BAis which are both of size M(d), and are
# the ablations of each other
self.f_BAis = [
f_A_B_ABi[
self.num_mc_samples * (i + 2 + self.dim) : self.num_mc_samples
* (i + 3 + self.dim)
]
for i in range(self.dim)
]
if self.bootstrap:
f_BAis = self.f_BAis
self.f_BAis_btsp = [
[torch.index_select(f_BAi, 0, indices) for f_BAi in f_BAis]
for indices in self.bootstrap_indices
]
def first_order_indices(self) -> Tensor:
r"""Computes the first order Sobol indices:
Returns:
if num_bootstrap_samples>1
Tensor: (values,var_mc,stderr_mc)x dim
else
Tensor: (values)x dim
"""
f_A = none_throws(self.f_A)
f_B = none_throws(self.f_B)
f_ABis = none_throws(self.f_ABis)
f_total_var = none_throws(self.f_total_var)
first_order_idcs = []
for i in range(self.dim):
# The only difference between self.f_ABis[i] and self.f_A is in dimension i
# So we get the variance in the component that corresponds to dimension i
vi = torch.mean(f_B * (f_ABis[i] - f_A)) / f_total_var
first_order_idcs.append(vi.unsqueeze(0))
self.first_order_idcs = torch.cat(first_order_idcs, dim=0).detach()
if not self.bootstrap:
return self.first_order_idcs
else:
f_B_btsp = none_throws(self.f_B_btsp)
f_A_btsp = none_throws(self.f_A_btsp)
f_ABis_btsp = none_throws(self.f_ABis_btsp)
f_total_var_btsp = none_throws(self.f_total_var_btsp)
first_order_idcs_btsp = [torch.cat(first_order_idcs, dim=0).unsqueeze(0)]
for b in range(self.num_bootstrap_samples):
first_order_idcs = []
for i in range(self.dim):
vi = (
torch.mean(f_B_btsp[b] * (f_ABis_btsp[b][i] - f_A_btsp[b]))
/ f_total_var_btsp[b]
)
first_order_idcs.append(vi.unsqueeze(0))
first_order_idcs_btsp.append(
torch.cat(first_order_idcs, dim=0).unsqueeze(0)
)
self.first_order_idcs_btsp = torch.cat(first_order_idcs_btsp, dim=0)
if self.bootstrap_array:
return self.first_order_idcs_btsp.detach()
else:
btsp = none_throws(self.first_order_idcs_btsp)
return (
torch.cat(
[
btsp.mean(dim=0).unsqueeze(0),
btsp.var(dim=0).unsqueeze(0),
torch.sqrt(
btsp.var(dim=0) / (self.num_bootstrap_samples + 1)
).unsqueeze(0),
],
dim=0,
)
.t()
.detach()
)
def total_order_indices(self) -> Tensor:
r"""Computes the total Sobol indices:
Returns:
if num_bootstrap_samples>1
Tensor: (values,var_mc,stderr_mc)x dim
else
Tensor: (values)x dim
"""
f_A = none_throws(self.f_A)
f_ABis = none_throws(self.f_ABis)
f_total_var = none_throws(self.f_total_var)
total_order_idcs = []
for i in range(self.dim):
vti = 0.5 * torch.mean(torch.pow(f_A - f_ABis[i], 2)) / f_total_var
total_order_idcs.append(vti.unsqueeze(0))
if not (self.bootstrap):
total_order_idcs = torch.cat(total_order_idcs, dim=0).detach()
return total_order_idcs
else:
f_A_btsp = none_throws(self.f_A_btsp)
f_ABis_btsp = none_throws(self.f_ABis_btsp)
f_total_var_btsp = none_throws(self.f_total_var_btsp)
total_order_idcs_btsp = [torch.cat(total_order_idcs, dim=0).unsqueeze(0)]
for b in range(self.num_bootstrap_samples):
total_order_idcs = []
for i in range(self.dim):
vti = (
0.5
* torch.mean(torch.pow(f_A_btsp[b] - f_ABis_btsp[b][i], 2))
/ f_total_var_btsp[b]
)
total_order_idcs.append(vti.unsqueeze(0))
total_order_idcs_btsp.append(
torch.cat(total_order_idcs, dim=0).unsqueeze(0)
)
total_order_idcs_btsp = torch.cat(total_order_idcs_btsp, dim=0)
if self.bootstrap_array:
return total_order_idcs_btsp.detach()
else:
return (
torch.cat(
[
total_order_idcs_btsp.mean(dim=0).unsqueeze(0),
total_order_idcs_btsp.var(dim=0).unsqueeze(0),
torch.sqrt(
total_order_idcs_btsp.var(dim=0)
/ (self.num_bootstrap_samples + 1)
).unsqueeze(0),
],
dim=0,
)
.t()
.detach()
)
def second_order_indices(
self,
first_order_idcs: torch.Tensor | None = None,
first_order_idcs_btsp: torch.Tensor | None = None,
) -> Tensor:
r"""Computes the Second order Sobol indices:
Args:
first_order_idcs: Tensor of previously computed first order indices, where
first_order_idcs.shape = torch.Size([dim]).
first_order_idcs_btsp: Tensor of all first order indices given by bootstrap.
Returns:
if num_bootstrap_samples>1
Tensor: (values,var_mc,stderr_mc)x dim
else
Tensor: (values)x dim
"""
if not self.second_order:
raise ValueError(
"Second order indices has to be specified in the sensitivity definition"
)
# TODO Improve this part of the code by vectorization T204291129
if first_order_idcs is None:
if self.first_order_idcs is None:
self.first_order_indices()
first_order_idcs = self.first_order_idcs
f_A = none_throws(self.f_A)
f_B = none_throws(self.f_B)
f_ABis = none_throws(self.f_ABis)
f_BAis = none_throws(self.f_BAis)
f_total_var = none_throws(self.f_total_var)
first_order_idcs_val = none_throws(first_order_idcs)
second_order_idcs = []
for i in range(self.dim):
for j in range(i + 1, self.dim):
vij = torch.mean(f_BAis[i] * f_ABis[j] - f_A * f_B)
vij = (
(vij / f_total_var)
- first_order_idcs_val[i]
- first_order_idcs_val[j]
)
second_order_idcs.append(vij.unsqueeze(0))
if not self.bootstrap:
second_order_idcs = torch.cat(second_order_idcs, dim=0).detach()
return second_order_idcs
else:
f_A_btsp = none_throws(self.f_A_btsp)
f_B_btsp = none_throws(self.f_B_btsp)
f_ABis_btsp = none_throws(self.f_ABis_btsp)
f_BAis_btsp = none_throws(self.f_BAis_btsp)
f_total_var_btsp = none_throws(self.f_total_var_btsp)
second_order_idcs_btsp = [torch.cat(second_order_idcs, dim=0).unsqueeze(0)]
if first_order_idcs_btsp is None:
first_order_idcs_btsp = none_throws(self.first_order_idcs_btsp)
for b in range(self.num_bootstrap_samples):
second_order_idcs = []
for i in range(self.dim):
for j in range(i + 1, self.dim):
vij = torch.mean(
f_BAis_btsp[b][i] * f_ABis_btsp[b][j]
- f_A_btsp[b] * f_B_btsp[b]
)
vij = (
(vij / f_total_var_btsp[b])
- first_order_idcs_btsp[b][i]
- first_order_idcs_btsp[b][j]
)
second_order_idcs.append(vij.unsqueeze(0))
second_order_idcs_btsp.append(
torch.cat(second_order_idcs, dim=0).unsqueeze(0)
)
second_order_idcs_btsp = torch.cat(second_order_idcs_btsp, dim=0)
if self.bootstrap_array:
return second_order_idcs_btsp.detach()
else:
return (
torch.cat(
[
second_order_idcs_btsp.mean(dim=0).unsqueeze(0),
second_order_idcs_btsp.var(dim=0).unsqueeze(0),
torch.sqrt(
second_order_idcs_btsp.var(dim=0)
/ (self.num_bootstrap_samples + 1)
).unsqueeze(0),
],
dim=0,
)
.t()
.detach()
)
def GaussianLinkMean(mean: torch.Tensor, var: torch.Tensor) -> torch.Tensor:
return mean
def ProbitLinkMean(mean: torch.Tensor, var: torch.Tensor) -> torch.Tensor:
a = mean / torch.sqrt(1 + var)
return torch.distributions.Normal(0, 1).cdf(a)
class SobolSensitivityGPMean:
def __init__(
self,
model: GPyTorchModel,
bounds: torch.Tensor,
num_mc_samples: int = 10**4,
second_order: bool = False,
input_qmc: bool = False,
num_bootstrap_samples: int = 1,
link_function: Callable[
[torch.Tensor, torch.Tensor], torch.Tensor
] = GaussianLinkMean,
discrete_features: list[int] | None = None,
) -> None:
r"""Computes three types of Sobol indices:
first order indices, total indices and second order indices (if specified ).
Args:
model: BoTorch model whose posterior is a `GPyTorchPosterior`.
bounds: `2 x d` parameter bounds over which to evaluate model sensitivity.
method: if "predictive mean", the predictive mean is used for indices
computation. If "GP samples", posterior sampling is used instead.
num_mc_samples: The number of montecarlo grid samples
second_order: If True, the second order indices are computed.
input_qmc: If True, a qmc Sobol grid is use instead of uniformly random.
num_bootstrap_samples: If bootstrap is true, the number of bootstraps has
to be specified.
link_function: The link function to be used when computing the indices.
Indices can be computed for the mean or on samples of the posterior,
predictive, but defaults to computing on the mean (GaussianLinkMean).
discrete_features: If specified, the inputs associated with the indices in
this list are generated using an integer-valued uniform distribution,
rather than the default (pseudo-)random continuous uniform distribution.
"""
self.model = model
self.second_order = second_order
self.input_qmc = input_qmc
self.bootstrap: bool = num_bootstrap_samples > 1
self.num_bootstrap_samples = num_bootstrap_samples
self.num_mc_samples = num_mc_samples
def input_function(x: Tensor) -> Tensor:
assert x.ndim == 2, "Input must be a 2D tensor."
with torch.no_grad():
# NOTE: Do not unsqueeze x_ here. Likely due to a matmul issue
# this ends up using a lot of memory with batched models.
# See https://github.com/pytorch/botorch/issues/2310.
means, variances = [], []
for x_ in x.split(4096):
p = self.model.posterior(x_)
means.append(p.mean)
variances.append(p.variance)
mean = torch.cat(means, dim=-2)
variance = torch.cat(variances, dim=-2)
return link_function(mean, variance)
self.sensitivity = SobolSensitivity(
bounds=bounds,
num_mc_samples=self.num_mc_samples,
input_function=input_function,
second_order=self.second_order,
input_qmc=self.input_qmc,
num_bootstrap_samples=self.num_bootstrap_samples,
discrete_features=discrete_features,
)
self.sensitivity.evalute_function()
def first_order_indices(self) -> Tensor:
r"""Computes the first order Sobol indices:
Returns:
if num_bootstrap_samples>1
Tensor: (values,var_mc,stderr_mc)x dim
else
Tensor: (values)x dim
"""
return self.sensitivity.first_order_indices()
def total_order_indices(self) -> Tensor:
r"""Computes the total Sobol indices:
Returns:
if num_bootstrap_samples>1
Tensor: (values,var_mc,stderr_mc)x dim
else
Tensor: (values)x dim
"""
return self.sensitivity.total_order_indices()
def second_order_indices(self) -> Tensor:
r"""Computes the Second order Sobol indices:
Returns:
if num_bootstrap_samples>1
Tensor: (values,var_mc,stderr_mc)x dim(dim-1)/2
else
Tensor: (values)x dim(dim-1)/2
"""
return self.sensitivity.second_order_indices()
class SobolSensitivityGPSampling:
def __init__(
self,
model: Model,
bounds: torch.Tensor,
num_gp_samples: int = 10**3,
num_mc_samples: int = 10**4,
second_order: bool = False,
input_qmc: bool = False,
gp_sample_qmc: bool = False,
num_bootstrap_samples: int = 1,
discrete_features: list[int] | None = None,
) -> None:
r"""Computes three types of Sobol indices:
first order indices, total indices and second order indices (if specified ).
Args:
model: Botorch model.
bounds: `2 x d` parameter bounds over which to evaluate model sensitivity.
num_gp_samples: If method is "GP samples", the number of GP samples
has to be set.
num_mc_samples: The number of montecarlo grid samples
second_order: If True, the second order indices are computed.
input_qmc: If True, a qmc Sobol grid is use instead of uniformly random.
gp_sample_qmc: If True, the posterior sampling is done using
SobolQMCNormalSampler.
num_bootstrap_samples: If bootstrap is true, the number of bootstraps has
to be specified.
discrete_features: If specified, the inputs associated with the indices in
this list are generated using an integer-valued uniform distribution,
rather than the default (pseudo-)random continuous uniform distribution.
"""
self.model = model
self.second_order = second_order
self.input_qmc = input_qmc
self.gp_sample_qmc = gp_sample_qmc
self.bootstrap: bool = num_bootstrap_samples > 1
self.num_bootstrap_samples = num_bootstrap_samples
self.num_mc_samples = num_mc_samples
self.num_gp_samples = num_gp_samples
self.first_order_idcs_list: torch.Tensor = torch.empty(0)
self.sensitivity = SobolSensitivity(
bounds=bounds,
num_mc_samples=self.num_mc_samples,
second_order=self.second_order,
input_qmc=self.input_qmc,
num_bootstrap_samples=self.num_bootstrap_samples,
bootstrap_array=True,
discrete_features=discrete_features,
)
# TODO: Ideally, we would reduce the memory consumption here as well
# but this is a tricky since it uses joint posterior sampling.
posterior = self.model.posterior(self.sensitivity.A_B_ABi)
if self.gp_sample_qmc:
sampler = SobolQMCNormalSampler(
sample_shape=torch.Size([self.num_gp_samples]), seed=0
)
self.samples: torch.Tensor = sampler(posterior)
else:
with torch.no_grad():
self.samples = posterior.rsample(torch.Size([self.num_gp_samples]))
@property
def dim(self) -> int:
"""Returns the input dimensionality of `self.model`."""
return self.sensitivity.dim
def first_order_indices(self) -> Tensor:
r"""Computes the first order Sobol indices:
Returns:
if num_bootstrap_samples>1
Tensor: (values, var_gp, stderr_gp, var_mc, stderr_mc) x dim
else
Tensor: (values, var, stderr) x dim
"""
first_order_idcs_list = []
for j in range(self.num_gp_samples):
self.sensitivity.evalute_function(self.samples[j])
first_order_idcs = self.sensitivity.first_order_indices()
first_order_idcs_list.append(first_order_idcs.unsqueeze(0))
self.first_order_idcs_list: torch.Tensor = torch.cat(
first_order_idcs_list, dim=0
)
if not (self.bootstrap):
first_order_idcs_mean_var_se = []
for i in range(self.dim):
first_order_idcs_mean_var_se.append(
torch.tensor(
[
torch.mean(self.first_order_idcs_list[:, i]),
torch.var(self.first_order_idcs_list[:, i]),
torch.sqrt(
torch.var(self.first_order_idcs_list[:, i])
/ self.num_gp_samples
),
]
).unsqueeze(0)
)
first_order_idcs_mean_var_se = torch.cat(
first_order_idcs_mean_var_se, dim=0
)
return first_order_idcs_mean_var_se
else:
var_per_bootstrap = torch.var(self.first_order_idcs_list, dim=0)
gp_var = torch.mean(var_per_bootstrap, dim=0)
gp_se = torch.sqrt(gp_var / self.num_gp_samples)
var_per_gp_sample = torch.var(self.first_order_idcs_list, dim=1)
mc_var = torch.mean(var_per_gp_sample, dim=0)
mc_se = torch.sqrt(mc_var / (self.num_bootstrap_samples + 1))
total_mean = self.first_order_idcs_list.reshape(-1, self.dim).mean(dim=0)
first_order_idcs_mean_vargp_segp_varmc_segp = torch.cat(
[
torch.tensor(
[total_mean[i], gp_var[i], gp_se[i], mc_var[i], mc_se[i]]
).unsqueeze(0)
for i in range(self.dim)
],
dim=0,
)
return first_order_idcs_mean_vargp_segp_varmc_segp
def total_order_indices(self) -> Tensor:
r"""Computes the total Sobol indices:
Returns:
if num_bootstrap_samples>1
Tensor: (values, var_gp, stderr_gp, var_mc, stderr_mc) x dim
else
Tensor: (values, var, stderr) x dim
"""
total_order_idcs_list = []
for j in range(self.num_gp_samples):
self.sensitivity.evalute_function(self.samples[j])
total_order_idcs = self.sensitivity.total_order_indices()
total_order_idcs_list.append(total_order_idcs.unsqueeze(0))
total_order_idcs_list = torch.cat(total_order_idcs_list, dim=0)
if not (self.bootstrap):
total_order_idcs_mean_var = []
for i in range(self.dim):
total_order_idcs_mean_var.append(
torch.tensor(
[
torch.mean(total_order_idcs_list[:, i]),
torch.var(total_order_idcs_list[:, i]),
torch.sqrt(
torch.var(total_order_idcs_list[:, i])
/ self.num_gp_samples
),
]
).unsqueeze(0)
)
total_order_idcs_mean_var = torch.cat(total_order_idcs_mean_var, dim=0)
return total_order_idcs_mean_var
else:
var_per_bootstrap = torch.var(total_order_idcs_list, dim=0)
gp_var = torch.mean(var_per_bootstrap, dim=0)
gp_se = torch.sqrt(gp_var / self.num_gp_samples)
var_per_gp_sample = torch.var(total_order_idcs_list, dim=1)
mc_var = torch.mean(var_per_gp_sample, dim=0)
mc_se = torch.sqrt(mc_var / (self.num_bootstrap_samples + 1))
total_mean = total_order_idcs_list.reshape(-1, self.dim).mean(dim=0)
total_order_idcs_mean_vargp_segp_varmc_segp = torch.cat(
[
torch.tensor(
[total_mean[i], gp_var[i], gp_se[i], mc_var[i], mc_se[i]]
).unsqueeze(0)
for i in range(self.dim)
],
dim=0,
)
return total_order_idcs_mean_vargp_segp_varmc_segp
def second_order_indices(self) -> Tensor:
r"""Computes the Second order Sobol indices:
Returns:
if num_bootstrap_samples>1
Tensor: (values, var_gp, stderr_gp, var_mc, stderr_mc) x dim(dim-1) / 2
else
Tensor: (values, var, stderr) x dim(dim-1) / 2
"""
if not (self.bootstrap):
second_order_idcs_list = []
second_order_idcs: Tensor = torch.empty(0)
for j in range(self.num_gp_samples):
self.sensitivity.evalute_function(self.samples[j])
second_order_idcs = self.sensitivity.second_order_indices(
self.first_order_idcs_list[j]
)
second_order_idcs_list.append(second_order_idcs.unsqueeze(0))
second_order_idcs_list = torch.cat(second_order_idcs_list, dim=0)
second_order_idcs_mean_var = []
for i in range(len(second_order_idcs)):
second_order_idcs_mean_var.append(
torch.tensor(
[
torch.mean(second_order_idcs_list[:, i]),
torch.var(second_order_idcs_list[:, i]),
torch.sqrt(
torch.var(second_order_idcs_list[:, i])
/ self.num_gp_samples
),
]
).unsqueeze(0)
)
second_order_idcs_mean_var = torch.cat(second_order_idcs_mean_var, dim=0)
return second_order_idcs_mean_var
else:
second_order_idcs_list = []
for j in range(self.num_gp_samples):
self.sensitivity.evalute_function(self.samples[j])
second_order_idcs = self.sensitivity.second_order_indices(
self.first_order_idcs_list[j][0],
self.first_order_idcs_list[j][1:],
)
second_order_idcs_list.append(second_order_idcs.unsqueeze(0))
second_order_idcs_list = torch.cat(second_order_idcs_list, dim=0)
var_per_bootstrap = torch.var(second_order_idcs_list, dim=0)
gp_var = torch.mean(var_per_bootstrap, dim=0)
gp_se = torch.sqrt(gp_var / self.num_gp_samples)
var_per_gp_sample = torch.var(second_order_idcs_list, dim=1)
mc_var = torch.mean(var_per_gp_sample, dim=0)
mc_se = torch.sqrt(mc_var / (self.num_bootstrap_samples + 1))
num_second_order = second_order_idcs_list.shape[-1]
total_mean = second_order_idcs_list.reshape(-1, num_second_order).mean(
dim=0
)
second_order_idcs_mean_vargp_segp_varmc_segp = torch.cat(
[
torch.tensor(
[total_mean[i], gp_var[i], gp_se[i], mc_var[i], mc_se[i]]
).unsqueeze(0)
for i in range(num_second_order)
],
dim=0,
)
return second_order_idcs_mean_vargp_segp_varmc_segp
def compute_sobol_indices_from_model_list(
model_list: list[GPyTorchModel],
bounds: Tensor,
order: str = "first",
discrete_features: list[int] | None = None,
fixed_features: dict[int, float] | None = None,
**sobol_kwargs: Any,
) -> Tensor:
"""
Computes Sobol indices of a list of models on a bounded domain.
Args:
model_list: A list of botorch.models.model.Model types for which to compute
the Sobol indices.
bounds: A 2 x d Tensor of lower and upper bounds of the domain of the models.
order: A string specifying the order of the Sobol indices to be computed.
Supports "first", "second" and "total" and defaults to "first". "total"
computes the importance of a variable considering its main effect and
all of its higher-order interactions, whereas "first" and "second"
the variance when altering the variable in isolation or with one other
variable, respectively.
discrete_features: If specified, the inputs associated with the indices in
this list are generated using an integer-valued uniform distribution,
rather than the default (pseudo-)random continuous uniform distribution.
fixed_features: If specified, a dictionary mapping feature indices to fixed
values. These features will be held constant during sensitivity analysis,
and their sensitivity will not be computed. The bounds tensor should
still include all features (including fixed ones).
sobol_kwargs: keyword arguments passed on to SobolSensitivityGPMean.
Returns:
With m GPs, returns a (m x d') tensor of `order`-order Sobol indices,
where d' is the number of non-fixed features.
"""
if order not in ["first", "total", "second"]:
raise NotImplementedError(
f"Order {order} is not supported. Plese choose one of"
" 'first', 'total' or 'second'."
)
# Handle fixed features by reducing bounds and wrapping models
models_to_use: list[GPyTorchModel] | list[FixedFeatureModel] = model_list
if fixed_features is not None and len(fixed_features) > 0:
models_to_use, bounds, discrete_features = prepare_fixed_feature_inputs(
model_list=model_list,
bounds=bounds,
discrete_features=discrete_features,
fixed_features=fixed_features,
)
indices = []
method = getattr(SobolSensitivityGPMean, f"{order}_order_indices")
second_order = order == "second"
for model in models_to_use:
sens_class = SobolSensitivityGPMean(
model=model, # pyre-ignore[6]: FixedFeatureModel wraps GPyTorchModel
bounds=bounds,
discrete_features=discrete_features,
second_order=second_order,
**sobol_kwargs,
)
indices.append(method(sens_class))
return torch.stack(indices)
def ax_parameter_sens(
adapter: TorchAdapter,
metrics: list[str] | None = None,
order: str = "first",
signed: bool = True,
exclude_map_key: bool = True,
exclude_task: bool = False,
**sobol_kwargs: Any,
) -> dict[str, dict[str, npt.NDArray]]:
"""
Compute sensitivity for all metrics on an TorchAdapter.
Sobol measures are always positive regardless of the direction in which the
parameter influences f. If `signed` is set to True, then the Sobol measure for each
parameter will be given as its sign the sign of the average gradient with respect to
that parameter across the search space. Thus, important parameters that, when
increased, decrease f will have large and negative values; unimportant parameters
will have values close to 0.
Args:
adapter: A Adapter object with models that were fit.
metrics: The names of the metrics and outcomes for which to compute
sensitivities. This should preferably be metrics with a good model fit.
Defaults to adapter.outcomes.
order: A string specifying the order of the Sobol indices to be computed.
Supports "first" and "total" and defaults to "first".
signed: A bool for whether the measure should be signed.
exclude_map_key: If True (default), the MAP_KEY ("step") feature will be
excluded from sensitivity analysis by fixing it at the maximum step value.
This makes the sensitivity analysis more interpretable for users who
care about the effect of parameters on final performance.
exclude_task: If True, task parameters (those with ``is_task=True``, e.g.
synthetic parameters from the TrialAsTask transform) will be excluded
from the sensitivity results.
sobol_kwargs: keyword arguments passed on to SobolSensitivityGPMean, and if
signed, GpDGSMGpMean.
Returns:
Dictionary {'metric_name': {'parameter_name' or
(parameter_name_1, 'parameter_name_2'): sensitivity_value}}, where the
`sensitivity` value is cast to a Numpy array in order to be compatible with
`plot_feature_importance_by_feature`.
"""
if order not in ["first", "total", "second"]:
raise NotImplementedError(
f"Order {order} is not supported. Plese choose one of"
" 'first', 'total' or 'second'."
)
if metrics is None:
metrics = adapter.outcomes
generator, digest = _get_generator_and_digest(adapter=adapter)
model_list = _get_model_per_metric(generator=generator, metrics=metrics)
# get device and dtype of the first model
first_model = next(model_list[0].parameters())
device = first_model.device
bounds = torch.tensor(
digest.bounds,
device=device,
).T # transposing to make it 2 x d
feature_names = digest.feature_names
# Determine if we need to fix the MAP_KEY feature
fixed_features: dict[int, float] | None = None
output_feature_names = feature_names
discrete_features = digest.categorical_features + digest.ordinal_features
if exclude_map_key and MAP_KEY in feature_names:
step_idx = feature_names.index(MAP_KEY)
# Use upper bound (maximum step value in normalized space)
step_value = float(bounds[1, step_idx])
fixed_features = {step_idx: step_value}
# Remove MAP_KEY from output feature names
output_feature_names = [f for f in feature_names if f != MAP_KEY]
# Exclude task parameters (e.g. TRIAL_PARAM from TrialAsTask transform)
# by fixing them at their target values.
if exclude_task and digest.task_features:
if fixed_features is None:
fixed_features = {}
for task_idx in digest.task_features:
if task_idx < len(feature_names):
fixed_features[task_idx] = float(
digest.target_values.get(task_idx, bounds[1, task_idx])
)
task_name = feature_names[task_idx]
output_feature_names = [
f for f in output_feature_names if f != task_name
]
# for second order indices, we need to compute first order indices first
# which is what is done here. With the first order indices, we can then subtract
# appropriately using the first-order indices to extract the second-order indices.
ind = compute_sobol_indices_from_model_list(
model_list=model_list,
bounds=bounds,
order="first" if order == "second" else order,
discrete_features=discrete_features,
fixed_features=fixed_features,
**sobol_kwargs,
)
if signed:
ind_deriv = compute_derivatives_from_model_list(
model_list=model_list,
bounds=bounds,
discrete_features=discrete_features,
fixed_features=fixed_features,
**sobol_kwargs,
)
# categorical features don't have a direction, so we set the derivative to 1.0
# in order not to zero out their sensitivity. We treat categorical features
# separately in the sensitivity analysis plot as well, to make clear that they
# are affecting the metric, but neither increasing nor decreasing. Note that the
# original variables have a well defined direction, so we do not need to treat
# them differently here.
for i in digest.categorical_features:
ind_deriv[:, i] = 1.0
ind *= torch.sign(ind_deriv).to(device)
indices = array_with_string_indices_to_dict(
rows=metrics, cols=output_feature_names, A=ind.cpu().numpy()
)
if order == "second" and len(output_feature_names) >= 2:
second_order_values = compute_sobol_indices_from_model_list(
model_list=model_list,
bounds=bounds,
order="second",
discrete_features=discrete_features,
fixed_features=fixed_features,
**sobol_kwargs,
)
second_order_feature_names = [
f"{f1} & {f2}" for f1, f2 in itertools.combinations(output_feature_names, 2)
]
second_order_dict = array_with_string_indices_to_dict(
rows=metrics,
cols=second_order_feature_names,
A=second_order_values.cpu().numpy(),
)
for metric in metrics:
indices[metric].update(second_order_dict[metric])
return indices
def _get_generator_and_digest(
adapter: TorchAdapter,
) -> tuple[BoTorchGenerator, SearchSpaceDigest]:
"""Returns the generator of the adapter and the SearchSpaceDigest
that was used to fit the adapter.
"""
if not isinstance(adapter, TorchAdapter):
raise NotImplementedError(
f"{type(adapter)=}, but only TorchAdapter is supported."
)
generator = adapter.generator
if not isinstance(generator, (BoTorchGenerator)):