docstr: Shows distribution changes for confounding variables before and after applying inverse propensity weights.
docstr_tok: ["Shows","distribution","changes","for","confounding","variables","before","and","after","applying","inverse","propensity","weights","."]
code: def interpret(self, data: pd.DataFrame):
      """
      Shows distribution changes for confounding variables before and after applying inverse propensity weights.
      """
  
      cols = (
          self.estimator._observed_common_causes_names
          + self.estimate._treatment_name
      )
      df = data[cols].copy()
      treated = self.estimate._treatment_name[0]
      propensity = self.estimate.propensity_scores
  
      # add weight column
      df.loc[:, "weight"] = df.loc[:, treated] * (propensity) ** (
          -1
      ) + (1 - df.loc[:, treated]) * (1 - propensity) ** (-1)
  
      # before weights are applied we count number rows in each category
      # which is equivalent to summing over weight=1
      barplot_df_before = (
          df.groupby([self.var_name, treated])
          .size()
          .reset_index(name="count")
      )
  
      # after weights are applied we need to sum over the given weights
      barplot_df_after = (
          df.groupby([self.var_name, treated])
          .agg({"weight": np.sum})
          .reset_index()
      )
      barplot_df_after.rename(columns={"weight": "count"}, inplace=True)
  
      title1 = (
          "Distribution of "
          + self.var_name
          + " before applying the weights"
      )
      title2 = (
          "Distribution of "
          + self.var_name
          + " after applying the weights"
      )
  
      import matplotlib.pyplot as plt
  
      fig, (ax1, ax2) = plt.subplots(1, 2, figsize=self.fig_size)
      iterable = zip(
          [barplot_df_before, barplot_df_after],
          [ax1, ax2],
          [title1, title2],
      )
      for plot_df, ax, title in iterable:
          aggregated_not_treated = plot_df[
              plot_df[treated] == False
          ].reset_index()
          aggregated_treated = plot_df[
              plot_df[treated] == True
          ].reset_index()
  
          labels = aggregated_not_treated[self.var_name].astype("float")
          not_treated_counts = aggregated_not_treated["count"]
  
          treated_counts = aggregated_treated["count"]
          self.discrete_dist_plot(
              labels,
              not_treated_counts,
              treated_counts,
              ax,
              title,
              self.var_name,
              self.font_size,
          )
  
      fig.tight_layout()
      plt.show()
  
file_path: dowhy/dowhy/interpreters/confounder_distribution_interpreter.py
imports: import numpy
  import pandas
  from dowhy.causal_estimator import CausalEstimate
  from dowhy.causal_estimators.propensity_score_weighting_estimator import PropensityScoreWeightingEstimator
  from dowhy.interpreters.visual_interpreter import VisualInterpreter
label_desc: Use image node_id 3 for calling the ConfounderDistributionInterpreter obj's underlying member method code with example usage: obj.interpret(data) without return types