Population stability index#
Measure how far a distribution has shifted between two samples. psi_table() and psi() give the population stability index, and csi_table() gives the change of a scorecard’s average points that the shift causes.
See Population stability index for examples and the usual thresholds.
|
Compare two samples using their population stability index. |
|
Compute the population stability index from counts per group. |
|
Measure how much a variable's shift moves the average score. |
- kivi.stability.psi_table(reference, current, *, smoothing=0.5, reference_weights=None, current_weights=None)#
Compare two samples using their population stability index.
Each group’s share of the current sample is compared with its share of the reference sample. Sum the
psicolumn for the index.- Parameters:
- reference, currentarray_like
Labels or values binned using the same edges. Missing values and categories observed in only one sample are included.
- smoothingfloat, default 0.5
Positive replacement for a zero count when calculating shares.
- reference_weights, current_weightsarray_like, optional
Non-negative weight of each observation in the reference and the current sample, matched like the samples. The counts are then sums of weights, and groups whose weights sum to zero in both samples are left out. smoothing is then in units of weight.
- Returns:
- pandas.DataFrame
Counts, normalized shares and the PSI contribution of each group. Sum the psi column for the population stability index.
- Raises:
- ValueError
If either sample is empty or not one-dimensional, or smoothing is not finite and positive. Also if a weight array is not numeric, does not give each observation of its sample one finite, non-negative weight with a positive total, or has an index other than that of its sample.
See also
psiThe same index from counts per group.
csi_tableWeight the change of each group’s share with its points.
kivi.binning.bin_edgesFit edges on the reference sample.
kivi.woe.woe_tableRelate the groups to a binary target.
Notes
For group \(i\), let \(q_i\) be its share of the reference sample and \(p_i\) its share of the current sample. Then
\[\mathrm{PSI}_i = (p_i - q_i) \ln \frac{p_i}{q_i}, \qquad \mathrm{PSI} = \sum_i \mathrm{PSI}_i.\]Each zero count is replaced with smoothing before the shares are normalized, so a group observed in only one sample has a finite contribution. The
referenceandcurrentcolumns always report the observed counts.If both samples are categorical with the same dtype, rows follow the order of the categories with missing values last. Otherwise they follow the order in which the groups first appear, in the reference sample and then in the current sample.
References
[1]N. Siddiqi, “Credit Risk Scorecards: Developing and Implementing Intelligent Credit Scoring”, Wiley, 2006.
Examples
>>> import pandas as pd >>> from kivi.stability import psi_table >>> reference = pd.Series(["x", "x", "y", "y"]) >>> current = pd.Series(["x", "y", "y", "y"]) >>> psi_table(reference, current)[["reference", "current", "psi"]].round(4) reference current psi bin x 2 1 0.1733 y 2 3 0.1014
- kivi.stability.psi(reference_counts, current_counts, *, smoothing=0.5)#
Compute the population stability index from counts per group.
Use this when the samples are already aggregated, for example monthly counts per score band from a monitoring report.
- Parameters:
- reference_counts, current_countsarray_like
One-dimensional, non-negative counts of each group in the reference and the current sample, in the same group order. Two Series must have identical indices. Weighted counts need not be integers.
- smoothingfloat, default 0.5
Positive replacement for a zero count when calculating shares.
- Returns:
- float
The population stability index.
- Raises:
- ValueError
If the counts are not one-dimensional, numeric, finite and non-negative, have different lengths or a zero total, or are two Series with different indices, or smoothing is not finite and positive.
See also
psi_tableThe index and its contributions from group labels.
kivi.woe.ivThe same divergence between events and non-events.
Notes
With \(q_i\) and \(p_i\) the smoothed shares of group \(i\) in the reference and the current sample,
\[\mathrm{PSI} = \sum_i (p_i - q_i) \ln \frac{p_i}{q_i},\]which equals the sum of the
psicolumn of psi_table for the same groups.Examples
>>> from kivi.stability import psi >>> round(psi([2, 2], [1, 3]), 4) 0.2747
- kivi.stability.csi_table(reference, current, points, *, unseen_points=None, reference_weights=None, current_weights=None)#
Measure how much a variable’s shift moves the average score.
The characteristic stability index (CSI) weights the change of each group’s share, from the reference to the current sample, with the scorecard points of the group. Sum the
csicolumn for the change of the variable’s average points.- Parameters:
- reference, currentarray_like
Labels or values binned using the same edges, as for psi_table. Missing values and groups observed in only one sample are included.
- pointspandas.DataFrame, pandas.Series or mapping
Points of each group. A DataFrame holds the groups of one variable in a
bincolumn, or in its index without one, and their points in apointscolumn, such astable.loc[[variable]]of a kivi.scorecard.points_table result. The variable may be named in the index or in avariablecolumn, as afterreset_index(), but only one variable may appear. A Series or mapping gives the points by group. A missing label gives the points of missing values.- unseen_pointsfloat, optional
Points of observed groups that are missing from points, such as a new category that kivi.woe.woe_transform encoded with
unseen. Its applicants receive the kivi.scorecard.woe_to_points result for that WOE, not the WOE itself; see the Notes. By default such groups raise an error.- reference_weights, current_weightsarray_like, optional
Non-negative weight of each observation in the reference and the current sample, matched like the samples. The counts and shares are then sums of weights, and groups whose weights sum to zero in both samples are left out.
- Returns:
- pandas.DataFrame
One row per observed group, indexed by
bin, with the countsreferenceandcurrent, their sharesreference_shareandcurrent_share, the group’spointsand its contributioncsi. Sum the csi column for the index.
- Raises:
- ValueError
If either sample is empty or not one-dimensional, points is not a DataFrame, Series or mapping, holds more than one variable, repeats a group or has values that are not finite numbers, unseen_points is not a finite number, or an observed group has no points and unseen_points is not given. Also if a weight array is invalid, as in psi_table.
See also
psi_tableThe population stability index of the same groups.
kivi.scorecard.points_tableThe points of every group of a scorecard.
kivi.binning.bin_edgesFit edges on the reference sample.
Notes
For group \(i\) with points \(s_i\), let \(q_i\) be its share of the reference sample and \(p_i\) its share of the current sample. Then
\[\mathrm{CSI}_i = (p_i - q_i)\, s_i, \qquad \mathrm{CSI} = \sum_i \mathrm{CSI}_i = \sum_i p_i s_i - \sum_i q_i s_i,\]the average points of the variable in the current sample minus those in the reference sample [1]. The shares sum to 1 in both samples, so adding the same number to the points of every group leaves the CSI unchanged: only the differences between the groups count, not the share of the intercept and offset that kivi.scorecard.points_table gives each variable. Over all variables of a scorecard, the CSI values add up to the change of the average score.
Unlike in psi_table, the shares are not smoothed. A contribution has no logarithm, so an empty group contributes a finite amount without smoothing, and a smoothed share would credit an empty group with applicants it does not have, so the sum would no longer be the change of the average points.
unseen_points is in points, like points. For applicants that
woe_transform(..., unseen=woe)encoded, it iswoe_to_pointsof a one-row frame that holds that WOE; a WOE of 0 gives the variable’s share of the intercept and offset, the same for every variable. Passing the WOE itself would give a wrong CSI without an error.Some texts also call the PSI of a single variable its CSI; that is psi_table of the variable’s groups. Rows are ordered as in psi_table, and groups of points observed in neither sample are left out, since they contribute nothing.
References
[1]N. Siddiqi, “Credit Risk Scorecards: Developing and Implementing Intelligent Credit Scoring”, Wiley, 2006.
Examples
>>> import pandas as pd >>> from kivi.stability import csi_table >>> reference = pd.Series(["x", "x", "y", "y"]) >>> current = pd.Series(["x", "y", "y", "y"]) >>> csi_table(reference, current, {"x": 40.0, "y": 25.0}) reference current reference_share current_share points csi bin x 2 1 0.5 0.25 40.0 -10.00 y 2 3 0.5 0.75 25.0 6.25
The average points fall from 32.5 to 28.75, by the sum of the
csicolumn, -3.75.