习题3:

写一个函数postRetirement,参数:存款savings、投资账户(其实就是养老金么...)的年增长率growthRates是一个数组,数组的长度表示退休后的年份,也就是说这个数组的范围是从退休后的第一年到len(growthRates)年,当年消费expenses

假设账户结算利率在取存款前;

返回一个数组,包含每一年账户资金。

完全按下面来定义这个函数

def postRetirement(savings, growthRates, expenses):

第一年投资账户资金:savings * (1 + growthRates[0]) - expenses

第二年投资账户资金:( savings * (1 + growthRates[0]) - expenses) * (1 + growthRates[1]) - expense

第三年投资账户资金:(( savings * (1 + growthRates[0]) - expenses) * (1 + growthRates[1]) - expense) * (1 + growthRates[2])- expense

当第x年的时候账户资金: x-1年的savings * (1 + growthRate[x]) - expense

def postRetirement(savings, growthRates, expenses):def annual_fund_Retired(savings, growthRates, expenses):years = len(growthRates)if years > 1:years = years - 1annual_total = annual_fund_Retired(savings, growthRates[:years], expenses) * (1 + growthRates[years]) - expensesreturn annual_totalelif years == 1:annual_total = savings * (1 + growthRates[0]) - expensesreturn annual_totalelse:return 0fund = []for time in range(0, len(growthRates)):fund.append(annual_fund_Retired(savings, growthRates[:time + 1], expenses))return fund

习题4:

写一个叫findMaxExpenses的函数,这个函数有五个参数:工资salary、工资投资百分比save、工作时投资账户的年增长率preRetireGrowthRates(数组)、退休后投资账户的年增长率postRetireGrowthRates(数组)、epsilon。

其实之前感觉不是很好很科学啊,因为退休后不拿的话不就代表挂了么...人能提前知道什么时候挂吗?...

使用二分法找出退休后到退休结束之间(不就是挂了么)每年固定能消费的数额,这个数额能保证退休结束时账户余额的绝对值小于epsilon(生不带来死不带去,一定要用光...),初始搜索的消费数额在0和退休开始的未取存款之间。

提示1#可以用习题2。

这个函数必须在每次迭代时候显示出当前预估的消费数额。
提示2#可以利用习题3
提示3#答案在0和初始未取存款+epsilon之间。
这样定义函数:
def findMaxExpenses(salary, save, preRetireGrowthRates, postRetireGrowthRates, epsilon):

第一年投资账户资金:savings * (1 + growthRates[0]) - expenses

第二年投资账户资金:( savings * (1 + growthRates[0]) - expenses) * (1 + growthRates[1]) - expense

第三年投资账户资金:(( savings * (1 + growthRates[0]) - expenses) * (1 + growthRates[1]) - expense) * (1 + growthRates[2])- expense

当第i年的时候账户资金: x-i年的savings * (1 + growthRates[x-i]) - expense

savings为常数,设为a;growthRate,设第一年的为b0、第二2为b1、第n年为bn-1;

expense为未知数,设为x;账户资金为y

第一年投资账户资金:y = -x + a + ab0

第二年投资账户资金:y = -(2 + b1)x + a + ab0 + ab1 + abob1

可以推导出资金账户的金额: y = -mx + n;m、n为正数

y的绝对值需要小于epsilon;

x在0和a+epsilon之间;

下图黄线为|y|。

low = 0,high = a + epsilon;guess = (low + high) / 2

当y>0时,如果y>epsilon,取右边,也就是 low = guess。

当y<0时,如果y>epsilon,取左边,也就是 high = guess,

