Skip to main content

Causing: CAUSal INterpretation using Graphs

Project description

Causing: CAUSal INterpretation using Graphs

License: MIT Python 3.7

Causing is a multivariate graphical analysis tool helping you to interpret the causal effects of a given equation system. Get a nice colored graph and immediately understand the causal effects between the variables.

Input: You simply have to put in a dataset and provide an equation system in form of a python function. The endogenous variable on the left-hand side are assumed being caused by the variables on the right-hand side of the equation. Thus, you provide the causal structure in form of a directed acyclic graph (DAG).

Output: As an output you will get a colored graph of quantified effects acting between the model variables. You are able to immediately interpret mediation chains for every individual observation - even for highly complex nonlinear systems.

Here is a table relating Causing to other approaches:

Causing is Causing is NOT
:heavy_check_mark: causal model given :negative_squared_cross_mark: causal search
:heavy_check_mark: DAG directed acyclic graph :negative_squared_cross_mark: cyclic, undirected or bidirected graph
:heavy_check_mark: latent variables :negative_squared_cross_mark: just observed / manifest variables
:heavy_check_mark: individual effects :negative_squared_cross_mark: just average effects
:heavy_check_mark: direct, total and mediation effects :negative_squared_cross_mark: just total effects
:heavy_check_mark: linear algebra effect formulas :negative_squared_cross_mark: no iterative do-calculus rules
:heavy_check_mark: structural model :negative_squared_cross_mark: reduced model
:heavy_check_mark: small data :negative_squared_cross_mark: big data requirement
:heavy_check_mark: graphical results :negative_squared_cross_mark: just numerical results
:heavy_check_mark: XAI explainable AI :negative_squared_cross_mark: black box neural network

The Causing approach is quite flexible. The most severe restriction certainly is that you need to specify the causal model / causal ordering. If you know the causal ordering but not the specific equations, you can let the Causing model estimate a linear relationship. Just plug in sensible starting values.

Introduction Video

This 5 minute introductory video gives you a short overview and a real data example:

See Causing_Introduction_Video

Software

Causing is a free software written in Python 3. Graphs are generated using Graphviz.

See dependencies in setup.py.

Abstract

We propose simple linear algebra formulas for the causal analysis of equation systems. The effect of one variable on another is the total derivative. We extend them to endogenous system variables. These total effects are identical to the effects used in graph theory and its do-calculus. Further, we define mediation effects, decomposing the total effect of one variable on a final variable of interest over all its directly caused variables. This allows for an easy but in-depth causal and mediation analysis.

Citation

The Causing approach and its formulas together with an application are given in:

Bartel, Holger (2020), "Causal Analysis - With an Application to Insurance Ratings" DOI: 10.13140/RG.2.2.31524.83848 https://www.researchgate.net/publication/339091133

Note that in this paper the mediation effects on the final variable of interest are called final effects.

Example

Assume a model defined by the equation system:

Y1 = X1

Y2 = X2 + 2 * Y12

Y3 = Y1 + Y2.

This gives the following graphs. Some notes are in order to understand them:

  • The data used consist of 200 observations. They are available for the x variables X1 and X2 with mean(X1) = 3 and mean(X2) = 2. Variables Y1 and Y2 are assumed to be latent / unobserved. Y3 is assumed to be manifest / observed. Therefore 200 observations are available for Y3.

  • Average effects are based on the hypothesized model. The median values of all exogenous data is put into the given graph function, giving the corresponding endogenous values. The effects are computed at this point.

  • Individual effects are also based on the hypothesized model. For each individual, however its own exogenous data is put into the given graph function to yiel the corresponding endogenous values. The effects are computed at this individual point.

  • Estimated effects are based on the hypothesized model: The zero restrictions (effects being always exactly zero by model construction) are carried over and the average hypothesized effects are used as starting values. However, effects are estimated by fitting a linearized approximate model using a structural neural network. Effects are fitted by minimizing squared errors of observed endogenous variables. This corresponds to a nonlinear structural regression of Y3 on X1, X2 using all 200 observations.

  • Mediation effects are shown exemplary for the final variable of interest, assumed here to be Y3. In the mediation graph the nodes depict the total effect of that variable on Y3. This effect is partitioned over all outgoing edges, representing the mediation effects and thus enabling path interpretation. Note however that incoming edges do not sum up to the node value.

  • Individual effects are shown exemplary for individual no. 1 out of the 200 observations. To ease their interpretation, each individual effect is multiplied by the absolute difference of its causing variable to the median of all observations. Further, we color nodes and edges, showing positive (green) and negative (red) effects these deviations have on the final variable Y3.

