January 2020
Beginner to intermediate
432 pages
11h 24m
English
Execute the following steps to detect outliers using the 3σ approach, and mark them on a plot.
df_rolling = df[['simple_rtn']].rolling(window=21) \ .agg(['mean', 'std'])df_rolling.columns = df_rolling.columns.droplevel()
df_outliers = df.join(df_rolling)
def indentify_outliers(row, n_sigmas=3): x = row['simple_rtn'] mu = row['mean'] sigma = row['std'] if (x > mu + 3 * sigma) | (x < mu - 3 * sigma): return 1 else: return 0
df_outliers['outlier'] = df_outliers.apply(indentify_outliers, axis=1) outliers = df_outliers.loc[df_outliers['outlier'] ...
Read now
Unlock full access