<s> '''<STR_LIT>''' <EOL> import numpy as np <EOL> import matplotlib . pyplot as plt <EOL> import QSTK . qstkutil . DataAccess as da <EOL> import QSTK . qstkutil . tsutil as tsu <EOL> import QSTK . qstkutil . qsdateutil as du <EOL> def eventprofiler ( df_events_arg , d_data , i_lookback = <NUM_LIT:20> , i_lookforward = <NUM_LIT:20> , <EOL> s_filename = '<STR_LIT>' , b_market_neutral = True , b_errorbars = True , <EOL> s_market_sym = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> df_close = d_data [ '<STR_LIT>' ] . copy ( ) <EOL> df_rets = df_close . copy ( ) <EOL> df_events = df_events_arg . copy ( ) <EOL> tsu . returnize0 ( df_rets . values ) <EOL> if b_market_neutral == True : <EOL> df_rets = df_rets - df_rets [ s_market_sym ] <EOL> del df_rets [ s_market_sym ] <EOL> del df_events [ s_market_sym ] <EOL> df_close = df_close . reindex ( columns = df_events . columns ) <EOL> df_events . values [ <NUM_LIT:0> : i_lookback , : ] = np . NaN <EOL> df_events . values [ - i_lookforward : , : ] = np . NaN <EOL> i_no_events = int ( np . logical_not ( np . isnan ( df_events . values ) ) . sum ( ) ) <EOL> assert i_no_events > <NUM_LIT:0> , "<STR_LIT>" <EOL> na_event_rets = "<STR_LIT:False>" <EOL> for i , s_sym in enumerate ( df_events . columns ) : <EOL> for j , dt_date in enumerate ( df_events . index ) : <EOL> if df_events [ s_sym ] [ dt_date ] == <NUM_LIT:1> : <EOL> na_ret = df_rets [ s_sym ] [ j - i_lookback : j + <NUM_LIT:1> + i_lookforward ] <EOL> if type ( na_event_rets ) == type ( "<STR_LIT>" ) : <EOL> na_event_rets = na_ret <EOL> else : <EOL> na_event_rets = np . vstack ( ( na_event_rets , na_ret ) ) <EOL> if len ( na_event_rets . shape ) == <NUM_LIT:1> : <EOL> na_event_rets = np . expand_dims ( na_event_rets , axis = <NUM_LIT:0> ) <EOL> na_event_rets = np . cumprod ( na_event_rets + <NUM_LIT:1> , axis = <NUM_LIT:1> ) <EOL> na_event_rets = ( na_event_rets . T / na_event_rets [ : , i_lookback ] ) . T <EOL> na_mean = np . mean ( na_event_rets , axis = <NUM_LIT:0> ) <EOL> na_std = np . std ( na_event_rets , axis = <NUM_LIT:0> ) <EOL> li_time = range ( - i_lookback , i_lookforward + <NUM_LIT:1> ) <EOL> plt . clf ( ) <EOL> plt . axhline ( y = <NUM_LIT:1.0> , xmin = - i_lookback , xmax = i_lookforward , color = '<STR_LIT:k>' ) <EOL> if b_errorbars == True : <EOL> plt . errorbar ( li_time [ i_lookback : ] , na_mean [ i_lookback : ] , <EOL> yerr = na_std [ i_lookback : ] , ecolor = '<STR_LIT>' , <EOL> alpha = <NUM_LIT> ) <EOL> plt . plot ( li_time , na_mean , linewidth = <NUM_LIT:3> , label = '<STR_LIT>' , color = '<STR_LIT:b>' ) <EOL> plt . xlim ( - i_lookback - <NUM_LIT:1> , i_lookforward + <NUM_LIT:1> ) <EOL> if b_market_neutral == True : <EOL> plt . title ( '<STR_LIT>' + str ( i_no_events ) + '<STR_LIT>' ) <EOL> else : <EOL> plt . title ( '<STR_LIT>' + str ( i_no_events ) + '<STR_LIT>' ) <EOL> plt . xlabel ( '<STR_LIT>' ) <EOL> plt . ylabel ( '<STR_LIT>' ) <EOL> plt . savefig ( s_filename , format = '<STR_LIT>' ) </s>
