Machine learning solution approach to match loan repayments I'm relatively new to the AI/ML space, but come from a programming background.

The problem: I have a dataset of users transactions who have taken short-term loans from a single loan provider and I need to match the loan repayments to a particular loan receipt.
The sample dataset is below:



transactionDate | amount | creditDebit  

2017-09-01      | 500 | Credit
2017-09-08 | 250 | Debit
2017-09-17 | 294 | Debit
2017-12-11 | 300 | Credit
2017-12-15 | 150 | Debit
2018-01-07 | 200 | Credit
2018-01-12 | 398.8 | Debit
2018-01-19 | 200 | Debit
2018-02-02 | 150 | Debit

Here the 'Credit' values for creditDebit column indicate a received loan and 'Debit' indicates a repayment of some loan received previously.

As you can see the first 3 rows are pretty easy to match because there is 1 Credit (amount of 500) followed by 2 Debits (amounts 250 and 294).

From 4th row onward is where i cannot figure out how best to match.


2017-12-11 there is a Credit transaction for a loan
2017-12-15 is a partial repayment of this loan
Then is another Credit for a new loan
2018-01-12 is a partial repayment for the 2 loans previously
The final 2 Debits are partial repayments also


I have been able to manually determine which repayment makes most sense for which loan, but trying to write it out into an if-else function would be very hard in my opinion and not the best.
For example the hardest part is to determine the Debit on 2018-01-12 which is a combined repayment transaction for both of the previously received loans. I have manually calculated a reasonable split of this Debit for the loans at 170 for the loan received on 2017-12-11 and 228 for loan on 2018-01-11.

Is there some model/algorithm that I could use to match which debit transactions are most likely to be repayments of which credit transactions and if the repayment transaction looks like a compound repayment of multiple previous loans - then what would be a reasonable split of the debit to satisfy the credit transactions?

I don't know if this makes sense to you..

I tried thinking about how I could use the clustering algorithms, but i don't I could since the clusters would not indicate matching of credit and debit transactions, especially the compound repayment debits.

Any ideas on how to tackles this are much appreciated!
I don't think machine learning is the right tool.  It's difficult (at least for me) to formulate this as a learning problem.

You could consider genetic algorithms as an alternative approach.  Based on your description of how you do the matching by hand, it seems like the following assumptions hold:


You're looking for an assignment of payments to loans such that the difference between (principle + interest) and $\sum debits$ is minimized
You favor solutions where the time between loan and repayment is minimized.


If the above is true, you may want to consider genetic algorithms as an approach.   For GAs, you need to specify a representation, an objective function, and mutation/crossover operations.  I think the following would work:  

Individuals in the population (i.e. candidate solutions) are represented as vectors of length $N$, where $N$ is the total number of debits.  Each entry in this vector assigns the debit to a credit.  For example, the individual $[1, 1, 2, 3, 2, 3]$ means that the first two debits are associated with the first credit, the third debit is associated with the second credit, the fourth debit is associated with the third credit, and so on.

The objective function that you wish to minimize is described by the two bullet points above.  I think it should be pretty easy to programmatically compute this function.  For each credit, compute the magnitude of the difference between (principle + interest) and $\sum debits$.  Sum these differences, then add a penalty for temporal separation between credit and repayment.  One easy-ish way to implement the penalty is to add the edit distance between a solution $V$ and its sorted counterpart $sort(V)$.  You'll want to play with the weight of the time penalty such that some delay between credit and debits is allowed, but you still favor quick repayments.

You'll need to pick suitable mutation and crossover operations.  For this representation, the traditional two-point crossover would likely work well, and uniform mutation would probably work fine.



As an aside, I sure hope this isn't a real-world approach to loan repayment matching.  You will wind up with some angry customers when a debit is inevitably assigned to the incorrect credit.