Policy rules#
Measure the policy rules of a credit strategy. rule_table() counts the hits and the events of each rule of a set, and rule_set_table() applies the rules in order, as a decision waterfall, with the new hits of each rule and the book that the rules leave approved.
See Policy rules for examples on the simulated applications, the overlap of two rules and the outcomes of declined applicants.
|
Count the hits of each rule, those it alone hits and, with a target, their events. |
|
Apply the rules in order and summarize what each adds and what they leave approved. |
- kivi.rules.rule_table(rules, target=None, *, weights=None)#
Count the hits of each rule, those it alone hits and, with a target, their events.
Each rule is evaluated on every row, whatever the other rules do, so an applicant that two rules hit counts in both rows. The unique hits are the applicants that only this rule of the set hits, the declines that rest on this rule alone.
- Parameters:
- rulespandas.DataFrame
One column per rule, named after the rule, and one row per applicant. Each column holds booleans, or 0 and 1, with True or 1 where the rule hits, such as a decline or a referral. Missing values are not allowed: decide first whether the rule hits when its data are missing, for example with
fillna.- targetarray_like, optional
Binary labels, with 1 indicating the event, and missing values for unknown outcomes, such as those of declined applicants. Without target, the table only counts the hits. If target is a Series, it must have the index of rules; otherwise it is matched by position.
- weightsarray_like, optional
Non-negative frequency weight of each row, such as the inverse of its sampling rate, matched like target. Counts become sums of weights, so integer weights give the same table as repeating rows.
- Returns:
- pandas.DataFrame
One row per rule, in the order of the columns of rules, indexed by the rule names in an index named
rule. The columns arehits,hit_rateandunique_hits, and with target alsoknown,event,event_rate,lift,recallandunique_event; see the Notes.
- Raises:
- ValueError
If rules is not a DataFrame, has no rows or no columns, more than one level of column names or a repeated name, or a column with a missing value or a value other than True, False, 0 and 1. Also if target is not one-dimensional, does not give each row one label or has a label other than 0, 1 and missing, weights is not numeric or does not give each row one finite, non-negative weight with a positive total, or a Series argument has an index other than that of rules.
See also
rule_set_tableApply the rules in order, as a decision waterfall.
kivi.strategy.cutoff_tableThe same summary for the cut-offs of a score.
kivi.strategy.swap_setCompare the decisions of two scores.
Notes
For a rule that hits the rows \(H\), with weights \(w_i\) and the known outcomes \(K\) and events \(E\) of all rows, the columns are:
hits: \(\sum_{i \in H} w_i\), the number of rows the rule hits without weights;hit_rate:hitsdivided by the whole sample;unique_hits: the rows that this rule hits and no other rule of rules does;known: the hits with a known outcome, \(H \cap K\);event: the events among the hits, \(H \cap E\);event_rate:eventdivided byknown, NaN when no hit has a known outcome;lift:event_ratedivided by the event rate of all known outcomes, \(\sum_{E} w_i / \sum_{K} w_i\), so a rule whose hits default twice as often as the sample has a lift of 2. It is NaN when the sample has no known event;recall:eventdivided by all known events, the share of the events that the rule hits, NaN without known events;unique_event: the events among the unique hits.
The rows do not add up, because one row can hit under several rules: rule_set_table counts each hit once. The overlap of two rules is the product of their columns,
rules.astype(int).T @ rules.astype(int)for every pair, with the hits on the diagonal.The outcome of a declined applicant is usually unknown. Pass missing labels for it: the applicant then counts in
hits,hit_rateandunique_hitsbut not inknown,eventand the rates, so a rule that has declined every applicant it hits has no known outcome at all.Examples
>>> import pandas as pd >>> from kivi.rules import rule_table >>> rules = pd.DataFrame({ ... "high_debt": [1, 1, 0, 0, 0, 1, 0, 0], ... "young": [0, 1, 1, 0, 0, 0, 0, 0], ... "inquiries": [0, 0, 1, 1, 0, 0, 0, 0], ... }) >>> target = [1, 1, 0, 1, 0, 0, 0, 0] >>> table = rule_table(rules, target) >>> table[["hits", "hit_rate", "unique_hits", "known", "event"]] hits hit_rate unique_hits known event rule high_debt 3 0.375 2 3 2 young 2 0.250 0 2 1 inquiries 2 0.250 1 2 1 >>> table[["event_rate", "lift", "recall", "unique_event"]].round(3) event_rate lift recall unique_event rule high_debt 0.667 1.778 0.667 1 young 0.500 1.333 0.333 0 inquiries 0.500 1.333 0.333 1
The outcomes of the applicants that a rule declined are often unknown:
>>> observed = [None, None, 0, 1, 0, None, 0, 0] >>> rule_table(rules, observed)[["hits", "known", "event", "event_rate"]] hits known event event_rate rule high_debt 3 0 0 NaN young 2 1 0 0.0 inquiries 2 2 1 0.5
- kivi.rules.rule_set_table(rules, target=None, *, weights=None)#
Apply the rules in order and summarize what each adds and what they leave approved.
The rules form a decision waterfall: the first rule declines the rows it hits, the second those of the remaining rows that it hits, and so on. Each row shows the new hits of a rule, those that no earlier rule hit, and the book that the rules up to it leave approved, with the columns of kivi.strategy.cutoff_table.
- Parameters:
- rulespandas.DataFrame
One column per rule, in the order in which the rules apply, and one row per applicant. Each column holds booleans, or 0 and 1, with True or 1 where the rule hits. Missing values are not allowed: decide first whether the rule hits when its data are missing.
- targetarray_like, optional
Binary labels, with 1 indicating the event, and missing values for unknown outcomes, such as those of declined applicants. Without target, the table only counts the hits and the approved rows. If target is a Series, it must have the index of rules; otherwise it is matched by position.
- weightsarray_like, optional
Non-negative frequency weight of each row, such as the inverse of its sampling rate, matched like target. Counts become sums of weights, so integer weights give the same table as repeating rows.
- Returns:
- pandas.DataFrame
One row per rule, in the order of the columns of rules, indexed by the rule names in an index named
rule. The columns arenew_hits, and with targetnew_event, thenapprovedandapproval_rate, and with target alsoapproved_event,approved_event_rate,rejected_event_shareandrejected_non_event_share; see the Notes.
- Raises:
- ValueError
If rules is not a DataFrame, has no rows or no columns, more than one level of column names or a repeated name, or a column with a missing value or a value other than True, False, 0 and 1. Also if target is not one-dimensional, does not give each row one label or has a label other than 0, 1 and missing, weights is not numeric or does not give each row one finite, non-negative weight with a positive total, or a Series argument has an index other than that of rules.
See also
rule_tableThe hits of each rule on its own.
kivi.strategy.cutoff_tableThe same cumulative columns for the cut-offs of a score.
Notes
The new hits of a rule are the rows that it hits and that no rule before it hits. The columns are:
new_hits: the new hits of the rule, or the sum of their weights;new_event: the events among the new hits;approved: the rows that no rule up to this one hits;approval_rate:approveddivided by the whole sample;approved_event: the events among the approved rows;approved_event_rate:approved_eventdivided by the approved rows with a known outcome, NaN when none is known;rejected_event_shareandrejected_non_event_share: the shares of all known events and of all known non-events that a rule up to this one hits, NaN when the sample has no known event or non-event.
Every hit counts once, under the first rule that hits it, so the new hits of all rules add up to the rows that any rule hits. The new hits of a rule depend on the rules before it, and moving a rule forward gives it more. The last row describes the whole set: its cumulative columns do not depend on the order of the rules.
With every outcome known, the cumulative columns of a row equal those of
kivi.strategy.cutoff_table(score, target, cutoffs=[0])for a score that is 1 where a rule up to that row hits and 0 elsewhere. With unknown outcomes,approvedandapproval_ratestill count every row, while the event rate and the shares count only the known outcomes.Examples
>>> import pandas as pd >>> from kivi.rules import rule_set_table >>> rules = pd.DataFrame({ ... "high_debt": [1, 1, 0, 0, 0, 1, 0, 0], ... "young": [0, 1, 1, 0, 0, 0, 0, 0], ... "inquiries": [0, 0, 1, 1, 0, 0, 0, 0], ... }) >>> target = [1, 1, 0, 1, 0, 0, 0, 0] >>> table = rule_set_table(rules, target) >>> table[["new_hits", "new_event", "approved", "approval_rate"]] new_hits new_event approved approval_rate rule high_debt 3 2 5 0.625 young 1 0 4 0.500 inquiries 1 1 3 0.375 >>> table[["approved_event", "approved_event_rate"]] approved_event approved_event_rate rule high_debt 1 0.20 young 1 0.25 inquiries 0 0.00 >>> table[["rejected_event_share", "rejected_non_event_share"]].round(3) rejected_event_share rejected_non_event_share rule high_debt 0.667 0.2 young 0.667 0.4 inquiries 1.000 0.4
Applying
inquiriesfirst gives it both of its hits and leaveshigh_debttwo new ones, while the last row stays the same:>>> rule_set_table(rules[["inquiries", "young", "high_debt"]], target)[ ... ["new_hits", "new_event", "approved", "approved_event"] ... ] new_hits new_event approved approved_event rule inquiries 2 1 6 2 young 1 1 5 1 high_debt 2 1 3 0