def nestEggVariable(salary, save, growthRates):def annual_save(salary, save):annual_save = salary * savereturn annual_savedef annual_fund_Variable(salary, save, growthRates):years = len(growthRates)if years > 0:years = years - 1annual_total = annual_fund_Variable(salary, save, growthRates[:years]) * (1 + growthRates[years]) + annual_save(salary, save)return annual_totalelse:return 0fund = []for time in range(0, len(growthRates)):fund.append(annual_fund_Variable(salary, save, growthRates[:time + 1]))return funddef postRetirement(savings, growthRates, expenses):def annual_fund_Retired(savings, growthRates, expenses):years = len(growthRates)if years > 1:years = years - 1annual_total = annual_fund_Retired(savings, growthRates[:years], expenses) * (1 + growthRates[years]) - expensesreturn annual_totalelif years == 1:annual_total = savings * (1 + growthRates[0]) - expensesreturn annual_totalelse:return 0fund = []for time in range(0, len(growthRates)):fund.append(annual_fund_Retired(savings, growthRates[:time + 1], expenses))return funddef findMaxExpenses(salary, save, preRetireGrowthRates, postRetireGrowthRates, epsilon):savings = nestEggVariable(salary, save, preRetireGrowthRates).pop()high = savingslow = 0count = 1while True:guess = (high + low) / 2retirement_fund = postRetirement(savings, postRetireGrowthRates, guess).pop()absolute = abs(retirement_fund)if absolute >= epsilon and count <= 100:print count,": estimated expense = ", guessif retirement_fund > 0:low = guesselse:high = guesscount = count + 1else: breakreturn guess

最后发上来几个验证的函数

def testNestEggFixed():salary     = 10000save       = 0.10growthRate = 0.15years      = 5savingsRecord = nestEggFixed(salary, save, growthRate, years)print savingsRecord# Output should have values close to:# [1000.0, 2150.0, 3472.5, 4993.375, 6742.3812499999995]
def testNestEggVariable():salary      = 10000save        = 0.10growthRates = [0.03, 0.04, 0.05, 0, 0.03]savingsRecord = nestEggVariable(salary, save, growthRates)print savingsRecord# Output should have values close to:# [1000.0, 2040.0, 3142.0, 4142.0, 5266.2600000000002]
def testPostRetirement():savings     = 100000growthRates = [0.10, 0.05, 0, 0.05, 0.01]expenses    = 30000savingsRecord = postRetirement(savings, growthRates, expenses)print savingsRecord# Output should have values close to:# [80000.000000000015, 54000.000000000015, 24000.000000000015,# -4799.9999999999854, -34847.999999999985]
def testFindMaxExpenses():salary                = 10000save                  = 0.10preRetireGrowthRates  = [0.03, 0.04, 0.05, 0, 0.03]postRetireGrowthRates = [0.10, 0.05, 0, 0.05, 0.01]epsilon               = .01expenses = findMaxExpenses(salary, save, preRetireGrowthRates,postRetireGrowthRates, epsilon)print expenses# Output should have a value close to:# 1229.95548986

