[Music]
I'd like to welcome Lizzy Sila uh to
talk about causal Discovery in Python
causal Discovery means learning what
causes what from your data that's an
incred incredibly important thing for
anyone who wants to make conclusions
from their data or to use their data to
develop a strategic plan for Meaningful
decisions for what to do next Lizzie
Sila is a senior data scientist at wsp
she has broad interest in applied data
science and is worked in projects in
electricity distribution water
distribution abandoned M shaft detection
fish ecology and arthitis monitoring via
wearable devices among others she did
her PhD in Al horal Discovery at carnegi
melan University her pastimes include
singing and choirs and running the
monthly Melbourne machine learning Andi
Meetup as well as the Melbourne chapter
of puzzled fight she'll present a review
and comparison of softwares available
for causal Discovery in Python first a
brief intro into causal Discovery um
then uh run through uh various packages
intended for that purpose and if there's
time available maybe we'll get a quick
demonstration at the end everybody
please welcome Lizzie Silva
thanks Genevie and thank you all for
being here uh I have had spoilers for my
first slide which is what is causal
Discovery um so for those unfamiliar
causal Discovery is about learning what
causes what um great you can see my
cursor uh so causal Discovery is what
you would do first before later doing
causal effect estimation that is
learning how much does each feature have
an effect the input to a causal
Discovery algorithm is some kind of
table with features in the columns and
observations in the rows the output is a
graph with each feature being one node
in the graph and there's an arrow from
one feature to another if there's a
causal effect so smoking causes lung
cancer we have an arrow from smoking to
lung cancer and in between is the causal
Discovery algorithm which is very
complicated and can be explained in
detail but in 30 minutes not really
doable uh so what I'm going to do is
point you if you want the mathematical
details to a previous talk I gave on
this topic that that QR code will be on
the final slide as well and instead I'm
just going to give you some reason to
believe you might want to do this some
intuition about why it might work and
then just go straight into how would you
do this in
Python so when would I want to use
causal Discovery given that we've all
heard that correlation is not causation
and we don't want to make mistakes the
trouble is that we have to take actions
in the world and we can't always do a
randomized control trial to find out
what causes what it is unethical to
force people to smoke can see whether it
gives them lung cancer so sometimes we
have to make causal conclusions from
observational data now usually we'd like
to rely on our background knowledge
about what causes what but unfortunately
sometimes we don't know all the causal
relationships in this case we can add
what we do know as constraints and use
causal Discovery to learn the
rest sometimes our experts are not
perfect and they may believe something
that's not true in this case it might be
worth just trying causal Discovery and
see what it gets you you may find that
the model you get out of a causal
Discovery algorithm actually fits the
data much better than an expert
guess and the last situation is that you
might just have way too many features to
have any idea what's going on if you're
if you're trying to learn a genetic
regulatory network over a 20,000 genes
uh you won't have background knowledge
about which genes affect the others in
this case causal Discovery is a great
way to come up with a first guess to
generate hypotheses and prioritize which
experiments you're going to do
first when can't I use causal
Discovery if you have measured only two
things and those two things have a gan
distribution by which I mean the bell
curve the normal
distribution and they have a linear
relationship between them in that case
you're very sad pardon me uh you cannot
do anything with causal Discovery in
this situation that has been proven and
this is why you've heard that
correlation is not
causation however in every other case we
can learn
something for example let's say we've
only measured two features but
those features have a non- Galan
distribution so this is not the bell
curve this is a uniform
distribution in this case if I get the
direction of causation right it's a
little tick mark and I have the
predictor
X1 uh predicting the effect X2 then you
can see the amount of noise I'm adding
has got no relationship to the X1 value
but if I get the direction of causation
wrong suddenly the amount of noise
depends on the value of X1 in a way that
feels
counterintuitive we expect intuitively
that noise is just random other stuff
that's happening and should have nothing
to do with the relationship between X1
and X2 so if you get the direction right
you see a change in the dependence of
the noise on the
cause um so that's true for the this is
a situation which you can use uh linear
non- gasna cyclic models to learn causal
relationships uh if you have Gan noise
but nonlinear relationships you can use
the same kind of trick if I get the
direction of causation right the noise
is unrelated to my predictor this is
real data this is rings on Abalone
predicting their length rings
basically being a proxy for age age
increases the length of the Abalone but
if uh that length doesn't increase the
age
um so that that this is just trying to
give you some intuition that it might be
possible to learn causal
relationships if you have data from a
certain
distribution the other thing you might
have is more than two
features on this one slide on the right
hand side I want to give you some
feeling for why this is doable and on
the left hand side why it's hard this is
the observable universe one picture uh
and the number of directed ayylic graphs
or causal models with 25 features is at
least 10 orders of magnitude larger than
the number of atoms in the known
universe the space of models that we're
trying to find the true model in is very
large that's why there's a hard problem
when you have more than two features
however it is
doable because we have sometimes these V
structures where you've got two things
that are not causally related but they
both influence a common
effect uh so in this case the in this
example
uh there's no dependence between the
battery charge and the fuel tank level
in a car they both influence whether a
car starts so they're independent but
when you condition on their common
effect let's say the car did not start
and you know that the battery is fully
charged you can learn something about
the value of the fuel tank you learn
that it's empty so what we see with
these V structures is in Independence of
the predictors but dependence
conditional on their common effect that
unusual pattern of conditional
Independence uh of Independence and
conditional dependence indicates the
presence of a v structure and when you
see it in the data you can learn that V
structure exists and Orient these edges
in your causal model so it's doable but
it is computation Ally intense so if
you're in this
situation um my suggestion for which
algorithm to use depends on the number
of features you're trying to learn on
um I will say the grasp and boss
algorithm they were invented after I
graduated so I don't really understand
how they work but I do understand
they're way more accurate than anything
that existed when I was studying
especially on dense graphs so I
recommend commend them up to maybe 100
features after that you want something
more efficient like the PC algorithm or
fges
um other questions that
influence choice of
algorithm you may believe that there are
some latent confounders that you have
not measured in your data set in which
case there are algorithms all based on
FCI which will represent the extra
uncertainty related to those
confounders many of these algorithms
take a statistical test or a score
function as a
plugin and those tests and score
functions depend on whether the features
are continuous or discrete or a mixture
of each um when you have a mixture of
continuous and discrete features this is
actually the hardest case because you
need a score or a test there are similar
levels of power no matter how many
features are discreete or
continuous um people often ask me what
about time doesn't time make it much
more easy
um it's actually much easier when you
just have a snapshot of a population
because typically when people want to
take time into account it's because
they've only measured one system over
time if you measure say Australia's
economy over time your sample size is
one there's one Australia and uh you
want to use the different points in time
as effectively different samples but you
can have strange things happening like
different causal
relationships are working depending on
seasonality um so time actually makes
things much more difficult and I won't
talk about it further
um so instead now I'm going to jump to a
worked
example for this worked example I wanted
a really small data set just three
features where we could all agree on the
causal model um so I picked uh the
relationship between altitude latitude
and
temperature I hope we can agree that
warming up a location will not move it
but moving may change the temperature so
we can agree on on these causal
directions hopefully um this data set
also has some nice nonlinearity and
non-sanity so and it has a v structure
so it should be extremely
learnable
um I also want to mention
tetrad uh tetrad was the first software
available for causal Discovery it is a
massive Java Library it has been in
development since the early 90s and it's
mostly the work of Joe Ramsey who's
pictured here holding his Mark of
blanket which is a joke for the the real
stats nerds in the audience um and it
has
everything
um Joe is at the carnegi melan
University philosophy Department which
is where I studied um and the philosophy
department has realized that most data
scientists use Python so they now have
not one but two python libraries for
causal
Discovery the first is called causal
learn developed in 2020 and this is part
of the Cari melon University Center for
causal learning and
reasoning um all of the implementations
in csal learn are new they're all 100%
python um and because they're 100%
python they have limited performance for
the really large graphs they don't have
all the optimizations that are in tetrad
but it's really actively maintained it's
got a large user Community by the
standards of causal Discovery user
communities um so I will now jump
to vs code yeah I wanted to show you in
3D the relationship between temperature
latitude and
altitude um so this is temperature on
the Y
AIS uh latitude and altitude on the
horizontal axes and you can see that
there's this sort of surface of
different temperatures at different
locations um so caal
learn
um see this this big
[Music]
off we'll start by importing our
libraries loading our
data and then learning a graph is just
this one
line
um and it's really quick and it learns
the right graph but you may have
noticed uh a little tell here what
happens if I change this seed to
one
[Music]
um it uh picks up the relationship
between latitude and temperature but it
can't Orient it because it no longer has
a v structure and it's missed the
altitude to temperature relationship so
the
um if I don't set the random seat at all
and just keep generating this it uh
flips between the two um there is some
random in the permutation searches uh
I'm not exactly sure where it is I dug
around in the code and I couldn't see it
um but I will say that they take a score
function and the score function that is
the default is best for Gan data which
this is not so I I haven't exactly tried
to optimize this um but yeah that is
causal learn on this
example second python
package um also presented also produced
by the CMU philosophy department is py
tetrad it's a python wrapper for
tetrad um it's got some some python API
in it it's got this tetrad search class
some data set type translators and graph
translators and you can also access the
rest of tetrad through
jpipe given tetrad has everything this
is really powerful it's also got a bunch
of optimizations for dealing with large
graphs and all the newest algorithms get
implemented in tetrad first um so yeah
it's maintained it's got a small but
dedicated user base um but it's mostly
maintained by Joe who's a Java guy so
could use some help with the python
packaging getting it up on piie things
like that how about learning that same
model in
petrad once again load up our
data
and again it's one line oh two it's a
few
lines
um and in this case I don't get a nice
image out of the box um but you can read
these edges are what we want we've got
the altitude to temperature Edge and the
latitude to temperature Edge
and I can
output an image of that
graph which it's looking good good job
patrade um now I I should admit that I
signed myself up for talks in order to
force myself to do things that are on my
to-do list so the rest of the packages
that I'm about to talk about are ones
that I had intended to understand uh so
I put in the abstract so that I had to
review them um I really wanted to love
the causal Discovery toolbox um it's
built by devian Kenan who's now at
fentech um and he did his PhD in caal
Discovery under Isabelle guon who set up
the causal pairs challenge so it's got a
real emphasis on the pairwise methods
things like the additive noise method I
mentioned earlier and it's also got some
deep learning methods um and and other
things related to using lots of features
are imports from the PC ALR package
unfortunately it's no longer
maintained hasn't been updated since
2022 and uh let's just go over the um
that same example in
CDT
um come on
there we are load in our
data uh you have to feed it a complete
graph as an input
um and then it learns that altitude
causes
latitude
um so this is using the additive noise
method which um if we review our our
data the noise
does look roughly additive depending on
what point on the surface you're at but
it may be that it's just not working at
a pairwise level because if I just look
at one view of this the noise doesn't
look additive um so I I'm not entirely
sure why CDT is not learning the right
thing but just based on that quick
evaluation can't exactly recommend it um
um the
next option was
uh the causal NEX package this one is
another um uh one built by an actual
company it's built by McKenzie's AI
Consulting team Quantum black um not
sure if it will be maintained further
last commit was 9 months ago um and its
emphasis is on learning the size of
causal effects not caal Cal Discovery
but it does have a couple of causal
Discovery methods and they're gradient
descent based methods so if you wanted
to fit causal Discovery into a deep
learning pipeline this would be the way
to do it um except that they're not very
accurate so I if
I try to learn this graph once again
using um
the same cities data it outputs an HTML
file and it learns
that latitude and temperature cause
altitude but I do like this clicky drag
you think it's extremely
satisfying
um so I can't really recommend the uh
the gradient descent based methods
either they're based on a relaxation of
the acyclicity constraint and they don't
work very well um the last package that
I put in the um abstract and promise to
talk about was Tiger might um and when I
went and reviewed that uh it turned out
that it only had time series methods um
and was also very complicated academic
code
um so I will say that if you have time
series data and you know what you're
doing tiger might is the way to go but I
cannot do my three variable example in
it
um so that that is basically it that's
my review of the the causal Discovery
packages in Python and it shock surprise
I recommend the two produced by my
academic Department um so in summary uh
we all know that correlation doesn't
equal causation but in some cases under
some circumstances you can make an
assumption and uh infer
causation um most users who want to do
this in Python should use causal learn
if you have a large set of features like
a genetic regulatory Network or a niche
use case I suggest using p
instead um and if you're an expert and
you have time series data tiger
might uh that is it thank you any
questions
[Applause]
thanks um apologies if this is more for
your 101 to which I will go and watch
but what else can you do with these
models so if the output is the outputs
you showed were yeah finding the correct
causal relationship but then can these
models then be used to predict things
like you if you get new values of these
input features can you you know provide
estimations of whether you know
something is going to happen or not yeah
great question how is a causal Discovery
Model uh how is a causal model different
to a statistical model um the difference
is you're making predictions about what
would happen if you were to intervene
and change something in the world I'm
predicting that given the the smoking
and and yellow fingers and lung cancer
example I'm predicting that if I were to
paint someone's fingers yellow they
wouldn't get lung cancer even though
there is a correlation between having
yellow fingers and having lung cancer
because of the the nicotine stains um
the causal effect is from smoking to
lung cancer and smoking to having yellow
fingers so you're predicting the result
of
interventions a statistical model just
predicts what will happen if there are
no interventions if you're drawing from
the same distribution and not
intervening and changing anything um so
we want to use causal models if we want
to do anything in the world um causal
models are widely used in uh advertising
because they they want to know like what
is the causal effect of showing someone
this ad because maybe they would have
gone and bought the product
anyway um does the ad actually change
their propensity to buy
um they're widely used in in health
which is a much nicer and more fun
example uh I think they should be used
more often in asset
maintenance uh yeah there's a lot of
applications thank you everybody please
join me again in thanking Lizzy Sila
Lizzy we have a gift for you thank you
so much for your talk