This interest rate, how ?-VBForums

Thread: This interest rate, how ?

1. This interest rate, how ?

Hi,
I used these formulae to code a loan calculator with reverse calculating the three variables below,
Payment, Amount borrowed, and Number of months
Unfortunately, I have no idea how to get at the interest rate.
I would be a happy beginner if I could achieve this ultimate step.
A : Amount borrowed
N : Number of months (in this example)
I : Interest rate = I / 1200 (convert annual interest rate to monthly rate)
P : Payment
The payment is calculated by this formula:

P = (A x I)/(1-(1 + I)^(-N))

The amount borrowed is given by the formula:

A= P x (1-(1 + I)^(-N)) / I

And the number of months is given by the formula:

N = (log(P) - log(P - A x I))/(log(1 + I) with (P - A x I) > 0

2. Re: This interest rate, how ?

That's maths, not programming. You need to solve the equation for I. Once you have I alone on the left-hand side of the equation, then you can write code to implement it.

3. Re: This interest rate, how ?

This is the problem.
Mathematically, It is not possible. I is present in both numerator and denominator with different exponent.
And first I must derive the main formula to get zero in right side.
Second, I must write (code) a function that computes I
But actually the two steps are hard for me.

4. Re: This interest rate, how ?

as a Maths question, the place to ask is the Maths Forum:

http://www.vbforums.com/forumdisplay.php?20-Maths-Forum

5. Re: This interest rate, how ?

Please, It's not dealing with math only.
It's a programming matter also.
Because mathematically it's not possible.
I hoped for alead or clue.But if you see that my post here is inappropriate, then it should be removed.
Thank you.

6. Re: This interest rate, how ?

there are members in the Maths Forum who are much more advanced than most of the members here, + it is a Maths question

7. Re: This interest rate, how ?

Welcome to VBForums

At this stage it is definitely more maths based, so I've moved this thread from the 'VB.Net' forum to our 'Maths' forum... we have several maths experts here (who are also programmers), and I'm sure one of them will be able to help you work this out.

8. Re: This interest rate, how ?

Originally Posted by si_the_geek
....we have several maths experts here (who are also programmers), and I'm sure one of them will be able to help you work this out.
Yeah....Jemidiah could chew up this problem without breaking a sweat. Hope he's still active.

9. Re: This interest rate, how ?

Originally Posted by Niya
Hope he's still active.
I am, just intermittently.

Atenk is right, inasmuch as the problem can't in general be solved algebraically (and standard special functions don't seem to be enough). Nonetheless, the function is very well-behaved, so something as simple as Newton's Method can solve it easily. The following is a Python script illustrating the technique. If you need it explained or translated, please ask.

Code:
```A = 1000
P = 30
N = 56

def f(I):
return P*(1-(1+I)**(-N))/I - A

def f_prime(I):
return ((-1 + (1+I)**(-1-N) * (1+I+I*N)) * P)/(I*I)

def newt(I,n):
for i in range(n):
if f_prime(I) == 0:
return I
I = I - f(I)/f_prime(I)
return I

print(newt(0.0001, 5)) # prints 0.020223511346654406```
Some notes:
* You can choose to iterate more or fewer steps, or to iterate until a desired precision (or until too many steps have passed).
* I haven't checked the stability of this initial value. You should check this method with many varied inputs to make sure it works as desired in all cases. For instance, if your initial value for I is 0.5, the algorithm doesn't find the root and returns something enormous.

10. Re: This interest rate, how ?

Thank you very much

I would appreciate if you could translate this code into vb.

11. Re: This interest rate, how ?

Here's a minimal working example in VB.NET.

Code:
```Public Class Form1
Dim A As Double = 1000
Dim P As Double = 30
Dim N As Double = 56

Private Function f(ByVal I As Double) As Double
Return P * (1 - (1 + I) ^ (-N)) / I - A
End Function

Private Function f_prime(ByVal I As Double) As Double
Return ((-1 + (1 + I) ^ (-1 - N) * (1 + I + I * N)) * P) / (I * I)
End Function

Private Function newt(ByVal I As Double, ByVal n As Long) As Double
For k As Long = 1 To n
If f_prime(I) = 0 Then Return I
I = I - f(I) / f_prime(I)
Next k
Return I
End Function

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MessageBox.Show(newt(0.0001, 5)) ' prints 0.0202235113466544
End Sub
End Class```
For what it's worth, I got to thinking that this curve is well enough behaved that quadrature is probably the way to go for an elegant solution, but oh well.

12. Re: This interest rate, how ?

Thank you very much.

13. Re: This interest rate, how ?

