causallib.evaluation.plots.mixins module

Mixins for plotting.

To work the mixin requires the class to implement get_data_for_plot with the supported plot names. See .data_extractors for examples.

class causallib.evaluation.plots.mixins.ClassificationPlotterMixin[source]

Bases: object

Mixin to add members to for classification/binary prediction estimation.

This occurs for propensity models (treatment assignment is inherently binary) and for outcome models where the outcome is binary.

Class must implement:
  • get_data_for_plot(plots.ROC_CURVE_PLOT)

  • get_data_for_plot(plots.PR_CURVE_PLOT)

  • get_data_for_plot(plots.CALIBRATION_PLOT)

plot_calibration_curve(phase='train', n_bins=10, plot_se=True, plot_rug=False, plot_histogram=False, quantile=False, ax=None)[source]

Plot calibration curves for multiple models (presumably in folds)

Parameters
  • phase (str, optional) – Phase to plot: “train” or “valid”. Defaults to “train”.

  • n_bins (int) – number of bins to evaluate in the plot

  • plot_se (bool) – Whether to plot standard errors around the mean bin-probability estimation.

  • plot_rug (bool) –

  • plot_histogram (bool) –

  • quantile (bool) – If true, the binning of the calibration curve is by quantiles. Defaults to False.

  • ax (matplotlib.axes.Axes, optional) – axis to plot on, if None creates new axis. Defaults to None.

Note

One of plot_propensity or plot_model must be True.

Returns

matplotlib.axes.Axes

plot_pr_curve(phase='train', plot_folds=False, label_folds=False, label_std=False, ax=None)[source]

Plot precision-recall (PR) curve.

Parameters
  • phase (str, optional) – Phase to plot: “train” or “valid”. Defaults to “train”.

  • plot_folds (bool, optional) – Whether to plot individual folds. Defaults to False.

  • label_folds (bool, optional) – Whether to label folds. Defaults to False.

  • label_std (bool, optional) – Whether to label std. Defaults to False.

  • ax (matplotlib.axes.Axes, optional) – axis to plot on, if None creates new axis. Defaults to None.

Returns

matplotlib.axes.Axes

plot_roc_curve(phase='train', plot_folds=False, label_folds=False, label_std=False, ax=None)[source]

Plot ROC curve.

Parameters
  • phase (str, optional) – Phase to plot: “train” or “valid”. Defaults to “train”.

  • plot_folds (bool, optional) – Whether to plot individual folds. Defaults to False.

  • label_folds (bool, optional) – Whether to label folds. Defaults to False.

  • label_std (bool, optional) – Whether to label std. Defaults to False.

  • ax (matplotlib.axes.Axes, optional) – axis to plot on, if None creates new axis. Defaults to None.

Returns

matplotlib.axes.Axes

class causallib.evaluation.plots.mixins.ContinuousOutcomePlotterMixin[source]

Bases: object

Mixin to add members to for continous outcome estimation.

Class must implement:
  • get_data_for_plot(plots.CONTINUOUS_ACCURACY_PLOT)

  • get_data_for_plot(plots.RESIDUALS_PLOT)

  • get_data_for_plot(plots.CONTINUOUS_ACCURACY_PLOT)

plot_common_support(phase='train', alpha_by_density=True, ax=None)[source]

Plot the scatter plot of y0 vs. y1 for multiple scoring results, colored by the treatment

Parameters
  • alpha_by_density (bool) – Whether to calculate points alpha value (transparent-opaque) with density estimation. This can take some time to compute for a large number of points. If False, alpha calculation will be a simple fast heuristic.

  • ax (plt.Axes) – The axes on which the plot will be displayed. Optional.

plot_continuous_accuracy(phase='train', alpha_by_density=True, plot_residuals=False, ax=None)[source]

Plot continuous accuracy,

