Post Reply 
TVM solve for interest rate, revisited
06-21-2024, 12:44 PM (This post was last modified: 06-25-2024 06:40 PM by Albert Chan.)
Post: #49
RE: TVM solve for interest rate, revisited
Code:
function npmt(n,i,pv,pmt,fv) return ((pv+fv)/EFF(i,n)+pv)*i + pmt end

Due to decay shape d = i/EFF(i,n), if we have 2 edges, I think zero slope (if exist) is on the left of edges center.
If my gut is correct, we may use center of 2 edges to reach big root. (*)

Proof welcome! Disproof with counter example also nice.

Center guess may overshoot to the right, but with non-zero slope, should not go crazy big.
If overshooted, we would at worst wasted 2 iteration. (next of next back to edge)
If not, we saved all iterations, from big edge to center of 2 edges.

If there is only 1 valid edge, it is improved with I ≈ 1/P - P/N^2 formula, where 1/P = edge.
Improved guess may not be in correct region for 1-sided convergence, but Newton would fix it.

Both cases, first Newton iteration will not be used to test termination.

10 INPUT "B,N,P,M,F? ";B,N,P,M,F
15 IF B THEN P=P+M @ F=F-M ! end mode
20 DEF FNZ(I,A,C) @ C=A*C @ D=(P-F+A)/2+C @ FNZ=A+M+D*I @ D=D+C @ END DEF
30 DEF FNF(I) @ IF 1+N*I*I=1 THEN FNF=FNZ(I,(P+F)/N,(N*N-1)/12*I) @ END
40 S=EXPM1(LOGP1(I)*N)
50 D=(P+F)/S*(1-N*(S+1)/(S+S/I))+P @ FNF=((P+F)/S+P)*I+M @ END DEF
51 I=-1 @ IF F THEN I=M/F
52 J=-1 @ IF P THEN J=M/-P
54 IF I<=-1 THEN I=J-1/(J*N*N) @ GOTO 60
55 IF J<=-1 THEN I=I-1/(I*N*N) @ GOTO 60
57 I=(I+J)/2 ! 2 good edges
60 DISP 100*I,FNF(I) @ I=I-RES/D @ Y=FNF(I)
70 DISP 100*I,Y @ H=-Y/D @ I=I+H @ Y0=Y @ Y=FNF(I)
80 IF SGN(Y0)=SGN(Y) AND ABS(Y0)>ABS(Y) THEN 70
90 DISP 100*I,Y @ IF Y THEN I=I-Y/D/2 @ DISP 100*I

Line 90 only correct i when FNF(i)≠0. This is not an optimization.
I want D=0 to give divide-by-0 warning, but not when FNF(i)=0 already.

>run
B,N,P,M,F? 0,10,50,-30,80
Code:
 11.25               -16.6938992149
 139.952615683        40.0050644805
 59.7832470725        .6147808653
 58.4651530528        .0014842067
 58.4619552859        .0000000092
 58.4619552661       -.0000000001
 58.4619552662

Center guess is on the inside, Newton pushed it out.
2 iterations less with big edge guess, but won't know until tried it.

Here is a case center guess help, by a lot. (no overshoot this time)

>run
B,N,P,M,F? 0,10,50,-12.8277219567,80
Code:
 4.81039573375        .0048896134
 4.45818094799        .0012163008
 4.28359009886        .0003008647
 4.19806387387        .0000724336
 4.15834013895        .0000156502
 4.14322447289        .0000022677
 4.14014044165        .0000000945
 4.14000024866        .0000000001
 4.14000009969        0

Same problem, but with big edge guess.

>i=m/-p @ run60
Code:
 25.6554439134        3.7843086116
 13.5327655001        .816004209
 8.60628229142        .1941800627
 6.31331024557        .0475787922
 5.20035064432        .0117855308
 4.65151470732        .0029310812
 4.37935459727        .0007284364
 4.24473730211        .000179131
 4.17956294095        .0000420926
 4.15066279831        .0000082864
 4.14127814623        .0000008742
 4.14002330217        .0000000156
 4.14000007907        0

Example with only 1 valid edge.

>run ! 5
B,N,P,M,F? 0,365*24*60,0,-0.01,5260.382426
Code:
 3.17141393E-7       -1.14278E-9
 3.1709792021E-7      0
 3.1709792021E-7      0

Same problem, but with edge guess.

>i=m/f @ run 60
Code:
-1.90100247286E-4     .005827435363
-2.2550930229E-5      6.131837704E-4
-1.232943057E-7       1.15811690742E-5
 3.16928059686E-7     4.4652E-9
 3.17097921451E-7    -5.E-14
 3.170979205E-7

(*) I was picturing f(x) = npmt(n,x,pv,pmt,fv) in my head
With 2 valid edges, we can make g(x) = f(edge1 + edge2 - x)

2 curves would not overlap, because of decay factor.
1 curve will have zero slope (if exist) on the left, the other on the right.
Center guess (where 2 curve intersect) would not hit with zero slope.

plot {(130/((1+x)^10-1)+50)*x-30, (130/((1+(.6-.375-x))^10-1)+50)*(.6-.375-x)-30}, x = -.375 .. 0.6
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: TVM solve for interest rate, revisited - Albert Chan - 06-21-2024 12:44 PM



User(s) browsing this thread: 8 Guest(s)