Evaluate early-warning signals of crises¶
Find out which indicators would have warned of banking crises in time, and how often they would have cried wolf: here five candidates for twelve countries, judged by their power to signal a crisis in the next two years against their false alarms, with the threshold a policymaker would set and how early the first warning came.
Before you start¶
- The events as a 0/1 series, 1 in every period of a crisis (a recession, debt distress, a default). This page uses the
example dataset
crisis_panel: twelve simulated countries, 1990Q1 to 2019Q4, with 17 banking crises whose chance of starting rises with the credit gap a year before. With your own data, load a panel with a country and a date column. - The candidates, as they were known at the time: gaps, ratios and growth rates rather than trending levels. Here the credit-to-GDP gap, the debt service ratio, the house price gap, reserves growth (which falls before crises) and a term spread that is pure noise, a control that should fail.
- How early a warning must come: the horizon. Two years (8 quarters) is common for banking crises, so that policy has time to act; and how many quarters after each crisis to leave out, since indicators then behave as neither calm nor pre-crisis (here 4).
- How much the policymaker minds a missed crisis against a false alarm: the weight mu. Crises are rare (here one quarter in nine is pre-crisis), so with mu = 0.5 never acting is hard to beat; 0.9 says a missed crisis costs nine false alarms.
In the app, or in code¶
Follow this step by step in the app
View → Guide → How do I → Evaluate early-warning signals of crises. The Guide panel takes you through these steps one at a time: Open…
opens each step's dialog filled in, Show me runs it, and each step is ticked when its result is there. In
the app's help, the page's Walk me through this opens it. The Guide is in the
pymetrix-guide plugin, which is experimental
(pip install --pre "pymetrix[guide]"; the walkthroughs come with its release after 0.1.0a1).
- File → Load example data…, with Example dataset: crisis_panel and Save as: panel.
- Forecast → Evaluate early-warning signals…, with Indicators: credit_gap, dsr, house_gap, reserves, spread, Events: crisis, Data: panel, Horizon (periods): 8, Post-event periods left out: 4, Weight on missed events (mu): 0.9 and Save as: ew.
- The window: Ranking is each indicator's AUROC with its interval and DeLong's p-value against the best, the threshold, the hit and false-alarm rates there, the noise-to-signal ratio, the usefulness, the crises called and the average lead time; ROC curves draws the two best with their thresholds; Signals the best indicator in one country with its threshold, the crises in grey and the two years before each in orange; Thresholds the trade-off as the threshold moves; Classification and Counts the best indicator's 2 x 2 table; Events each crisis, whether it was called and how early.
from pymetrix import Dataset, early_warning
panel = Dataset.example("crisis_panel")
ew = early_warning(panel, ["credit_gap", "dsr", "house_gap", "reserves", "spread"], "crisis",
horizon=8, post=4, mu=0.9)
ew.ranking # credit_gap: AUROC 0.855 (0.828 to 0.882), threshold 2.43, hit rate 0.912, false alarms 0.334
cls = ew.classification("credit_gap")
cls.counts() # 124 pre-crisis quarters signalled, 12 missed; 350 false alarms, 698 calm quarters quiet
cls.rates() # each rate with its Wilson interval: hit rate 0.912 (0.852 to 0.949)
ew.signal_plot("credit_gap", entity="C05") # the credit gap in country C05, its threshold and three crises
The pieces are functions of their own: the windows, the ROC analysis with a bootstrap that resamples whole countries, the threshold by another criterion, and a logit of the pre-crisis target on three indicators, its fitted probability evaluated as one more indicator.
from pymetrix import Equation, Logit, ML, choose_threshold, event_windows, roc_analysis
w = event_windows("crisis", horizon=8, post=4, data=panel)
w["status"].value_counts() # 1048 calm, 136 pre-event, 104 in a crisis, 68 post-crisis, 84 unknown (the last two years)
keep = w["include"]
by_country = roc_analysis(panel.frame.loc[keep, "credit_gap"], w.loc[keep, "target"], method="bootstrap",
resample="cluster", draws=1000) # 0.789 to 0.912: wider than DeLong's
klr = choose_threshold(panel.frame.loc[keep, "credit_gap"], w.loc[keep, "target"], criterion="noise_to_signal")
klr.chosen # threshold 6.07: hit rate 0.397, false alarms 0.067, noise-to-signal 0.168
data = panel.add_series("pre_crisis", w["target"].where(keep))
logit = Equation("pre_crisis ~ credit_gap + dsr + house_gap", family=Logit()).fit(data, method=ML())
data = data.add_series("p_crisis", logit.predict(data))
model = early_warning(data, ["p_crisis", "credit_gap"], "crisis", horizon=8, post=4, mu=0.9)
model.ranking # the logit 0.865 against the credit gap's 0.855; DeLong's p-value 0.011
Check the result¶
- The credit gap ranks first (AUROC 0.855), house prices and debt service next, reserves (signalling when low) fourth, and the noise spread last at 0.505, its interval, 0.453 to 0.556, around the 0.5 of no skill: the data were made that way. Every other indicator's DeLong p-value against the credit gap is below 0.001.
- The counts add up: 124 + 12 = 136 pre-crisis quarters and 350 + 698 = 1048 calm ones, 1184 evaluated. The 104 crisis quarters, the 68 within a year after one and the 84 at the end of each country's data whose next two years are not observed are left out.
- The usefulness by hand: P1 = 136 / 1184 = 0.115, so the loss is 0.9 x 0.088 x 0.115 + 0.1 x 0.334 x 0.885 = 0.039 against min(0.9 x 0.115, 0.1 x 0.885) = 0.089 for ignoring the indicator: a relative usefulness of 0.563.
- With mu = 0.9 the threshold is low and the credit gap calls all 17 crises, on average 7.6 quarters ahead, at the cost of a false alarm in a third of calm quarters. Minimising the noise-to-signal ratio instead sets it at 6.07: 15 of the 17 crises called, 6.0 quarters ahead on average, and false alarms in 6.7% of calm quarters.
- The AUROC is the Mann-Whitney statistic, so scikit-learn's
roc_auc_scoreon the same periods gives the same 0.855; the DeLong interval and test are R's pROC's (the validation register lists the check).
Pitfalls¶
- In-sample is flattering. The thresholds, the directions and the logit's coefficients are all chosen on the periods they are judged on. Judge a model out of sample (estimate on the years before each crisis) before relying on it, and treat a small gain in AUROC, as the logit's here, with suspicion.
- Quarters are not independent. The pre-crisis quarters of one crisis are eight readings of one event, and a country's quarters move together; DeLong's interval treats them as independent. Resample whole countries (AUROC interval: bootstrap, Resample: whole entities) for an honest interval.
- The weight mu decides the threshold. With rare events and mu = 0.5, no threshold beats never acting, and the chosen threshold signals almost never. Say which mu you used, and look at the Thresholds graph rather than one number.
- The noise-to-signal ratio rewards silence. A threshold so high that one lucky quarter signals has a ratio of zero; the default guard needs a tenth of the quarters to signal (Fewest signals, under Advanced settings).
- The last horizon is unknown. The final two years of each country cannot be judged calm, since a crisis may still follow; they are left out, so the latest readings are not in the evaluation.
See also¶
- Early-warning signals and ROC analysis, in the help.
- Make a surveillance heatmap: watch the same indicators each quarter.
- Check a forecast's record (backtest): judging forecasts of numbers rather than events.