{ "cells": [ { "cell_type": "markdown", "id": "6ea7f5c2", "metadata": {}, "source": [ "# Causal Inference Example - Coupon Case" ] }, { "cell_type": "markdown", "id": "7dbe21e9", "metadata": {}, "source": [ "## Your data driven results may be wrong!" ] }, { "cell_type": "markdown", "id": "65490d28", "metadata": {}, "source": [ "Numbers don't lie - but they sure do tell a lot of half-truths. In the following example we explore one such case, where the difference between correlation and causation is crucial. Can we even separate the two? Yes of course, but we need to take a step beyond pure statistics.\n", "\n", " In our example case we model the buying behavior from households. For a certain year we know their income, how much they spend on gasoline, how often they use coupons for groceries, and how much revenue they make at the grocery store.\n", "\n", " So far households look for coupons themselves (pull principle). Your task is to assess to potential of a push principle." ] }, { "cell_type": "markdown", "id": "e6ad4fda", "metadata": {}, "source": [ "## Analyze Data" ] }, { "cell_type": "code", "execution_count": 1, "id": "bec8108e", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "data = pd.read_csv(\"coupon_data.csv\")\n", "data = data.rename(\n", " columns={\"household_income\": \"income\", \"gas_expenditure\": \"gas\",\n", " \"coupon_fraction\": \"coupon\", \"store_revenue\": \"revenue\"})" ] }, { "cell_type": "markdown", "id": "3b845bfe", "metadata": {}, "source": [ "A common way to get first impressions of the data is the correlation matrix. Evaluating our data with this approach yields an interesting result:" ] }, { "cell_type": "code", "execution_count": 2, "id": "f3e8cbdb", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
income gas coupon revenue
income1.0000000.700277-0.7519530.698591
gas0.7002771.000000-0.5197250.473422
coupon-0.751953-0.5197251.000000-0.490240
revenue0.6985910.473422-0.4902401.000000
" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.corr().style.background_gradient(cmap=\"RdYlGn\", vmin=-1, vmax=1)" ] }, { "cell_type": "markdown", "id": "c0c487a2", "metadata": {}, "source": [ "There is a negative correlation between the usage of coupons and the revenue. That’s odd!\n", "\n", "**Doesn’t common sense dictate that coupons incentivize to buy more?**" ] }, { "cell_type": "markdown", "id": "60ef3eff", "metadata": {}, "source": [ "We will have to take a step beyond mere correlations and use a structured model with causal calculus.\n", "\n", "In the following we will pin this against the standard machine learning approach – a black-box model." ] }, { "cell_type": "code", "execution_count": 3, "id": "346470d3", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "By using this Application, You are agreeing to be bound by the terms and conditions of the Halerium End-User License Agreement that can be downloaded here: https://erium.de/halerium-eula.txt\n" ] } ], "source": [ "from halerium import CausalStructure" ] }, { "cell_type": "markdown", "id": "e429aa1a", "metadata": {}, "source": [ "## Black-Box Model" ] }, { "cell_type": "markdown", "id": "170afd82", "metadata": {}, "source": [ "## Define a Black-Box Model" ] }, { "cell_type": "markdown", "id": "e2c23b4e", "metadata": {}, "source": [ "Machine Learning usually utilizes black box models. All inputs are assumed to be independent and there are no prior assumptions about their effects on the outputs. Here our inputs are the household income, the gas expenditure, and the coupon usage. Our output is the store revenue." ] }, { "cell_type": "code", "execution_count": 4, "id": "9381680b", "metadata": {}, "outputs": [], "source": [ "cs_bb = CausalStructure([[\n", " ['gas', 'income', 'coupons'], 'revenue']])\n", "cs_bb.train(data)" ] }, { "cell_type": "markdown", "id": "b683d5a7", "metadata": {}, "source": [ "As we later want to predict revenue from gas expenditure and coupon usage, we could also omit income as an input. There is no reason to prefer one variant over another in the black box model." ] }, { "cell_type": "markdown", "id": "c8ae3b7d", "metadata": {}, "source": [ "## Predict with the Black-Box Model" ] }, { "cell_type": "code", "execution_count": 5, "id": "adc6d7cb", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
couponsincomegasrevenue
00.060328.6305561000.04211.810692
\n", "
" ], "text/plain": [ " coupons income gas revenue\n", "0 0.0 60328.630556 1000.0 4211.810692" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cs_bb.predict({\n", " 'gas': [1000.0],\n", " 'coupon': [0.30]\n", "})" ] }, { "cell_type": "markdown", "id": "31bc3a13", "metadata": {}, "source": [ "The Black-Box model ignores the information that gas expenditure and coupon usage carry about the income. Therefore, the missing value for the income can only be imputed as the mean income. The revenue prediction thus suffers from a systematic bias." ] }, { "cell_type": "markdown", "id": "7bae1d87", "metadata": {}, "source": [ "## Interventions on the Black-Box Model are Pointless" ] }, { "cell_type": "markdown", "id": "70734e2c", "metadata": {}, "source": [ "We now want to assess a different couponing strategy: \n", "\n", "\"What if we just gave people coupons independent of their income via a push principle (e.g. via email or an app)?\"" ] }, { "cell_type": "code", "execution_count": 6, "id": "f23d671b", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
couponsincomegasrevenue
01.387779e-1960328.6305561000.04214.449284
\n", "
" ], "text/plain": [ " coupons income gas revenue\n", "0 1.387779e-19 60328.630556 1000.0 4214.449284" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cs_bb.predict_interventions(\n", " data={\n", " 'gas': [1000.0],\n", " },\n", " interventions={\n", " 'coupon': [0.30]\n", " }\n", ")" ] }, { "cell_type": "markdown", "id": "1cb179dd", "metadata": {}, "source": [ "In the Black-Box model coupon is an independent input. An intervention on the coupon changes nothing. We get the same result as for the prediction.\n", " \n", "The Black-Box Model cannot distinguish between the two couponing strategies." ] }, { "cell_type": "markdown", "id": "02cc829a", "metadata": {}, "source": [ "## Structured Model" ] }, { "cell_type": "markdown", "id": "e1edfb8d", "metadata": {}, "source": [ "## Define a Structured Model" ] }, { "cell_type": "markdown", "id": "62fe82f8", "metadata": {}, "source": [ "In a Structured Model we can define the causal relationships between different parameters. Those relationships can reflect physical laws, an expert’s experience or even just assumptions. Here we assume that the frequency in which people use coupons depends on their income. Rich people do not bother looking for coupons (on average). Furthermore, we assume that wealthier households will drive more and with less fuel-efficient cars. " ] }, { "cell_type": "code", "execution_count": 7, "id": "050bb124", "metadata": {}, "outputs": [], "source": [ "cs_st = CausalStructure([\n", " ['income', ['gas', 'revenue', 'coupon']],\n", " ['coupon', 'revenue']])\n", "cs_st.train(data)" ] }, { "cell_type": "markdown", "id": "04be6208", "metadata": {}, "source": [ "## Predict with the Structured Model" ] }, { "cell_type": "code", "execution_count": 8, "id": "87d19234", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
incomegascouponrevenue
063430.6732141000.00.34455.597629
\n", "
" ], "text/plain": [ " income gas coupon revenue\n", "0 63430.673214 1000.0 0.3 4455.597629" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cs_st.predict({\n", " 'gas': [1000.0],\n", " 'coupon': [0.30]\n", "})" ] }, { "cell_type": "markdown", "id": "634dd9c2", "metadata": {}, "source": [ "In the Structured Model income influences gas and coupon. The income is imputed from the information about coupon usage and gas expenditure. The model learned that a higher income causes lower coupon usage and imputes the income accordingly. This is correct! If you observe that a household never uses coupons your best bet is to assume that it has an above average income and therefore spends more money at the store." ] }, { "cell_type": "markdown", "id": "3f56c4b5", "metadata": {}, "source": [ "## Interventions on the Structured Model are Illuminating" ] }, { "cell_type": "markdown", "id": "e57f5b82", "metadata": {}, "source": [ "We now want to assess a different couponing strategy: \n", "\n", "\"What if we just gave people coupons independent of their income via a push principle (e.g. via email or an app)?\"" ] }, { "cell_type": "code", "execution_count": 9, "id": "80cbe790", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
incomegascouponrevenue
055289.1094281000.00.33606.484593
\n", "
" ], "text/plain": [ " income gas coupon revenue\n", "0 55289.109428 1000.0 0.3 3606.484593" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cs_st.predict_interventions(\n", " data={\n", " 'gas': [1000.0],\n", " },\n", " interventions={\n", " 'coupon': [0.30]\n", " }\n", ")" ] }, { "cell_type": "markdown", "id": "15effc85", "metadata": {}, "source": [ "The structured model knows that the coupon ratio depends on the income. With an intervention we „cut-away” this influence - and with it the influence the income has on the coupon usage. In contrast to the observation, revenue now increases with the usage of coupons, as we would expect." ] }, { "cell_type": "markdown", "id": "cfae5ec1", "metadata": {}, "source": [ "## So do coupons decrease or increase revenue?" ] }, { "cell_type": "markdown", "id": "fd2d5800", "metadata": {}, "source": [ "The correlation analysis said decrease, the black-box model said increase. Only structured model with causal calculus let us understand the issue. If we observe that a household uses lots of coupons (in a pull principle) its expected revenue is low, but if we actively supply it with coupons (push principle) its expected revenue will increase. This difference between observation and intervention can only be handled with causal inference" ] }, { "cell_type": "markdown", "id": "b13b3ee2", "metadata": {}, "source": [ "## Appendix - Alternative Black-Box Model" ] }, { "cell_type": "markdown", "id": "25835f0a", "metadata": {}, "source": [ "As we mentioned above the black-box model could be set up differently. We could omit income as an input, since we later want to make predictions without it. This changes the behavior of the black-box model quite a bit." ] }, { "cell_type": "code", "execution_count": 10, "id": "373b768c", "metadata": {}, "outputs": [], "source": [ "cs_bb2 = CausalStructure([[\n", " ['gas', 'coupons'], 'revenue']])\n", "cs_bb2.train(data)" ] }, { "cell_type": "code", "execution_count": 11, "id": "86c89766", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
couponsgasrevenue
00.01000.03431.674476
\n", "
" ], "text/plain": [ " coupons gas revenue\n", "0 0.0 1000.0 3431.674476" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cs_bb2.predict({\n", " 'gas': [1000.0],\n", " 'coupon': [0.30]\n", "})" ] }, { "cell_type": "markdown", "id": "f4abe31f", "metadata": {}, "source": [ "The predictions now behave more like the ones of the structured model. With a higher coupon rate the revenue now decreases. The black box model has implicitly learned, that coupons and revenue have another connection to each other via the income.\n", "\n", "Since it is a black-box model interventions make no difference." ] }, { "cell_type": "code", "execution_count": 12, "id": "3b466643", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
couponsgasrevenue
00.01000.03608.108074
\n", "
" ], "text/plain": [ " coupons gas revenue\n", "0 0.0 1000.0 3608.108074" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cs_bb2.predict_interventions(\n", " data={\n", " 'gas': [1000.0],\n", " },\n", " interventions={\n", " 'coupon': [0.30]\n", " }\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "0ccabbe7", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.8" } }, "nbformat": 4, "nbformat_minor": 5 }