class_docstr: Compute effect of treatment using the instrumental variables method.
  
  This is also a superclass that can be inherited by other specific methods.
  
  Supports additional parameters as listed below.
class_docstr_tok: ["Compute","effect","of","treatment","using","the","instrumental","variables","method",".","This","is","also","a","superclass","that","can","be","inherited","by","other","specific","methods",".","Supports","additional","parameters","as","listed","below","."]
docstr: data: dataframe containing the data on which treatment effect is to be estimated.
  treatment_value: value of the treatment variable for which the effect is to be estimated.
  control_value: value of the treatment variable that denotes its absence (usually 0)
  target_units: The units for which the treatment effect should be estimated.
               It can be a DataFrame that contains values of the effect_modifiers and effect will be estimated only for this new data.
               It can also be a lambda function that can be used as an index for the data (pandas DataFrame) to select the required rows.
docstr_tok: ["data",":","dataframe","containing","the","data","on","which","treatment","effect","is","to","be","estimated",".","treatment_value",":","value","of","the","treatment","variable","for","which","the","effect","is","to","be","estimated",".","control_value",":","value","of","the","treatment","variable","that","denotes","its","absence","(","usually","0",")","target_units",":","The","units","for","which","the","treatment","effect","should","be","estimated",".","It","can","be","a","DataFrame","that","contains","values","of","the","effect_modifiers","and","effect","will","be","estimated","only","for","this","new","data",".","It","can","also","be","a","lambda","function","that","can","be","used","as","an","index","for","the","data","(","pandas","DataFrame",")","to","select","the","required","rows","."]
code: def estimate_effect(
      self,
      data: pd.DataFrame,
      treatment_value: Any = 1,
      control_value: Any = 0,
      target_units=None,
      **_,
  ):
      """
      data: dataframe containing the data on which treatment effect is to be estimated.
      treatment_value: value of the treatment variable for which the effect is to be estimated.
      control_value: value of the treatment variable that denotes its absence (usually 0)
      target_units: The units for which the treatment effect should be estimated.
                   It can be a DataFrame that contains values of the effect_modifiers and effect will be estimated only for this new data.
                   It can also be a lambda function that can be used as an index for the data (pandas DataFrame) to select the required rows.
      """
      self._target_units = target_units
      self._treatment_value = treatment_value
      self._control_value = control_value
      if (
          len(self.estimating_instrument_names) == 1
          and len(self._target_estimand.treatment_variable) == 1
      ):
          instrument = self._estimating_instruments.iloc[:, 0]
          self.logger.debug(
              "Instrument Variable values: {0}".format(instrument)
          )
          num_unique_values = len(np.unique(instrument))
          instrument_is_binary = num_unique_values <= 2
          if instrument_is_binary:
              # Obtain estimate by Wald Estimator
              y1_z = np.mean(
                  data[self._target_estimand.outcome_variable[0]][
                      instrument == 1
                  ]
              )
              y0_z = np.mean(
                  data[self._target_estimand.outcome_variable[0]][
                      instrument == 0
                  ]
              )
              x1_z = np.mean(
                  data[self._target_estimand.treatment_variable[0]][
                      instrument == 1
                  ]
              )
              x0_z = np.mean(
                  data[self._target_estimand.treatment_variable[0]][
                      instrument == 0
                  ]
              )
              num = y1_z - y0_z
              deno = x1_z - x0_z
              iv_est = num / deno
          else:
              # Obtain estimate by 2SLS estimator: Cov(y,z) / Cov(x,z)
              num_yz = np.cov(
                  data[self._target_estimand.outcome_variable[0]],
                  instrument,
              )[0, 1]
              deno_xz = np.cov(
                  data[self._target_estimand.treatment_variable[0]],
                  instrument,
              )[0, 1]
              iv_est = num_yz / deno_xz
      else:
          # More than 1 instrument. Use 2sls.
          est_treatment = data[
              self._target_estimand.treatment_variable
          ].astype(np.float32)
          est_outcome = data[
              self._target_estimand.outcome_variable[0]
          ].astype(np.float32)
          ivmodel = IV2SLS(
              est_outcome, est_treatment, self._estimating_instruments
          )
          reg_results = ivmodel.fit()
          self.logger.debug(reg_results.summary())
          iv_est = sum(
              reg_results.params
          )  # the effect is the same for any treatment value (assume treatment goes from 0 to 1)
      estimate = CausalEstimate(
          data=data,
          treatment_name=self._target_estimand.treatment_variable,
          outcome_name=self._target_estimand.outcome_variable,
          estimate=iv_est,
          control_value=control_value,
          treatment_value=treatment_value,
          target_estimand=self._target_estimand,
          realized_estimand_expr=self.symbolic_estimator,
      )
  
      estimate.add_estimator(self)
      return estimate
  
file_path: dowhy/dowhy/causal_estimators/instrumental_variable_estimator.py
imports: from typing import Any, Dict, List, Optional, Union
  import numpy
  import pandas
  import sympy
  import sympy.stats
  from statsmodels.sandbox.regression.gmm import IV2SLS
  from dowhy.causal_estimator import CausalEstimate, CausalEstimator, RealizedEstimand
  from dowhy.causal_identifier import IdentifiedEstimand
  from dowhy.utils.api import parse_state
label_desc: Use image node_id 3 for calling the InstrumentalVariableEstimator obj's underlying member method code with example usage: obj.estimate_effect(data, treatment_value, control_value, target_units) and returns: estimate