习题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 - 1
annual_total = annual_fund_Retired(savings, growthRates[:years], expenses) * (1 + growthRates[years]) - expenses
return annual_total
elif years == 1:
annual_total = savings * (1 + growthRates[0]) - expenses
return annual_total
else:
return 0
fund = []
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。
第一年投资账户资金: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 * save
return annual_save
def annual_fund_Variable(salary, save, growthRates):
years = len(growthRates)
if years > 0:
years = years - 1
annual_total = annual_fund_Variable(salary, save, growthRates[:years]) * (1 + growthRates[years]) + annual_save(salary, save)
return annual_total
else:
return 0
fund = []
for time in range(0, len(growthRates)):
fund.append(annual_fund_Variable(salary, save, growthRates[:time + 1]))
return fund
def postRetirement(savings, growthRates, expenses):
def annual_fund_Retired(savings, growthRates, expenses):
years = len(growthRates)
if years > 1:
years = years - 1
annual_total = annual_fund_Retired(savings, growthRates[:years], expenses) * (1 + growthRates[years]) - expenses
return annual_total
elif years == 1:
annual_total = savings * (1 + growthRates[0]) - expenses
return annual_total
else:
return 0
fund = []
for time in range(0, len(growthRates)):
fund.append(annual_fund_Retired(savings, growthRates[:time + 1], expenses))
return fund
def findMaxExpenses(salary, save, preRetireGrowthRates, postRetireGrowthRates, epsilon):
savings = nestEggVariable(salary, save, preRetireGrowthRates).pop()
high = savings
low = 0
count = 1
while True:
guess = (high + low) / 2
retirement_fund = postRetirement(savings, postRetireGrowthRates, guess).pop()
absolute = abs(retirement_fund)
if absolute >= epsilon and count <= 100:
print count,": estimated expense = ", guess
if retirement_fund > 0:
low = guess
else:
high = guess
count = count + 1
else:
break
return guess
最后发上来几个验证的函数
def testNestEggFixed():
salary = 10000
save = 0.10
growthRate = 0.15
years = 5
savingsRecord = 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 = 10000
save = 0.10
growthRates = [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 = 100000
growthRates = [0.10, 0.05, 0, 0.05, 0.01]
expenses = 30000
savingsRecord = 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 = 10000
save = 0.10
preRetireGrowthRates = [0.03, 0.04, 0.05, 0, 0.03]
postRetireGrowthRates = [0.10, 0.05, 0, 0.05, 0.01]
epsilon = .01
expenses = findMaxExpenses(salary, save, preRetireGrowthRates,
postRetireGrowthRates, epsilon)
print expenses
# Output should have a value close to:
# 1229.95548986