
import numpy as np
#########
mobility_deviation = pd.read_excel(r'china mobility percent deviation from baseline.xlsx')

plt.figure(figsize=(10,8))
plt.plot(mobility_deviation['date'], mobility_deviation['percent_deviation'])

plt.title("China Mobility, Percent Deviation from Baseline", fontsize = 23)
plt.ylim( -70, 10)
plt.rc('ytick', labelsize=18) 
plt.xticks( rotation=45, fontsize="18", ha = 'center') 
plt.tight_layout()




os.chdir(r'C:\Users\dingq\Documents\200_academic\220_Academic_My dissertation\220_papaer_preperation\covid\CSMAR\4 sets firms')
only_a = pd.read_csv(r'AH share return_v2.csv')

only_a = only_a[['date','a_return_from1_13_onlyA']]
only_a = only_a.iloc[7:]
only_a = only_a.reset_index(drop = True)
only_a['date'] = pd.to_datetime(only_a['date'])
only_a['a_return_from1_13_onlyA'] = only_a['a_return_from1_13_onlyA'].interpolate(method = 'linear')

only_a_mobility = pd.merge(mobility_deviation, only_a, on = 'date', how ='inner')
only_a_mobility['percent_deviation'].iloc[9:15] = np.nan
only_a_mobility['percent_deviation'] = only_a_mobility['percent_deviation'].interpolate(method = 'linear')

# to import stringency index and death
strin_death = pd.read_excel(r'china stringency and death cases.xlsx')
strin_death['date'] = pd.to_datetime(strin_death['date'], format = '%Y%m%d')

only_a_mobility_strin_death = pd.merge(only_a_mobility, strin_death, on = 'date', how = 'inner')


### plot the time path; SSE use dashline from 1/24, 1/27 to 1/31; 
plt.figure(figsize=(7.2,5.8))
plt.rcParams.update({'font.size': 17.5})

x = only_a_mobility_strin_death['percent_deviation']
y = only_a_mobility_strin_death['a_return_from1_13_onlyA']

x0 =  only_a_mobility_strin_death['percent_deviation'].iloc[9:15]
y0 = only_a_mobility_strin_death['a_return_from1_13_onlyA'].iloc[9:15]
plt.plot(x, y,'-o')
plt.plot(x0, y0,'--', color = 'red')


plt.ylim(-30,30)
plt.xlim(-70,10)

x1 = only_a_mobility_strin_death.loc[only_a_mobility_strin_death.new_death>0]['percent_deviation'].iloc[0]
y1 = only_a_mobility_strin_death.loc[only_a_mobility_strin_death.new_death>0]['a_return_from1_13_onlyA'].iloc[0]
z1 = only_a_mobility_strin_death.loc[only_a_mobility_strin_death.new_death>0]['date'].dt.strftime('%m/%d').iloc[0]
plt.scatter(x1,y1, marker = 'D', s =200, c = 'darkorange')
plt.annotate(z1, (x1, y1), textcoords="offset points",arrowprops=dict(arrowstyle="->",
                    connectionstyle="arc3"),
             xytext=(10,-35),ha='center')

x2 = only_a_mobility_strin_death.loc[only_a_mobility_strin_death['stringency'] >70]['percent_deviation'].iloc[0]
y2 = only_a_mobility_strin_death.loc[only_a_mobility_strin_death['stringency'] >70]['a_return_from1_13_onlyA'].iloc[0]
z2 = only_a_mobility_strin_death.loc[only_a_mobility_strin_death['stringency'] >70]['date'].dt.strftime('%m/%d').iloc[0]
plt.scatter(x2,y2, marker = 'X', s =200, c = 'limegreen')
plt.annotate(z2, (x2, y2), textcoords="offset points",arrowprops=dict(arrowstyle="->",
                    connectionstyle="arc3"),
             xytext=(-15,-45),ha='center') #, bbox=dict(boxstyle="round4", fc="w")

c3 = only_a_mobility_strin_death.iloc[-30:]
x3 = c3.loc[c3['stringency'] <=70]['percent_deviation'].iloc[0]
y3 = c3.loc[c3['stringency'] <=70]['a_return_from1_13_onlyA'].iloc[0]
z3 = c3.loc[c3['stringency'] <=70]['date'].dt.strftime('%m/%d').iloc[0]
plt.scatter(x3,y3, marker = 'o', s =200, c = 'crimson')
plt.annotate(z3,
    xy=(x3, y3), xycoords='data',
    xytext=(-15, -20), textcoords='offset points',
    arrowprops=dict(arrowstyle="->",
                    connectionstyle="arc3"))


dates0 = only_a_mobility_strin_death.date
dates0 = dates0.dt.strftime('%m/%d')
plt.annotate(dates0.iloc[0], (x.iloc[0], y.iloc[0]), textcoords="offset points",
             xytext=(0,10),ha='center')
plt.annotate(dates0.iloc[-1], (x.iloc[-1], y.iloc[-1]), textcoords="offset points",
         xytext=(0,5),ha='center', arrowprops=dict(arrowstyle="->",
                        connectionstyle="arc3"))

plt.title('China')
plt.xlabel("Mobility, Percentage Change from Baseline")
plt.ylabel("Only A-shares Percent Deviation from Jan. 13")
plt.tight_layout()
