<commit_before>rateflask
========

rateflask predicts the rate of return at inception of a Lending Club loan, web 
version at [rateflask.com][rateflask.com].

### Description
Analysis of Lending Club data tends to focus on loans that have 
[already matured][already matured]. Matured loans, however, comprise 
[less than 10%][less than 10%] of loans issued. This conundrum inspired a new 
methodology to enable loan comparisons be made, regardless as to whether the 
loan has matured, is ongoing or yet to be issued.

The methodology involves using Random Forests to predict the expected loan 
payment for a particular month, then aggregated across the whole period to give 
a single rate of return metric for each loan. This allows 90% of loans issued be 
used as training data, and the remaining set of matured loans as validation.

![][issuance]

### Details

The model consists of 4 x 36 individual [Random Forests][Random Forests] 
sub-models, one for each grade-month pair (grades A - D, in the period Jan 2012 
- Dec 2014). The training data is the set of 3-year loans issued between 2012 
and 2014, i.e. ongoing loans. Loan details (FICO, monthly income, etc.) are used 
as features, and the loan status (current, in default, etc) as targets.

The [loan status][loan status] is used to calculate the probability of each 
payment made, and aggregated to give the rate of return of that loan. Viewed as 
a black box, the model takes in loan features as input, and outputs the expected 
rate of return.

The trained model is validated against 3-year loans issued between 2009 and
2011, i.e. loans that have matured. The validation process involves comparing 
the actual rate of return, calculated purely with actual payment data, against
the expected rate of return, calculated purely on loan features. The actual and 
predicted rate of return are illustrated in the graph below by blue and green
respectively.

![][compare]

The graph below shows the improvement in rate of return with an active selection
strategy based on the model, compared to choosing a loan of a specified sub-
grade at random. The active selection strategy involves using the model to
generate the predicted rate of return, ranking the loans and identifying the top
quartile. Details on how the chart is generated can be found [here][charts].

![][quartile]

For further details:
* [Mechanics: How does the model work?][mechanics]
* [Validation: Does the model work?][validation]
* [Application: Why do I care?][application]

Presentation slides can be found [here][presentation]. The charts were generated
with R's ggplot via rpy2, and details can be found [here][charts].

### Requirements
* numpy 1.9.0
* scipy 0.14.0
* pandas 0.14.1
* scikit-learn 0.14.1
* matplotlib 1.3.1
* flask 0.10.1
* lendingclub 0.1.8
* pymongo 2.7.2
* psycopg2 2.5.3
* dill 0.2.2

### Installation
1. Clone this repo.
2. Download the full version data (~270 MB) from the 
[Lending Club website][Lending Club website] or from the following 
[Dropbox address][Dropbox address], and place in a directory labeled `data`.
3. Install the listed requirements.<commit_msg>added notebooks<commit_after>rateflask
========

rateflask predicts the rate of return at inception of a Lending Club loan, web 
version at [rateflask.com][rateflask.com].

### Description
Analysis of Lending Club data tends to focus on loans that have 
[already matured][already matured]. Matured loans, however, comprise 
[less than 10%][less than 10%] of loans issued. This conundrum inspired a new 
methodology to enable loan comparisons be made, regardless as to whether the 
loan has matured, is ongoing or yet to be issued.

The methodology involves using Random Forests to predict the expected loan 
payment for a particular month, then aggregated across the whole period to give 
a single rate of return metric for each loan. This allows 90% of loans issued be 
used as training data, and the remaining set of matured loans as validation.

![][issuance]

### Details

The model consists of 4 x 36 individual [Random Forests][Random Forests] 
sub-models, one for each grade-month pair (grades A - D, in the period Jan 
2012 - Dec 2014). The training data is the set of 3-year loans issued between 
2012 and 2014, i.e. ongoing loans. Loan details (FICO, monthly income, etc.) are 
used as features, and the loan status (current, in default, etc) as targets.

The [loan status][loan status] is used to calculate the probability of each 
payment made, and aggregated to give the rate of return of that loan. Viewed as 
a black box, the model takes in loan features as input, and outputs the expected 
rate of return.

The trained model is validated against 3-year loans issued between 2009 and
2011, i.e. loans that have matured. The validation process involves comparing 
the actual rate of return, calculated purely with actual payment data, against
the expected rate of return, calculated purely on loan features. The actual and 
predicted rate of return are illustrated in the graph below by blue and green
respectively. The headline rate in red is the Lending Club rate.

![][compare]

The graph below shows the improvement in rate of return with an active selection
strategy based on the model, compared to choosing a loan of a specified sub-
grade at random. The active selection strategy involves using the model to
generate the predicted rate of return, ranking the loans and identifying the top
quartile. Details on how the charts are generated can be found [here][charts].

![][quartile]

For further details:
* [Mechanics: How does the model work?][mechanics]
* [Validation: Does the model work?][validation]
* [Application: Why do I care?][application]

Presentation slides can be found [here][presentation]. The charts were generated
with R's ggplot via rpy2, and details can be found [here][charts].

### Requirements
* numpy 1.9.0
* scipy 0.14.0
* pandas 0.14.1
* scikit-learn 0.14.1
* matplotlib 1.3.1
* flask 0.10.1
* lendingclub 0.1.8
* pymongo 2.7.2
* psycopg2 2.5.3
* dill 0.2.2

### Installation
1. Clone this repo.
2. Download the full version data (~270 MB) from the 
[Lending Club website][Lending Club website] or from the following 
[Dropbox address][Dropbox address], and place in a directory labeled `data`.
3. Install the listed requirements.
{'fraction_non_alphanumeric': 0.06715351643117955, 'fraction_numerical': 0.02444832513097317, 'mean_word_length': 4.370843989769821, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6550383', 'n_tokens_mistral': 1843, 'n_tokens_neox': 1677, 'n_words': 944}