week 8

作者: 猪蹄炖粥 | 来源:发表于2018-08-06 22:12 被阅读3次

    视频总结:
    视频范围:week1

    The recipe for deducing square root cannot be made into a real computer program because computers cannot guess a starting value for y.

    answer : True

    计算机不能根据经验猜初始值

    程序计数器A program counter:
    指向程序将执行的下一条命令points the computer to the next instruction to execute in the program.

    Determines whether a string is legal:Syntax
    Determines whether a string has meaning:Static semantics
    Assigns a meaning to a legal sentence:Semantics

    十一章总结
    解决python3.x版本报错:No module named 'pylab'

    conda install numpy
    conda install scipy
    conda install matplotlib
    

    完整代码:

    import pylab
    def findPayment(loan, r, m):
        """
        假设loan和r是浮点数, m是整数
        返回一个总额为loan,月利率为r,期限为m个月的抵押贷款的每月还款额
        """
        return loan*((r*(1+r)**m)/((1+r)**m - 1))
    
    class Mortgage(object):
        """建立不同种类抵押贷款的抽象类"""
        def __init__(self, loan, annRate, months):
            self.loan = loan
            self.rate = annRate/12.0
            self.months = months
            self.paid = [0.0]
            self.outstanding = [loan]
            self.payment = findPayment(loan, self.rate, months)
            self.legend = None #description of mortgage
    
        def makePayment(self):
            self.paid.append(self.payment)
            reduction = self.payment - self.outstanding[-1]*self.rate
            self.outstanding.append(self.outstanding[-1] - reduction)
    
        def getTotalPaid(self):
            return sum(self.paid)
    
        def __str__(self):
            return self.legend
    
        def plotPayments(self, style):
            pylab.plot(self.paid[1:], style, label = self.legend)
    
        def plotBalance(self, style):
            pylab.plot(self.outstanding, style, label = self.legend)
    
        def plotTotPd(self, style):
            totPd = [self.paid[0]]
            for i in range(1, len(self.paid)):
                totPd.append(totPd[-1] + self.paid[i])
            pylab.plot(totPd, style, label = self.legend)
    
        def plotNet(self, style):
            totPd = [self.paid[0]]
            for i in range(1, len(self.paid)):
                totPd.append(totPd[-1] + self.paid[i])
            equityAcquired = pylab.array([self.loan] * len(self.outstanding))
            equityAcquired = equityAcquired - pylab.array(self.outstanding)
            net = pylab.array(totPd) - equityAcquired
            pylab.plot(net, style, label = self.legend)
    
    class Fixed(Mortgage):
        def __init__(self, loan, r, months):
            Mortgage.__init__(self, loan, r, months)
            self.legend = 'Fixed, ' + str(r*100) + '%'
    
    class FixedWithPts(Mortgage):
        def __init__(self, loan, r, months, pts):
            Mortgage.__init__(self, loan, r, months)
            self.pts = pts
            self.paid = [loan*(pts/100.0)]
            self.legend = 'Fixed, ' + str(r*100) + '%, '\
            + str(pts) + ' points'
    
    class TwoRate(Mortgage):
        def __init__(self, loan, r, months, teaserRate, teaserMonths):
            Mortgage.__init__(self, loan, teaserRate, months)
            self.teaserMonths = teaserMonths
            self.teaserRate = teaserRate
            self.nextRate = r/12.0
            self.legend = str(teaserRate*100)\
            + '% for ' + str(self.teaserMonths)\
            + ' months, then ' + str(r*100) + '%'
    
        def makePayment(self):
            if len(self.paid) == self.teaserMonths + 1:
                self.rate = self.nextRate
                self.payment = findPayment(self.outstanding[-1],self.rate,self.months - self.teaserMonths)
            Mortgage.makePayment(self)
    
    def compareMortgages(amt, years, fixedRate, pts, ptsRate,
        varRate1, varRate2, varMonths):
        totMonths = years*12
        fixed1 = Fixed(amt, fixedRate, totMonths)
        fixed2 = FixedWithPts(amt, ptsRate, totMonths, pts)
        twoRate = TwoRate(amt, varRate2, totMonths, varRate1, varMonths)
        morts = [fixed1, fixed2, twoRate]
        for m in range(totMonths):
            for mort in morts:
                mort.makePayment()
        plotMortgages(morts, amt)
    
    def plotMortgages(morts, amt):
        def labelPlot(figure, title, xLabel, yLabel):
            pylab.figure(figure)
            pylab.title(title)
            pylab.xlabel(xLabel)
            pylab.ylabel(yLabel)
            pylab.legend(loc = 'best')
        styles = ['k-', 'k-.', 'k:']
        #给图编号赋名
        payments, cost, balance, netCost = 0, 1, 2, 3
        for i in range(len(morts)):
            pylab.figure(payments)
            morts[i].plotPayments(styles[i])
            pylab.figure(cost)
            morts[i].plotTotPd(styles[i])
            pylab.figure(balance)
            morts[i].plotBalance(styles[i])
            pylab.figure(netCost)
            morts[i].plotNet(styles[i])
        labelPlot(payments, 'Monthly Payments of $' + str(amt) +
        ' Mortgages', 'Months', 'Monthly Payments')
        labelPlot(cost, 'Cash Outlay of $' + str(amt) +
        'Mortgages', 'Months', 'Total Payments')
        labelPlot(balance, 'Balance Remaining of $' + str(amt) +
        'Mortgages', 'Months', 'Remaining Loan Balance of $')
        labelPlot(netCost, 'Net Cost of $' + str(amt) + ' Mortgages',
        'Months', 'Payments - Equity $')
    
    compareMortgages(amt=200000, years=30, fixedRate=0.07,pts = 3.25, ptsRate=0.05,varRate1=0.045, varRate2=0.095, varMonths=48)
    

    相关文章

      网友评论

          本文标题:week 8

          本文链接:https://www.haomeiwen.com/subject/ubzgvftx.html