<jupyter_start><jupyter_text>How  do the infected cases evolve for a country (response) as a function of countermeasures (treatment)?  Data and Descriprion------------ BGS Government Response Tracker. See here (https://www.bsg.ox.ac.uk/news/worlds-first-covid-19-government-response-tracker-launched-today)- Data contains countermeasures introduced by different countries (with their dates) along with a Stringency Index. Note that Stringency Index is a time varying i.e. countries probably were probably 'less stringent' at the beginningHypothesis------------Countermeasures introduced by Governments slow the rate of infection. What we would like to uncover is the causal estimate, i.e. what is the rate of new infections as a response to a particular countermeasure?<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')
from dowhy import CausalModel
from IPython.display import Image, display
import dowhy.plotter
np.random.seed(42)
%matplotlib inline
import logging
logging.getLogger().setLevel(logging.CRITICAL)
from IPython.display import HTML

HTML('''<script>
code_show=true; 
function code_toggle() {
 if (code_show){
 $('div.input').hide();
 } else {
 $('div.input').show();
 }
 code_show = !code_show
} 
$( document ).ready(code_toggle);
</script>
<form action="javascript:code_toggle()"><input type="submit" value="Click here to toggle on/off the raw code."></form>''')
df_all = pd.read_excel('OxCGRT_Download_latest_data.xlsx')
# Reform col names by replacing empty spaces with _
df_all.columns = df_all.columns.str.replace(' ', '_')<jupyter_output><empty_output><jupyter_text>Methodology--------------------------We consider the Confirmed cases for China as it seems to have been only country that has navigated the S-curve (as modelled by SIR dynamics), as seen in the figure below.<jupyter_code>df = df_all[df_all['CountryName']=='China']
x_vals = list(range(len(df)))
df['Delta7'] = df['ConfirmedCases'].values-df['ConfirmedCases'].shift(7).values
df['Delta14'] = df['ConfirmedCases'].values-df['ConfirmedCases'].shift(14).values

#df['Delta7'] = df['Delta7'].diff()
#df['Delta14'] = df['Delta14'].diff()
fig,ax = plt.subplots(4,1,sharex=True,figsize=(8,20))



df.replace(np.nan,0,inplace=True)
ax[0].plot(x_vals,df['S1_School_closing'],'b')
ax[0].plot(x_vals,df['S2_IsGeneral'],'g')
ax[0].plot(x_vals,df['S3_Cancel_public_events'],'y')
ax[0].plot(x_vals,df['S4_Close_public_transport'],'r')
ax[0].plot(x_vals,df['S5_Public_information_campaigns'],'m')
ax[0].plot(x_vals,df['S6_Restrictions_on_internal_movement'],'k')
ax[0].plot(x_vals,df['S7_International_travel_controls'],'c')
ax[0].plot(x_vals,df['S9_Monetary_measures'],'brown')
#ax[0].plot(df['S10_Emergency investment in health care'],'navy')
ax[0].legend(['school close','general','public events',
                 'no oevv','info','movectrl','intlmovectrl','money','healthcare spend'],prop={'size': 6})

ax[1].plot(x_vals,df['StringencyIndex'],'r')
ax[1].set_ylabel('StringencyIndex')
ax[3].plot(x_vals,df['ConfirmedCases'],'y')
ax[3].set_ylabel('Confirmed cases')
ax[3].set_xlabel('# Days since outbreak')

ax[2].plot(x_vals,df['Delta7'],'b')
ax[2].plot(x_vals,df['Delta14'],'k')
ax[2].legend(['Delta 7 days','Delta 14 days'],prop={'size':10});<jupyter_output><empty_output><jupyter_text>A simple causal model is built with the cause effect relationships (as seen in the causal graph). I assume that Stringency Index influences all forms of mobility, travel and closures. It is a beginning, however it is a strong assumption which needs to be validated with Domain Experts.Note on Delta7: Delta 7 is the difference between current value of Confirmed Cases and the Confirmed Cases 7 days ago.<jupyter_code>from graphviz import Source
s = Source.from_file('causal_model.dot',format='png')
Image(s.view())<jupyter_output><empty_output><jupyter_text>Model and Effects--------------------------------With the causal graph modelled above, consider each of the effects and estimate effect using a linear regression with backdoor adjustment along with the p - values<jupyter_code>for _treatment in ['S1_School_closing','S3_Cancel_public_events','S4_Close_public_transport','S6_Restrictions_on_internal_movement']:
    model = CausalModel(data=df,treatment=[_treatment],outcome='Delta7',instruments=[],graph='causal_model.dot',proceed_when_unidentifiable=True)
    causal_estimate_reg = model.estimate_effect(model.identify_effect(),method_name='backdoor.linear_regression',
                                                test_significance=True)
    print('%s->%s-->%s'%(_treatment,causal_estimate_reg.value,causal_estimate_reg.significance_test['p_value']))<jupyter_output>INFO:dowhy.causal_graph:If this is observed data (not from a randomized experiment), there might always be missing confounders. Adding a node named "Unobserved Confounders" to reflect this.
INFO:dowhy.causal_model:Model to find the causal effect of treatment ['S1_School_closing'] on outcome ['Delta7']
INFO:dowhy.causal_identifier:Common causes of treatment and outcome:['StringencyIndex', 'U']
WARNING:dowhy.causal_identifier:If this is observed data (not from a randomized experiment), there might always be missing confounders. Causal effect cannot be identified perfectly.
INFO:dowhy.causal_identifier:Continuing by ignoring these unobserved confounders because proceed_when_unidentifiable flag is True.
INFO:dowhy.causal_identifier:Instrumental variables for treatment and outcome:[]
INFO:dowhy.causal_estimator:INFO: Using Linear Regression Estimator
INFO:dowhy.causal_estimator:b: Delta7~S1_School_closing+StringencyIndex+S1_School_closing*S5_Public_information_campaigns+S1_School_closing*S4[...]
{'fraction_non_alphanumeric': 0.13906822544058628, 'fraction_numerical': 0.013086721340080265, 'mean_word_length': 6.571994715984148, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '570083', 'license': 'MIT', 'n_tokens_mistral': 1885, 'n_tokens_neox': 1778, 'n_words': 444, 'path': 'code/notebooks/causality/Intervention-response.ipynb', 'repo_name': 'dazzag24/EMER2GENT', 'size': '5731'}