pym
pymatch
pypi i pymatch
pym

pymatch

Matching techniques for Observational Studies

by Ben Miroglio

0.3.4 (see all)
pypi i pymatch
Readme

pymatch

Matching techniques for observational studies. Inspired by and adapted from Jasjeet Singh Sekhon's Matching package in R. I wrote an adaptation in Python that is better suited for my work at Mozilla which incorporates:

  • Integration with Jupyter Notebooks (we use Jupyter + Pyspark)
  • Additional Non-Parametric Tests / Plotting Functionality to assess balance.
  • A more modular, user-specified matching process

This package was used to support this research project.

Installation

Install through pip!

$ pip install pymatch

The best way to get familiar with the package is to work through an example. The example below leaves out much of the theory behind matching and focuses on the application within pymatch. If interested, Sekhon gives a nice overview in his Introduction to the Matching package in R.

Example

The following example demonstrates how to the use the pymatch package to match Lending Club Loan Data. Follow the link to download the dataset from Kaggle (you'll have to create an account, it's fast and free!). You can follow along this document or download the corresponding Example.ipynb notebook (just be sure to change the path when loading data!).

Here we match Lending Club users that fully paid off loans (control) to those that defaulted (test). The example is contrived, however a use case for this could be that we want to analyze user sentiment with the platform. Users that default on loans may have worse sentiment because they are predisposed to a bad situation--influencing their perception of the product. Before analyzing sentiment, we can match users that paid their loans in full to users that defaulted based on the characteristics we can observe. If matching is successful, we could then make a statement about the causal effect defaulting has on sentiment if we are confident our samples are sufficiently balanced and our model is free from omitted variable bias.

This example, however, only goes through the matching procedure, which can be broken down into the following steps:


Data Prep

import warnings
warnings.filterwarnings('ignore')
from pymatch.Matcher import Matcher
import pandas as pd
import numpy as np

%matplotlib inline

Load the dataset (loan.csv) and select a subset of columns.

path = "/Users/bmiroglio/Downloads/lending-club-loan-data/loan.csv"
fields = \
[
    "loan_amnt",
    "funded_amnt",
    "funded_amnt_inv",
    "term",
    "int_rate",
    "installment",
    "grade",
    "sub_grade",
    "loan_status"
]

data = pd.read_csv(path)[fields]

Create test and control groups and reassign loan_status to be a binary treatment indicator. This is our reponse in the logistic regression model(s) used to generate propensity scores.

test = data[data.loan_status == "Default"]
control = data[data.loan_status == "Fully Paid"]
test['loan_status'] = 1
control['loan_status'] = 0

Matcher

Initialize the Matcher object.

Note that:

  • Upon initialization, Matcher prints the formula used to fit logistic regression model(s) and the number of records in the majority/minority class.
    • The regression model(s) are used to generate propensity scores. In this case, we are using the covariates on the right side of the equation to estimate the probability of defaulting on a loan (loan_status= 1).
  • Matcher will use all covariates in the dataset unless a formula is specified by the user. Note that this step is only fitting model(s), we assign propensity scores later.
  • Any covariates passed to the (optional) exclude parameter will be ignored from the model fitting process. This parameter is particularly useful for unique identifiers like a user_id.
m = Matcher(test, control, yvar="loan_status", exclude=[])
Formula:
loan_status ~ loan_amnt+funded_amnt+funded_amnt_inv+term+int_rate+installment+grade+sub_grade
n majority: 207723
n minority: 1219

There is a significant Class Imbalance in our data--the majority group (fully-paid loans) having many more records than the minority group (defaulted loans). We account for this by setting balance=True when calling Matcher.fit_scores() below. This tells Matcher to sample from the majority group when fitting the logistic regression model(s) so that the groups are of equal size. When undersampling this way, it is highly recommended that nmodels is explicitly assigned to a integer much larger than 1. This ensures that more of the majority group is contributing to the generation of propensity scores. The value of this integer should depend on the severity of the imbalance: here we use nmodels=100.

# for reproducibility
np.random.seed(20170925)

m.fit_scores(balance=True, nmodels=100)
Fitting 100 Models on Balanced Samples...
Average Accuracy: 70.21%

The average accuracy of our 100 models is 70.21%, suggesting that there's separability within our data and justifiying the need for the matching procedure. It's worth noting that we don't pay much attention to these logistic models since we are using them as a feature extraction tool (generation of propensity scores). The accuracy is a good way to detect separability at a glance, but we shouldn't spend time tuning and tinkering with these models. If our accuracy was close to 50%, that would suggest we cannot detect much separability in our groups given the features we observe and that matching is probably not necessary (or more features should be included if possible).

Predict Scores

m.predict_scores()
m.plot_scores()

png

The plot above demonstrates the separability present in our data. Test profiles have a much higher propensity, or estimated probability of defaulting given the features we isolated in the data.


Tune Threshold

The Matcher.match() method matches profiles that have propensity scores within some threshold.

i.e. for two scores s1 and s2, |s1 - s2| <= threshold

By default matches are found from the majority group for the minority group. For example, if our test group contains 1,000 records and our control group contains 20,000, Matcher will iterate through the test (minority) group and find suitable matches from the control (majority) group. If a record in the minority group has no suitable matches, it is dropped from the final matched dataset. We need to ensure our threshold is small enough such that we get close matches and retain most (or all) of our data in the minority group.

Below we tune the threshold using method="random". This matches a random profile that is within the threshold as there could be many. This is much faster than the alternative method "min", which finds the closest match for every minority record.

m.tune_threshold(method='random')

png

It looks like a threshold of 0.0001 retains 100% of our data. Let's proceed with matching using this threshold.


Match Data

