School Question:
Build a function retirement_age(PMT, i, FV, start_age) that calculates the (whole) age at which your customer can retire, if they:
Invest an amount, PMT at the END of every YEAR (with the first
payment made exactly one year from now),
at an interest rate of i% per year, compounded annually.
They require an amount of AT LEAST FV in order to be able to afford
retirement.
They just turned start_age years old.
I am struggling to solve the number of years PMT would take to reach FV
This is my code:
def retirement_age(PMT, i, FV, start_age):
count = 0
while PMT <= FV: #PMT set to loop till it reaches FV
PMT = PMT * (1+i)
count = count + 1 #adds 1 to count value until while loop satisfied
age = count + start_age #adds count value to start_age to determine retirement age
return int(age) #returns age
print (retirement_age(20000, 0.1, 635339.63, 20))
my answer with this code:
57
The answer is supposed to be:
35
I can't tell what I'm doing wrong. And the task specifically mentions that we are not allowed to import external functions like math for example, which means I can't use math.log() which would probably solve all my problems.
First, I'll note that broad debugging questions like this aren't very appropriate for SO.
Having said that, I played around with it and after reading the specs again, I found the issue(s). I figured I might as well post it.
You only need to keep calculating while the principal is less than the future value. You can stop once they're equal.
The main issues however were that you aren't adding any money each year. You're just accumulating interest on the initial principal. And...
You invested PMT immediately. The investment doesn't happen until the end of the year, as the instructions emphasize. That means at the start of the looping, he has 0 invested. That means he doesn't start accumulating interest until the start of the second loop/year.
def retirement_age(PMT, i, FV, start_age):
age = start_age
p = 0
while p < FV:
p = PMT + p * (1+i)
age += 1
return int(age)
print(retirement_age(20000, 0.1, 635339.63, 20))
# 35
I introduced p to keep track of the running balance since it's separate from what's being added each year. Your logic for keeping track of age was also a little convoluted, so I simplified it down a bit.
Related
Zenyk recorded losses daily throughout
n
days, but suddenly noticed that he had made a mistake and lost data on enemy losses on one of the days. However, Zenyk knows the total number of losses
y
, as well as the fact that every day the enemy lost a positive number of soldiers. In this way he can reconstruct the number of enemy casualties for that day, provided that Zenic has not made another mistake in his records. Help Zenic recover the number of enemy casualties for a day for which there is no data. If Zenyk made an additional mistake in the notebook, display Another mistake!
Incoming data
The first line contains two integers
n
and
x
— the number of days in which Zenyk recorded statistics, as well as the total losses of the enemy.
In the next line is given
n - 1
numbers
ai
, separated by blanks — data on daily enemy losses according to Zenik's notebook.
Output data
Output one number - the number of enemy casualties on the day Zenyk forgot to record the data.
If Zenyk made another mistake in his notebook and the data in the notebook is contradictory, then output Another mistake!.
n, x = map(int, input().split())
for i in range(n-1):
a = list(map(int, input().split()))
b = sum(a)
c = x - b
print(c)
if x < b:
print('Another mistake!')
Input:
1 100000
Output:
100000
Input:
2, 10
2
Output: 8
Input:
2 15
47
Output:
'Another mistake!'
This is my code, but the test does not skip any further!
Can you suggest what is wrong and correct my code for this task
Can you help me to get the manual calc for get the result of the function PAGO from excel using the value of future value, for example this method
=PMT(0.0158,12,-300000,0.6*300000) return 13,900.50 if I remove the future value the function is =PMT(0.0158,12,-300000,0) and the result is 27,641.20
in this second example the function for get this value is ((Ir * (1 + Ir)^Np ) * M ) / ((1+ Ir)^Np -1)
where Ir = interes rate, Np number of payments and M the financing amount.
but Im not sure how I can add the additional value (future value) in this function.
The function is for calculating loan repayment installments. PV if Present amount of the loan outstanding. FV is the loan amount that will be outstanding after the NPER(number of installments) payments.
So, can there be different amounts outstanding FVs for the same loan at the same time?
If you are planning to calculate loan installments in blocks of periods. Say 12 months, 18 months and hence, there will be different FVs at different times. But in that case you should have a new cell for calculation. Where FV of previous block will become PV for calculation block.
More the PV more will be the result amount
More the FV less will be the result amount
Please see if image below helps.
Link
I cannot figure out the approach to this as the principle amount shall change after every year(if calculated annually, which shall be the easiest). Eventual goal is to calculate exact number of years, months and days to earn say 150000 as interest on a deposit of 1000000 at an interest rate of say 6.5%. I have tried but cannot seem to figure out how to increment the year/month/day in the loop. I don't mind if this is down voted because I have not posted any code(Well, they are wrong). This is not as simple as it might seem to beginners here.
It is a pure maths question. Compound interest is calculated as follows:
Ptotal = Pinitial*(1+rate/100)time
where Ptotal is the new total. rate is usually given in percentages so divide by 100; time is in years. You are interested in the difference, though, so use
interest = Pinitial*(1+rate/100)time – Pinitial
instead, which is in Python:
def compound_interest(P,rate,time):
interest = P*(1+rate/100)**time - P
return interest
A basic inversion of this to yield time, given P, r, and target instead, is
time = log((target+Pinitial)/Pinitial)/log(1+rate/100)
and this will immediately return the number of years. Converting the fraction to days is simple – an average year has 365.25 days – but for months you'll have to approximate.
At the bottom, the result is fed back into the standard compound interest formula to show it indeed returns the expected yield.
import math
def reverse_compound_interest(P,rate,target):
time = math.log((target+P)/P)/math.log(1+rate/100)
return time
timespan = reverse_compound_interest(2500000, 6.5, 400000)
print ('time in years',timespan)
years = math.floor(timespan)
months = math.floor(12*(timespan - years))
days = math.floor(365.25*(timespan - years - months/12))
print (years,'y',months,'m',days,'d')
print (compound_interest(2500000, 6.5, timespan))
will output
time in years 2.356815854829652
2 y 4 m 8 d
400000.0
Can we do better? Yes. datetime allows arbitrary numbers added to the current date, so assuming you start earning today (now), you can immediately get your date of $$$:
from datetime import datetime,timedelta
# ... original script here ...
timespan *= 31556926 # the number of seconds in a year
print ('time in seconds',timespan)
print (datetime.now() + timedelta(seconds=timespan))
which shows for me (your target date will differ):
time in years 2.356815854829652
time in seconds 74373863.52648607
2022-08-08 17:02:54.819492
You could do something like
def how_long_till_i_am_rich(investment, profit_goal, interest_rate):
profit = 0
days = 0
daily_interest = interest_rate / 100 / 365
while profit < profit_goal:
days += 1
profit += (investment + profit) * daily_interest
years = days // 365
months = days % 365 // 30
days = days - (months * 30) - (years * 365)
return years, months, days
years, months, days = how_long_till_i_am_rich(2500000, 400000, 8)
print(f"It would take {years} years, {months} months, and {days} days")
OUTPUT
It would take 1 years, 10 months, and 13 days
I'm trying to calculate the remaining balance of a home loan at any point in time for multiple home loans.
Its looks like it is not possible to find the home loan balance w/ out creating one of those long tables (example). Finding the future balance for multiple home loans would require setting up a table for ea. home (in this case, 25).
With a table, when you want to look at the balance after a certain amount of payments have been made for the home loan, you would just visually scan the table for that period...
But is there any single formula which shows the remaining loan balance by just changing the "time" variable? (# of years/mths in the future)...
An example of the information I'm trying to find is "what would be the remaining balance on a home loan with the following criteria after 10 years":
original loan amt: $100K
term: 30-yr
rate: 5%
mthly pmts: $536.82
pmts per yr: 12
I'd hate to have to create 25 different amortization schedules - a lot of copy-paste-dragging...
Thanks in advance!
You're looking for =FV(), or "future value).
The function needs 5 inputs, as follows:
=FV(rate, nper, pmt, pv, type)
Where:
rate = interest rate for the period of interest. In this case, you are making payments and compounding interest monthly, so your interest rate would be 0.05/12 = 0.00417
nper = the number of periods elapsed. This is your 'time' variable, in this case, number of months elapsed.
pmt = the payment in each period. in your case $536.82.
pv = the 'present value', in this case the principle of the loan at the start, or -100,000. Note that for a debt example, you can use a negative value here.
type = Whether payments are made at the beginning (1) or end (0) of the period.
In your example, to calculate the principle after 10 years, you could use:
=FV(0.05/12,10*12,536.82,-100000,0)
Which produces:
=81,342.32
For a loan this size, you would have $81,342.32 left to pay off after 10 years.
I don't like to post answer when there already exist a brilliant answer, but I want to give some views. Understanding why the formula works and why you should use FV as P.J correctly states!
They use PV in the example and you can always double-check Present Value (PV) vs Future Value (FV), why?
Because they are linked to each other.
FV is the compounded value of PV.
PV is the discounted value at interest rate of FV.
Which can be illustrated in this graph, source link:
In the example below, where I replicated the way the example calculate PV (Column E the example from excel-easy, Loan Amortization Schedule) and in Column F we use Excel's build in function PV. You want to know the other way... therefore FV Column J.
Since they are linked they need to give the same Cash Flows over time (bit more tricky if the period/interest rate is not constant over time)!!
And they indeed do:
Payment number is the number of periods you want to look at (10 year * 12 payments per year = 120, yellow cells).
PV function is composed by:
rate: discount rate per period
nper: total amount of periods left. (total periods - current period), (12*30-120)
pmt: the fixed amount paid every month
FV: is the value of the loan in the future at end after 360 periods (after 30 year * 12 payments per year). A future value of a loan at the end is always 0.
Type: when payments occur in the year, usually calculated at the end.
PV: 0.05/12, (12*30)-120, 536.82 ,0 , 0 = 81 342.06
=
FV: 0.05/12, 120, 536.82 , 100 000.00 , 0 = -81 342.06
I am trying to solve an iterative problem in Excel. I want to be able to calculate the sum of rent for x years. The rent is increasing at a rate of 10 percent every year. I quickly came up with this python code on a REPL for clarity:
year = 6
rent = 192000
total_rent = rent
for x in range(1 , year):
rent= rent + .1*rent
total_rent = total_rent + rent
print(total_rent) # 1481397.12 is what it prints
This is a trivial problem in programming but I am not sure the best way to achieve this in excel.
In excel I am doing it this something like this:
But all the intermediate rent amount(s) are not really needed. I guess there should be a for loop here as well too, but is there a mathematical representation of this problem which I can use to create the expected result?
If you have a financial problem, you might try the financial functions of excel.
=-FV(0.1, 6, 192000)
or
=FV(0.1, 6, -192000)
the detail: FV on Office Support
Description
FV, one of the financial functions, calculates the future value of an investment based on a constant interest rate. You can use FV with either periodic, constant payments, or a single lump sum payment.
Syntax
FV(rate, nper, pmt, [pv], [type])
For a more complete description of the arguments in FV and for more information on annuity functions, see PV.
The FV function syntax has the following arguments:
Rate Required
The interest rate per period.
Nper Required
The total number of payment periods in an annuity.
Pmt Required
The payment made each period; it cannot change over the life of the annuity. Typically, pmt contains principal and interest but no other fees or taxes. If pmt is omitted, you must include the pv argument.
Pv Optional
The present value, or the lump-sum amount that a series of future payments is worth right now. If pv is omitted, it is assumed to be 0 (zero), and you must include the pmt argument.
Type Optional
The number 0 or 1 and indicates when payments are due. If type is omitted, it is assumed to be 0.
Your problem is a geometric series where the initial term is a = 192000 and the common ratio is r = 1.1. (The ratio is not just the 10% added, it includes the 100% that is added to.) To refresh your Algebra II memory, a geometric series is
total = a + a*r + a*r**2 + ... + a*r**(n-1)
The closed-form formula for the sum of the geometric series is
total = a * (r**n - 1) / (r - 1)
(using Python syntax), or, using something closer to Excel syntax,
total = a * (r^n - 1) / (r - 1)
where n is the number of years. Just substitute your values for a, r, and n.
As the question is about excel it is possible by
Or by using the FV function.
FV returns the future value of an investment based on regular payments and a constant interest rate.
Attributes of the FV function;:
Rate: The interest rate per period.
Nper: The total number of payment periods in an annuity.
Pmt: The payment made each period; it cannot change over the life of the annuity. Typically, pmt contains principal and interest but no other fees or taxes. If pmt is omitted, you must include the pv argument.
Pv: The present value, or the lump-sum amount that a series of future payments is worth right now. If pv is omitted, it is assumed to be 0 (zero), and you must include the pmt argument.
Type: The number 0 or 1 and indicates when payments are due. If type is omitted, it is assumed to be 0.
Yet another way is computing it as a geometric series with the non-financial function SERIESSUM:
=SERIESSUM(1.1,0,1,192000*{1,1,1,1,1,1})
The rate multiplier is 1.1, starting from 1.1^0 == 1 and increasing by 1 each year. The result is 1*a + 1.1*b + 1.1^2*c.... The array 192000*{1,1,...} provides the coefficients a, b, c, ... : one array value for the initial total_rent = rent, and one for each subsequent year 1..5 (from range(1,year)).