Parameters
  • phase (str, optional) – Phase to plot: “train” or “valid”. Defaults to “train”.

  • alpha_by_density (bool, optional) – Whether to calculate points alpha value (transparent-opaque) with density estimation. This can take some time to compute for a large number of points. If False, alpha calculation will be a simple fast heuristic.

  • plot_residuals (bool, optional) – Whether to plot residuals. Defaults to False.

  • ax (matplotlib.axes.Axes, optional) – axis to plot on, if None creates new axis. Defaults to None.

Returns

matplotlib.axes.Axes

plot_residuals(phase='train', alpha_by_density=True, ax=None)[source]

Plot residuals of predicted outcome vs ground truth.

Parameters
  • phase (str, optional) – Phase to plot: “train” or “valid”. Defaults to “train”.

  • alpha_by_density (bool, optional) – Whether to calculate points alpha value (transparent-opaque) with density estimation. This can take some time to compute for a large number of points. If False, alpha calculation will be a simple fast heuristic.

  • ax (matplotlib.axes.Axes, optional) – axis to plot on, if None creates new axis. Defaults to None.

Returns

matplotlib.axes.Axes

class causallib.evaluation.plots.mixins.PlotAllMixin[source]

Bases: object

Mixin to make all the train and validation plots.

Class must implement:
  • all_plot_names

  • get_data_for_plot(name) for every name in all_plot_names

plot_all(phase=None)[source]

Create plot of all available EvaluationResults.

Will create a figure with a subplot for each plot name in all_plot_names. If results have train and validation data, will create separate “train” and “valid” figures. If a single plot is requested, only that plot is created.

Parameters

phase (Union[str, None], optional) – phase to plot “train” or “valid”. If not supplied, defaults to both if available.

Returns

the Axis objects of the plots in a nested dictionary:
  • First key is the phase (“train” or “valid”)

  • Second key is the plot name.

Return type

Dict[str, matplotlib.axis.Axis]]

class causallib.evaluation.plots.mixins.WeightPlotterMixin[source]

Bases: object

Mixin to add members to for weight estimation plotting.

Class must implement:
  • get_data_for_plot(plots.COVARIATE_BALANCE_GENERIC_PLOT)

  • get_data_for_plot(plots.WEIGHT_DISTRIBUTION_PLOT)

plot_covariate_balance(kind='love', phase='train', ax=None, aggregate_folds=True, thresh=None, plot_semi_grid=True, label_imbalanced=True, **kwargs)[source]

Plot covariate balance before and after weighting.

Parameters
  • kind (str, optional) – Plot kind, “love” ,”slope” or “scatter”. Defaults to “love”.

  • phase (str, optional) – Phase to plot: “train” or “valid”. Defaults to “train”.

  • ax (matplotlib.axes.Axes, optional) – axis to plot on, if None creates new axis. Defaults to None.

  • aggregate_folds (bool, optional) – Whether to aggregate folds. Defaults to True. Ignored when kind=”slope”.

  • thresh (float, optional) – Draw threshold line at value. Defaults to None.

  • plot_semi_grid (bool, optional) – Defaults to True. only for kind=”love”.

  • label_imbalanced (bool) – Label covariates that weren’t properly balanced. Ignored when kind=”love”.

Returns

axis with plot

Return type

matplotlib.axes.Axes

plot_weight_distribution(phase='train', reflect=True, kde=False, cumulative=False, norm_hist=True, ax=None)[source]

Plot the distribution of propensity score.

Parameters
  • phase (str, optional) – Phase to plot: “train” or “valid”. Defaults to “train”.

  • reflect (bool) – Whether to plot treatment groups on opposite sides of the x-axis. This can only work if there are exactly two groups.

  • kde (bool) – Whether to plot kernel density estimation

  • cumulative (bool) – Whether to plot cumulative distribution.

  • norm_hist (bool) – If False - use raw counts on the y-axis. If kde=True, then norm_hist should be True as well.

  • ax (matplotlib.axes.Axes, optional) – axis to plot on, if None creates new axis. Defaults to None.

Returns

matplotlib.axes.Axes