# Event Study script
set verbose off

# First step is to load our data into Gretl
open "NewEvent_gretl_YYYYMMDD.csv"
# After uploading our data, we need to compute returns
loop foreach i Apple Microsoft Google Nvidia Meta NASDAQ
    series Ret_$i_N = 100 * ldiff($i)
endloop

# Next, we need to estimate the market model for each firm in th estimation window
# R_it = alpha_i + beta_i * R_mt + eps_it
smpl 2023-10-03 2024-05-24

loop foreach i Apple Microsoft Google Nvidia Meta
    ols Ret_$i_N const Ret_NASDAQ_N --quiet
    scalar a_$i = $coeff(const)
    scalar b_$i = $coeff(Ret_NASDAQ_N)
    scalar sig2_$i = $sigma^2
endloop

# We will then compute expected returns and abnormal returns in the event window [-5,+25]
smpl 2024-06-03 2024-07-17
# Expected Returns(ER)
loop foreach i Apple Microsoft Google Nvidia Meta
    series Exp_$i_N = a_$i + b_$i * Ret_NASDAQ_N
    series AR_$i  = Ret_$i_N - Exp_$i_N
endloop

# We can now check the level of significance of the Abnormal returns for the event day (2024-06-10)
smpl 2024-06-10 2024-06-10
loop foreach i Apple Microsoft Google Nvidia Meta
    scalar AR_$i_0 = sum(AR_$i)
    scalar t_AR_$i_0 = AR_$i_0 / sqrt(sig2_$i)
endloop

# We will then estimate cumulative abnormal returns(CAR) for any event window
# [-5,-1]
smpl 2024-06-03 2024-06-07
scalar L_n5_n1 = $nobs
loop foreach i Apple Microsoft Google Nvidia Meta
    scalar CAR_$i_n5_n1 = sum(AR_$i)
    scalar t_CAR_$i_n5_n1 = CAR_$i_n5_n1 / sqrt(L_n5_n1 * sig2_$i)
    printf "%12s: %8.5f\n", "$i", t_CAR_$i_n5_n1
endloop    

# the rest is similar
