[6ac965]: / src / iterpretability / simulators.py

Download this file

1499 lines (1210 with data), 59.5 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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
# stdlib
import random
from typing import Tuple
import src.iterpretability.logger as log
# third party
import numpy as np
import torch
from scipy.special import expit
from scipy.stats import zscore
from omegaconf import DictConfig, OmegaConf
from src.iterpretability.utils import enable_reproducible_results
from abc import ABC, abstractmethod
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# For computing the propensities from scores
from scipy.special import softmax
from scipy.stats import zscore
from sklearn.model_selection import train_test_split
EPS = 0
class SimulatorBase(ABC):
"""
Base class for simulators.
"""
@abstractmethod
def simulate(self, X: np.ndarray, outcomes: np.ndarray = None) -> Tuple:
raise NotImplementedError
@abstractmethod
def get_simulated_data(self, train_ratio: float) -> Tuple:
raise NotImplementedError
@property
@abstractmethod
def selective_features(self) -> np.ndarray:
raise NotImplementedError
@property
@abstractmethod
def prognostic_features(self) -> np.ndarray:
raise NotImplementedError
@property
@abstractmethod
def predictive_features(self) -> np.ndarray:
raise NotImplementedError
class TYSimulator(SimulatorBase):
"""
Data generation process class for simulating treatment selection and outcomes (and effects)
"""
nonlinear_fcts = [
#lambda x: np.abs(x),
lambda x: np.exp(-(x**2) / 2),
# lambda x: 1 / (1 + x**2),
# lambda x: np.sqrt(x)*(1+x),
#lambda x: np.cos(5*x),
#lambda x: x**2,
# lambda x: np.arctan(x),
# lambda x: np.tanh(x),
# lambda x: np.sin(x),
# lambda x: np.log(1 + x**2),
#lambda x: np.sqrt(0.02 + x**2),
#lambda x: np.cosh(x),
]
def __init__(
self,
# Data dimensionality
dim_X: int,
# Seed
seed: int = 42,
# Simulation type
simulation_type: str = "ty",
# Dimensionality of treatments and outcome
num_binary_outcome: int = 0,
outcome_unbalancedness_ratio: float = 0,
standardize_outcome: bool = False,
num_T: int = 3,
dim_Y: int = 3,
# Scale parameters
predictive_scale: float = 1,
prognostic_scale: float = 1,
propensity_scale: float = 1,
unbalancedness_exp: float = 0,
nonlinearity_scale: float = 1,
propensity_type: str = "prog_pred",
alpha: float = 0.5,
enforce_balancedness: bool = False,
# Control
include_control: bool = False,
# Important features
num_pred_features: int = 5,
num_prog_features: int = 5,
num_select_features: int = 5,
feature_type_overlap: str = "sel_none",
treatment_feature_overlap: bool = False,
# Feature selection
random_feature_selection: bool = False,
nonlinearity_selection_type: bool = True,
# Noise
noise: bool = True,
noise_std: float = 0.1,
) -> None:
# Number of features
self.dim_X = dim_X
# Make sure results are reproducible by setting seed for np, torch, random
self.seed = seed
enable_reproducible_results(seed=self.seed)
# Simulation type
self.simulation_type = simulation_type
# Store dimensions
self.num_binary_outcome = num_binary_outcome
self.outcome_unbalancedness_ratio = outcome_unbalancedness_ratio
self.standardize_outcome = standardize_outcome
self.num_T = num_T
self.dim_Y = dim_Y
# Scale parameters
self.predictive_scale = predictive_scale
self.prognostic_scale = prognostic_scale
self.propensity_scale = propensity_scale
self.unbalancedness_exp = unbalancedness_exp
self.nonlinearity_scale = nonlinearity_scale
self.propensity_type = propensity_type
self.alpha = alpha
self.enforce_balancedness = enforce_balancedness
# Control
self.include_control = include_control
# Important features
self.num_pred_features = num_pred_features
self.num_prog_features = num_prog_features
self.num_select_features = num_select_features
self.num_important_features = self.num_T*(num_pred_features + num_select_features) + num_prog_features
self.feature_type_overlap = feature_type_overlap
self.treatment_feature_overlap = treatment_feature_overlap
# Feature selection
self.random_feature_selection = random_feature_selection
self.nonlinearity_selection_type = nonlinearity_selection_type
# Noise
self.noise = noise
self.noise_std = noise_std
# Setup variables
self.nonlinearities = None
self.prog_mask, self.pred_masks, self.select_masks = None, None, None
self.prog_weights, self.pred_weights, self.select_weights = None, None, None
# Setup
self.setup()
# Simulation variables
self.X = None
self.prog_scores, self.pred_scores, self.select_scores = None, None, None
self.select_scores_pred_overlap = None
self.select_scores_prog_overlap = None
self.propensities, self.outcomes, self.T, self.Y = None, None, None, None
def get_simulated_data(self):
"""
Extract results and split into training and test set. Include counterfactual outcomes and propensities.
"""
return self.X, self.T, self.Y, self.outcomes, self.propensities
## OLD CODE
# Split data
# train_size = int(train_ratio * self.X.shape[0])
# if self.num_binary_outcome > 0:
# (
# X_train, X_test,
# Y_train, Y_test,
# T_train, T_test,
# outcomes_train, outcomes_test,
# propensities_train, propensities_test,
# ) = train_test_split(self.X, self.Y, self.T, self.outcomes, self.propensities, train_size=train_size, stratify=self.Y)
# else:
# X_train, X_test = self.X[:train_size], self.X[train_size:]
# T_train, T_test = self.T[:train_size], self.T[train_size:]
# Y_train, Y_test = self.Y[:train_size], self.Y[train_size:]
# outcomes_train, outcomes_test = self.outcomes[:train_size,:,:], self.outcomes[train_size:,:,:]
# propensities_train, propensities_test = self.propensities[:train_size], self.propensities[train_size:]
# if train_ratio == 1:
# return self.X, self.T, self.Y, self.outcomes, self.propensities
# return X_train, X_test, T_train, T_test, Y_train, Y_test, outcomes_train, outcomes_test, propensities_train, propensities_test
def simulate(self, X, outcomes=None) -> Tuple:
"""
Simulate treatment and outcome for a dataset based on the configuration.
"""
log.debug(
f'Simulating treatment and outcome for a dataset with:'
f'\n==================================================================='
f'\nDim X: {self.dim_X}'
f'\nDim T: {self.num_T}'
f'\nDim Y: {self.dim_Y}'
f'\nPredictive Scale: {self.predictive_scale}'
f'\nPrognostic Scale: {self.prognostic_scale}'
f'\nPropensity Scale: {self.propensity_scale}'
f'\nUnbalancedness Exponent: {self.unbalancedness_exp}'
f'\nNonlinearity Scale: {self.nonlinearity_scale}'
f'\nNum Pred Features: {self.num_pred_features}'
f'\nNum Prog Features: {self.num_prog_features}'
f'\nNum Select Features: {self.num_select_features}'
f'\nFeature Overlap: {self.treatment_feature_overlap}'
f'\nRandom Feature Selection: {self.random_feature_selection}'
f'\nNonlinearity Selection Type: {self.nonlinearity_selection_type}'
f'\nNoise: {self.noise}'
f'\nNoise Std: {self.noise_std}'
f'\n===================================================================\n'
)
# 1. Store data with min max scaling to range [0, 1]
self.X = X
# self.X = (X - X.min(axis=0)) / (X.max(axis=0) - X.min(axis=0) + EPS)
# 2. Compute scores for prognostic, predictive, and selective features
self.compute_scores()
# 3. Compute factual and counterfactual outcomes based on the data and the predictive and prognostic scores
self.compute_all_outcomes()
# 4. Compute propensities based on the data and the selective scores
self.compute_propensities()
# 5. Sample treatment assignment based on the propensities
self.sample_T()
# 6. Extract the outcome based on the treatment assignment
self.extract_Y()
return None
def setup(self) -> None:
"""
Setup the simulator by defining variables which remain the same across simulations with different samples but the same configuration.
"""
# 1. Sample nonlinearities used
num_nonlinearities = 2 + self.dim_Y # Different non-linearities for each outcome (predictive), same for all treatments
self.nonlinearities = self.sample_nonlinearities(num_nonlinearities)
# 2. Set important feature masks - determine which features should be used for treatment selection, outcome prediction
self.sample_important_feature_masks()
# 3. Sample weights for features
self.sample_uniform_weights()
def get_true_cates(self,
X: np.ndarray,
T: np.ndarray,
outcomes: np.ndarray) -> np.ndarray:
"""
Compute true CATEs for each treatment based on the data and the outcomes.
Always use the selected treatment as the base treatment.
"""
# Compute CATEs for each treatment
cates = np.zeros((X.shape[0], self.num_T, self.dim_Y))
for i in range(X.shape[0]):
for j in range(self.num_T):
cates[i,j,:] = outcomes[i,j,:] - outcomes[i,int(T[i]),:]
log.debug(
f'\nCheck if true CATEs are computed correctly:'
f'\n==================================================================='
f'\nOutcomes: {outcomes.shape}'
f'\n{outcomes}'
f'\n\nTreatment Assignment: {T.shape}'
f'\n{T}'
f'\n\nTrue CATEs: {cates.shape}'
f'\n{cates}'
f'\n===================================================================\n'
)
return cates
def extract_Y(self) -> None:
"""
Extract the outcome based on the treatment assignment.
"""
self.Y = self.outcomes[np.arange(self.X.shape[0]), self.T]
log.debug(
f'\nCheck if outcomes are extracted correctly:'
f'\n==================================================================='
f'\nOutcomes'
f'\n{self.outcomes}'
f'\n{self.outcomes.shape}'
f'\n\nTreatment Assignment'
f'\n{self.T}'
f'\n{self.T.shape}'
f'\n\nExtracted Outcomes'
f'\n{self.Y}'
f'\n{self.Y.shape}'
f'\n===================================================================\n'
)
return None
def compute_all_outcomes_toy(self) -> None:
# Compute outcomes for each treatment and outcome
outcomes = np.zeros((self.X.shape[0], self.num_T, self.dim_Y))
X0 = self.X[:,0]
X1 = self.X[:,1]
k=20
nonlinearity = lambda x: 1 / (1 + np.exp(-k * (x - 0.5))) #logistic
if self.propensity_type.startswith("toy1") or self.propensity_type.startswith("toy3") or self.propensity_type.startswith("toy4"):
fun_y0 = lambda X0, X1: X0
fun_y1 = lambda X0, X1: 1-X0
elif self.propensity_type.startswith("toy2"):
fun_y0 = lambda X0, X1: X0
fun_y1 = lambda X0, X1: 1-X1
elif self.propensity_type.startswith("toy6"):
fun_y0 = lambda X0, X1: X0
fun_y1 = lambda X0, X1: X1
elif self.propensity_type.startswith("toy5"):
fun_y0 = lambda X0, X1: np.sin(X0*10*np.pi)
fun_y1 = lambda X0, X1: np.sin((1-X0)*10*np.pi)
elif self.propensity_type.startswith("toy7"):
fun_y0 = lambda X0, X1: nonlinearity(X0)-nonlinearity(X1)
fun_y1 = lambda X0, X1: nonlinearity(X0)+nonlinearity(X1)
elif self.propensity_type.startswith("toy8"):
fun_y0 = lambda X0, X1: X0
fun_y1 = lambda X0, X1: 1-X0
Y = np.array([fun_y0(X0, X1),fun_y1(X0, X1)]).T
if self.propensity_type.endswith("nonlinear"):
Y = nonlinearity(Y)
Y = zscore(Y, axis=None)
outcomes[:,:,0] = Y
return outcomes
def compute_all_outcomes(self) -> None:
"""
Compute factual and counterfactual outcomes based on the data and the predictive and prognostic scores.
"""
if self.propensity_type.startswith("toy"):
outcomes = self.compute_all_outcomes_toy()
else:
# Compute outcomes for each treatment and outcome
outcomes = np.zeros((self.X.shape[0], self.num_T, self.dim_Y))
for i in range(self.num_T):
for j in range(self.dim_Y):
if self.include_control and i == 0:
outcomes[:,i,j] = self.prognostic_scale*self.prog_scores[:,j]
else:
outcomes[:,i,j] = self.prognostic_scale*self.prog_scores[:,j] + self.predictive_scale*self.pred_scores[:,i,j]
# Add gaussian noise to outcomes
if self.noise:
outcomes = outcomes + np.random.normal(0, self.noise_std, size=outcomes.shape)
# Create binary outcomes and introduce unbalancedness
if int(self.num_binary_outcome) > 0:
for j in range(self.num_binary_outcome):
scores = zscore(outcomes[:,:,j], axis=0)
prob = expit(scores)
outcomes[:,:,j] = prob > self.outcome_unbalancedness_ratio
self.outcomes = outcomes
# Standardize outcomes
if self.standardize_outcome:
# normalize outcomes per outcome
self.outcomes = zscore(self.outcomes, axis=0)
log.debug(
f'\nCheck if outcomes are computed correctly:'
f'\n==================================================================='
f'\nProg Scores'
f'\n{self.prog_scores}'
f'\n{self.prog_scores.shape}'
f'\n\nPred Scores'
f'\n{self.pred_scores}'
f'\n{self.pred_scores.shape}'
f'\n\nOutcomes'
f'\n{self.outcomes}'
f'\n{self.outcomes.shape}'
f'\n\nMean Outcomes'
f'\n{self.outcomes.mean(axis=0)}'
f'\n\nVariance Outcomes'
f'\n{self.outcomes.var(axis=0)}'
f'\n===================================================================\n'
)
return None
def sample_T(self) -> None:
"""
Sample treatment assignment based on the propensities.
"""
# Sample from the resulting categorical distribution per row
self.T = np.array([np.random.choice([tre for tre in range(self.propensities.shape[1])], p=row) for row in self.propensities])
log.debug(
f'\nCheck if treatment assignment is sampled correctly:'
f'\n==================================================================='
f'\nPropensities'
f'\n{self.propensities}'
f'\n{self.propensities.shape}'
f'\n\nTreatment Assignment'
f'\n{self.T}'
f'\n{self.T.shape}'
f'\n\nUnique Treatment Counts'
f'\n{np.unique(self.T, return_counts=True)}'
f'\n===================================================================\n'
)
return None
def get_unbalancedness_weights(self, size: int) -> np.ndarray:
"""
Create weights for introducing unbalancedness for class probabilities.
"""
# Sample initial distribution of treatment assignment
unb_weights = np.random.uniform(0, 1, size=size)
unb_weights = unb_weights / unb_weights.sum()
# Standardize the weights and make sure that a treatment doesn't completely disappear for small unbalancedness exponents
min_val = unb_weights.min()
range_val = unb_weights.max() - min_val
unb_weights = (unb_weights - min_val) / range_val
unb_weights = 0.01 + unb_weights * 0.98
return unb_weights
def compute_propensity_scores_toy(self) -> np.ndarray:
X0 = self.X[:,0]
X1 = self.X[:,1]
if self.propensity_type.startswith("toy1"):
fun_t0 = lambda X0, X1: X0
fun_t1 = lambda X0, X1: 1-X0
elif self.propensity_type.startswith("toy2"):
fun_t0 = lambda X0, X1: X0
fun_t1 = lambda X0, X1: 1-X1
elif self.propensity_type.startswith("toy3"):
fun_t0 = lambda X0, X1: X1
fun_t1 = lambda X0, X1: 1-X1
elif self.propensity_type.startswith("toy4"):
fun_t0 = lambda X0, X1: np.sin(X0*10*np.pi)
fun_t1 = lambda X0, X1: np.sin((1-X0)*10*np.pi)
elif self.propensity_type.startswith("toy5"):
fun_t0 = lambda X0, X1: 1-X0
fun_t1 = lambda X0, X1: X0
elif self.propensity_type.startswith("toy6"):
fun_t0 = lambda X0, X1: 1-X0
fun_t1 = lambda X0, X1: X0
elif self.propensity_type.startswith("toy7"):
fun_t0 = lambda X0, X1: 1-X0
fun_t1 = lambda X0, X1: X0
elif self.propensity_type.startswith("toy8"):
fun_t0 = lambda X0, X1: 1-X0
fun_t1 = lambda X0, X1: X0
scores = np.array([fun_t0(X0, X1),fun_t1(X0, X1)]).T
return scores
def compute_propensities(self) -> None:
"""
Compute propensities based on the data and the selective scores.
"""
select_scores_pred_overlap = zscore(self.select_scores_pred_overlap, axis=0) # Comment for Predictive Epertise
select_scores_prog_overlap = zscore(self.select_scores_prog_overlap, axis=0) # Comment for Predictive Epertise
select_scores_none = zscore(self.select_scores, axis=0) # Comment for Predictive Epertise
select_scores_pred = np.zeros((self.X.shape[0], self.num_T))
select_scores_pred_flipped = np.zeros((self.X.shape[0], self.num_T))
select_scores_prog = np.zeros((self.X.shape[0], self.num_T))
select_scores_tre = np.zeros((self.X.shape[0], self.num_T))
select_scores_pred[:,0] = self.outcomes[:,0,0] - self.outcomes[:,1,0]
select_scores_pred[:,1] = self.outcomes[:,1,0] - self.outcomes[:,0,0]
select_scores_pred_flipped[:,0] = self.outcomes[:,1,0] - self.outcomes[:,0,0]
select_scores_pred_flipped[:,1] = self.outcomes[:,0,0] - self.outcomes[:,1,0]
select_scores_prog[:,0] = self.outcomes[:,0,0]
select_scores_prog[:,1] = -self.outcomes[:,0,0]
select_scores_tre[:,0] = -self.outcomes[:,1,0]
select_scores_tre[:,1] = self.outcomes[:,1,0]
if self.propensity_type == "prog_tre":
scores = self.alpha * select_scores_tre + (1 - self.alpha) * select_scores_prog
# Standardize all scores
select_scores_pred = zscore(select_scores_pred, axis=0)
select_scores_pred_flipped = zscore(select_scores_pred_flipped, axis=0)
select_scores_prog = zscore(select_scores_prog, axis=0)
select_scores_tre = zscore(select_scores_tre, axis=0)
if self.propensity_type == "prog_pred":
scores = self.alpha * select_scores_pred + (1 - self.alpha) * select_scores_prog
elif self.propensity_type == "prog_tre":
pass
elif self.propensity_type == "none_prog":
scores = self.alpha * select_scores_prog + (1 - self.alpha) * select_scores_none
elif self.propensity_type == "none_pred":
scores = self.alpha * select_scores_pred + (1 - self.alpha) * select_scores_none
elif self.propensity_type == "none_tre":
scores = self.alpha * select_scores_tre + (1 - self.alpha) * select_scores_none
elif self.propensity_type == "none_pred_flipped":
scores = self.alpha * select_scores_pred_flipped + (1 - self.alpha) * select_scores_none
elif self.propensity_type == "pred_pred_flipped":
scores = self.alpha * select_scores_pred_flipped + (1 - self.alpha) * select_scores_pred
elif self.propensity_type == "none_pred_overlap":
scores = self.alpha * select_scores_pred_overlap + (1 - self.alpha) * select_scores_none
elif self.propensity_type == "none_prog_overlap":
scores = self.alpha * select_scores_prog_overlap + (1 - self.alpha) * select_scores_none
elif self.propensity_type == "pred_overalp_prog_overlap":
scores = self.alpha * select_scores_prog_overlap + (1 - self.alpha) * select_scores_pred_overlap
elif self.propensity_type == "rct_none":
scores = select_scores_none
elif self.propensity_type.startswith("toy"):
scores = self.compute_propensity_scores_toy()
else:
raise ValueError(f"Unknown propensity type {self.propensity_type}.")
if self.enforce_balancedness:
scores = zscore(scores, axis=0)
if self.propensity_type == "rct_none":
scores = self.alpha * select_scores_none
# Introduce unbalancedness and manipulate unbalancedness weights for comparable experiments with different seeds
unb_weights = self.get_unbalancedness_weights(size=scores.shape[1])
# Apply the softmax function to each row to get probabilities
p = softmax(self.propensity_scale*scores, axis=1)
# Scale probabilities to introduce unbalancedness
p = p * (1 - unb_weights) ** self.unbalancedness_exp
# Make sure rows add up to one again
row_sums = p.sum(axis=1, keepdims=True)
p = p / row_sums
self.propensities = p
log.debug(
f'\nCheck if propensities are computed correctly:'
f'\n==================================================================='
f'\nSelect Scores'
f'\n{self.select_scores}'
f'\n{self.select_scores.shape}'
f'\n\nPropensities'
f'\n{self.propensities}'
f'\n{self.propensities.shape}'
f'\n===================================================================\n'
)
return None
def compute_scores(self) -> None:
"""
Compute scores for prognostic, predictive, and selective features based on the data and the feature weights.
"""
# Each column of the score matrix corresponds to the score for a specific outcome. Rows correspond to samples.
prog_lin = self.X @ self.prog_weights.T
select_lin = self.X @ self.select_weights.T
select_lin_pred = self.X @ self.select_weights_pred.T
select_lin_prog = self.X @ self.select_weights_prog.T
log.debug(
f'\nCheck if linear scores are computed correctly for selective features:'
f'\n==================================================================='
f'\nself.X'
f'\n{self.X}'
f'\n{self.X.shape}'
f'\n\nSelect Weights'
f'\n{self.select_weights}'
f'\n{self.select_weights.shape}'
f'\n\nSelect Lin'
f'\n{select_lin}'
f'\n{select_lin.shape}'
f'\n===================================================================\n'
)
# Compute scores for predictive and selective features for each treatment and outcome
pred_lin = np.zeros((self.X.shape[0], self.num_T, self.dim_Y))
# This creates a score for each treatment and outcome for each sample
for i in range(self.num_T):
pred_lin[:,i,:] = self.X @ self.pred_weights[i].T
# Introduce non-linearity and get final scores
prog_scores = (1 - self.nonlinearity_scale) * prog_lin + self.nonlinearity_scale * self.nonlinearities[0](prog_lin)
select_scores = (1 - self.nonlinearity_scale) * select_lin + self.nonlinearity_scale * self.nonlinearities[1](select_lin)
select_scores_pred_overlap = (1 - self.nonlinearity_scale) * select_lin_pred + self.nonlinearity_scale * self.nonlinearities[1](select_lin_pred)
select_scores_prog_overlap = (1 - self.nonlinearity_scale) * select_lin_prog + self.nonlinearity_scale * self.nonlinearities[1](select_lin_prog)
pred_scores = np.zeros((self.X.shape[0], self.num_T, self.dim_Y))
for i in range(self.dim_Y):
pred_scores[:,:,i] = (1 - self.nonlinearity_scale) * pred_lin[:,:,i] + self.nonlinearity_scale * self.nonlinearities[i+2](pred_lin[:,:,i])
log.debug(
f'\nCheck if all scores are computed correctly for predictive features:'
f'\n==================================================================='
f'\nself.X'
f'\n{self.X}'
f'\n{self.X.shape}'
f'\n\nPred Weights'
f'\n{self.pred_weights}'
f'\n{self.pred_weights.shape}'
f'\n\nPred Lin'
f'\n{pred_lin}'
f'\n{pred_lin.shape}'
f'\n\nPred Scores'
f'\n{pred_scores}'
f'\n{pred_scores.shape}'
f'\n===================================================================\n'
)
self.prog_scores = prog_scores
self.select_scores = select_scores
self.select_scores_pred_overlap = select_scores_pred_overlap
self.select_scores_prog_overlap = select_scores_prog_overlap
self.pred_scores = pred_scores
return None
@property
def weights(self) -> Tuple:
"""
Return weights for prognostic, predictive, and selective features.
"""
return self.prog_weights, self.pred_weights, self.select_weights
def sample_uniform_weights(self) -> None:
"""
sample uniform weights for the features.
"""
if self.propensity_type.startswith("toy"):
self.prog_weights = np.zeros((self.dim_Y, self.dim_X))
self.pred_weights = np.zeros((self.num_T, self.dim_Y, self.dim_X))
self.select_weights = np.zeros((self.num_T, self.dim_X))
self.select_weights_pred = np.zeros((self.num_T, self.dim_X))
self.select_weights_prog = np.zeros((self.num_T, self.dim_X))
return None
# Sample weights for prognostic features, a weight for every outcome
prog_weights = np.random.uniform(-1, 1, size=(self.dim_Y, self.dim_X)) * self.prog_mask
# Sample weights for predictive and selective features, a weight for every dimension for every treatment and outcome
pred_weights = np.random.uniform(-1, 1, size=(self.num_T, self.dim_Y, self.dim_X))
select_weights = np.random.uniform(-1, 1, size=(self.num_T, self.dim_X))
select_weights_pred = select_weights.copy()
select_weights_prog = select_weights.copy()
# # Sample weights for prognostic features, a weight for every outcome
# prog_weights = np.random.uniform(0, 1, size=(self.dim_Y, self.dim_X)) * self.prog_mask
# # Sample weights for predictive and selective features, a weight for every dimension for every treatment and outcome
# pred_weights = np.random.uniform(0, 1, size=(self.num_T, self.dim_Y, self.dim_X))
# select_weights = np.random.uniform(0, 1, size=(self.num_T, self.dim_X))
# # Make sure treatments are different
# pred_weights[0] = -pred_weights[0]
# select_weights[0] = -select_weights[0]
# # Ones as weights
# prog_weights = np.ones((self.dim_Y, self.dim_X)) * self.prog_mask#/ self.prog_mask.sum()
# pred_weights = np.ones((self.num_T, self.dim_Y, self.dim_X)) #/ self.pred_masks.sum(axis=1, keepdims=True)
# select_weights = np.ones((self.num_T, self.dim_X)) #/ self.select_masks.sum(axis=1, keepdims=True)
# Mask weights for features that are not important
for i in range(self.num_T):
pred_weights[i] = pred_weights[i] * self.pred_masks[:,i]
select_weights[i] = select_weights[i] * self.select_masks[:,i]
select_weights_pred[i] = select_weights_pred[i] * self.select_masks_pred[:,i]
select_weights_prog[i] = select_weights_prog[i] * self.select_masks_prog[:,i]
# for i in range(self.num_T):
# row_sums = pred_weights[i].sum(axis=1, keepdims=True)
# pred_weights[i] = pred_weights[i] / row_sums
# row_sums = select_weights[i].sum()
# select_weights[i] = select_weights[i] / row_sums
# # Make sure that prog weights sum to one per outcome
# row_sums = prog_weights.sum(axis=1, keepdims=True)
# prog_weights = prog_weights / row_sums
log.debug(
f'\nCheck if masks are applied correctly:'
f'\n==================================================================='
f'\nSelect Weights'
f'\n{select_weights}'
f'\n{select_weights.shape}'
f'\n\nSelect Masks'
f'\n{self.select_masks}'
f'\n{self.select_masks.shape}'
f'\n\nPred Weights'
f'\n{pred_weights}'
f'\n{pred_weights.shape}'
f'\n\nPred Masks'
f'\n{self.pred_masks}'
f'\n{self.pred_masks.shape}'
f'\n===================================================================\n'
)
self.prog_weights = prog_weights
self.pred_weights = pred_weights
self.select_weights = select_weights
self.select_weights_pred = select_weights_pred
self.select_weights_prog = select_weights_prog
return None
@property
def all_important_features(self) -> np.ndarray:
"""
Return all important feature indices.
"""
all_important_features = np.union1d(self.predictive_features, self.prognostic_features)
all_important_features = np.union1d(all_important_features, self.selective_features)
log.debug(
f'\nCheck if all important features are computed correctly:'
f'\n==================================================================='
f'\nProg Features'
f'\n{self.prognostic_features}'
f'\n\nPred Features'
f'\n{self.predictive_features}'
f'\n\nSelect Features'
f'\n{self.selective_features}'
f'\n\nAll Important Features'
f'\n{all_important_features}'
f'\n===================================================================\n'
)
return all_important_features
@property
def prognostic_features(self) -> np.ndarray:
"""
Return prognostic feature indices.
"""
prog_features = np.where((self.prog_mask).astype(np.int32) != 0)
return prog_features
@property
def predictive_features(self) -> np.ndarray:
"""
Return predictive feature indices.
"""
pred_features = np.where((self.pred_masks.sum(axis=1)).astype(np.int32) != 0)
return pred_features
@property
def selective_features(self) -> np.ndarray:
"""
Return selective feature indices.
"""
select_features = np.where((self.select_masks.sum(axis=1)).astype(np.int32) != 0)
return select_features
def sample_important_feature_masks(self) -> None:
"""
Pick features that are important for treatment selection, outcome prediction, and prognostic prediction based on the configuration.
"""
if self.propensity_type.startswith("toy"):
self.prog_mask = np.zeros(shape=(self.dim_X))
self.pred_masks = np.zeros(shape=(self.dim_X, self.num_T))
self.select_masks = np.zeros(shape=(self.dim_X, self.num_T))
self.prog_mask[0] = 1
self.pred_masks[0,0] = 1
self.pred_masks[1,1] = 1
self.select_masks[0,0] = 1
self.select_masks[1,1] = 1
return None
# Get indices for features and shuffle if random_feature_selection is True
all_indices = np.arange(self.dim_X)
n = self.num_pred_features
if self.random_feature_selection:
np.random.shuffle(all_indices)
# Initialize masks
prog_mask = np.zeros(shape=(self.dim_X))
pred_masks = np.zeros(shape=(self.dim_X, self.num_T))
select_masks = np.zeros(shape=(self.dim_X, self.num_T))
# Handle case with feature overlap
if self.feature_type_overlap == "sel_pred":
prog_indices = all_indices[:n]
prog_mask[prog_indices] = 1
if self.treatment_feature_overlap:
assert 2*n <= int(self.dim_X)
pred_indices = np.array(self.num_T * [all_indices[n:2*n]])
select_indices = np.array(self.num_T * [all_indices[n:2*n]])
prog_mask[prog_indices] = 1
pred_masks[pred_indices] = 1
select_masks[select_indices] = 1
else:
assert n*(1+self.num_T) <= int(self.dim_X)
for i in range(self.num_T):
pred_indices = all_indices[(i+1)*n: (i+2)*n]
select_indices = all_indices[(i+1)*n: (i+2)*n]
pred_masks[pred_indices,i] = 1
select_masks[select_indices,i] = 1
elif self.feature_type_overlap == "sel_prog":
if self.treatment_feature_overlap:
assert 2*n <= int(self.dim_X)
prog_indices = all_indices[:n]
prog_mask[prog_indices] = 1
pred_indices = np.array(self.num_T * [all_indices[n:2*n]])
select_indices = np.array(self.num_T * [all_indices[:n]])
prog_mask[prog_indices] = 1
pred_masks[pred_indices] = 1
select_masks[select_indices] = 1
else:
assert 2*n*self.num_T <= int(self.dim_X)
prog_indices = all_indices[:n*self.num_T:self.num_T]
prog_mask[prog_indices] = 1
for i in range(self.num_T):
select_indices = all_indices[i*n: (i+1)*n]
pred_indices = all_indices[(i+self.num_T+1)*n: (i+self.num_T+2)*n]
pred_masks[pred_indices,i] = 1
select_masks[select_indices,i] = 1
elif self.feature_type_overlap == "sel_none":
prog_indices = all_indices[:n]
prog_mask[prog_indices] = 1
if self.treatment_feature_overlap:
assert 3*n <= int(self.dim_X)
pred_indices = np.array(self.num_T * [all_indices[n:2*n]])
select_indices = np.array(self.num_T * [all_indices[2*n:3*n]])
prog_mask[prog_indices] = 1
pred_masks[pred_indices] = 1
select_masks[select_indices] = 1
else:
#assert n+2*n*self.num_T <= int(self.dim_X)
for i in range(1,self.num_T+1):
select_indices = all_indices[i*n: (i+1)*n]
pred_indices = all_indices[(i+self.num_T)*n: (i+self.num_T+1)*n]
pred_masks[pred_indices,i-1] = 1
select_masks[select_indices,i-1] = 1
# # Handle case with feature overlap
# if self.feature_overlap:
# assert max(self.num_pred_features, self.num_prog_features, self.num_select_features) <= int(self.dim_X)
# prog_indices = all_indices[:self.num_prog_features]
# pred_indices = np.array(self.num_T * [all_indices[:self.num_pred_features]])
# select_indices = np.array(self.num_T * [all_indices[:self.num_select_features]])
# prog_mask[prog_indices] = 1
# pred_masks[pred_indices] = 1
# select_masks[select_indices] = 1
# # Handle case without feature overlap
# else:
# assert (self.num_prog_features + self.num_T * (self.num_pred_features + self.num_select_features)) <= int(self.dim_X)
# prog_indices = all_indices[:self.num_prog_features]
# prog_mask[prog_indices] = 1
# pred_indices = all_indices[self.num_prog_features : (self.num_prog_features + self.num_T*self.num_pred_features)]
# select_indices = all_indices[(self.num_prog_features + self.num_T*self.num_pred_features):(self.num_prog_features + self.num_T*(self.num_pred_features+self.num_select_features))]
# # Mask features for every treatment
# for i in range(self.num_T):
# pred_masks[pred_indices[i*self.num_pred_features:(i+1)*self.num_pred_features],i] = 1
# select_masks[select_indices[i*self.num_select_features:(i+1)*self.num_select_features],i] = 1
self.prog_mask = prog_mask
self.pred_masks = pred_masks
self.select_masks = select_masks
self.select_masks_pred = pred_masks.copy()
self.select_masks_prog = select_masks.copy()
log.debug(
f'\nCheck if important features are sampled correctly:'
f'\n==================================================================='
f'\nProg Indices'
f'\n{prog_indices}'
f'\n\nPred Indices'
f'\n{pred_indices}'
f'\n\nSelect Indices'
f'\n{select_indices}'
f'\n\nProg Mask'
f'\n{prog_mask}'
f'\n\nPred Masks'
f'\n{pred_masks}'
f'\n\nSelect Masks'
f'\n{select_masks}'
f'\n===================================================================\n'
)
return None
def sample_nonlinearities(self, num_nonlinearities: int):
"""
Sample non-linearities for each outcome.
"""
if self.nonlinearity_selection_type == "random":
# pick num_nonlinearities
return random.choices(population=self.nonlinear_fcts, k=num_nonlinearities)
else:
raise ValueError(f"Unknown nonlinearity selection type {self.selection_type}.")
class TSimulator(SimulatorBase):
"""
Data generation process class for simulating treatment selection only, when counterfactual outcomes are available (as for in-vitro/pharmacoscopy data).
"""
nonlinear_fcts = [
lambda x: np.abs(x),
lambda x: np.exp(-(x**2) / 2),
lambda x: 1 / (1 + x**2),
lambda x: np.cos(x),
lambda x: np.arctan(x),
lambda x: np.tanh(x),
lambda x: np.sin(x),
lambda x: np.log(1 + x**2),
lambda x: np.sqrt(1 + x**2),
lambda x: np.cosh(x),
]
def __init__(
self,
# Data dimensionality
dim_X: int,
# Seed
seed: int = 42,
# Simulation type
simulation_type: str = "T",
# Dimensionality of treatments and outcome
num_binary_outcome: int = 0,
standardize_outcome: bool = False,
standardize_per_outcome: bool = False,
num_T: int = 3,
dim_Y: int = 3,
# Scale parameters
propensity_scale: float = 1,
unbalancedness_exp: float = 0,
nonlinearity_scale: float = 1,
propensity_type: str = "prog_pred",
alpha: float = 0.5,
enforce_balancedness: bool = False,
# Important features
num_select_features: int = 5,
treatment_feature_overlap: bool = False,
# Feature selection
random_feature_selection: bool = True,
nonlinearity_selection_type: bool = True,
) -> None:
# Number of features
self.dim_X = dim_X
# Make sure results are reproducible by setting seed for np, torch, random
self.seed = seed
enable_reproducible_results(seed=self.seed)
# Simulation type
self.simulation_type = simulation_type
# Store dimensions
self.num_binary_outcome = num_binary_outcome
self.standardize_outcome = standardize_outcome
self.standardize_per_outcome = standardize_per_outcome
self.num_T = num_T
self.dim_Y = dim_Y
# Scale parameters
self.propensity_scale = propensity_scale
self.unbalancedness_exp = unbalancedness_exp
self.nonlinearity_scale = nonlinearity_scale
self.propensity_type = propensity_type
self.alpha = alpha
self.enforce_balancedness = enforce_balancedness
# Important features
self.num_select_features = num_select_features
self.treatment_feature_overlap = treatment_feature_overlap
self.num_important_features = num_select_features
# Feature selection
self.random_feature_selection = random_feature_selection
self.nonlinearity_selection_type = nonlinearity_selection_type
# Setup variables
self.nonlinearities = None
self.select_masks = None
self.select_weights = None
# Setup
self.setup()
# Simulation variables
self.X = None
self.select_scores = None
self.propensities, self.outcomes, self.T, self.Y = None, None, None, None
def get_simulated_data(self, train_ratio: float = 0.8):
"""
Extract results and split into training and test set. Include counterfactual outcomes and propensities.
"""
return self.X, self.T, self.Y, self.outcomes, self.propensities
# Split data
# train_size = int(train_ratio * self.X.shape[0])
# X_train, X_test = self.X[:train_size], self.X[train_size:]
# T_train, T_test = self.T[:train_size], self.T[train_size:]
# Y_train, Y_test = self.Y[:train_size], self.Y[train_size:]
# outcomes_train, outcomes_test = self.outcomes[:train_size,:,:], self.outcomes[train_size:,:,:]
# propensities_train, propensities_test = self.propensities[:train_size], self.propensities[train_size:]
# if train_ratio == 1:
# return self.X, self.T, self.Y, self.outcomes, self.propensities
# return X_train, X_test, T_train, T_test, Y_train, Y_test, outcomes_train, outcomes_test, propensities_train, propensities_test
def simulate(self, X, outcomes=None) -> Tuple:
"""
Simulate treatment and outcome for a dataset based on the configuration.
"""
log.debug(
f'Simulating treatment and outcome for a dataset with:'
f'\n==================================================================='
f'\nDim X: {self.dim_X}'
f'\nDim T: {self.num_T}'
f'\nDim Y: {self.dim_Y}'
f'\nPropensity Scale: {self.propensity_scale}'
f'\nUnbalancedness Exponent: {self.unbalancedness_exp}'
f'\nNonlinearity Scale: {self.nonlinearity_scale}'
f'\nNum Select Features: {self.num_select_features}'
f'\nFeature Overlap: {self.treatment_feature_overlap}'
f'\nRandom Feature Selection: {self.random_feature_selection}'
f'\nNonlinearity Selection Type: {self.nonlinearity_selection_type}'
f'\n===================================================================\n'
)
# 1. Store data
self.X = X
# 2. Compute scores for prognostic, predictive, and selective features
self.compute_scores()
# 3. Retrieve factual and counterfactual outcomes based on the data and the predictive and prognostic scores
self.outcomes = outcomes
assert self.outcomes.shape == (self.X.shape[0], self.num_T, self.dim_Y)
if self.standardize_outcome:
if self.standardize_per_outcome:
self.outcomes = zscore(self.outcomes, axis=0) #, axis=None) # add axis=None to make problem easier again
else:
self.outcomes = zscore(self.outcomes, axis=None) #, axis=None) # add axis=None to make problem easier again
log.debug(
f'\nCheck if outcomes are processed correctly:'
f'\n==================================================================='
f'\n\nOutcomes'
f'\n{self.outcomes}'
f'\n{self.outcomes.shape}'
f'\n\nMean Outcomes'
f'\n{self.outcomes.mean(axis=0)}'
f'\n\nVariance Outcomes'
f'\n{self.outcomes.var(axis=0)}'
f'\n===================================================================\n'
)
# 4. Compute propensities based on the data and the selective scores
self.compute_propensities()
# 5. Sample treatment assignment based on the propensities
self.sample_T()
# 6. Extract the outcome based on the treatment assignment
self.extract_Y()
return None
def setup(self) -> None:
"""
Setup the simulator by defining variables which remain the same across simulations with different samples but the same configuration.
"""
# 1. Sample nonlinearities used
num_nonlinearities = 1 # Same non-linearity for all treatment selection mechanisms
self.nonlinearities = self.sample_nonlinearities(num_nonlinearities)
# 2. Set important feature masks - determine which features should be used for treatment selection, outcome prediction
self.sample_important_feature_masks()
# 3. Sample weights for features
self.sample_uniform_weights()
def get_true_cates(self,
X: np.ndarray,
T: np.ndarray,
outcomes: np.ndarray) -> np.ndarray:
"""
Compute true CATEs for each treatment based on the data and the outcomes.
Always use the selected treatment as the base treatment.
"""
# Compute CATEs for each treatment
cates = np.zeros((X.shape[0], self.num_T, self.dim_Y))
for i in range(X.shape[0]):
for j in range(self.num_T):
cates[i,j,:] = outcomes[i,j,:] - outcomes[i,int(T[i]),:]
log.debug(
f'\nCheck if true CATEs are computed correctly:'
f'\n==================================================================='
f'\nOutcomes: {outcomes.shape}'
f'\n{outcomes}'
f'\n\nTreatment Assignment: {T.shape}'
f'\n{T}'
f'\n\nTrue CATEs: {cates.shape}'
f'\n{cates}'
f'\n===================================================================\n'
)
return cates
def extract_Y(self) -> None:
"""
Extract the outcome based on the treatment assignment.
"""
self.Y = self.outcomes[np.arange(self.X.shape[0]), self.T]
log.debug(
f'\nCheck if outcomes are extracted correctly:'
f'\n==================================================================='
f'\nOutcomes'
f'\n{self.outcomes}'
f'\n{self.outcomes.shape}'
f'\n\nTreatment Assignment'
f'\n{self.T}'
f'\n{self.T.shape}'
f'\n\nExtracted Outcomes'
f'\n{self.Y}'
f'\n{self.Y.shape}'
f'\n===================================================================\n'
)
return None
def sample_T(self) -> None:
"""
Sample treatment assignment based on the propensities.
"""
# Sample from the resulting categorical distribution per row
self.T = np.array([np.random.choice([tre for tre in range(self.propensities.shape[1])], p=row) for row in self.propensities])
log.debug(
f'\nCheck if treatment assignment is sampled correctly:'
f'\n==================================================================='
f'\nPropensities'
f'\n{self.propensities}'
f'\n{self.propensities.shape}'
f'\n\nTreatment Assignment'
f'\n{self.T}'
f'\n{self.T.shape}'
f'\n\nUnique Treatment Counts'
f'\n{np.unique(self.T, return_counts=True)}'
f'\n===================================================================\n'
)
return None
def get_unbalancedness_weights(self, size: int) -> np.ndarray:
"""
Create weights for introducing unbalancedness for class probabilities.
"""
# Sample initial distribution of treatment assignment
unb_weights = np.random.uniform(0, 1, size=size)
unb_weights = unb_weights / unb_weights.sum()
# Standardize the weights and make sure that a treatment doesn't completely disappear for small unbalancedness exponents
min_val = unb_weights.min()
range_val = unb_weights.max() - min_val
unb_weights = (unb_weights - min_val) / range_val
unb_weights = 0.01 + unb_weights * 0.98
return unb_weights
def compute_propensities(self) -> None:
"""
Compute propensities based on the data and the selective scores.
"""
select_scores_none = zscore(self.select_scores, axis=0) # Comment for Predictive Epertise
select_scores_pred = np.zeros((self.X.shape[0], self.num_T))
select_scores_pred_flipped = np.zeros((self.X.shape[0], self.num_T))
select_scores_prog = np.zeros((self.X.shape[0], self.num_T))
select_scores_tre = np.zeros((self.X.shape[0], self.num_T))
select_scores_pred[:,0] = self.outcomes[:,0,0] - self.outcomes[:,1,0]
select_scores_pred[:,1] = self.outcomes[:,1,0] - self.outcomes[:,0,0]
select_scores_pred_flipped[:,0] = self.outcomes[:,1,0] - self.outcomes[:,0,0]
select_scores_pred_flipped[:,1] = self.outcomes[:,0,0] - self.outcomes[:,1,0]
select_scores_prog[:,0] = self.outcomes[:,0,0]
select_scores_prog[:,1] = -self.outcomes[:,0,0]
select_scores_tre[:,0] = -self.outcomes[:,1,0]
select_scores_tre[:,1] = self.outcomes[:,1,0]
if self.propensity_type == "prog_tre":
scores = self.alpha * select_scores_tre + (1 - self.alpha) * select_scores_prog
# Standardize all scores
select_scores_pred = zscore(select_scores_pred, axis=0)
select_scores_pred_flipped = zscore(select_scores_pred_flipped, axis=0)
select_scores_prog = zscore(select_scores_prog, axis=0)
select_scores_tre = zscore(select_scores_tre, axis=0)
if self.propensity_type == "prog_pred":
scores = self.alpha * select_scores_pred + (1 - self.alpha) * select_scores_prog
elif self.propensity_type == "prog_tre":
pass
elif self.propensity_type == "none_prog":
scores = self.alpha * select_scores_prog + (1 - self.alpha) * select_scores_none
elif self.propensity_type == "none_pred":
scores = self.alpha * select_scores_pred + (1 - self.alpha) * select_scores_none
elif self.propensity_type == "none_tre":
scores = self.alpha * select_scores_tre + (1 - self.alpha) * select_scores_none
elif self.propensity_type == "none_pred_flipped":
scores = self.alpha * select_scores_pred_flipped + (1 - self.alpha) * select_scores_none
elif self.propensity_type == "pred_pred_flipped":
scores = self.alpha * select_scores_pred_flipped + (1 - self.alpha) * select_scores_pred
elif self.propensity_type == "rct_none":
scores = select_scores_none
else:
raise ValueError(f"Unknown propensity type {self.propensity_type}.")
if self.enforce_balancedness:
scores = zscore(scores, axis=0)
if self.propensity_type == "rct_none":
scores = self.alpha * select_scores_none
# Introduce unbalancedness and manipulate unbalancedness weights for comparable experiments with different seeds
unb_weights = self.get_unbalancedness_weights(size=scores.shape[1])
# Apply the softmax function to each row to get probabilities
p = softmax(self.propensity_scale*scores, axis=1)
# Scale probabilities to introduce unbalancedness
p = p * (1 - unb_weights) ** self.unbalancedness_exp
# Make sure rows add up to one again
row_sums = p.sum(axis=1, keepdims=True)
p = p / row_sums
self.propensities = p
log.debug(
f'\nCheck if propensities are computed correctly:'
f'\n==================================================================='
f'\nSelect Scores'
f'\n{self.select_scores}'
f'\n{self.select_scores.shape}'
f'\n\nPropensities'
f'\n{self.propensities}'
f'\n{self.propensities.shape}'
f'\n===================================================================\n'
)
return None
def compute_scores(self) -> None:
"""
Compute scores for prognostic, predictive, and selective features based on the data and the feature weights.
"""
# Each column of the score matrix corresponds to the score for a specific outcome. Rows correspond to samples.
select_lin = self.X @ self.select_weights.T
log.debug(
f'\nCheck if linear scores are computed correctly for selective features:'
f'\n==================================================================='
f'\nself.X'
f'\n{self.X}'
f'\n{self.X.shape}'
f'\n\nSelect Weights'
f'\n{self.select_weights}'
f'\n{self.select_weights.shape}'
f'\n\nSelect Lin'
f'\n{select_lin}'
f'\n{select_lin.shape}'
f'\n===================================================================\n'
)
# Introduce non-linearity and get final scores
select_scores = (1 - self.nonlinearity_scale) * select_lin + self.nonlinearity_scale * self.nonlinearities[0](select_lin)
self.select_scores = select_scores
return None
@property
def weights(self) -> Tuple:
"""
Return weights for prognostic, predictive, and selective features.
"""
return None, None, self.select_weights
def sample_uniform_weights(self) -> None:
"""
sample uniform weights for the features.
"""
# Sample weights for selective features, a weight for every dimension for every treatment and outcome
select_weights = np.random.uniform(-1, 1, size=(self.num_T, self.dim_X))
# Mask weights for features that are not important
for i in range(self.num_T):
select_weights[i] = select_weights[i] * self.select_masks[:,i]
log.debug(
f'\nCheck if masks are applied correctly:'
f'\n==================================================================='
f'\nSelect Weights'
f'\n{select_weights}'
f'\n{select_weights.shape}'
f'\n\nSelect Masks'
f'\n{self.select_masks}'
f'\n{self.select_masks.shape}'
f'\n===================================================================\n'
)
self.select_weights = select_weights
return None
@property
def all_important_features(self) -> np.ndarray:
"""
Return all important feature indices.
"""
all_important_features = self.selective_features
log.debug(
f'\nCheck if all important features are computed correctly:'
f'\n==================================================================='
f'\n\nSelect Features'
f'\n{self.selective_features}'
f'\n\nAll Important Features'
f'\n{all_important_features}'
f'\n===================================================================\n'
)
return all_important_features
@property
def predictive_features(self) -> np.ndarray:
"""
Return predictive feature indices.
"""
return None
@property
def prognostic_features(self) -> np.ndarray:
"""
Return prognostic feature indices.
"""
return None
@property
def selective_features(self) -> np.ndarray:
"""
Return selective feature indices.
"""
select_features = np.where((self.select_masks.sum(axis=1)).astype(np.int32) != 0)
return select_features
def sample_important_feature_masks(self) -> None:
"""
Pick features that are important for treatment selection based on the configuration.
"""
# Get indices for features and shuffle if random_feature_selection is True
all_indices = np.arange(self.dim_X)
if self.random_feature_selection:
np.random.shuffle(all_indices)
# Initialize masks
select_masks = np.zeros(shape=(self.dim_X, self.num_T))
# Handle case with feature overlap
if self.treatment_feature_overlap:
assert self.num_select_features <= int(self.dim_X)
select_indices = np.array(self.num_T * [all_indices[:self.num_select_features]])
select_masks[select_indices] = 1
# Handle case without feature overlap
else:
assert (self.num_T * self.num_select_features) <= int(self.dim_X)
select_indices = all_indices[:self.num_select_features*self.num_T]
# Mask features for every treatment
for i in range(self.num_T):
select_masks[select_indices[i*self.num_select_features:(i+1)*self.num_select_features],i] = 1
self.select_masks = select_masks
return None
def sample_nonlinearities(self, num_nonlinearities: int):
"""
Sample non-linearities for each outcome.
"""
if self.nonlinearity_selection_type == "random":
# pick num_nonlinearities
return random.choices(population=self.nonlinear_fcts, k=num_nonlinearities)
else:
raise ValueError(f"Unknown nonlinearity selection type {self.selection_type}.")