LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-24-2013, 04:14 PM   #1
joshp
LQ Newbie
 
Registered: Aug 2006
Location: Chicago IL
Distribution: To many to list.
Posts: 27

Rep: Reputation: 1
Python Help


Hey All,
I am working on a MITx class and having problems with the following, I am supposed to use bisection search to find the min payment of the given balance over the course of year for it to be 0. Been working on this the last twoish days and have not gotten stuck.

Code:
balance = 320000
annualInterestRate = 0.2


monInterest=annualInterestRate/12
originalBalance=balance
low=originalBalance/12
epsilon = 0.01
high = (originalBalance*(1 + monInterest)**12)/12
min_payment = (high+low)/2
total_interest=0

print "Before While: " + str(min_payment*12 - (originalBalance + total_interest) )

while min_payment*12 - (originalBalance + total_interest)  >= 0:   
    
    print "Total Inter: "+ str(total_interest)
    print "Min Paymen: " + str(min_payment)
    
    for month in range(0, 11):
        
        balance = balance - min_payment * ( monInterest) 
        print "Bal in For: " + str(balance)
        total_interest += (balance * monInterest)
        #print total_interest
    
    
    print "Bal " + str(balance)
    if balance  < 0:
        low = min_payment
      
    elif balance > 0:
        high = min_payment
        min_payment=(high + low)/2.0
  
     
print "Lowest Payment: " + str(round(balance,2))
Its outputting that the Lowest Payment is 314574.88. From the answer I see on the site it should be 29157.09. I have a feeling I am missing something obivios here. Any one have any ideas?

Josh
 
Old 02-24-2013, 09:49 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
There are a lot of errors in your program, I noted them in red:

Code:
balance = 320000
annualInterestRate = 0.2 # not an error, but 20% interest rate, yikes!


monInterest=annualInterestRate/12
originalBalance=balance
low=originalBalance/12
epsilon = 0.01 # your epsilon is too big
high = (originalBalance*(1 + monInterest)**12)/12
min_payment = (high+low)/2
total_interest=0

print "Before While: " + str(min_payment*12 - (originalBalance + total_interest) )

# should be using epsilon here
while min_payment*12 - (originalBalance + total_interest)  >= 0:   
    
    print "Total Inter: "+ str(total_interest)
    print "Min Paymen: " + str(min_payment)
    

    # you need to reset balance and total_interest here
    for month in range(0, 11): # a year has 12 months
        
        balance = balance - min_payment * ( monInterest) # interest doesn't apply to the payment
        print "Bal in For: " + str(balance)
        total_interest += (balance * monInterest)
        # you need to compound your interest
        #print total_interest
    
    
    print "Bal " + str(balance)
    # you have your bisectioning backwards
    if balance  < 0:
        low = min_payment
      
    elif balance > 0:
        high = min_payment
        min_payment=(high + low)/2.0 # this should not be exclusive to the left bisection
  

# you printed the wrong variable here     
print "Lowest Payment: " + str(round(balance,2))
 
Old 02-25-2013, 03:14 AM   #3
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Code:
# should be using epsilon here
while min_payment*12 - (originalBalance + total_interest)  >= 0:
You should also use an absolute value function.


Code:
    for month in range(0, 11): # a year has 12 months
        
        balance = balance - min_payment * ( monInterest) # interest doesn't apply to the payment
Counting from 0 to 11 gives 12 months so that is not the problem.

The correct formula for calculating the balance is:
balance = balance * (1 + monInterest) - min_payment

Last edited by psionl0; 02-25-2013 at 03:16 AM.
 
Old 02-25-2013, 06:12 AM   #4
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
@joshp, I fixed up your code (there are errors in your code that haven't been listed yet) and the correct answer is $29,643.04. The $29,057.09 that you see on the site is the amount owing at the beginning of the 12th month but interest still has to be added for that month too.

I verified my answer using a spreadsheet program and this is what the spreadsheet displays:
Code:
month	    start            interest       pay        end
  1     320000            5333.3333333333 29643.0415 295690.291833333
  2     295690.291833333  4928.1715305556 29643.0415 270975.421863889
  3     270975.421863889  4516.2570310648 29643.0415 245848.637394954
  4     245848.637394954  4097.4772899159 29643.0415 220303.07318487
  5     220303.07318487   3671.7178864145 29643.0415 194331.749571284
  6     194331.749571284  3238.8624928547 29643.0415 167927.570564139
  7     167927.570564139  2798.7928427357 29643.0415 141083.321906875
  8     141083.321906875  2351.3886984479 29643.0415 113791.669105322
  9     113791.66910532   1896.5278184221 29643.0415  86045.1554237445
 10     86045.1554237445  1434.0859237291 29643.0415  57836.1998474736
 11     57836.1998474736   963.9366641246 29643.0415  29157.0950115982
 12     29157.0950115982   485.9515835266 29643.0415      0.0050951248
				
                         35716.5030951247
If you can't get your code working properly, PM me and I will send you my listing. (It's actually written in blassic but the syntax is similar to python).

Last edited by psionl0; 02-25-2013 at 06:16 AM.
 
Old 02-25-2013, 06:35 AM   #5
audriusk
Member
 
