Our model calculations all use Numpy arrays — this helps in two ways; (1) calculations for all loans are done in parallel, and (2) Numpy actually compiles into C code, which is significantly faster than Python.
Because we are using arrays, we first need to create them. There are two different types arrays that we will use here. The first containing values to be calculated. For these we simply initialize zero arrays.
The second are arrays containing data from
loanbook, we first build the
Then we initialize the arrays where the values from
loanbook are used in month zero.
erc dataframes are used as lookup arrays, because of this they do not match the row-axis dimension of
Fortunately, we can use the same code to translate both into the correct format.
Now everything is initialized, we can move onto the actual cash-flow modelling.
The model calculates per month. We start at month one, calculate all values for that month, then increase the month. Once we reach the final month defined by
m_max modelling is complete and outputs our results. This is implemented in a loop with
for m in range(1, m_max).
For every month, several calculations are performed. These are:
First, we calculate
cumulative_payment. Note that array vectors for the current month are indicated by
[:, m], whereas previous month values are indicated by
scheduled_payment, we must use an if-else statement due to additional reversion date logic. To implement this in a vectorized* fashion, we define the calculation function in
scheduled_payment_calc then use
np.vectorize (note that these should be defined outside of the loop).
early_repayment is another if-else statement function, albeit much simpler.
The remaining calculations are again much simpler.
Finally, we have our cash-flows! We can see the way each array evolves over time by visualizing them as line-plots, with the month number on the x-axis.