← All Base Form Math projects
BM-18NOT STARTEDBASE FORM · CALCULUSMATHPYTHON

Loan and investment solver

Find the real interest rate of a loan or investment (IRR) with Newton's method, and compare its speed with plain gradient descent.

EST.6 h
REWARD+100 XP
AFTERoriginal

ML-ARENA BENCHMARKS

YOUR NUMBER VS THE INDUSTRY'S
METRICINDUSTRYMINE
rate and iterations
GOAL · same rate to 1e-10; fewer iterations than GD
scipy.optimize.newton—
no results yet
YOUR RESULTS

Your recorded results appear here.

Brief

A loan advertised at 5% with a fee is not a 5% loan. To know the true rate you have to solve for it. A cash flow CtC_t at time tt is worth Ct/(1+r)tC_t / (1+r)^t today. The internal rate of return is the rr where all of them cancel:

NPV(r)=∑t=0TCt(1+r)t=0\text{NPV}(r) = \sum_{t=0}^{T} \frac{C_t}{(1+r)^t} = 0

There is no formula for rr, so you solve numerically. Newton's method uses the derivative. At the current guess, replace NPV by its tangent line and jump to where the tangent hits zero: r←r−NPV(r)/NPV′(r)r \leftarrow r - \text{NPV}(r) / \text{NPV}'(r). Near the answer the error gets squared each step.

The same idea finds minimums. The second-order Taylor expansion f(x+h)≈f(x)+f′(x) h+12f′′(x) h2f(x+h) \approx f(x) + f'(x)\,h + \tfrac12 f''(x)\,h^2 is a parabola. Jump to its bottom: h=−f′(x)/f′′(x)h = -f'(x) / f''(x). With many variables, f′′f'' becomes the Hessian HH, the matrix of second partial derivatives, and the step solves Hh=−∇fH h = -\nabla f. Curvature tells Newton how far to go. Gradient descent only knows the direction, so it needs a hand-tuned learning rate and many more steps.

VISUALFig. 1
0% 10% 20% 30% 40% 500 0 -400 r₀ r₁ IRR ≈ 15.24% rate rNPV(r) STEPRATE 00.00000000 10.11111111 20.14917043 30.15236255 40.15238237 50.15238237 -1000 now, then +300 a yearfor 5 years. Correct digitsroughly double each step.
Newton's method on NPV for −1000 now, then +300 a year for 5 years. Each tangent lands closer to the IRR of about 15.24%. Why it's here: It shows what one Newton step does and how fast the digits settle, which is what objective 3 asks you to measure.
HINT · OBJECTIVE 3
Stuck? Hints stay hidden until you ask.
HINT · OBJECTIVE 4
Stuck? Hints stay hidden until you ask.

DATA · your own cash-flow examples

Objectives

0 / 6 CLEARED

Read-only. Log in to update progress.

VICTORY CONDITIONS

  1. Your Newton IRR matches `scipy.optimize.newton` to 1e-10 on every example, in fewer iterations than gradient descent. Record the loan example with `python tools/mlarena.py record BM-18 "<Newton iters> vs <GD iters>"`.
  2. The README has a per-step error table showing the correct digits roughly doubling near the answer.
  3. Newton with the Hessian reaches the Rosenbrock minimum in fewer steps than your BM-15 optimizers.

Side quests

Make Newton safe: if a step leaves the interval where NPV changes sign, fall back to a bisection step. scipy.optimize.brentq is built on the same idea.
Cross-check your IRRs with numpy_financial.irr from the numpy-financial package.

Terms in plain words

Net present value (NPV)
The sum of all cash flows, each discounted to today: Σ C_t / (1 + r)^t.
Internal rate of return (IRR)
The rate r that makes NPV zero. For a loan, it is the true interest rate once fees are counted.
Newton's method
Replace the function by its tangent line (or, when minimising, by the parabola from its Taylor expansion) and jump to where that model says the answer is. Repeat.
Hessian
The matrix of second partial derivatives. It measures curvature. With one variable it is just f″.
Quadratic convergence
Near the answer the error is roughly squared each step, so the number of correct digits about doubles.