麻省理工学院公开课:计算机科学及编程导论习题4下相关推荐

  1. 麻省理工学院公开课:单变量微积分习题课

    http://open.163.com/special/opencourse/calculus.html [第1集] 课程简介 (已看) [第2集] 导数的定义 (已看) [第3集] 导数的图像 (已 ...

  2. Python语言程序设计之urllib.request抓取页面,网易公开课之《麻省理工学院公开课:算法导论》

    Python语言用urllib.request模块抓取页面非常简单,再将抓取的页面内容用re模块解析,找出自己想要的东西.下面就就此方法来抓取网易公开课之<麻省理工学院公开课:算法导论>, ...

  3. 麻省理工学院公开课:计算机科学及编程导论习题2

    习题1: 已知6a + 9b + 20c = n,当n = 50, 51, 52,53, 54, 55时,a.b.c有自然数解(我不知道现在是怎么定义的,但我以前学的时候自然数包括0), 如何求出n ...

  4. 计算机教学及其编程视频教学,计算机科学及编程导论教学视频

    全集 http://video.1kejian.com/university/open/13647/ 评分: 5(力荐)分 视频类型: 人气:372 次点播 更新时间:2012-09-14 22:39 ...

  5. 计算机科学与python编程导论_【基于Python】MIT OCW 计算机科学与编程导论

    [基于Python]MIT OCW 计算机科学与编程导论 (MIT Course Number 6.0001)Introduction to Computer Science and Programm ...

  6. 麻省电气工程与计算机科学专业,美国留学 麻省理工学院电气工程与计算机科学理科专业介绍...

    关于美国麻省理工学院(Massachusetts Institute of Technology) 麻省理工学院体育馆 美国麻省理工学院(Massachusetts Institute of Tech ...

  7. 麻省理工公开课:线性代数》中文笔记来了

    MLNLP社区在Github上最新发布了一套MIT线性代数课程Linear Algebra的学习笔记,目前已获得1600star.项目简介如下所示,欢迎大家关注! 1.项目动机 <麻省理工公开课 ...

  8. 算法导论-麻省理工公开课-百度云

    资源来源于网络,若有侵权,请告知,我会在24小时之内删除~~ 我整理的资源已汇总,请访问:资源汇总贴,选择自己还需要的资源~ 算法导论的视频教程,算法导论,算法中的圣经啊.分享麻省理工的公开课,需要的 ...

  9. 电气工程学计算机,麻省理工学院电气工程与计算机科学专业解读

    2015年US News研究生专业排名中麻省理工学院工程学院下的电气工程与计算机科学全美第二,麻省理工学院本身不论是综合排名还是具体到某个学科都是部分国际学生的梦想学校.接下来,就来具体了解一下麻省理 ...

  10. 麻省电气工程与计算机科学专业,麻省理工学院电气工程与计算机科学专业解读...

    编者按:2015年US News研究生专业排名中麻省理工学院工程学院下的电气工程与计算机科学全美第二,麻省理工学院本身不论是综合排名还是具体到某个学科都是部分国际学生的梦想学校. 2015年US Ne ...

最新文章

  1. react绑定this_React绑定模式:处理“ this”的5种方法
  2. 一天学习一点之express demo
  3. unity调整旋转需要传什么参数?参数在数学上叫做什么?_人脸识别背后,卷积神经网络的数学原理原来是这样的...
  4. 【BZOJ1048】分割矩阵(记忆化搜索,动态规划)
  5. An Energy-Efficient Ant-Based Routing Algorithm for Wireless Sensor Networks (无线传感网中一种基于蚁群算法的能量有效路由)
  6. html动态网站维护页面
  7. python数据可视化仪表盘,Python 数据可视化?
  8. 浅析APP应用内及新媒体类推广渠道
  9. Webpack 配置中的一股清流
  10. 小程序源码:游戏扫码登录多功能工具箱集合
  11. 伽罗华域(Galois Field)理解、基于伽罗华域的四则运算(附详细python代码)
  12. uni-app【多媒体API】
  13. 腾讯云服务器购买详细流程(手把手教程)
  14. 阿里mysql迁移mongodb_快速掌握 MongoDB 数据库
  15. 在SAR-Opt数据融合领域针对深度学习的SEN1-2数据集
  16. 撤消git stash pop导致合并冲突
  17. idea报错Artifact is not deployed. Press ‘Deploy‘ to start deployment
  18. POI生成Excel
  19. Python数据分析实战2.4-注释【python】
  20. MYC归来(2)第三次测试

热门文章

  1. python中sys.stdout和sys.stderr
  2. 15.正则表达式扩展正则字符处理
  3. shell基础--正则表达式行列提取
  4. DBF文件简介(转)
  5. mysql error 1236_【MySQL】Got fatal error 1236原因和解决方法
  6. MAC打开outlook提示”正在修复 Main Profile“
  7. Movist Pro for Mac 2.2.16 — 播放器
  8. android 局域网socket,Android基于局域网socket通信
  9. 计划bom表 java_ERP总结系列(BOM浅谈)
  10. diamond源码解析