diff options
Diffstat (limited to 'python/exploration')
| -rw-r--r-- | python/exploration/dispersion.py | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/python/exploration/dispersion.py b/python/exploration/dispersion.py index fac383c5..b33f2796 100644 --- a/python/exploration/dispersion.py +++ b/python/exploration/dispersion.py @@ -104,9 +104,54 @@ def create_models(conn, df) -> (pd.DataFrame, float): return temp df["predict"] = df.groupby(["index", "series", "date"])["predict"].transform(aux) + df = df.assign( + mispricing=(df.exp_percentage - df.predict) + * df.index_expected_loss + / (df.detach_adj - df.attach_adj) + ) return (df, model) +def create_models_separate(df): + # Takes the output of get_tranche_data + model, calc = {}, {} + df = df.assign( + tranche_loss_per=(df.exp_percentage * df.index_expected_loss) + / (df.detach_adj - df.attach_adj) + ) + df = df.groupby(["date", "index", "series", "tenor", "attach"]).nth(-1) + for attach in df.index.get_level_values("attach").unique(): + calc[attach] = df.loc(axis=0)[:, :, :, "5yr", attach] + model[attach] = smf.ols( + "logit(tranche_loss_per) ~ " + "np.log(moneyness)* logit(gini) + " + "np.log(index_expected_loss)* logit(gini) + " + "np.log(index_duration) + " + "I(np.log(moneyness)**2) + I(np.log(moneyness)**3)", + data=calc[attach], + ).fit() + + calc[attach] = calc[attach].assign( + predict=expit(model[attach].predict(calc[attach])) + * (df.detach_adj - df.attach_adj) + / df.index_expected_loss + ) + + calc = pd.concat(calc, sort=False).reset_index(level=0, drop=True) + normalization = calc.groupby(["date", "index", "series", "tenor"])["predict"].sum() + calc = calc.merge( + normalization, left_index=True, right_index=True, suffixes=["_preN", "_sum"] + ) + calc["predict_N"] = calc["predict_preN"] / calc["predict_sum"] + calc["mispricing"] = ( + (calc["exp_percentage"] - calc["predict_N"]) + * calc["index_expected_loss"] + / (calc["detach_adj"] - calc["attach_adj"]) + * 100 + ) + return model, calc + + if __name__ == "__main__": index_type = "HY" series = 29 |
