Visualising Causal Test Results
This tutorial will show how to visualise your causal test results for easier interpretability.
Important note
You must use the web-interface for jupyter notebook, using VSCode or other inbuilt renderers, will likely not work.
Additional Dependencies
To run visualisations there are additional dependencies that need to be installed.
First, install the pip available packages with pip install -e .[visualise].
Then, unfortunately, to run the backend, you will need the following additional native dependencies installed an on your PATH.
Selenium and geckodriver or Chromium browser (not the same as Google Chrome)
Unfortunately, these can be a little tricky to set up, especially on Windows. We hope to resolve this soon by providing an apptainer image that you can “just use” instead of having to set these up locally.
Dashboard Interface
The Causal Testing Framework incorporates a full graphical interface that operates from you web browser. From here, you can generate and run causal tests, and will be provided with a full report of the results with helpful visualisations. To access this, simply run
causal-testing dashboard
You will then see a message along the lines of Launching server at [http://localhost:5006](http://localhost:5006). You should then be able to navigate to this URL in your web browser of choice and see the dashboard interface.
From here, you can “upload” your DAG and data, then generate, customise, and test cases.
NOTE: Your dashboard interface runs completely locally. Nothing you “upload” will leave your machine or travel over the internet. However, if you choose to set up a web server, you should be able to run the causal testing framework over a network.
The dashboard will then display some helpful visualisations to help you interpret your causal test outcomes. We will explain each of these plots in detail below.
Test Outcomes
[6]:
# First we need some test results to display
# Let's run the Poisson line process example from the other tutorials
from causal_testing.causal_testing_framework import CausalTestingFramework
ctf = CausalTestingFramework()
ctf.setup(dag_path="dag.dot", data_paths=["data_random_1000.csv"])
ctf.generate_causal_tests()
ctf.run_tests(adequacy=True)
100%|██████████| 26/26 [00:11<00:00, 2.32it/s]
The first plot is an interactive version of the causal DAG. Green edges represent passing tests. Red edges represent failing tests. Orange edges represent inestimable tests. That is, tests for which there was not enough data to calculate a causal effect estimate. (If you need to, you can supply a custom colour_map to change these colours.) Dashed edges represent independences in the DAG for which the corresponding causal test failed, indicating that the data suggests a statistically
significant causal relationship. You can hover over edges to see further details of the estimated causal effect, and to highlight the treatment and outcome variables.
[ ]:
from causal_testing.visualisation.visualisation_plotter import VisualisationPlotter
from causal_testing.testing.causal_test_result import TestOutcome
from bokeh.palettes import RdYlGn
colour_map = { # If you need to, you can pass in a custom colour map like this
TestOutcome.PASS: RdYlGn[11][0], # green
TestOutcome.INESTIMABLE: RdYlGn[11][7], # orange
TestOutcome.FAIL: RdYlGn[11][9], # red
}
plotter = VisualisationPlotter(dag=ctf.dag, df=ctf.test_dataframe(), colour_map=colour_map)
plotter.interactive_results_dag(width=800, height=400)
Next to the interactive results DAG on the dashboard appears an adjacency matrix that displays all the causal test results. Again, passing tests appear by default as green, failing as red, and inestimable as orange, and you can hover over individual results to see further information about the test outcome. Grey squares here correspond to untested relationships. For example, if the causal DAG has an edge X -> Y, then it does not make sense to test Y -> X.
[8]:
plotter.outcome_adjacency(frame_width=400)
[8]:
Test Adequacy
As with all testing techniques, the question of “How do I know when I can stop testing?” is a tricky one to answer. Since Causal Testing is a statistical technique that relies on multiple runs of the system under test, we need to know whether we have collected sufficient data to be able to trust our test outcomes. For example, several tests above fail, but would our tests pass if we collected more (or different) data? This is what data adequacy aims gives insight into.
The value is calculated by repeatedly resampling the dataset such that some values are duplicated and some values are omitted. For each resample, the tests cases are re-evaluated by first estimating the causal effect of each treatment on each outcome and comparing this to the expected causal effect. The adequacy value then represents the distribution of these estimates (specifically the kurtosos. If the dataset is adequate, we expect the distribution of the resampled causal effect estimates to be normal For a more detailed description, see this paper.
Unlike traditional test adequacy metrics, where higher numbers are normally better, we are here aiming for an adequacy value of 0. Higher values represent unstable causal effect estimates, and thus potentially untrustworthy test outcomes. Lower values represent “suspiciously stable” causal effect estimates, indicating that the dataset does not contain the full stocasticity range of the system under test. You can hover over individual squares for more detailed information.
[9]:
plotter.data_adequacy_heatmap(frame_width=400)
[9]:
In addition to observing the data adequacy with respect to the causal effect estimates, it can also be useful to observe the stability of the test outcomes themselves in the resampled test evaluations. The more stable a test outcome, the more trustworthy the outcome is likely to be. For example, if we resample the data 100 times (the default) and a test fails in all of them, this likely warrants further investigation. However, if the test only fails in half of them, then it may be beneficial to collect more data.
[10]:
plotter.dag_adequacy_heatmap(frame_width=400)
[10]: