I now have two DataFrames. One is called returns
, which contains data starting from 2001 with a shape of (5100, 5000). The other is called cs
, which has a similar structure to returns
but starts from 2007, with a shape of (2000, 5000).
The cs
DataFrame represents event data: if there’s an event, the value is 1
; otherwise, it’s NaN
.
Now, I’d like to perform an event study to analyze how cross-sectional return dispersion behaves before and after earnings announcements. Specifically, I want to visualize the pattern of volatility (i.e., cross-sectional dispersion) from –2 days to +2 days around the event date. The x-axis should represent days relative to the event (–2 to +2), and the y-axis should represent volatility (dispersion)
Then i had the codes like this:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
returns_aligned = returns.loc[cs.index]
daily_volatility = returns_aligned.std(axis=1)
event_dates = cs.dropna(how='all').index
window_days = [-2, -1, 0, 1, 2]
volatility_pattern = []
for day in window_days:
day_volatilities = []
for event_date in event_dates:
target_date = event_date + pd.Timedelta(days=day)
if target_date in daily_volatility.index:
day_volatilities.append(daily_volatility[target_date])
if day_volatilities:
avg_vol = np.mean(day_volatilities)
volatility_pattern.append(avg_vol)
else:
volatility_pattern.append(0)
I expected the plot is like a V shape, but it turns out an upward line..
Who can tell me whats wrong with the code...
submitted by /u/ddldog_
[link] [comments]