Results 1 to 22 of 22

Thread: This interest rate, how ?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    212

    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.
    Thanks for your help.
    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. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    212

    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. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    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

    I've asked a moderator to move your thread.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    212

    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. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    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. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    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. #8
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: This interest rate, how ?

    Quote Originally Posted by si_the_geek View Post
    ....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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  9. #9
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: This interest rate, how ?

    Quote Originally Posted by Niya View Post
    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.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    212

    Re: This interest rate, how ?

    Thank you very much

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

  11. #11
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    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.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    212

    Re: This interest rate, how ?

    Thank you very much.

  13. #13
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    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. #14
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: This interest rate, how ?

    Quote Originally Posted by Schmidt View Post
    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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    212

    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. #16
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    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:
    1. def my_function(parameter1, parameter2):
    2.     returned_val = parameter1 + parameter2
    3.  
    4.     return returned_val
    5.  
    6. print (str(my_function(1, 2)))

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

    for a class you may have something like this:
    python Code:
    1. class person:
    2.     name = ""
    3.     age = 0
    4.     height_feet = 0
    5.     height_inches = 0
    6.     weight = 0

    but in vb.net a class would be defined like this:
    vb.net Code:
    1. Public Class Person
    2.     Property name As String = String.Empty()
    3.     Property age As Integer = 0
    4.     Property height_feet As Integer = 0
    5.     Property height_inches As Integer = 0
    6.     Property weight As Integer = 0
    7. End 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.
    Last edited by dday9; Oct 11th, 2013 at 09:59 PM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  17. #17
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    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.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  18. #18
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: This interest rate, how ?

    Quote Originally Posted by jemidiah View Post
    ...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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  19. #19
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    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.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  20. #20
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: This interest rate, how ?

    Ah well that makes sense
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  21. #21
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,900

    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
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  22. #22

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    212

    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
  •  



Click Here to Expand Forum to Full Width