How good a warning was¶
limen_core.verification ¶
How good a warning was: separation, contingency, calibration, lead time.
Limen measures itself in many places: a backtest per hazard, the measures of the radar and of the triggers, the training of the landslide model, the daily check of the events of the news. Each had its own copy of the same few numbers. They live here once, in plain Python (no numpy, no scikit-learn), so that a measure in Limen, in a notebook or in a library gives the same answer.
- Separation: :func:
auc(Mann-Whitney, ties count half), with the variants :func:auc_of(scores and labels), :func:paired_auc(each case against its own controls) and :func:stratified_auc(within strata, weighted by the pairs each holds). - Contingency at a threshold: :class:
Contingency, with hit rate, false alarm ratio, specificity, TSS and CSI; :func:threshold_for_recallfor the operating point that catches a given share of the events. - Cutoff at an alert budget: :func:
cutoff_for_share(the cutoff that fires a share of the values, and the share it really fires) and :func:cutoff_at_most(the cutoff that fires at most a share of many rows of values). - Calibration: :func:
brierand :func:reliability_bins. - Lead time: :func:
mean_lead_hours, and :func:event_outcome, the rules that call a warned event ahead, at the same time, missed or not verifiable, judging the forecast against the background of the place.
Not measured is not zero: a rate with an empty denominator is None, and a
None or NaN score is left out. These are measures, not alerts: the
official warning is the criticality bulletin of the Civil Protection
Department.
Contingency
dataclass
¶
The four counts of a yes/no warning against what happened.
hit_rate
property
¶
hit_rate: float | None
Of the events, the share warned (probability of detection, sensitivity).
false_alarm_ratio
property
¶
false_alarm_ratio: float | None
Of the warnings, the share with no event.
tss
property
¶
tss: float | None
True skill statistic: hit rate + specificity - 1, blind to how rare the events are.
at
classmethod
¶
at(labels: Sequence[bool], scores: Sequence[float], *, threshold: float) -> Contingency
The counts with a warning wherever score >= threshold.
ReliabilityBin
dataclass
¶
One band of forecast probability: what was said and how often it happened.
Run
dataclass
¶
One forecast run that aimed at the event, with the class it gave.
EventRecord
dataclass
¶
What the forecasts said about one event's place in the days before it.
None on a flag means not recorded (an older record), which never
downgrades the verdict; False means recorded as not done.
auc ¶
auc(cases: Iterable[float | None], controls: Iterable[float | None]) -> float | None
The chance that a case scores higher than a control; ties count half.
None, NaN and infinite scores are left out; None without a case or
without a control. With many tied scores (zeros, say) it measures the
order of the few that are not tied, and says nothing about the level.
auc_of ¶
auc_of(scores: Sequence[float | None], labels: Sequence[bool]) -> float | None
:func:auc from one list of scores and one of labels (True is a case).
paired_auc ¶
paired_auc(groups: Iterable[tuple[Sequence[float], Sequence[float]]]) -> tuple[float | None, int]
Each case against the controls of its own group only, and how many pairs.
A group is (cases, controls): a landslide and the places chosen for it
on the same day, say. A case above its control scores 1, a tie 0.5. The
answer is the share over every pair, None with no pair.
stratified_auc ¶
stratified_auc(scores: Sequence[float], labels: Sequence[bool], strata: Sequence[str]) -> tuple[float | None, int]
The AUC within each stratum, averaged with the weight of its pairs.
A stratum (a node of a grid and a band of height, say) compares a case
only with controls like it; one with no case or no control does not
count. Returns the average, None when no stratum counts, and how many
strata did.
quantile ¶
quantile(values: Sequence[float], q: float) -> float
The q quantile with linear interpolation (numpy's default).
threshold_for_recall ¶
threshold_for_recall(labels: Sequence[bool], scores: Sequence[float], recall: float) -> float | None
The highest threshold that still warns about recall of the events.
The 1 - recall quantile of the scores of the events: "what does it
cost to catch 70 % of them?". None with no event.
cutoff_for_share ¶
cutoff_for_share(values: Iterable[float], share: float) -> tuple[float, float]
The cutoff that fires the share share of values, and the share it really fires.
The cutoff is the k-th highest value, k = max(1, round(share * n)); a
value fires when it is >= the cutoff. On a tie at the k-th value the
share that really fires can exceed share. Zero never fires: a cutoff
of zero or below rises to the lowest positive value, inf when there is
none. Without values the answer is (inf, 0.0).
Limen's gate at equal load (#243, #352): two variants compared on the
events they catch when they fire the same share of cells. It is the
cutoff of rainfall_thresholds.measure.cutoff_at_budget(values, share,
floor=0), but it also returns the share that really fires, does not
drop None or NaN and does not check share. For a cutoff that
never fires more than the share, see :func:cutoff_at_most.
cutoff_at_most ¶
cutoff_at_most(rows: Iterable[Collection[float]], share: float) -> float
The cutoff that fires at most the share share of all the values of rows.
rows are the values in pieces (one per hour, say, each a list or an
array): they are read as one, with no copy, keeping only the
k + 1 highest (k = round(share * n)). A value fires when it is
>= the cutoff. When the values tied at the k-th would fire more than
k, the cutoff rises to the next higher distinct value (inf when
there is none), so a calibrated score with many equal steps cannot win
by firing more (Limen #483). A share that rounds to no value, and a
cutoff of zero or below, give inf: zero never fires.
Unlike :func:cutoff_for_share and
rainfall_thresholds.measure.cutoff_at_budget, which keep the k-th
value and let a tie exceed the share, this one never exceeds it.
brier ¶
brier(labels: Sequence[bool], probabilities: Sequence[float]) -> float | None
The mean squared difference between probability and outcome; None when empty.
reliability_bins ¶
reliability_bins(pairs: Iterable[tuple[float, bool]], *, n_bins: int = 10) -> list[ReliabilityBin]
The pairs (probability, outcome) in n_bins equal bands; empty bands left out.
A band whose predicted_mean is close to its observed_freq is well
calibrated.
mean_lead_hours ¶
mean_lead_hours(issued: Sequence[datetime], events: Sequence[datetime], *, hits: Sequence[bool]) -> float | None
The mean hours from warning to event over the hits that came before it; None if none.
lead_above_background ¶
lead_above_background(record: EventRecord) -> tuple[float, str] | None
Hours from the first run with a class above the background to the event.
With the highest class among those runs; None when none is above.
Unrounded. This is the lead that counts: with a High background, a High
forecast 72 hours ahead and a VeryHigh 20 hours ahead are 20 hours of
lead, not 72.
event_outcome ¶
event_outcome(record: EventRecord, *, min_lead_hours: float = MIN_LEAD_HOURS, background_span: timedelta = BACKGROUND_SPAN) -> Outcome
Was the event warned ahead, at the same time, missed, or can it not be told?
In order: an event before the forecast history, a background no longer
stored, a place with no cell, a hazard or a background not assessed, or
missing weather with nothing else to go on, are not verifiable. A run
above the background at least min_lead_hours ahead is ahead. The
hourly sweep above the background before the event is same time. With
no run that aimed at the event the silence is not calm: not verifiable.
Otherwise missed.
A forecast at the background class is not a signal: where a place sits at Moderate every day, a Moderate forecast is the ground, not a warning.