Registered: Mar 2011
Location: Klaipėda, Lithuania
Distribution: Slackware
Posts: 360

Rep: Reputation: 199Reputation: 199
Quote:
Originally Posted by psionl0 View Post
Code:
    for month in range(0, 11): # a year has 12 months
        
        balance = balance - min_payment * ( monInterest) # interest doesn't apply to the payment
Counting from 0 to 11 gives 12 months so that is not the problem.
The end point is omitted in range() results, so it is a problem.
 
Old 02-25-2013, 07:43 AM   #6
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Quote:
Originally Posted by audriusk View Post
The end point is omitted in range() results, so it is a problem.
Sounds like a gotcha to me. That's why I prefer the for syntax used in C.
 
Old 02-25-2013, 08:36 AM   #7
audriusk
Member
 
Registered: Mar 2011
Location: Klaipėda, Lithuania
Distribution: Slackware
Posts: 360

Rep: Reputation: 199Reputation: 199
Not really a gotcha, unless one is new to Python. It's documented (see help(range) in Python's interactive mode or pydoc range in the shell) and consistent to how list slicing works: foo[n:m] will return list elements from n (inclusive) to m (exclusive).
 
Old 02-25-2013, 10:06 AM   #8
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by psionl0
Code:
# should be using epsilon here
while min_payment*12 - (originalBalance + total_interest)  >= 0:
You should also use an absolute value function.
Yes, I was trying to leave a bit of thought for the OP to do.

Quote:
Code:
    for month in range(0, 11): # a year has 12 months
Counting from 0 to 11 gives 12 months so that is not the problem.
As audriusk mentioned, python's range is right-open, so range(0, 11) means [0, 11).
Quote:
Sounds like a gotcha to me. That's why I prefer the for syntax used in C.
It would be even more of a gotcha if you had to do range(0, len(list)-1) to get the indexes of a list. Also note that range(12) is the same as range(0, 12).

Quote:
The correct formula for calculating the balance is:
balance = balance * (1 + monInterest) - min_payment
...
the correct answer is $29,643.04. The $29,057.09 that you see on the site is the amount owing at the beginning of the 12th month but interest still has to be added for that month too.
$29,643.04 is the answer if you make payments at the end of the month, $29,157.09 if you make them at the beginning.

Quote:
If you can't get your code working properly, PM me and I will send you my listing. (It's actually written in blassic but the syntax is similar to python).
They won't be able to use PMs until 150 posts. I would also ask you to refrain from handing out answers.
 
Old 02-25-2013, 05:31 PM   #9
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Quote:
Originally Posted by ntubski View Post
$29,643.04 is the answer if you make payments at the end of the month, $29,157.09 if you make them at the beginning.
If the payments are made at the beginning of the month then my formula for the balance is incorrect. Also, the loan would effectively be paid off in 11 months instead of 12 so the exit condition of the outer loop as written in the OP is also incorrect (of course there was a much simpler test that could have been used).

Quote:
Originally Posted by ntubski View Post
They won't be able to use PMs until 150 posts.
I was unaware of that. It seems unnecessarily restrictive to me.

Quote:
Originally Posted by ntubski View Post
I would also ask you to refrain from handing out answers.
In my experience (I have been a private tutor for many years), it will never happen.
 
Old 02-25-2013, 08:06 PM   #10
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Quote:
Originally Posted by psionl0 View Post
Sounds like a gotcha to me. That's why I prefer the for syntax used in C.
It's actually pretty much like the syntax in C if you are used to using less-than. range(x, y) maps to for (i = x; i < y; ++i) and range(x, y, z) maps to for (i = x; i < y; i += z).
 
Old 02-25-2013, 08:12 PM   #11
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by psionl0 View Post
If the payments are made at the beginning of the month then my formula for the balance is incorrect. Also, the loan would effectively be paid off in 11 months instead of 12 so the exit condition of the outer loop as written in the OP is also incorrect (of course there was a much simpler test that could have been used).
The time between the first and last payments is 11 months in both cases, but there are 12 payments in both cases so the outer loop test is still correct (except for the abs() and epsilon thing).
 
Old 02-25-2013, 09:52 PM   #12
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Quote:
Originally Posted by ntubski View Post
The time between the first and last payments is 11 months in both cases, but there are 12 payments in both cases so the outer loop test is still correct (except for the abs() and epsilon thing).
The outer loop test does not give the balance as you would expect because total_interest is not calculated correctly.

Last edited by psionl0; 02-25-2013 at 10:21 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Python 2.6 dependencies error installing python-sqlite2-2.5.5-1.1.i586.rpm jmp007 Linux - Newbie 1 02-11-2011 11:05 AM
LXer: Python Python Python (aka Python 3) LXer Syndicated Linux News 0 08-05-2009 08:30 PM
LXer: Review: Programming in Python 3: A Complete Introduction to the Python Language LXer Syndicated Linux News 0 01-26-2009 04:50 AM
python update - Unable to load GTK2 Python bindings: No module named gtk itpedersen Linux - Software 2 10-03-2008 03:44 AM
LXer: Move to python 2.4 / Changing the packaging style for python packages LXer Syndicated Linux News 0 06-13-2006 07:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration