I think you will use numpy's pv function for mortgage simulations. I was curious about the implementation of numpy, so I checked it.
reference
I tried the example described in [Documentation] of numpy (https://github.com/numpy/numpy/blob/v1.12.0/numpy/lib/financial.py#L419-L511).
What is the present value (e.g., the initial investment) of an investment that needs to total $15692.93 after 10 years of saving $100 every month? Assume the interest rate is 5% (annually) compounded monthly.
Translated into Japanese, we will consider an investment that requires an initial investment of $ 15692.93 and will generate $ 100 in profits for 10 years. What is the present value, assuming an annual interest rate of 5%? What a place, such as. It's an example that seems to appear in finance textbooks.
import numpy as np
np.pv(0.05/12, 10*12, -100, 15692.93)
-100.00067131625819
Although np.pv itself is made for this purpose, the same idea can be applied to mortgage simulations. Let's simulate how much you can borrow on the premise of "payment of 150,000 yen monthly", "repayment for 35 years", and "annual interest rate of 3%". in this case,
import numpy as np
np.pv(0.03/12, 35*12, -1500000, 0)
389762052.66542333
As a result, you can see that you can buy a house for about 38.97 million yen.
fv +
pv(1 + rate)^{nper} +
\frac{pmt((1 + rate)^{nper} - 1)}{rate} = 0
It is said that pv is calculated by solving. (Only the expression when = 0 is described.) You can see the sum of geometric series by looking at it, but let's derive it.
The future value can be expressed by the sum of "the positive future value pv (borrowed amount) of the first amount obtained" and "the future value of the monthly income and expenditure pmt (monthly repayment amount)".
fv = pv (1 + rate)^{nper} + \frac{pmt}{(1-rate)^0} + \dots + \frac{pmt}{(1-rate)^{nper}}
fv = pv (1 + rate)^{nper} + pmt \Sigma_k^{nper} (1+rate)^{-(k-1)}
fv = pv (1 + rate)^{nper} + pmt \frac{1 - (1 + rate)^{nper}}{rate}
With that feeling, I was able to derive it.
Looking at the formula, pv can be calculated in one shot by four arithmetic operations, but numpy teacher seems to be doing complicated things. Even so, it's about array-like argument support.
when = _convert_when(when)
(rate, nper, pmt, fv, when) = map(np.asarray, [rate, nper, pmt, fv, when])
temp = (1+rate)**nper
miter = np.broadcast(rate, nper, pmt, fv, when)
zer = np.zeros(miter.shape)
fact = np.where(rate == zer, nper+zer, (1+rate*when)*(temp-1)/rate+zer)
return -(fv + pmt*fact)/temp
I found a simple mortgage simulator. Inside, I feel like I'm doing the same calculation. https://www.hownes.com/loan/sim/ http://sukkiri.loan
Recommended Posts