Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python for finance
#1
Hei, after several tries, I haven't managed to find the answer, or run my code.

My problem is to find a specific interest rate for an annual annuity for a period of time.
I tried using a while loop to find alpha and beta interest rates, but my setup is wrong in some way...
Would appreciate all help!

Consider for example an annuity α, which pays $1,000 per year for 25 years, at a cost of $15,000 today. Now consider annuity β, which pays $1,250 per year for 20 years, also at a cost of $15,000 today. Which one is a better investment?
Write a function annuity_rate(n, a, p)
Calculate:
α rate -> annuity_rate(25,1000,15000)
β rate -> annuity_rate(20,1250,15000)

Kind regards
Engineering student
Reply
#2
Maybe this is interesting for you: https://docs.scipy.org/doc/numpy-1.13.0/...ncial.html

But first you should calculate this on a paper.
Otherwise you're using some functions without knowledge how it's calculated.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
First step to get help is to post also your code, not just the assignment
Larz60+ likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
I do understand the finance part of it, and most likely beta is the one with highest interest rate.
But it is the problem of using a while loop to find the interest rate, which is required for this assignment.
In excel this would have been done so much easier.

My code is:

def annuity_rate(n, a, p):
    i = 0.001
    while delta > 0.001:
        calculate_p_annuity = p_annuity(i, n, a)
        RealP = p
        clp = p_annuity(i, 1500, 20)
        delta = calculate_p_annuity - p
        i = i + 0.001
    return

print(annuity_rate(20, 1000, 15000))
Reply
#5
what is p_annuity?
Also, probably you want your function to return something
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
p_annuity is a function that I recall from a previous task, this function calculates and return the present value of an annuity to be received annually for n number of years.

p_annuity (PV) = Annual annuity * (1 - (1 + interest rate)-number of years) / interest rate
Reply
#7
This python forum is completely useless.
Reply
#8
(Sep-13-2019, 06:12 AM)Not_skilled_at_all Wrote: In excel this would have been done so much easier.
This python forum is completely useless.

A bad workman always blames his tools.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#9
def pv_annuity(rate,n,c):
    pv=c/rate*(1-1/(1+rate)**n)
    return pv
   
def annuity_rate(n,cash, pv):
    i = 0.001
    delta=10000
    while delta > 10:
        pv0 = pv_annuity(i, n,cash)
        delta = abs(pv0- pv)
        i+=0.001
        #print(delta)
    return i

    
#Example
pv_annuity(0.1,20,100)      #Out[890]: 851.3563719758565
annuity_rate(20, 100, 851)  # Out[892]: 0.10000000000000007
annuity_rate(20, 100, 700)  # Out[891]: 0.1300000000000001
Reply
#10
Look at your code and errors. Insulting is not going to get you help, and we don't do your homework for you.
In the first code you posted,
Line 3 - you reference delta but that variable is not defined at that point. Set it to an arbitrarily large value (1)

Now run it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help me with yahoo finance excel sheet Learner4life 2 2,284 Nov-27-2019, 12:55 PM
Last Post: Learner4life
  Python with Finance - i have no code but don't do it for me give me tips please Akainu 1 2,391 May-30-2019, 07:57 PM
Last Post: nilamo
  3 Finance Python Exercises mmkthen 2 5,403 Oct-29-2017, 02:55 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020