Below we match one record from the majority group to each record in the minority group. This is done with replacement, meaning a single majority record can be matched to multiple minority records. Matcher assigns a unique record_id to each record in the test and control groups so this can be addressed after matching. If subsequent modeling is planned, one might consider weighting models using a weight vector of 1/f for each record, f being a record's frequency in the matched dataset. Thankfully Matcher can handle all of this for you :).

m.match(method="min", nmatches=1, threshold=0.0001)
m.record_frequency()
freq n_records
0 1 2264
1 2 68
2 3 10
3 4 2

It looks like the bulk of our matched-majority-group records occur only once, 68 occur twice, ... etc. We can preemptively generate a weight vector using Matcher.assign_weight_vector()

m.assign_weight_vector()

Let's take a look at our matched data thus far. Note that in addition to the weight vector, Matcher has also assigned a match_id to each record indicating our (in this cased) paired matches since we use nmatches=1. We can verify that matched records have scores within 0.0001 of each other.

m.matched_data.sort_values("match_id").head(6)
record_id weight loan_amnt funded_amnt funded_amnt_inv term int_rate installment grade sub_grade loan_status scores match_id
0 0 1.0 18000.0 18000.0 17975.000000 60 months 17.27 449.97 D D3 1 0.644783 0
2192 191970 1.0 2275.0 2275.0 2275.000000 36 months 16.55 80.61 D D2 0 0.644784 0
1488 80665 1.0 18400.0 18400.0 18250.000000 36 months 16.29 649.53 C C4 0 0.173057 1
1 1 1.0 21250.0 21250.0 21003.604048 60 months 14.27 497.43 C C2 1 0.173054 1
2 2 1.0 5600.0 5600.0 5600.000000 60 months 15.99 136.16 D D2 1 0.777273 2
1828 153742 1.0 12000.0 12000.0 12000.000000 60 months 18.24 306.30 D D5 0 0.777270 2

Assess Matches

We must now determine if our data is "balanced" across our covariates. Can we detect any statistical differences between the covariates of our matched test and control groups? Matcher is configured to treat categorical and continuous variables separately in this assessment.

categorical

For categorical variables, we look at plots comparing the proportional differences between test and control before and after matching.

For example, the first plot shows:

  • prop_test - prop_control for all possible term values, prop_test and prop_control being the proportion of test and control records with a given term value, respectively. We want these (orange) bars to be small after matching.
  • Results (pvalue) of a Chi-Square Test for Independence before and after matching. After matching we want this pvalue to be > 0.05, resulting in our failure to reject the null hypothesis that the frequency of the enumerated term values are independent of our test and control groups.
categorical_results = m.compare_categorical(return_table=True)

png

png

png

categorical_results
var before after
0 term 0.0 0.433155
1 grade 0.0 0.532530
2 sub_grade 0.0 0.986986

Looking at the plots and test results, we did a pretty good job balancing our categorical features! The p-values from the Chi-Square tests are all > 0.05 and we can verify by observing the small proportional differences in the plots.

Continuous

For continous variables we look at Empirical Cumulative Distribution Functions (ECDF) for our test and control groups before and after matching.

For example, the first plot pair shows:

  • ECDF for test vs ECDF for control before matching (left), ECDF for test vs ECDF for control after matching (right). We want the two lines to be very close to each other (or indistiguishable) after matching.

  • Some tests + metrics are included in the chart titles.

    • Tests performed:

      • Kolmogorov-Smirnov Goodness of fit Test (KS-test) This test statistic is calculated on 1000 permuted samples of the data, generating an imperical p-value. See pymatch.functions.ks_boot() This is an adaptation of the ks.boot() method in the R "Matching" package
      • Chi-Square Distance: Similarly this distance metric is calculated on 1000 permuted samples. See pymatch.functions.grouped_permutation_test()
    • Other included Stats:

      • Standarized mean and median differences. How many standard deviations away are the mean/median between our groups before and after matching i.e. abs(mean(control) - mean(test)) / std(control.union(test))
cc = m.compare_continuous(return_table=True)

png

png

png

png

png

cc
var ks_before ks_after grouped_chisqr_before grouped_chisqr_after std_median_diff_before std_median_diff_after std_mean_diff_before std_mean_diff_after
0 loan_amnt 0.0 0.530 0.000 1.000 0.207814 0.067942 0.229215 0.013929
1 funded_amnt 0.0 0.541 0.000 1.000 0.208364 0.067942 0.234735 0.013929
2 funded_amnt_inv 0.0 0.573 0.933 1.000 0.242035 0.067961 0.244418 0.013981
3 int_rate 0.0 0.109 0.000 0.349 0.673904 0.091925 0.670445 0.079891
4 installment 0.0 0.428 0.004 1.000 0.169177 0.042140 0.157699 0.014590

We want the pvalues from both the KS-test and the grouped permutation of the Chi-Square distance after matching to be > 0.05, and they all are! We can verify by looking at how close the ECDFs are between test and control.

Conclusion

We saw a very "clean" result from the above procedure, achieving balance among all the covariates. In my work at Mozilla, we see much hairier results using the same procedure, which will likely be your experience too. In the case that certain covariates are not well balanced, one might consider tinkering with the parameters of the matching process (nmatches>1) or adding more covariates to the formula specified when we initialized the Matcher object. In any case, in subsequent modeling, you can always control for variables that you haven't deemed "balanced".

GitHub Stars

186

LAST COMMIT

1yr ago

MAINTAINERS

1

CONTRIBUTORS

8

OPEN ISSUES

26

OPEN PRs

5
VersionTagPublished
0.3.4
3yrs ago
0.3.3
4yrs ago
0.3.2
4yrs ago
0.3.1
4yrs ago
No alternatives found
No tutorials found
Add a tutorial