Created
June 6, 2022 00:25
-
-
Save hassanj47/9e7f0b87b2139f2f7802ccb351d4bc12 to your computer and use it in GitHub Desktop.
Utils for survival analysis of time series models
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def survival_mape_exp(df, win_size): | |
'''Calculate expanding window mape for survival analysis.''' | |
df['ape'] = np.abs((df['yhat'] - df['y'])/df['y'])*100 | |
df['exp_mape'] = df.ape.expanding(win_size).mean() | |
df['survival_metric'] = df['exp_mape'] | |
return df | |
def survival_events(results): | |
'''Generate events and censors for survival analysis.''' | |
results = results[(results.index >= STUDY_START_WEEKS) & \ | |
(results.index < STUDY_END_WEEKS)] | |
thresh_res = results[results['survival_metric'] >= THRESH] | |
# if some results are above threshold | |
if len(thresh_res): | |
# if all results are above thresh - this could be considered | |
# as a case of left censorship - in our case we start evaluating | |
# the model at STUDY_START_WEEKS and for this case it makes sense | |
# to force right censorship | |
if len(thresh_res) == len(results): | |
thresh_week = STUDY_START_WEEKS | |
else: | |
thresh_week = thresh_res.index[0] + 1 | |
event = 1 | |
else: | |
# event does not occur till the end of study | |
thresh_week = STUDY_END_WEEKS | |
event = 0 | |
return thresh_week, event |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment