The problem is from the MIT edX Python Course 6.00.1 Problem Set 1 Part C. Here are the problems. Scroll to part C. I'm aware that there are countless questions asked about the edX course but none of them have really helped me. Whenever I run my bisection search code, nothing happens. No error message, nothing. Could someone help me find the issue in my code? Sorry if code is horribly inefficient, very new to python and programming.
#Python script for finding optimal saving rate of monthly salary in order to purchase 1M house in 36 months
salary = float(input("Enter salary: "))
total_cost = 1000000
salary_raise = 0.07 #semiannual raise rate
down = 0.25 * total_cost #downpayment
steps = 0
r = 0.04 #annual investments returns
low = 0 #low parameter for bisection search
high = 10000 #high parameter
current_savings = 0
while (current_savings <= (down - 100)) or (current_savings >= (down + 100)):
current_savings = 0
monthly_salary = salary/12
guess_raw = (high + low)/2
guess = guess_raw/10000.0
months = 0
steps += 1
while months < 36: #Finds end amount of money after 36 months based on guess
months += 1
multiple = months%6
monthly_savings = monthly_salary * guess
current_savings = current_savings + monthly_savings + current_savings*r/12
if multiple == 0:
monthly_salary += salary_raise * monthly_salary
if (current_savings >= (down - 100)) and (current_savings <= (down + 100)): #If the guess is close enough, print the rate and number of steps taken
print ("Best savings rate: ",guess)
print ("Steps in bisection search: ",steps)
break
elif current_savings < (down - 100): #If the guess is low, set the low bound to the guess
if guess == 9999:
print ("It is not possible to pay the down payment in three years.")
break
else:
low = guess
elif current_savings > (down + 100): #If the guess is high, set the high bound to the guess
high = guess
Related
Python Learner. Working on a recurring monthly deposit, interest problem. Except I am being asked to build in a raise after every 6th month in this hypothetical. I am reaching the goal amount in fewer months than I'm supposed to.
Currently using the % function along with += function
annual_salary = float(input("What is your expected Income? "))
portion_saved = float(input("What percentage of your income you expect to save? "))
total_cost = float(input("what is the cost of your dream home? "))
semi_annual_raise = float(input("Enter your expected raise, as a decimal "))
monthly_salary = float(annual_salary/12)
monthly_savings = monthly_salary * portion_saved
down_payment= total_cost*.25
savings = 0
for i in range(300):
savings = monthly_savings*(((1+.04/12)**i) - 1)/(.04/12)
if float(savings) >= down_payment:
break
if i % 6 == 0 :
monthly_salary += monthly_salary * .03
monthly_savings = monthly_salary * portion_saved
Thanks for the advice all. My code is getting clearer and I reached correct outputs! The problem was with how and when I was calculating interest. In the case of a static contribution I successfully used the formula for interest on a recurring deposit, here, the simpler move of calculating interest at each month was needed to work with the flow of the loop.
annual_salary = float(input("What is your expected Income? "))
portion_saved = float(input("What percentage of your income you expect to save? "))
total_cost = float(input("what is the cost of your dream home? "))
semi_annual_raise = float(input("Enter your expected raise, as a decimal "))
monthly_salary = float(annual_salary/12)
monthly_savings = monthly_salary * portion_saved
down_payment = total_cost*.25
savings = 0
month = 1
while savings < down_payment :
print(savings)
savings += monthly_savings
savings = savings * (1+(.04/12))
month += 1
if month % 6 == 0 :
monthly_salary += (monthly_salary * semi_annual_raise)
monthly_savings = (monthly_salary * portion_saved)
print("")
print("it will take " + str(month) + " months to meet your savings goal.")
Does something like this work for you? Typically, we want to use while loops over for loops when we don't know how many iterations the loop will ultimately need.
monthly_savings = 1.1 # saving 10% each month
monthly_salary = 5000
down_payment = 2500
interest = .02
savings = 0
months = 0
while savings < goal:
print(savings)
savings = (monthly_salary * monthly_savings) + (savings * interest)
months += 1
if months % 6 == 0 :
monthly_salary += monthly_salary * .03
print("Took " + str(months) + " to save enough")
Highlighted in Bold(the while loop) is the area I'm struggling with. I understand that what's happening is I've initialized the function fictional_cost with the initial guess variable. What I'd like to have happen is, as I perform a bisection search, for the updated variable to be used to re-call said function. Any help would be appreciated.
low = 0
high = 10000
annual_salary = float(input("Please enter your annual salary:"))
monthly_salary = float(annual_salary / 12)
guess = (high + low) / 2.0
def fictional_cost(n):
global months
global current_savings
global annual_salary
global monthly_salary
while months < 36:
rcurrent_savings = float(current_savings * (r / 12))
current_savings = float(rcurrent_savings + (monthly_salary * n) + current_savings)
months += 1
if months % 6 == 0:
annual_salary = annual_salary * float(1 + semi_annual_raise)
monthly_salary = annual_salary / 12
return current_savings
**while abs(fictional_cost(n)- total_cost) >= epsilon:
if fictional_cost(guess) < total_cost:
low = guess
else:
high = guess
guess = (high + low)/2.0
num_guesses += 1**
print("Your best savings rate is:" + str(guess) + "!")
print("Performed in" + str(num_guesses)+
"steps!")
input("Press enter to exit")
There are several variables in your code which look undefined (e.g. r) but I'm not going to focus on these as they aren't the focus of your question.
If I've understood your requirements correctly, this should work:
x = fictional_cost(guess)
while (x - total_cost) >= epsilon:
if x < total_cost:
low = guess
else:
high = guess
guess = (high + low)/2.0
num_guesses += 1
x = fictional_cost(guess)
If this answer doesn't work exactly, it might be that I've misunderstood some of your variables so try playing around with it a little, or add some more detail to your question. If you add more detail, I'll refine my answer if I can.
PS: I dont have a way to comment to your question. So using the answer section to respond.
I am trying to answer the question. It looks like you have not assigned a value for n.
I am unable to find the value of n in your code.
Can you explain where you are defining the value of n please. Highlighted in bold.
while abs(fictional_cost(**n**)- total_cost) >= epsilon:
My assumption is that you are trying to check with the value of guess instead of n. So your while loop should be as follows:
while abs(fictional_cost(guess)- total_cost) >= epsilon:
try using references in function parameters. String, whole numbers and tuples are call by value. Try using mutable objects as changing their values anywhere in the scope will reflect it everywhere in scope.
First of all, I feel sorry. This is my first time to use this website so I don't know the rules.
I try to show my attempt but I don't know how to do this. I just rewrite the code directly day by day and don't have the saved drafts. I use many methods to work out, but I am fresh to coding and as you know, I keep on going the MIT CS introduction course.
For this problem, I would like to paste the original link but you need to sign in the website first. So I google it and find a Github page which contained the problem. enter link description here The problem set has three problems and I have solved first two of them.
It's about how to calculate credit cards latest monthly payment, and must use bisection search.
I have worked out once. However, I can only do one bisection search and then minus 0.01 step to step to approach the result. I show you my code before, here is the only old version which I saved.
balance = 999999
annualInterestRate = 0.18
monthly_interest_rate = annualInterestRate /12.0
lower_bound = balance / 12.0
upper_bound = (balance * (1 + monthly_interest_rate)**12) / 12.0
def calculate_balance(balance, fixed):
month = 0
while month < 12:
balance = (balance - fixed) * (monthly_interest_rate + 1)
month += 1
return balance
while True:
if calculate_balance(balance, lower_bound) > 0:
lower_bound = (lower_bound + upper_bound) / 2
else:
skipped_answer = lower_bound
break
#print(skipped_answer)
while True:
#print(balance, skipped_answer)
if calculate_balance(balance, skipped_answer) < 0:
skipped_answer -= 0.01
else:
break
print(round(skipped_answer+0.01, 2))
Anyway, this code works fine but the grader of edx says my code takes too much time.
So I think out of dual-directed bisection search and it takes me hours again. But this is the limit of my ability. I have no ideas. Here's the code below.
balance = 999999
annualInterestRate = 0.18
monthly_interest_rate = annualInterestRate /12.0
lower_bound = balance / 12.0
upper_bound = (balance * (1 + monthly_interest_rate)**12) / 12.0
def calculate_balance(balance, fixed):
month = 0
while month < 12:
balance = (balance - fixed) * (monthly_interest_rate + 1)
month += 1
balance
return balance
while True:
if abs(calculate_balance(balance, lower_bound) - balance) > 0.01:
if calculate_balance(balance, lower_bound) > 0:
mark = lower_bound
lower_bound = (lower_bound + upper_bound) / 2
elif calculate_balance(balance, lower_bound) < 0:
upper_bound = lower_bound
lower_bound = mark
else:
break
print(lower_bound)
I don't know why it will be an infinite loop. And how to solve it? What's wrong?
Thinking this for hours. I have tried all the methods that I know.
I worked out all of the exercises by myself and takes me too much time. This time, I know I must get help of experienced people. There must be something I don't know.
I don't knew is it now of any interest to see my code that works very fast. It is little
bit different then yours:
balance = 999999
annualInterestRate = 0.18
payment = 0
lower = balance/12.0
upper = (balance*(1+annualInterestRate/12)**12)/12.0
lbalance = 0
while payment < balance :
oldbalance = lbalance
lbalance = balance
payment = round((lower + upper)/2,3)
for i in range(0,12):
unpaidBalance = lbalance - payment
interest = unpaidBalance * annualInterestRate / 12.0
lbalance = unpaidBalance + interest
if round(lbalance,2) == 0 or oldbalance == lbalance:
print('Lowest Payment: ' + str(payment))
break
elif lbalance > 0 :
lower = payment
else:
upper = payment
Why both of your codes in some cases use bisection and in some don't? This is the main problem.
All the best to all of you
Have a nice day
Below I have a piece of code which calculates credit card balance, but it doesn't work when balance has an extreme value (such as balance=9999999999below). It throws the code through an infinite loop. I have a couple of theories as to how to fix this flaw, but don't know how to move forward with them. Here's my code:
balance = 9999999999
annualInterestRate = 0.2
monthlyPayment = 0
monthlyInterestRate = annualInterestRate /12
newbalance = balance
month = 0
while newbalance > 0:
monthlyPayment += .1
newbalance = balance
for month in range(1,13):
newbalance -= monthlyPayment
newbalance += monthlyInterestRate * newbalance
month += 1
print("Lowest Payment:" + str(round(monthlyPayment,2)))
My theory is that
while newbalance > 0
is causing the infinite loop, because newbalance is always larger than 0.
How can I change this while loop so that it doesn't cause my code to run infinitely?
By the way:
With moderate numbers, the program runs for a long time and finally gives an answer. For the larger numbers, the program just keeps on going.
This loop is not infinite, but will take a long time to resolve. For very large values of balance, monthlyPayment will have to get very large in order to drop it past zero.
The bisection method will execute much quicker if you're allowed to use it in your assignment. Will not help you though, if you're required to increment the monthly payment by .01.
static_balance = balance
interest = (annualInterestRate/12)
epsilon = 0.01
lo = balance/12
hi = balance
while abs(balance) > epsilon:
balance = static_balance
min_pmt = (hi+lo)/2
for i in range(12):
balance -= min_pmt
balance *= 1+interest
if balance > 0:
lo = min_pmt
else:
hi = min_pmt
print("Lowest payment: ", round(min_pmt, 2))
I am creating a program that will compute the lowest monthly payment to be paid for a credit card, given a certain credit card balance and interest rate. The time frame for the payoff is 12 months and the monthly payment has to be accurate down to the nearest penny, using bisection search.
I was able to get the answer, the problem is that I couldn't get my while loop to quit once the monthly payment was calculated to the nearest cent, so I had to make an infinite while loop that has a elif statement at the bottom of the while loop that quits for me. I was wondering if anyone can figure out what condition to give the while loop so it will quit on its own. Also I just started learning python a week ago, and want some advice on how good/bad my code is. Any ideas?
# random balance
balance = 999999
# random interest rate
annualInterestRate = 0.18
# assign balance to another variable that will undergo the testing
balance_tested = balance
# bounds of bisection search
low = (balance / 12.0)
high = ((balance * (1 + (annualInterestRate/12.0))**12)/12.0)
# start month
month = 1
monthlyPayment = (low + high) / 2.0 #Averages out the bounds to meet in the middle
while abs(balance_tested != 0): #While loop that I can't get right, just made it to run infinitely
balance_tested = balance #Resets balance being tested back to original balance
monthlyPayment = (low + high) / 2.0 #Bisection search recalculates
month = 1 #Month reset back to 1
while month <= 12: #Loops through all 12 months with the payments being made and interested getting added
balance_tested = (balance_tested - monthlyPayment)
balance_tested += (balance_tested * (annualInterestRate/12))
month += 1
print "Balance Remaining: %.20f" % balance_tested
if balance_tested < 0: #If the bisection search guesses to high, decreases the high bound
high = monthlyPayment
elif balance_tested <= 0.01: #Conditional statement that stops the testing if the balance gets paid off to the cent
break
else: #If bisection search guesses to low, increases low bound
low = monthlyPayment
print "Monthly Payment: %.2f" % monthlyPayment
print "Lowest Payment: %.2f" % monthlyPayment
Is there any reason you're using the break statement instead of just putting that as the condition for your while loop?
balance = 999999
annualInterestRate = 0.18
balance_tested = balance
low = (balance / 12.0) #Lower bound of bisection search
high = ((balance * (1 + (annualInterestRate/12.0))**12)/12.0)
month = 1
monthlyPayment = (low + high) / 2.0
while not (balance_tested <= 0.01):
balance_tested = balance
monthlyPayment = (low + high) / 2.0
month = 1
while month <= 12:
balance_tested = (balance_tested - monthlyPayment)
balance_tested += (balance_tested * (annualInterestRate/12))
month += 1
print "Balance Remaining: %.20f" % balance_tested
if balance_tested < 0:
high = monthlyPayment
else:
low = monthlyPayment
print "Monthly Payment: %.2f" % monthlyPayment
print "Lowest Payment: %.2f" % monthlyPayment
You already have the condition, why not put that for the while statement instead?
while not balance_tested <= 0.01:
# etc.
while abs(balance_tested != 0):
will stay in the loop if the balance_tested becomes negative. Make this
while balance_tested >= 0.1:
This will automatically handle the rounding when it drops below a penny.