Effects Direct Total Mediation for Y3
Average effects Average Direct Effects (ADE) Average Total Effects (ATE) Average Mediation Effects (AME)
Estimated effects Estimated Direct Effects (EDE) Estimated Total Effects (ETE) Estimated Mediation Effects (EME)
Individual effects for individual no. 1 Individual Direct Effects (IDE) Individual Total Effects (ITE) Individual Mediation Effects (IME)

As you can see in the bottom right graph for the individual mediation effects (IME), there is one green path starting at X1 passing through Y1, Y2 and finally ending in Y3. This means that X1 is the main cause for Y3 taking on a value above average with its effect being +37.44. However, this positive effect is slightly reduced by X2. In total, accounting for all exogenous and endogenous effects, Y3 is +29.34 above average. You can understand at one glance why Y3 is above average for individual no. 1.

The t-values corresponding to the estimated effects are also given as graphs. To assess model validation using the t-value graphs note the following:

  • Estimated standard errors for the effects are derived from the Hessian. Test and t-vales are asymptotically correct, but in small samples they suffer from the effects being biased in the case of regularization.

  • In this example regularization is required. The minimal regularization parameter is 0.000950 to obtain a well-posed optimization problem with a positive-definite Hessian. The optimal regularization parameter minimizing out-of-sample squared errors is 0.001545.

  • The t-values with respect to zero should be larger than two in absolute value, indicating that the specified model structure indeed yields significant effects.

  • The t-values with respect to the hypothesized model effects should be smaller than two in absolute value, indicating that there is no severe devation between model and data.

  • For the mediation t-value graphs EM0 and EM1 the outgoing edges do not some up to its outgoing node. In the EM0 graph all outgoing edges are even identical to their outgoing node because effects and standard deviations are partioned in the same way over their outgoing edges thus cancelling out in the t-values. However, this is not true for the EM1 graph since different partitioning schemes are used for the estimated and subtracted hypothesized model effects.

Effects Direct Total Mediation for Y3
t-values wrt. zero Estimated Direct Effects (ED0) Estimated Total Effects (ET0) Estimated Mediation Effects (EM0)
t-values wrt. model Estimated Direct Effects (ED1) Estimated Total Effects (ET1) Estimated Mediation Effects (EM1)

The t-values with respect to zero show that just some of the estimated effects are significant. This could be due to the small sample size. In this example we estimate five direct effects from 200 observations with the only observable endogenous variable being Y3.

None of the t-values with respect to the hypothesized model values is significant. This means that the specified model fits well to the observed data.

Biases are estimated for each endogenous variable. Estimation is done at the point of average effects implied by the specified model. That is, possible model misspecifications are captured by a single bias, one at a time. Biases therefore are just one simple way to detect wrong modeling assumptions.

Variable Bias value Bias t-value
Y1 0.00 0.64
Y2 0.06 0.55
Y3 0.06 0.55

In our example none of the biases is significant, further supporting correctness of model specification.

A Real World Example

To dig a bit deeper, here we have a real world example from social sciences. We analyze how the wage earned by young American workers is determined by their educational attainment, family characteristics, and test scores.

See education.md

Start your own Model

When starting python -m causing.examples example after cloning / downloading the Causing repository you will find the example results described above in the output folder. The results are saved in SVG files.

See causing/examples for the code generating these examples.

To run a model, you have to provide the following information, as done in the example code below:

  • Define all your model variables as SymPy symbols.

  • Note that in Sympy some operators are special, e.g. Max() instead of max().

  • Provide the model equations in topological order, that is, in order of computation.

  • Then the model is specified with:

    • xvars: exogenous variables
    • yvars: endogenous variables in topological order
    • equations: previously defined equations
    • final_var: the final variable of interest used for mediation effects
  • To simulate data, we have to provide simulation parameters as in:

    • ymvars: manifest / observed endogenous variables
    • xmean_true: mean of exogenous data
    • sigx_theo: true scalar error variance of xvars
    • sigym_theo: true scalar error variance of ymvars
    • rho: true correlation within y and within x vars
    • tau: no. of simulated observations
  • In estimate_input, the inputs to be used for estimation, further specify

    • ymvars: manifest endogenous variables
    • ymdat: manifest endogenous data
    • estimate_bias: estimate equation biases, for model validation
    • alpha: regularization parameter, is estimated if None
    • dof: effective degrees of freedom, corresponding to alpha

