Quantcast

A community for students. Sign up today!

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

Aslander

  • 3 years ago

PS4.3: Ok I have this odd problem...the post retirement fund...my first three amounts add up, but then amount 4, 5 do not match. I cannot find the problem with my algorithm...any thoughts?

  • This Question is Closed
  1. Aslander
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    My code is this: def postretirement(savings, growthrates,expenses): savingsrecord = [] for i in range(len(growthrates)): fund = savings + float(savings*growthrates[i]) - expenses savingsrecord.append(fund) savings = fund print "SavingsRecord", savingsrecord

  2. Aslander
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    The amounts that were specified are this: [80000.000000000015, 54000.000000000015, 24000.000000000015, -4799.9999999999854, -34847.999999999985] My last two records are slightly different.

  3. Aslander
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    And are returned as this: SavingsRecord [80000.0, 54000.0, 24000.0, -4800.0, -39600.0]

  4. Aslander
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Ok I reworked my code a bit, but I am still off... Results: SavingsRecord [80000.0, 54000.0, 24000.0, -4800.0, -34800.0] def postretirement(savings, growthrates,expenses): savingsrecord = [] for i in range(len(growthrates)): if savings > 0: fund = savings + float(savings*growthrates[i]) - expenses savingsrecord.append(fund) savings = fund else: fund = float(savings*growthrates[i]) - expenses savingsrecord.append(fund) print "SavingsRecord", savingsrecord Ideas?

  5. Keen
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    "savings*growthrates[i]" This should be savings*0.01*growthrates[i] to convert the growthrates to percentages. I too am having issues on this problem. I'll report what I find. My current output is: [80000.00000000001, 54000.000000000015, 20000.000000000015, -1964.9999999999818, -38563.70919999998]

  6. Keen
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    This is my current code. Still baffled. #Initialize result with (savings * (1 + .01 * growthRates[0])) in each index, with #result's length being equal to the number of years of growth rates. result = [savings * (1 + .01 * growthRates[0]) - expenses] * len(growthRates) for j in range(1, len(growthRates)+1): for i in range(j, len(growthRates)): result[i] = result[i] * (1 + .01 * growthRates[i]) - expenses return result

  7. joelrob
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    i believe you are missing compounding.try f=[] f.append(savings) for i in range(len(growthRates)): f.append (f[i]*(1+.01*growthRates[i])-expenses) del f[0] return f

  8. Keen
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I'm using the same code I used for the earlier steps in the assignment, so I shouldn't have compounding issues. I would have gotten the wrong answers earlier in the problem set. I think we're having issues with error propagation. I added some prints to my code to track values of variables: result[i] * (1 + .01 * growthRates[i]) - expenses = 20000.0 result[i] = 50000.0 growthrates[i] = 0 But if I type the values of those variables right into the interpreter: >>> 50000*(1+.01*1) - 30000 20500.0

  9. Keen
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Err, nevermind. I dunno how I missed that 0 doesn't equal 1. Derp derp.

  10. Keen
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Yeah, now I've got it working. My previous method was failing to properly multiply against previous iterations. I like your use of append to iterate, very slick.

  11. Keen
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Isn't this code: f=[] f.append(savings) the same as: f = [savings]

  12. Aslander
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I did not add the "1+...." into my algorithm...I will recode that part and see what I get...

  13. Aslander
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Hey - I changed my code and got it to work perfectly - thanks for all your help!

  14. Aslander
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    If you want to take a look at the new trial and compare the results: def postretirement(savings, growthrates,expenses): """Growthrates as a list of percentages ex. (0.01 for 10%)""" savingsrecord = [] for i in range(len(growthrates)): if savings > 0: #fund = savings + float(savings*growthrates[i]) - expenses fund = float(savings*(1+(0.01*growthrates[i]))) - expenses savingsrecord.append(fund) savings = fund print "Expenses", expenses else: fund = float(savings*(1+0.01*growthrates[i])) - expenses savingsrecord.append(fund) savings = fund print "SavingsRecord", savingsrecord return savingsrecord

  15. Aslander
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    The commented out code was the original equation I used...don't know why I missed the 1+, but now its there on the line just below. Thanks again.

  16. Aslander
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Of course, this: """Growthrates as a list of percentages ex. (0.01 for 10%)""" would no longer apply either, as I changed the code to accept just the raw percentage, rather than floating points.

  17. Aslander
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Isn't this code: f=[] f.append(savings) the same as: f = [savings] You are making a list of a list. So, if you take as an example, savings = [1,2,3,4,5] then f =[savings] would return [[1,2,3,4,5]]. I don't know if that would matter, unless you append savings or f with another list, because then you would have to be careful about indexes. Example: Append savings with 7, then f with 8. Notice the difference? savings = [1,2,3,4,5,6,7] f = [[1,2,3,4,5,6,7],8] So, savings[1] which is not the same as f[1].

  18. Aslander
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Savings[1] = 2 f[1] = 8

  19. Keen
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    > You are making a list of a list. So, if you take as an example, > savings = [1,2,3,4,5] Except savings is just an int for that problem, not a list.

  20. Aslander
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    If you simply need an int, then the original question: is f.append(savings) the same as f = [savings], is not really relevant, since both involve lists. If you just need an int, then use variables without list functions.

  21. Not the answer you are looking for?
    Search for more explanations.

    Search OpenStudy
    • Attachments:

Ask your own question

Ask a Question
Find more explanations on OpenStudy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.