Well, not sure if there's something similar in the "VB-Compatibility-Namespace" in .NET - but for VB6/VBA there's the Rate-function
(alongside a few other useful methods inside the VBA.Financial-Namespace):

Code:
```Private Sub Form_Load()
Const A As Double = 1000
Const P As Double = 30
Const N As Double = 56

Debug.Print VBA.Financial.Rate(N, -P, A) 'gives:   2.02235113466544E-02
End Sub```
Olaf

14. Re: This interest rate, how ?

Originally Posted by Schmidt
Well, not sure if there's something similar in the "VB-Compatibility-Namespace" in .NET...
Microsoft.VisualBasic.Rate

It doesn't need to be fully qualified though. You can just call Rate directly.

15. Re: This interest rate, how ?

I kept searching In the net until I found the link below

http://blog.bossylobster.com/2012/05...rest-rate.html

The code is written in python.

I would appreciate If you, guys, could translate it into vb.net

Thank you

This is the code:

def newton_raphson_method(guess, f, f_prime):
def next_value(value):
return value - f(value)*1.0/f_prime(value)

current = guess
while abs(f(current)) > 10**(-8):
current = next_value(current)

return current

def generate_polynomials(principal, term, payment):
def f(m):
return (principal*(m**(term + 1)) - (principal + payment)*(m**term) +
payment)

def f_prime(m):
return (principal*(term + 1)*(m**term) -
(principal + payment)*term*(m**(term - 1)))

return (f, f_prime)

def m(r):
return 1 + r/12.0

def m_inverse(m_value):
return 12.0*(m_value - 1)

def solve_for_interest_rate(principal, term, payment, m, m_inverse):
f, f_prime = generate_polynomials(principal, term, payment)

guess_m = m(0.10) # ten percent as a decimal
m_value = newton_raphson_method(guess_m, f, f_prime)
return m_inverse(m_value)

16. Re: This interest rate, how ?

You really need to keep the formatting in your code, especially in Python where the code is processed based on it's formatting(take indentation for example). Go ahead and edit your post and wrap the code in code or highlight tags. Those look like this:

[code]
#code here
[/code]

-or-
[highlight="python"]
#code here
[/highlight]

something to keep in mind is how you declare functions and classes in visual basic. In Python, you may have this as your function:
python Code:
`def my_function(parameter1, parameter2):    returned_val = parameter1 + parameter2     return returned_val print (str(my_function(1, 2)))`

but in visual basic we'd have something like this:
vb.net Code:
`Private Function my_function(ByVal parameter1 As Integer, ByVal parameter2 As Integer) As Integer    Dim returned_val As Integer = parameter1 + parameter2        Return returned_valEnd Function Sub Main()    Console.WriteLine(my_function(1, 2).ToString)    Console.ReadLine()End Sub`

for a class you may have something like this:
python Code:
`class person:    name = ""    age = 0    height_feet = 0    height_inches = 0    weight = 0`

but in vb.net a class would be defined like this:
vb.net Code:
`Public Class Person    Property name As String = String.Empty()    Property age As Integer = 0    Property height_feet As Integer = 0    Property height_inches As Integer = 0    Property weight As Integer = 0End Class`

Pretty much the reason why I give you these examples is because once you understand the concept of OOP, found in Python and in Visual Basic.Net, really the only differences between the languages are the syntax and the way the code is compiled. I gave you some examples of the syntax differences, but didn't explain how the compiling processes are different. In Visual Basic the code is processed through a compiler where as in Python the code is processed through an interpreter.

17. Re: This interest rate, how ?

It's not a math question at this point, so I won't take the time to do the translation, sorry. There are a zillion Python tutorials out there; it really shouldn't be hard to do it yourself. The only "strange" thing about the code you posted is that it takes in functions as arguments. FWIW, "Newton-Raphson" is another name for Newton's Method I posted up in #9. Best of luck.

18. Re: This interest rate, how ?

Originally Posted by jemidiah
...The only "strange" thing about the code you posted is that it takes in functions as arguments.
Actually its not as strange as you might think. Higher order functions are a part of many modern languages. We use them all the time in VB.Net. LINQ for example. Higher order functions are essential for LINQ to work as effectively as it does.

19. Re: This interest rate, how ?

I agree entirely Niya . I just thought for someone needing help translating from Python to .NET, functions as arguments might be a concept they hadn't seen before, so I pointed it out.

20. Re: This interest rate, how ?

Ah well that makes sense

21. Re: This interest rate, how ?

Is it just me or did JM turn a bit rasta back in post 2?
You need to solve the equation for I

22. Re: This interest rate, how ?

Thank you, guys, I'll take your recommendations and start learning python.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Featured