-
Notifications
You must be signed in to change notification settings - Fork 11
/
estimators_base.py
1011 lines (780 loc) · 36.3 KB
/
estimators_base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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) 2023, Haruka Kiyohara, Ren Kishimoto, HAKUHODO Technologies Inc., and Hanjuku-kaso Co., Ltd. All rights reserved.
# Licensed under the Apache 2.0 License.
"""Abstract base class for Off-Policy Estimator."""
from abc import ABCMeta, abstractmethod
from dataclasses import dataclass
from typing import Callable, Optional, Tuple, Union, Dict, List
import numpy as np
from d3rlpy.preprocessing import ActionScaler
from ..utils import (
gaussian_kernel,
epanechnikov_kernel,
triangular_kernel,
cosine_kernel,
uniform_kernel,
estimate_confidence_interval_by_bootstrap,
estimate_confidence_interval_by_hoeffding,
estimate_confidence_interval_by_empirical_bernstein,
estimate_confidence_interval_by_t_test,
)
@dataclass
class BaseOffPolicyEstimator(metaclass=ABCMeta):
"""Base class for (basic) OPE estimators.
Imported as: :class:`scope_rl.ope.BaseOffPolicyEstimator`
Note
-------
This abstract base class also implements the following private methods.
*abstract* _estimate_trajectory_value:
Estimate the trajectory-wise expected reward.
_calc_behavior_policy_pscore_discrete:
Calculate the behavior policy pscore (action choice probability) in the case of discrete action spaces.
_calc_behavior_policy_pscore_continuous:
Calculate the behavior policy pscore (action choice probability) in the case of continuous action spaces.
_calc_evaluation_policy_pscore_discrete:
Calculate the evaluation policy pscore (action choice probability) in the case of discrete action spaces.
_calc_similarity_weight:
Calculate the similarity weight (for continuous action case) in the case of continuous action spaces.
*property* _estimate_confidence_interval:
Dictionary containing names and functions of ci methods.
.. code-block:: python
key: [
bootstrap,
hoeffding,
bernstein,
ttest,
]
*property* _kernel_function:
Dictionary containing names and functions of kernels.
.. code-block:: python
key: [
gaussian,
epanechnikov,
triangular,
cosine,
uniform,
]
"""
@abstractmethod
def _estimate_trajectory_value(self) -> np.ndarray:
"""Estimate the trajectory-wise expected reward."""
raise NotImplementedError
@abstractmethod
def estimate_policy_value(self) -> float:
"""Estimate the policy value of the evaluation policy."""
raise NotImplementedError
@abstractmethod
def estimate_interval(self) -> Dict[str, float]:
"""Estimate the confidence interval of the policy value."""
raise NotImplementedError
@property
def _estimate_confidence_interval(self) -> Dict[str, Callable]:
"""Dictionary containing names and functions of ci methods."""
return {
"bootstrap": estimate_confidence_interval_by_bootstrap,
"hoeffding": estimate_confidence_interval_by_hoeffding,
"bernstein": estimate_confidence_interval_by_empirical_bernstein,
"ttest": estimate_confidence_interval_by_t_test,
}
@property
def _kernel_function(self) -> Dict[str, Callable]:
"""Dictionary containing names and functions of kernels."""
return {
"gaussian": gaussian_kernel,
"epanechnikov": epanechnikov_kernel,
"triangular": triangular_kernel,
"cosine": cosine_kernel,
"uniform": uniform_kernel,
}
def _calc_behavior_policy_pscore_discrete(
self,
step_per_trajectory: int,
pscore: np.ndarray,
pscore_type: str,
):
"""Calculate the behavior policy pscore (action choice probability).
Parameters
-------
step_per_trajectory: int (> 0)
Number of timesteps in an episode.
pscore: array-like of shape (n_trajectories * step_per_trajectory, )
Conditional action choice probability of the behavior policy,
i.e., :math:`\\pi_0(a \\mid s)`
pscore_type: {"trajectory-wise", "step-wise"}
Indicates whether to return trajectory-wise pscore or step-wise pscore.
Return
-------
behavior_policy_trajectory_wise_pscore: array-like of shape (n_trajectories, step_per_trajectory)
Trajectory-wise action choice probability of the behavior policy,
i.e., :math:`\\prod_{t=0}^T \\pi_0(a_t \\mid s_t)`
behavior_policy_step_wise_pscore: array-like of shape (n_trajectories, step_per_trajectory)
Step-wise action choice probability of the behavior policy,
i.e., :math:`\\prod_{t'=0}^t \\pi_0(a_{t'} \\mid s_{t'})`
"""
pscore = pscore.reshape((-1, step_per_trajectory))
# step-wise pscore
behavior_policy_pscore = np.cumprod(pscore, axis=1)
if pscore_type == "trajectory_wise":
behavior_policy_pscore = np.tile(
behavior_policy_pscore[:, -1], (step_per_trajectory, 1)
).T
return behavior_policy_pscore
def _calc_behavior_policy_pscore_continuous(
self,
step_per_trajectory: int,
pscore: np.ndarray,
pscore_type: str,
):
"""Calculate the behavior policy pscore (action choice probability).
Parameters
-------
step_per_trajectory: int (> 0)
Number of timesteps in an episode.
pscore: array-like of shape (n_trajectories * step_per_trajectory, action_dim)
Conditional action choice probability of the behavior policy,
i.e., :math:`\\pi_0(a \\mid s)`
pscore_type: {"trajectory-wise", "step-wise"}
Indicates whether to return trajectory-wise pscore or step-wise pscore.
Return
-------
behavior_policy_trajectory_wise_pscore: array-like of shape (n_trajectories, step_per_trajectory)
Trajectory-wise action choice probability of the behavior policy,
i.e., :math:`\\prod_{t=0}^T \\pi_0(a_t \\mid s_t)`
behavior_policy_step_wise_pscore: array-like of shape (n_trajectories, step_per_trajectory)
Step-wise action choice probability of the behavior policy,
i.e., :math:`\\prod_{t'=0}^t \\pi_0(a_{t'} \\mid s_{t'})`
"""
action_dim = pscore.shape[1]
pscore = pscore.reshape((-1, step_per_trajectory, action_dim))
# joint probability
pscore = np.prod(pscore, axis=2)
# step-wise pscore
behavior_policy_pscore = np.cumprod(pscore, axis=1)
if pscore_type == "trajectory_wise":
behavior_policy_pscore = np.tile(
behavior_policy_pscore[:, -1], (step_per_trajectory, 1)
).T
return behavior_policy_pscore
def _calc_evaluation_policy_pscore_discrete(
self,
step_per_trajectory: int,
action: np.ndarray,
evaluation_policy_action_dist: np.ndarray,
pscore_type: str,
):
"""Calculate the evaluation policy pscore (action choice probability).
Parameters
-------
step_per_trajectory: int (> 0)
Number of timesteps in an episode.
action: array-like of shape (n_trajectories * step_per_trajectory, )
Action chosen by the behavior policy.
evaluation_policy_action_dist: array-like of shape (n_trajectories * step_per_trajectory, n_action)
Conditional action distribution induced by the evaluation policy,
i.e., :math:`\\pi(a \\mid s_t) \\forall a \\in \\mathcal{A}`
pscore_type: {"trajectory-wise", "step-wise"}
Indicates whether to return trajectory-wise pscore or step-wise pscore.
Return
-------
evaluation_policy_trajectory_wise_pscore: array-like of shape (n_trajectories, step_per_trajectory)
Trajectory-wise action choice probability of the evaluation policy,
i.e., :math:`\\prod_{t=0}^T \\pi(a_t \\mid s_t)`
evaluation_policy_step_wise_pscore: array-like of shape (n_trajectories, step_per_trajectory)
Step-wise action choice probability of the evaluation policy,
i.e., :math:`\\prod_{t'=0}^t \\pi(a_{t'} \\mid s_{t'})`
"""
evaluation_policy_pscore = evaluation_policy_action_dist[
np.arange(len(action)), action
].reshape((-1, step_per_trajectory))
# step-wise pscore
evaluation_policy_pscore = np.cumprod(evaluation_policy_pscore, axis=1)
if pscore_type == "trajectory_wise":
evaluation_policy_pscore = np.tile(
evaluation_policy_pscore[:, -1], (step_per_trajectory, 1)
).T
return evaluation_policy_pscore
def _calc_similarity_weight(
self,
step_per_trajectory: int,
action: np.ndarray,
evaluation_policy_action: np.ndarray,
pscore_type: str,
kernel: str = "gaussian",
bandwidth: float = 1.0,
action_scaler: Optional[ActionScaler] = None,
):
"""Calculate the similarity weight.
Parameters
-------
step_per_trajectory: int (> 0)
Number of timesteps in an episode.
action: array-like of shape (n_trajectories * step_per_trajectory, action_dim)
Action chosen by the behavior policy.
evaluation_policy_action: array-like of shape (n_trajectories * step_per_trajectory, action_dim)
Action chosen by the evaluation policy.
pscore_type: {"trajectory-wise", "step-wise"}
Indicates whether to return trajectory-wise pscore or step-wise pscore.
kernel: {"gaussian", "epanechnikov", "triangular", "cosine", "uniform"}
Name of the kernel function to smooth importance weights.
bandwidth: float, default=1.0 (> 0)
Bandwidth hyperparameter of the kernel function.
action_scaler: d3rlpy.preprocessing.ActionScaler, default=None
Scaling factor of action.
Return
-------
trajectory_wise_similarity_weight: ndarray of shape (n_trajectories, step_per_trajectory)
Trajectory-wise similarity weight between the action chosen by the behavior policy and that chosen by the evaluation policy,
i.e., :math:`\\prod_{t'=0}^{T-1} K(\\pi(s_t), a_t)` where :math:`K(\\cdot, \\cdot)` is a kernel function.
step_wise_similarity_weight: ndarray of shape (n_trajectories, step_per_trajectory)
Step-wise similarity weight between the action chosen by the behavior policy and that chosen by the evaluation policy,
i.e., :math:`\\prod_{t'=0}^t K(\\pi(s_t), a_t)` where :math:`K(\\cdot, \\cdot)` is a kernel function.
"""
if action_scaler is not None:
evaluation_policy_action = action_scaler.transform_numpy(
evaluation_policy_action
)
action = action_scaler.transform_numpy(action)
similarity_weight = self._kernel_function[kernel](
evaluation_policy_action,
action,
bandwidth=bandwidth,
).reshape((-1, step_per_trajectory))
similarity_weight = np.cumprod(similarity_weight, axis=1)
if pscore_type == "trajectory_wise":
similarity_weight = np.tile(
similarity_weight[:, -1], (step_per_trajectory, 1)
).T
return similarity_weight
@dataclass
class BaseMarginalOPEEstimator(BaseOffPolicyEstimator):
"""Base class for OPE estimators with marginal importance sampling.
Bases: :class:`scope_rl.ope.BaseOffPolicyEstimator`
Imported as: :class:`scope_rl.ope.estimators_base.BaseMarginalOPEEstimator`
Note
-------
This abstract base class also implements the following private methods.
*abstract* _estimate_trajectory_value:
Estimate the trajectory-wise expected reward.
_calc_behavior_policy_pscore_discrete:
Calculate the behavior policy pscore (action choice probability) in the case of discrete action spaces.
_calc_behavior_policy_pscore_continuous:
Calculate the behavior policy pscore (action choice probability) in the case of continuous action spaces.
_calc_evaluation_policy_pscore_discrete:
Calculate the evaluation policy pscore (action choice probability) in the case of discrete action spaces.
_calc_similarity_weight:
Calculate the similarity weight (for continuous action case) in the case of continuous action spaces.
_calc_marginal_importance_weight(self):
Calculate the marginal importance weight.
(Specified either in :class:`BaseStateMarginalOffPolicyEstimator` or :class:`BaseStateActionMarginalOffPolicyEstimator`)
*property* _estimate_confidence_interval:
Dictionary containing names and functions of ci methods.
.. code-block:: python
key: [
bootstrap,
hoeffding,
bernstein,
ttest,
]
"""
def _calc_behavior_policy_pscore_discrete(
self,
n_step_pdis: int,
step_per_trajectory: int,
pscore: np.ndarray,
):
"""Calculate the behavior policy pscore (action choice probability).
Parameters
-------
n_step_pdis: int (> 0)
Number of previous steps considered when defining the step-wise importance weight.
step_per_trajectory: int (> 0)
Number of timesteps in an episode.
pscore: array-like of shape (n_trajectories * step_per_trajectory, )
Conditional action choice probability of the behavior policy,
i.e., :math:`\\pi_0(a \\mid s)`
Return
-------
behavior_policy_pscore: array-like of shape (n_trajectories, step_per_trajectory)
Step-wise action choice probability of the behavior policy,
i.e., :math:`\\prod_{t'=0}^t \\pi_0(a_{t'} \\mid s_{t'})`
(adjusted by n_step_pdis)
"""
pscore = pscore.reshape((-1, step_per_trajectory))
n_step_pscore = np.zeros_like(pscore)
for t in range(step_per_trajectory):
start_id = max(t - n_step_pdis + 1, 0)
n_step_pscore[:, t] = pscore[:, start_id : t + 1].prod(axis=1)
return n_step_pscore
def _calc_behavior_policy_pscore_continuous(
self,
n_step_pdis: int,
step_per_trajectory: int,
pscore: np.ndarray,
):
"""Calculate the behavior policy pscore (action choice probability).
Parameters
-------
n_step_pdis: int (> 0)
Number of previous steps considered when defining the step-wise importance weight.
step_per_trajectory: int (> 0)
Number of timesteps in an episode.
pscore: array-like of shape (n_trajectories * step_per_trajectory, action_dim)
Conditional action choice probability of the behavior policy,
i.e., :math:`\\pi_0(a \\mid s)`
Return
-------
behavior_policy_pscore: array-like of shape (n_trajectories, step_per_trajectory)
Step-wise action choice probability of the behavior policy,
i.e., :math:`\\prod_{t'=0}^t \\pi_0(a_{t'} \\mid s_{t'})`
(adjusted by n_step_pdis)
"""
pscore = pscore.prod(axis=1)
pscore = pscore.reshape((-1, step_per_trajectory))
n_step_pscore = np.zeros_like(pscore)
for t in range(step_per_trajectory):
start_id = max(t - n_step_pdis + 1, 0)
n_step_pscore[:, t] = pscore[:, start_id : t + 1].prod(axis=1)
return n_step_pscore
def _calc_evaluation_policy_pscore_discrete(
self,
n_step_pdis: int,
step_per_trajectory: int,
action: np.ndarray,
evaluation_policy_action_dist: np.ndarray,
):
"""Calculate the evaluation policy pscore (action choice probability).
Parameters
-------
n_step_pdis: int (> 0)
Number of previous steps considered when defining the step-wise importance weight.
step_per_trajectory: int (> 0)
Number of timesteps in an episode.
action: array-like of shape (n_trajectories * step_per_trajectory, )
Action chosen by the behavior policy.
evaluation_policy_action_dist: array-like of shape (n_trajectories * step_per_trajectory, n_action)
Conditional action distribution induced by the evaluation policy,
i.e., :math:`\\pi(a \\mid s_t) \\forall a \\in \\mathcal{A}`
Return
-------
evaluation_policy_pscore: array-like of shape (n_trajectories, step_per_trajectory)
Step-wise action choice probability of the evaluation policy,
i.e., :math:`\\prod_{t'=0}^t \\pi(a_{t'} \\mid s_{t'})`
(adjusted by n_step_pdis)
"""
evaluation_policy_pscore = evaluation_policy_action_dist[
np.arange(len(action)), action
].reshape((-1, step_per_trajectory))
n_step_pscore = np.zeros_like(evaluation_policy_pscore)
for t in range(step_per_trajectory):
start_id = max(t - n_step_pdis + 1, 0)
n_step_pscore[:, t] = evaluation_policy_pscore[:, start_id : t + 1].prod(
axis=1
)
return n_step_pscore
def _calc_similarity_weight(
self,
n_step_pdis: int,
step_per_trajectory: int,
action: np.ndarray,
evaluation_policy_action: np.ndarray,
kernel: str = "gaussian",
bandwidth: float = 1.0,
action_scaler: Optional[ActionScaler] = None,
):
"""Calculate the similarity weight.
Parameters
-------
n_step_pdis: int (> 0)
Number of previous steps considered when defining the step-wise importance weight.
step_per_trajectory: int (> 0)
Number of timesteps in an episode.
action: array-like of shape (n_trajectories * step_per_trajectory, action_dim)
Action chosen by the behavior policy.
evaluation_policy_action: array-like of shape (n_trajectories * step_per_trajectory, action_dim)
Action chosen by the evaluation policy.
kernel: {"gaussian", "epanechnikov", "triangular", "cosine", "uniform"}
Name of the kernel function to smooth importance weights.
bandwidth: float, default=1.0 (> 0)
Bandwidth hyperparameter of the kernel.
action_scaler: d3rlpy.preprocessing.ActionScaler, default=None
Scaling factor of action.
Return
-------
similarity_weight: ndarray of shape (n_trajectories, step_per_trajectory)
Similarity weight between the action chosen by the behavior policy and that chosen by the evaluation policy,
i.e., :math:`\\prod_{t'=0}^t K(\\pi(s_t), a_t)` where :math:`K(\\cdot, \\cdot)` is a kernel function.
(adjusted by n_step_pdis)
"""
if action_scaler is not None:
evaluation_policy_action = action_scaler.transform_numpy(
evaluation_policy_action
)
action = action_scaler.transform_numpy(action)
similarity_weight = self._kernel_function[kernel](
evaluation_policy_action,
action,
bandwidth=bandwidth,
).reshape((-1, step_per_trajectory))
n_step_similarity_weight = np.zeros_like(similarity_weight)
for t in range(step_per_trajectory):
start_id = max(t - n_step_pdis + 1, 0)
n_step_similarity_weight[:, t] = similarity_weight[
:, start_id : t + 1
].prod(axis=1)
return n_step_similarity_weight
def _calc_marginal_importance_weight(self):
"""Calculate the marginal importance weight."""
raise NotImplementedError
@dataclass
class BaseStateMarginalOPEEstimator(BaseMarginalOPEEstimator):
"""Base class for State Marginal OPE estimators.
Bases: :class:`scope_rl.ope.BaseMarginalOPEEstimator` -> :class:`scope_rl.ope.BaseOffPolicyEstimator`
Imported as: :class:`scope_rl.ope.BaseStateMarginalOPEEstimator`
Note
-------
This abstract base class also implements the following private methods.
*abstract* _estimate_trajectory_value:
Estimate the trajectory-wise expected reward.
_calc_behavior_policy_pscore_discrete:
Calculate the behavior policy pscore (action choice probability) in the case of discrete action spaces.
_calc_behavior_policy_pscore_continuous:
Calculate the behavior policy pscore (action choice probability) in the case of continuous action spaces.
_calc_evaluation_policy_pscore_discrete:
Calculate the evaluation policy pscore (action choice probability) in the case of discrete action spaces.
_calc_similarity_weight:
Calculate the similarity weight (for continuous action case) in the case of continuous action spaces.
_calc_marginal_importance_weight(self):
Calculate the marginal importance weight.
*property* _estimate_confidence_interval:
Dictionary containing names and functions of ci methods.
.. code-block:: python
key: [
bootstrap,
hoeffding,
bernstein,
ttest,
]
"""
def _calc_marginal_importance_weight(
self,
n_step_pdis: int,
step_per_trajectory: int,
state_marginal_importance_weight: np.ndarray,
):
"""Calculate the marginal importance weight.
Parameters
-------
n_step_pdis: int (> 0)
Number of previous steps considered when defining the step-wise importance weight.
step_per_trajectory: int (> 0)
Number of timesteps in an episode.
state_marginal_importance_weight: array-like of shape (n_trajectories * step_per_trajectory, )
Importance weight wrt the state marginal distribution, i.e., :math:`d_{\\pi}(s) / d_{\\pi_b}(s)`
Return
-------
state_marginal_importance_weight: ndarray of shape (n_trajectories, step_per_trajectory)
Marginal importance weight adjusted by n_step_pdis.
"""
state_marginal_importance_weight = state_marginal_importance_weight.reshape(
(-1, step_per_trajectory)
)
state_marginal_importance_weight = np.roll(
state_marginal_importance_weight, n_step_pdis, axis=1
)
state_marginal_importance_weight[:, :n_step_pdis] = 1
return state_marginal_importance_weight
@dataclass
class BaseStateActionMarginalOPEEstimator(BaseMarginalOPEEstimator):
"""Base class for State-Action Marginal OPE estimators.
Bases: :class:`scope_rl.ope.BaseMarginalOPEEstimator` -> :class:`scope_rl.ope.BaseOffPolicyEstimator`
Imported as: :class:`scope_rl.ope.BaseStateActionMarginalOPEEstimator`
Note
-------
This abstract base class also implements the following private methods.
*abstract* _estimate_trajectory_value:
Estimate the trajectory-wise expected reward.
_calc_behavior_policy_pscore_discrete:
Calculate the behavior policy pscore (action choice probability) in the case of discrete action spaces.
_calc_behavior_policy_pscore_continuous:
Calculate the behavior policy pscore (action choice probability) in the case of continuous action spaces.
_calc_evaluation_policy_pscore_discrete:
Calculate the evaluation policy pscore (action choice probability) in the case of discrete action spaces.
_calc_similarity_weight:
Calculate the similarity weight (for continuous action case) in the case of continuous action spaces.
_calc_marginal_importance_weight(self):
Calculate the marginal importance weight.
*property* _estimate_confidence_interval:
Dictionary containing names and functions of ci methods.
.. code-block:: python
key: [
bootstrap,
hoeffding,
bernstein,
ttest,
]
"""
def _calc_marginal_importance_weight(
self,
n_step_pdis: int,
step_per_trajectory: int,
state_action_marginal_importance_weight: np.ndarray,
):
"""Calculate the marginal importance weight.
Parameters
-------
n_step_pdis: int (> 0)
Number of previous steps considered when defining the step-wise importance weight.
step_per_trajectory: int (> 0)
Number of timesteps in an episode.
state_action_marginal_importance_weight: array-like of shape (n_trajectories * step_per_trajectory, )
Importance weight wrt the state-action marginal distribution, i.e., :math:`d_{\\pi}(s, a) / d_{\\pi_b}(s, a)`
Return
-------
state_action_marginal_importance_weight: ndarray of shape (n_trajectories, step_per_trajectory)
Marginal importance weight adjusted by n_step_pdis.
"""
state_action_marginal_importance_weight = (
state_action_marginal_importance_weight.reshape((-1, step_per_trajectory))
)
state_action_marginal_importance_weight = np.roll(
state_action_marginal_importance_weight, n_step_pdis, axis=1
)
state_action_marginal_importance_weight[:, :n_step_pdis] = 1
return state_action_marginal_importance_weight
@dataclass
class BaseCumulativeDistributionOPEEstimator(metaclass=ABCMeta):
"""Base class for Cumulative Distribution OPE estimators.
Imported as: :class:`scope_rl.ope.BaseCumulativeDistributionOPEEstimator`
Note
-------
This abstract base class also implements the following private methods.
_aggregate_trajectory_wise_statistics_discrete:
Calculate trajectory-wise summary statistics based on step-wise observations in the case of discrete action spaces.
_aggregate_trajectory_wise_statistics_continuous:
Calculate trajectory-wise summary statistics based on step-wise observations in the case of continuous action spaces.
_target_value_given_idx:
Obtain the reward value corresponding to the given idx when estimating the CDF.
*property* _kernel_function:
Dictionary containing names and functions of kernels.
.. code-block:: python
key: [
gaussian,
epanechnikov,
triangular,
cosine,
uniform,
]
"""
@abstractmethod
def estimate_cumulative_distribution_function(self) -> Tuple[np.ndarray]:
"""Estimate the cumulative distribution function (CDF) of the policy value."""
raise NotImplementedError
@abstractmethod
def estimate_mean(self) -> float:
"""Estimate the mean of the reward under the evaluation policy."""
raise NotImplementedError
@abstractmethod
def estimate_variance(self) -> float:
"""Estimate the variance of the reward under the evaluation policy."""
raise NotImplementedError
@abstractmethod
def estimate_conditional_value_at_risk(self) -> float:
"""Estimate the conditional value at risk (CVaR) of the reward under the evaluation policy."""
raise NotImplementedError
@abstractmethod
def estimate_interquartile_range(self) -> Dict[str, float]:
"""Estimate the interquartile range of the reward under the evaluation policy."""
raise NotImplementedError
@property
def _kernel_function(self) -> Dict[str, Callable]:
"""Dictionary containing names and functions of kernels."""
return {
"gaussian": gaussian_kernel,
"epanechnikov": epanechnikov_kernel,
"triangular": triangular_kernel,
"cosine": cosine_kernel,
"uniform": uniform_kernel,
}
def _target_value_given_idx(
self, idx_: Union[List[int], int], reward_scale: np.ndarray
):
"""Obtain the reward value corresponding to the given idx when estimating the CDF.
Parameters
-------
idx_: list of int or int
Indicating index. When list is given, the average of the two will be returned.
reward_scale: array-like of shape (n_partition, )
Scale of the trajectory-wise reward used for x-axis of the CDF plot.
Return
-------
target_value: float
Value of the given index.
"""
if len(idx_) == 0 or idx_[0] == len(reward_scale) - 1:
target_value = reward_scale[-1]
else:
target_idx = idx_[0]
target_value = (reward_scale[target_idx] + reward_scale[target_idx + 1]) / 2
return target_value
def _aggregate_trajectory_wise_statistics_discrete(
self,
step_per_trajectory: int,
action: Optional[np.ndarray] = None,
reward: Optional[np.ndarray] = None,
pscore: Optional[np.ndarray] = None,
evaluation_policy_action_dist: Optional[np.ndarray] = None,
state_action_value_prediction: Optional[np.ndarray] = None,
gamma: float = 1.0,
):
"""Calculate trajectory-wise summary statistics based on step-wise observations in the case of discrete action spaces.
Parameters
-------
step_per_trajectory: int (> 0)
Number of timesteps in an episode.
action: array-like of shape (n_trajectories * step_per_trajectory, )
Action chosen by the behavior policy.
reward: ndarray of shape (n_trajectories * step_per_trajectory, )
Observed immediate rewards.
pscore: array-like of shape (n_trajectories * step_per_trajectory, )
Conditional action choice probability of the behavior policy,
i.e., :math:`\\pi_0(a \\mid s)`
evaluation_policy_action_dist: array-like of shape (n_trajectories * step_per_trajectory, n_action)
Conditional action distribution induced by the evaluation policy,
i.e., :math:`\\pi(a \\mid s_t) \\forall a \\in \\mathcal{A}`
state_action_value_prediction: array-like of shape (n_trajectories * step_per_trajectory, n_action)
:math:`\\hat{Q}` for all actions, i.e., :math:`\\hat{Q}(s_t, a) \\forall a \\in \\mathcal{A}`.
gamma: float, default=1.0
Discount factor. The value should be within (0, 1].
Return
-------
trajectory_wise_reward: ndarray of shape (n_trajectories, )
Trajectory-wise reward observed under the behavior policy.
trajectory_wise_importance_weight: ndarray of shape (n_trajectories, )
Trajectory-wise importance weight.
initial_state_value_prediction: ndarray of shape (n_trajectories, )
Estimated initial state value.
"""
trajectory_wise_importance_weight = None
trajectory_wise_reward = None
initial_state_value_prediction = None
if reward is not None:
reward = reward.reshape((-1, step_per_trajectory))
discount = np.full(reward.shape[1], gamma).cumprod()
trajectory_wise_reward = (reward * discount).sum(axis=1)
if (
action is not None
and pscore is not None
and evaluation_policy_action_dist is not None
):
pscore = pscore.reshape((-1, step_per_trajectory))
behavior_policy_pscore = np.cumprod(pscore, axis=1)[:, -1]
evaluation_policy_pscore = evaluation_policy_action_dist[
np.arange(len(action)), action
].reshape((-1, step_per_trajectory))
evaluation_policy_pscore = np.cumprod(evaluation_policy_pscore, axis=1)[
:, -1
]
trajectory_wise_importance_weight = (
evaluation_policy_pscore / behavior_policy_pscore
)
if (
evaluation_policy_action_dist is not None
and state_action_value_prediction is not None
):
initial_state_value_prediction = (
(state_action_value_prediction * evaluation_policy_action_dist)
.sum(axis=1)
.reshape((-1, step_per_trajectory))[:, 0]
)
return (
trajectory_wise_reward,
trajectory_wise_importance_weight,
initial_state_value_prediction,
)
def _aggregate_trajectory_wise_statistics_continuous(
self,
step_per_trajectory: int,
action: Optional[np.ndarray] = None,
reward: Optional[np.ndarray] = None,
pscore: Optional[np.ndarray] = None,
evaluation_policy_action: Optional[np.ndarray] = None,
state_action_value_prediction: Optional[np.ndarray] = None,
gamma: float = 1.0,
kernel: str = "gaussian",
bandwidth: float = 1.0,
action_scaler: Optional[ActionScaler] = None,
):
"""Calculate trajectory-wise summary statistics based on step-wise observations in the case of continuous action spaces.
Parameters
-------
step_per_trajectory: int (> 0)
Number of timesteps in an episode.
action: ndarray of shape (n_trajectories * step_per_trajectory, action_dim)
Action chosen by the behavior policy.
reward: ndarray of shape (n_trajectories * step_per_trajectory, )
Observed immediate rewards.
pscore: array-like of shape (n_trajectories * step_per_trajectory, action_dim)
Conditional action choice probability of the behavior policy,
i.e., :math:`\\pi_0(a \\mid s)`
evaluation_policy_action: array-like of shape (n_trajectories * step_per_trajectory, action_dim)
Action chosen by the evaluation policy.
state_action_value_prediction: array-like of shape (n_trajectories * step_per_trajectory, 2)
:math:`\\hat{Q}` for the observed action and that chosen by the evaluation policy,
i.e., (row 0) :math:`\\hat{Q}(s_t, a_t)` and (row 2) :math:`\\hat{Q}(s_t, \\pi(a \\mid s_t))`.
gamma: float, default=1.0
Discount factor. The value should be within (0, 1].
kernel: {"gaussian", "epanechnikov", "triangular", "cosine", "uniform"}
Name of the kernel function to smooth importance weights.
bandwidth: float, default=1.0 (> 0)
Bandwidth hyperparameter of the kernel.
action_scaler: d3rlpy.preprocessing.ActionScaler, default=None
Scaling factor of action.
Return
-------
trajectory_wise_reward: ndarray of shape (n_trajectories, )
Trajectory-wise reward observed under the behavior policy.
trajectory_wise_importance_weight: ndarray of shape (n_trajectories, )
Trajectory-wise importance weight.
initial_state_value_prediction: ndarray of shape (n_trajectories, )
Estimated initial state value.
"""
trajectory_wise_importance_weight = None
trajectory_wise_reward = None
initial_state_value_prediction = None
if reward is not None:
reward = reward.reshape((-1, step_per_trajectory))
discount = np.full(reward.shape[1], gamma).cumprod()
trajectory_wise_reward = (reward * discount).sum(axis=1)
if (
action is not None
and pscore is not None
and evaluation_policy_action is not None
):
action_dim = action.shape[1]
pscore = pscore.reshape((-1, step_per_trajectory, action_dim))
behavior_policy_pscore = pscore.prod(axis=1).prod(axis=1)
if action_scaler is not None:
evaluation_policy_action = action_scaler.transform_numpy(
evaluation_policy_action
)
action = action_scaler.transform_numpy(action)
similarity_weight = (
self._kernel_function[kernel](
evaluation_policy_action,
action,
bandwidth=bandwidth,
)
.reshape((-1, step_per_trajectory))
.prod(axis=1)
)
trajectory_wise_importance_weight = (
similarity_weight / behavior_policy_pscore
)