In the example case the python SymPy function looks like this:

def example():
    """model example"""

    X1, X2, Y1, Y2, Y3 = symbols(["X1", "X2", "Y1", "Y2", "Y3"])
    equations = (               # equations in topological order (Y1, Y2, ...)
        X1,
        X2 + 2 * Y1 ** 2,
        Y1 + Y2,
    )
    m = Model(
        xvars=[X1, X2],         # exogenous variables in desired order
        yvars=[Y1, Y2, Y3],     # endogenous variables in topological order
        equations=equations,
        final_var=Y3,           # final variable of interest, for mediation analysis
    )

    ymvars = [Y3]               # manifest endogenous variables
    xdat, ymdat = simulate(
        m,
        SimulationParams(
            ymvars=ymvars,
            xmean_true=[3, 2],  # mean of exogenous data
            sigx_theo=1,        # true scalar error variance of xvars
            sigym_theo=1,       # true scalar error variance of ymvars
            rho=0.2,            # true correlation within y and within x vars
            tau=200,            # nr. of simulated observations
        ),
    )

    estimate_input = dict(
        ymvars=ymvars,
        ymdat=ymdat,
        estimate_bias=True,     # estimate equation biases, for model validation
        alpha=None,             # regularization parameter, is estimated if None
        dof=None,               # effective degrees of freedom, corresponding to alpha
    )

    return m, xdat, ymdat, estimate_input

The files ADE, ATE, AME contain the average effects based on the median xdat observation as well as the estimated effects (EDE, ETE, EME) using the observed endogenous data ymdat.

The graphs ED0, ET0, EM0 contain the t-values graphs with respect to zero and the graphs ED1, ET1, EM1 contain t-values graphs with respect to the hypothesised model.

The files IDE, ITE, IME show the individual effects for the respective individual.

Award

RealRate's AI software Causing is a winner of PyTorch AI Hackathon.

October 2020: We are very happy to announce that the RealRate AI software was announced a winner of the PyTorch Summer Hackathon 2020 in the Responsible AI category. This is quite an honor given that more than 2500 teams submitted their projects.

devpost.com/software/realrate-explainable-ai-for-company-ratings.

Causing means CAUSal INterpretation using Graphs. Causing is a tool for Explainable AI (XAI). We explain causality and ensure fair treatment.

The software is developed by RealRate, an AI rating agency aiming to re-invent the ratings market by using AI, interpretability and avoiding any conflict of interest. See www.realrate.ai.

License

Causing is available under MIT license. See LICENSE.

Contact

Dr. Holger Bartel
RealRate
Cecilienstr. 14, D-12307 Berlin
holger.bartel@realrate.ai
Phone: +49 160 957 90 844
www.realrate.ai

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

causing-2.0.0.tar.gz (21.3 kB view details)

Uploaded Source

Built Distribution

causing-2.0.0-py3-none-any.whl (16.2 kB view details)

Uploaded Python 3

File details

Details for the file causing-2.0.0.tar.gz.

File metadata

  • Download URL: causing-2.0.0.tar.gz
  • Upload date:
  • Size: 21.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.14

File hashes

Hashes for causing-2.0.0.tar.gz
Algorithm Hash digest
SHA256 7806822d3943129bfa8ac58057d5a45542c83ccb55892951893018d7aec146ac
MD5 532fc0e8a4a98928dbfabd72cdc6b76d
BLAKE2b-256 3a251aff8b92b12ae1f42ae001fd4f9a241623eea7ee0446f05d12b2a83a902f

See more details on using hashes here.

File details

Details for the file causing-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: causing-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 16.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.14

File hashes

Hashes for causing-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d505e2f3dfb750976d682eb4300c1c1b141285a8b74cbd752781b26b17bfabec
MD5 c893ff32ce0ffc1e2f0588e13c8f565a
BLAKE2b-256 0c7d6045313eae919d827118202c27c65ea9611e0ea98c7561fbbdf41c50e66b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page