class_docstr: For details, visit https://arxiv.org/pdf/1612.01205.pdf
class_docstr_tok: ["For","details",",","visit","https",":","\/\/arxiv.org\/pdf\/1612.01205.pdf"]
code: def _get_importance_sampling_estimates(
      self, isd: ImportanceSamplingData, hp: DoublyRobustHP
  ) -> Tuple[CpeEstimate, CpeEstimate, CpeEstimate]:
      # The score we would get if we evaluate the logged policy against itself
      logged_policy_score = float(
          torch.mean(isd.logged_rewards)
      )  # logged_rewards is N*1 tensor of historical rewards
      if logged_policy_score < 1e-6:
          logger.warning(
              "Can't normalize DR-CPE because of small or negative "
              + "logged_policy_score"
          )
          normalizer = 0.0
      else:
          normalizer = 1.0 / logged_policy_score
  
      if isd.model_rewards is None:
          # Fill with zero, equivalent to just doing IPS
          direct_method_values = torch.zeros(
              [isd.model_propensities.shape[0], 1], dtype=torch.float32
          )
      else:
          # model rewards is (N_samples)*N_actions tensor of predicted
          # counterfactual rewards for each possible action at each
          # historical context
          direct_method_values = torch.sum(
              isd.model_propensities * isd.model_rewards,
              dim=1,
              keepdim=True,
          )
  
      direct_method_score = float(torch.mean(direct_method_values))
      logger.info(
          f"Normalized Direct method score = {direct_method_score * normalizer}"
      )
      avg_model_reward_for_logged_actions = float(
          torch.mean(isd.model_rewards_for_logged_action)
      )
      # The reward model could have learned to assign larger rewards than the logged rewards
      # to all observed actions, in which case direct_method_score would be inflated.
      # Hence, it is probably more useful to look at the adjusted score, which is obtained by
      # dividing the normalized score by (avg_model_reward_for_logged_actions)/(average logged reward).
      # Since the "normalizer" variable gives the average logged reward, this adjustment is equivalent
      # to dividing direct_method_score by avg_model_reward_for_logged_actions, because the
      # normalizer variable cancels out.
      logger.info(
          f"Adjusted Direct method score = {direct_method_score / avg_model_reward_for_logged_actions}"
      )
      direct_method_std_error = bootstrapped_std_error_of_mean(
          direct_method_values.squeeze(),
          sample_percent=hp.bootstrap_sample_percent,
          num_samples=hp.bootstrap_num_samples,
      )
      direct_method_estimate = CpeEstimate(
          raw=direct_method_score,
          normalized=direct_method_score * normalizer,
          raw_std_error=direct_method_std_error,
          normalized_std_error=direct_method_std_error * normalizer,
      )
  
      ips = isd.importance_weight * isd.logged_rewards  # N*1
  
      doubly_robust = (
          isd.importance_weight
          * (isd.logged_rewards - isd.model_rewards_for_logged_action)
      ) + direct_method_values
      # model_rewards_for_logged_action is N*1 of estimated rewards for target
      # policy
  
      ips_score = float(torch.mean(ips))
      logger.info(f"Normalized IPS score = {ips_score * normalizer}")
  
      ips_score_std_error = bootstrapped_std_error_of_mean(
          ips.squeeze(),
          sample_percent=hp.bootstrap_sample_percent,
          num_samples=hp.bootstrap_num_samples,
      )
      inverse_propensity_estimate = CpeEstimate(
          raw=ips_score,
          normalized=ips_score * normalizer,
          raw_std_error=ips_score_std_error,
          normalized_std_error=ips_score_std_error * normalizer,
      )
  
      dr_score = float(torch.mean(doubly_robust))
      dr_score_std_error = bootstrapped_std_error_of_mean(
          doubly_robust.squeeze(),
          sample_percent=hp.bootstrap_sample_percent,
          num_samples=hp.bootstrap_num_samples,
      )
      doubly_robust_estimate = CpeEstimate(
          raw=dr_score,
          normalized=dr_score * normalizer,
          raw_std_error=dr_score_std_error,
          normalized_std_error=dr_score_std_error * normalizer,
      )
  
      return (
          direct_method_estimate,
          inverse_propensity_estimate,
          doubly_robust_estimate,
      )
  
imports: import logging
  from dataclasses import dataclass
  from typing import Dict, NamedTuple, Optional, Tuple, Union
  import numpy
  import torch
  from reagent.evaluation.cpe import bootstrapped_std_error_of_mean, CpeEstimate
  from reagent.evaluation.evaluation_data_page import EvaluationDataPage
  from torch import Tensor
label_desc: Use image node_id 4 for calling the DoublyRobustEstimator obj's underlying member method code with example usage: obj._get_importance_sampling_estimates(isd, hp) and returns: direct_method_estimate,  inverse_propensity_estimate,  doubly_robust_estimate