<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Vahe Hakobyan on Medium]]></title>
        <description><![CDATA[Stories by Vahe Hakobyan on Medium]]></description>
        <link>https://medium.com/@vahe-hakobyan?source=rss-832f5834e1c4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*eyOSKTCzSrL5L8As8dmnsQ.jpeg</url>
            <title>Stories by Vahe Hakobyan on Medium</title>
            <link>https://medium.com/@vahe-hakobyan?source=rss-832f5834e1c4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 06:44:21 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vahe-hakobyan/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Enabling configurable projects for ML experiments with GIN-CONFIG]]></title>
            <link>https://vahe-hakobyan.medium.com/enabling-configurable-projects-for-ml-experiments-through-gin-363b9196ccc7?source=rss-832f5834e1c4------2</link>
            <guid isPermaLink="false">https://medium.com/p/363b9196ccc7</guid>
            <category><![CDATA[pytorch-lightning]]></category>
            <category><![CDATA[machine-learning-engineer]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[experiment-tracking]]></category>
            <dc:creator><![CDATA[Vahe Hakobyan]]></dc:creator>
            <pubDate>Sat, 18 Feb 2023 17:48:26 GMT</pubDate>
            <atom:updated>2023-02-19T08:38:04.264Z</atom:updated>
            <content:encoded><![CDATA[<h3>Motivation</h3><p>Very often we find ourselves in a highly experimental setup. The task given to us has many approaches to solve that we learn from papers. On the top of that we have our own experience and gut feeling on what can help to achieve better results. And we want to try them ALL.</p><p>We have many tools out there to track experiments and log hyper-parameters. But is that enough? There are two questions come to my mind:</p><ul><li>Can we log everything ? e.g. what kind of augmentation do we use for a particular run — Gaussian blur? if yes, what’s the kernel size ?</li><li>Even if we do, how do we ensure maximal flexibility and visibility of that? How do we make most of the stuff configurable in the code without turning the code-base in to a total mess ?</li></ul><p>One desirable property of combining both a good configurable project and experiment tracking tool</p><ol><li>is to keep all configurables in a <strong>configuration file</strong>,</li><li>use experiment tracking tool to track metrics and losses and maybe some hyper-parameters.</li><li>upload <strong>configuration file</strong> as an <strong>artifact</strong></li></ol><p>These approach seems to elevate the first concern from above — we can keep all configurable aspects of the project in the configuration file. And log everything we want to track over time.</p><p><strong>Now how do we enable configuration flexible without harming the code-base ?</strong></p><p>Let’s go over an example…</p><h3>Pytorch Lightning example — vanilla</h3><p>Here I would like to talk about <a href="https://github.com/google/gin-config">gin-config</a> that does not get attention it deserves. Maybe it is too complicated or, maybe, google folks were not able to motivate it enough in their “documentation”.</p><p>I spent some time to create a walk through of a “real world example” and show where it shines. Real world is a bit stretch here, cause it’s gonna be….good old MNIST example.</p><p><a href="https://pytorch-lightning.readthedocs.io/en/stable/notebooks/lightning_examples/basic-gan.html">basic-gan</a> example can serve us a guinea pig to understand the benefits of having gin-config around. Instead of having one single file containing all the code from example, I have tried to create a project-like structure:</p><p><strong>run.py</strong> - entry point of the application</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/756/1*jxIOlWNbCgbEbxgWI07FSQ.png" /></figure><p><strong>data_module.py</strong> - PytorchLightning DataModule - responsible for Dataset and DataLoader creation</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/825/1*bnHFeTe0cdqdXmgFUeKNTw.png" /></figure><p><strong>model.py</strong> - contains the PytorchLightning module and training logic</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/839/1*8Fek2n5YVzZ8WGWGwXOP4A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/825/1*oED_9HOIdcqArxBqZNzV4g.png" /></figure><p>Red parts above mean that ideally one would like to change those parts through configuration. Then changes will traceable through configuration artifacts.</p><p>One can argue — this is already a good setup and when someone changes something, one can commit changes and track them throgh git. In practice, what happens in experimental mode is that many ideas are tried out together and code is not committed until a “stable” version is reached (whatever “stable” means here).</p><p><strong>So how do we do it?</strong></p><h3>Option 1: Command line arguments</h3><p>Let’s try to imagine the number arguments we would need to have to achieve flexibility we want — meaning, making the red areas configurable. For the sake of demonstration, let’s look at steps to make DataModule and some aspects of the Model training configurable.</p><p><strong>run.py</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1011/1*WBCHxLk4wFCqsfkNOnHHaw.png" /></figure><p><strong>model.py</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/734/1*9BFsfB7Z7oPxZN_Xe3ctiA.png" /></figure><p><strong>Pros</strong></p><ul><li>making the project configurable</li><li>injecting generator and discriminator to the LightningModule. Hardcoding generator and discriminator was a bad design choice from the very beginning. So I would not attribute this purely to configurability.</li></ul><p><strong>Cons</strong></p><ul><li>in order to run this application my command will look like this python run.py --path_dataset ../ --batch_size 256 --num_workers 64 --latent_dim 100 --generator_base_depth 128 --generator_arch arch1. And this is only for configuring small aspects of the dataset/dataloader and really negligible aspects of the generator. Imagine if we need to add more information about the structure of the generator, discriminator, loss functions, optimizers. Handling everything will turn into a nightmare and the size of the command blow up into a poem. This will result in confusion and high cognitive load. One can keep some arguments to default, but this will only add to the confusion rather than elevate it. Imagine going through the code and figuring out why a particular value was passed to a certain function.</li><li>No clear correspondence between CLI arguments and parameters that entities take. For example command line takes --path_dataset as argument, while DataModule has ‘data_dir’ parameter</li><li>pay attention to the get_generator one is forced to have series of such functions for each customizable component (losses, optimizers …).</li><li>Every time one needs to try out a different generator, one needs to do back and forth between get_generator and argparse to understand which key values to pass</li><li>Every time one adds a new generator one needs to accomodate the same knowledge about its configuration in <strong>BOTH</strong>, argparse and ggt_generator.</li><li>Having functions with structure of get_generator is not a good engineering practice and goes against SOLID principles</li><li>Last but not least, it adds the cognitive dissonance between purpose of particular argument and its implementation. As the argument intended for a DataLoader might be used for constructing model architecture. And there is no way of preventing that.</li></ul><h3>Option 2: Config yamls, tomls…</h3><p>Let’s see what gets better and what gets worse…</p><p><strong>config.yaml</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/708/1*Z9PSNkuGDv4QZ3gev447Mw.png" /></figure><p><strong>run.py</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1011/1*89vhcTwun0RLkFJORu5dWw.png" /></figure><p><strong>Pros</strong></p><ul><li>Cleaner CLI, removes the confusion by adding consisenes</li><li>Ability to naturally have nested structures</li></ul><p><strong>Cons</strong></p><p>Besides the first point <strong>nothing really improved compared to Command Line arguments</strong></p><ul><li>One still has to deal with functions like get_generator and all the headache associated with them</li><li>Problems with cognitive load and dissonance did not go away</li><li>Still a lot of if/elses in the code that is supposed to create objects in the code based on some configuration values.</li><li>Additionally, the parsed configuration is a dictionary, which means that user is free to add key/values (modify existing ones) in it. I have witnessed a good share of code examples where this is getting abused.</li></ul><h3>Solution: gin-configs</h3><p>Now let’s see how gin-configs help you with above problems …</p><p><strong>config.gin</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*LC0TruHVJdBBijM_po7_tQ.png" /></figure><p><strong>run.py</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/758/1*S4laXX7_LWWxVDLXvWa89g.png" /></figure><p><strong>model.py</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/821/1*CkrKHW7mD0lbfQbfGRMW3g.png" /></figure><p><strong>Pros</strong> (adding to the pros from yaml)</p><ul><li>Clear dependency between components defined in the configuration file</li><li>Possibility to define a value in a form of a <a href="https://github.com/cryptexis/gin_example/blob/main/configurable_gin/gin_macros.py"><strong>macro</strong></a> and pass it as a value to other objects</li><li>No more functions as get_generator as entities and their interaction is directly defined in the config file</li><li>Logic is defined in one place — from the perspective of cognitive load one has to look at object definition and fill parameter values for object/function creation in the config</li><li>Very hard to alter the configuration values, unless one of them is defined as a dictionary and afterwards altered in the application</li><li>Minimal entry barrier — object/function becomes gin configurable as soon as @gin.configurable is added as a decorator</li></ul><p><strong>Cons</strong></p><ul><li>one shortcoming that I found is that one has to import all configurables e.g. from arch.generators.generator import Generator1, Generator2 even though one generator is used during one run. This will result tools like pylint complain about having unused imports.</li></ul><p>There was a nasty code snippet with optimizers above. Let’s see how we can improve it elegantly with gin.</p><p><strong>config.gin</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/842/1*7Rcv4G6JvAfqMfac3ivYJA.png" /></figure><p><strong>model.py</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/796/1*9pQz366XWwTVeDWMsQzHtw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/674/1*gVlZgeD7L4SpduAUxzJ4VA.png" /></figure><p><strong>Notes</strong></p><ul><li><strong>Namespaces</strong> — are needed when multiple objects of the same class need to be created. Otherwise the last configuration values will be applied to every object</li><li><strong>Objects</strong> — objects are created during runtime with parameter values defined in the config file</li><li><strong>Classes</strong> — whenever object is created, unless specified differently, default paramete values will be used from the config file.</li></ul><p><strong>References:</strong></p><ul><li><a href="https://github.com/google/gin-config">GitHub - google/gin-config: Gin provides a lightweight configuration framework for Python</a></li><li><a href="https://github.com/cryptexis/gin_example">GitHub - cryptexis/gin_example</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=363b9196ccc7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Causal Inference — To Control or not to Control]]></title>
            <link>https://medium.com/data-science/causal-inference-to-control-or-not-to-control-417e2721e22e?source=rss-832f5834e1c4------2</link>
            <guid isPermaLink="false">https://medium.com/p/417e2721e22e</guid>
            <category><![CDATA[causality]]></category>
            <category><![CDATA[linear-regression]]></category>
            <category><![CDATA[backdoor-criterion]]></category>
            <category><![CDATA[adjustment-formula]]></category>
            <category><![CDATA[causal-inference]]></category>
            <dc:creator><![CDATA[Vahe Hakobyan]]></dc:creator>
            <pubDate>Mon, 05 Apr 2021 09:38:14 GMT</pubDate>
            <atom:updated>2021-04-19T14:50:07.397Z</atom:updated>
            <content:encoded><![CDATA[<h3>Causal Inference — To Control or not to Control</h3><h4>Making a connection between causal and statistical estimands</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/726/1*KtEHGiwdMoDZZNXsYKaKoA.png" /><figcaption><a href="https://github.com/cryptexis/causal_inference/blob/main/collider_fork/collider_fork.ipynb">https://github.com/cryptexis/causal_inference/blob/main/collider_fork/collider_fork.ipynb</a></figcaption></figure><p>I will avoid repeating most of the stuff that can be found in other tutorials and books e.g. what is a collider or fork. I will provide useful links when a relevant topic is discussed. Otherwise, I assume that the reader is either familiar with the topic or sufficiently motivated to learn more. Just in case you feel lack of knowledge or context, here is a set of resources I would recommend to consult with: [ <a href="https://www.bradyneal.com/causal-inference-course">Introductory course on Causal Inference</a>, <a href="https://mixtape.scunning.com/">Causal Inference: The Mixtape</a><a href="https://www.amazon.de/-/en/Judea-Pearl/dp/1119186846">Causal Inference in Statistics: A Primer</a>, <a href="https://www.amazon.de/-/en/Judea-Pearl/dp/052189560X">Causality</a> ]</p><p>As the first experiment to my writing I chose to write about causal inference and also conduct a small exercise based on this <a href="https://academic.oup.com/ije/article/48/2/640/5248195">paper</a>. This paper is discussed in the forth week of <a href="https://www.bradyneal.com/causal-inference-course">Introductory course on Causal Inference</a>.<br> The difference here is that I kept:</p><ol><li>the relationship between variables exactly the same, while in the video they were modified</li><li>the treatment variable continuous, while in the video it was modified to a binary variable in order to show one particular technique of estimating the average treatment effect.</li></ol><p>Also, I am willing to connect some dots on why certain techniques are working on estimating average causal effect and highlight what happens if proceed in estimating the causal effect naively.</p><h4>“My mama always said life was like a box of chocolates. You never know what you’re gonna get” © — Forest Gump</h4><p><strong>Causal Inference … where is it needed?</strong></p><p>Short: When Randomized Controlled Trials (RCTs) are not possible. There are plenty reasons why it would not be possible to conduct a RCT, some of them are described in the <a href="https://emilyriederer.netlify.app/post/causal-design-patterns/">“Why CI in industry”</a> subsection of the referred blog-post.</p><p>To motivate our exercise, let us imagine the following situation:<br> You are chilling at work, mangling with the data or playing a stare contest with the Tensorboard, when your boss calls you and asks you to look into the effect of variable <strong><em>X</em></strong> on the business KPI <strong><em>Y</em></strong>.</p><p>What would be your possible answer?</p><p><strong>A.</strong> Well to precisely understand the effect we need to setup a RCT, turn off the product feature <strong><em>X</em></strong> for some percentage of users for N amount of time.</p><p><strong>B.</strong> Okay, I will do what I can.</p><p>In case you answer <strong>A</strong> it leads to a lot of complications:</p><ol><li>It might be that feature <strong><em>X</em></strong> is a successful one: turning it off can result in a lot of opportunity cost</li><li>RCT? Easier said than done. Setting up RCT has its own challenges. Even companies with mature A/B testing frameworks struggle to get it right sometimes.</li><li>What if what you’re trying to randomize is not possible to randomize?</li></ol><p><strong>Outcome</strong>: if your intention was that your boss buggers off, well, mission accomplished!</p><p>In case your answer is B, then I hope the exercise below will shed some light on some practical and also theoretical questions. More specifically:</p><ul><li>How/Why linear regression coefficients can be used to estimate average causal effect of one variable on another?</li><li>What happens if wrong variables are controlled during the estimation of average causal effect?</li></ul><h3>A bit about Data Generation Process</h3><p>When we talk about data generation process, we have to talk about its causal structure. To represent the causal structure of the world, current literature suggests two instruments:</p><ol><li>Structural Causal Models (SCM)</li><li>Graphical Models</li></ol><p>Given random variables <strong><em>X</em></strong>, <strong><em>Y</em></strong> and <strong><em>Z, </em></strong>SCM could be <strong><em>Z=f(X, Y)</em></strong>. Which means that variables <strong><em>X</em></strong> and <strong><em>Y</em></strong> are causing the variable <strong><em>Z</em></strong>. Also, the part of the SCM is to describe the exact form of the <strong><em>f</em></strong>. It is often a convenient choice to model the relationship between variables as linear functions (the reason is interpretability).</p><p>Example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/188/1*FunzbTFrPNuV_OJ77akIrQ.png" /></figure><p>The graphical model corresponding to the SCM above would look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/179/0*uqsZfTZsszenOdFu.png" /></figure><p>To follow the data generation process offered in the <a href="https://academic.oup.com/ije/article/48/2/640/5248195">paper</a> I did two things. Translated the witchcraft written in R (no offense to R experts, I just detest that language) to Python and introduced a bit of structure to it:</p><p>In all studies presented in the paper, the effect of variable <strong><em>T</em></strong> to <strong><em>Y</em></strong> is always the subject of study. Therefore when talking about particular structures like <em>forks</em> or <em>colliders</em> it is meant to highlight how the 3rd variable is related to <strong><em>T</em></strong> and <strong><em>Y</em></strong>.</p><p><strong>Generating a Fork Structure</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/905/1*hblA9QAJVdcJSAQEH90TEw.png" /></figure><p>With corresponding SCM and graphical model:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/211/1*Tg7XZiDQmH_SzvLJon2A0w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/119/0*pNTRF2RpydVD190-.png" /></figure><p>Two more examples can be found here in this <a href="https://github.com/cryptexis/causal_inference/blob/main/collider_fork/collider_fork.ipynb">notebook</a>. In all of them the average causal effect (<strong><em>ACE</em></strong>: vocabulary borrowed from Judea Pearl) of <strong><em>T</em></strong> on <strong><em>Y </em></strong>is studied. In other literature <strong><em>ACE</em></strong> is named as <strong><em>ATE</em></strong> (average treatment effect). Personally I like average causal effect as average treatment effect suggest binarization of variable T. E.g. whether subjects they were treated or not. These are just semantics. Maybe it’s just me :)</p><p>To understand the procedure of estimating the <strong><em>ACE</em></strong>, let us do one step back.</p><h3>A bit of formalism: A little tale about Regression coefficients</h3><p>For a second lets imagine that:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/347/1*SWOFCVUYjVN_HtvCNcPLXw.png" /></figure><p>and we fit a linear regression to satisfy</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/199/1*Ahc4tev_cTyyT1KZp89Cng.png" /></figure><p>where <strong><em>X</em></strong> is a vector of other covariates. Let us rewrite the equation in terms of expected values:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/274/1*CpJUutFi4YySeVQt5qLzNg.png" /></figure><p>And for every <em>value</em> of <strong><em>T</em></strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/385/1*IPpB1jwtzGH8sAliYljRDA.png" /></figure><p>Now let us see what happens with difference of expected values when we change the <em>value</em> by <strong><em>delta</em></strong>. <strong><em>delta</em></strong> represents a unit change int the value of $T$. Without loss of generality we can assume that <strong><em>delta = 1</em></strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/499/1*u1zY5GAHtDaw9eD_ksrvbg.png" /></figure><p>Which should not come to a surprise to us: this is how we always interpret coefficients of linear regression — as expected change in the outcome variable when the respective covariate is changed by one unit. However, is this estimate truly carries a causal nature? Let us do one more step to understand the difference.</p><h3>A bit more of formalism</h3><p>In case <strong><em>T </em></strong>is binary, we define <strong><em>ACE</em></strong> as:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/676/1*y7GGTe0-VNyDsZd3CvQYoQ.png" /></figure><p>The important distinction between left and right sides of the <strong><em>=?</em></strong> sign is the presence of the <strong><em>do</em></strong>-operator. <strong><em>do</em></strong>-operator represents the intervention. More specifically, <strong><em>E[Y|do(T=1)]</em></strong> measures the expected value of <strong><em>Y</em></strong> when everyone in the population took the treatment <strong><em>T</em></strong>, while <strong><em>E[Y|T=1]</em></strong> the expected value of <strong><em>Y</em></strong> conditioned on the sub-population that took the treatment <strong><em>T</em></strong>.</p><p>Now when <strong><em>T</em></strong> is a continuous value we are interested in:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/871/1*4BVvolCyBY9AJ1h8sjHobw.png" /></figure><p>As in case of linear regression, we can assume <strong><em>delta=1</em></strong>. The left side of the above equation translates to:</p><p><em>“What is the average causal effect of the treatment variable </em><strong><em>T</em></strong><em> on the outcome variable </em><strong><em>Y</em></strong><em>, when we change variable </em><strong><em>T</em></strong><em> by one unit”</em></p><p>An important question here is<em> “how can we express the left side of the =? in terms of the right side?”</em></p><p><strong>Definition </strong>(The Backdoor Criterion): Given an ordered pair of variables <strong><em>(T,Y)</em></strong> in a DAG <strong><em>G</em></strong>, a set of variables <strong><em>Z</em></strong> satisfies the backdoor criterion relative to <strong><em>(T, Y)</em></strong> if no node in <strong><em>Z</em></strong> is descendant of <strong><em>T</em></strong>, and <strong><em>Z</em></strong> blocks every path between <strong><em>T</em></strong> and <strong><em>Y</em></strong> that contains an arrow into <strong><em>T</em></strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/550/1*vgCbtJ7xygfhPOMx4CPLQg.png" /></figure><p>(above definition is taken from Judea Pearl)</p><p>Which basically means that in order to have the decomposition above, one <strong>needs to control</strong> for all variables that are not descendants of <strong><em>T</em></strong> and <strong>should not</strong> control for any of descendants.</p><p>Well, let’s see how this relates to regression coefficients in case we have to control for only one variable <strong><em>Z</em></strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/516/1*8VPIiyGWIoWW3R5qs7b0Uw.png" /></figure><p>Now, in case of linear relationship between variables:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/487/1*P8r3_-kuubHaoOHeqe4aMA.png" /></figure><p>And similarly to derivations for linear regression we can derive from just obtained results that:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/562/1*hrYPC91YMcngtiBzVT_4cQ.png" /></figure><p>Which basically shows in case we <strong>control for right variables</strong>, we can estimate the <strong><em>ACE</em></strong> by fitting a linear regression (of course it is true only when SCM consists of linear relationships) to the data and look at the coefficient of the treatment variable.</p><h3>Carrying out experiments</h3><p>The <a href="https://academic.oup.com/ije/article/48/2/640/5248195">paper</a> simulates data over a realistic study over the relationship of 24-h dietary sodium intake (<strong><em>T</em></strong>) and systolic blood pressure (<strong><em>Y</em></strong>). With age <strong><em>W</em></strong> our kidneys undergo transformations which effects the balance of sodium in our organism and systolic blood pressure. Thus, <strong><em>W</em></strong> is a common cause of both <strong><em>T</em></strong> and <strong><em>Y</em></strong>. On the other hand having 24-h dietary sodium intake and high systolic blood pressure both cause excretion of urinary protein <strong><em>Z</em></strong>. Thus, <strong><em>Z</em></strong> is a caused both by <strong><em>T</em></strong> and <strong><em>Y</em></strong>.</p><p>The data generation process and the resulting graph are depicted below:</p><p><strong>Generating Fork + Collider Data</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/903/1*cn1Zr1CCV-vD-5Lh7zpUPA.png" /></figure><p>With corresponding SCM and graphical model:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/214/1*PKn7ub_Ceuc-LC6C3cWAUA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/156/0*_G8zvjy0iGnHUTFe.png" /></figure><p>Here we can see that the causal effect of <strong><em>T</em></strong> on <strong><em>Y</em></strong> is <strong><em>1.05</em></strong>.</p><p><strong>Assumptions<br></strong>We assume that we have been provided with the graphical model and that we know that the relationship between variables is linear.</p><p><em>“How do we obtain such a graphical model in real world scenarios?” -</em> Good question!</p><p>Here is how:</p><ol><li>There is a branch of <a href="https://arxiv.org/abs/1803.01422">research</a> devoted to learning the causal DAG from the data.</li><li>Domain knowledge: Given the data and sufficient expertise in the domain one can come up with a pretty decent graphical model.</li></ol><p><strong>Estimation</strong><br>As was shown above, what we need is to fit a linear regression on a set of variables and look at the coefficient of the variable <strong><em>T</em></strong>.</p><p>Now let’s see how far we can get with different approaches of estimating the <strong><em>ACE</em></strong>…</p><p><strong>Approach 1:</strong> Adjust for nothing</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/156/0*3Z-760DV1CCz1HXT.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/919/1*GuPR9L2fr-aCmHKtYiQT8A.png" /></figure><p>Here we just overestimated the effect of <strong><em>T</em></strong> by <strong><em>207%</em></strong>. Imagine the implications on the business in case you communicated an estimate like this to your boss. Conclusion: just collecting the data and comparing group means without further checks is a <strong>NO-NO!</strong>.</p><p><strong>Approach 2:</strong> Brutal Data Scientist mode — pour all variables in: adjust for both <strong><em>W</em></strong> and <strong><em>Z</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/156/0*3lDrF7r0tbY7jTKr.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/914/1*Ga1VDvwsqAGonLkVrdN8Jw.png" /></figure><p>He? <strong><em>-0.9117</em></strong> ? Completely different sign. Which means not only the magnitude is wrong but also conclusions are 180 degrees different.</p><p>In a simplified world, for the first approach you could communicate to your boss <em>“if you spend one dollar, you will generate 3.22”</em> In the second case the message would be <em>“if you spend one dollar, you are going to lose additional 90 cents”.</em></p><p><strong>Approach 3:</strong> we stop cutting corners and look at the DAG. The DAG says, <strong><em>W</em></strong> is a fork and <strong><em>Z</em></strong> is a collider. Or putting in Backdoor Criterion (BC) words, <strong><em>Z</em></strong> does not satisfy BC criterion and <strong><em>W</em></strong> does. Therefore we need to control only for <strong><em>W</em></strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/156/0*VGErdcAxRXA6oVxF.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/913/1*8V1iQK-3yPVy0RVj-R-HNw.png" /></figure><p>Seems like we hit the target. One disturbing thing here is the width of the confidence interval. They look like Grond went through it.</p><p>But this is a story for another time….</p><p>If you’re reading this line it means you have made it through :)) Thanks for reading! I will come back to causal inference topic in the future to cover more realistic and interesting use-cases.</p><h3>References</h3><p>[1] Miguel Angel Luque-Fernandez, Michael Schomaker, Daniel Redondo-Sanchez, Maria Jose Sanchez Perez, Anand Vaidya, Mireille E Schnitzer, <a href="https://academic.oup.com/ije/article/48/2/640/5248195">Educational Note: Paradoxical collider effect in the analysis of non-communicable disease epidemiological data: a reproducible illustration and web application</a> (2019), International Journal of Epidemiology</p><p><em>Originally published at </em><a href="https://cryptexis.github.io/projects/colliders-and-forks"><em>https://cryptexis.github.io</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=417e2721e22e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/causal-inference-to-control-or-not-to-control-417e2721e22e">Causal Inference — To Control or not to Control</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>