【问题标题】:Getting negative rate for specific set of values in numpy rate function在 numpy 率函数中获取特定值集的负率
【发布时间】:2019-09-04 09:55:25
【问题描述】:

我是numpy 的新手,我使用numpy.rate() 计算固定利率月供贷款的 APR。

no_of_month = 24
payment = 8584
loan_amount = 50000
apr = rate(no_of_month,-payment,loan_amount,0.0) *12

apr 计算为-22.816,但实际值应为2.0102(在LibreOffice Calc 中计算)

对于no_of_month = 23apr2.00079,但对于no_of_month = 24apr-22.816

以下是我目前对为什么会发生这种情况的猜测(他们可能是错误的)

  • 因为持续时间是 12 的倍数(又是一整年)
  • numpy.rate 某些范围内的一些限制

我找不到任何相关资源。

根本原因是什么以及如何解决?

值的范围

no_of_month - 1 to 36
payment - 1000 to 1000000
loan_amount - 10000 to 10000000

所有组合都是可能的

【问题讨论】:

    标签: python numpy rate


    【解决方案1】:

    np.rate 返回用 x**24 求解多项式方程的利率。这有 24 个解决方案,其中一些可能是重复的,其中一些可能是复杂的。在这个特定数据的情况下:

    pv = 50000
    payment = 8584 
    mpr= np.rate(24, -payment, pv, 0.0)
    np.pv(mpr, 24, payment)
    # -49999.999999789325  This represents the 50000 pv 
    mpr
    # -1.901406995298687  #  mpr = -190.1% per month!
    
    mpr1 = np.rate(24, -payment, pv, 0.0, guess = .15)
    # guess lets you change the starting point for the search
    mpr1
    # 0.16750654293672343  # mar = 16.8% per month
    np.pv(mpr1, 24, payment)
    # -49999.99999999999
    
    def apr(mpr, periods = 12):
        """  apr is ( 1 + monthly_rate ) ** 12 - 1 """
        return (1+mpr)**periods-1
    
    apr(apr)
    # -0.7122263079633477  apr = -71.2%
    
    apr(mpr1)
    # 5.4137477809069345  apr of 541.4%
    

    即16.8% 和 -190.1% 都是方程的数学正确解。 -190.1% 在金融环境中没有多大意义。

    这两个时期可能更容易理解。

    loan_amount = 10
    payment = 6
    n_periods = 2
    
    Solve 10 - 6r -6r**2
    r = (6 +-sqrt(36-4*(-6)*10))/(2*-6)
    r = -1.8844373105 and 0.8844373105
    
    r = 1/(1+i) where i is the interest rate
    i = 1/r - 1
    
    r = -1.8844373105 
    1/r-1
    # -1.5306623862879625
    
    r1 = 0.8844373105
    1/r1-1
    # 0.13066238627435212   
    
    mpr = np.rate(2, -payment, loan_amount, 0.0)
    mpr
    # 0.13066238629183413
    
    mpr1 = np.rate(2, -payment, loan_amount, 0.0, guess = -1.5)
    mpr1
    # -1.5306623862918336
    

    在这种情况下,numpy 将求解两个速率,它们来自二次方程的两个根。

    这可能无济于事,但确实解释了为什么np.rate(和np.irr)可以解决意外的答案。

    编辑:

    我意识到如果 r = 1/(1 + interest_rate) r 将只有一个真正的正解。这是通常最具商业意义的解决方案。

    import numpy as np
    """
        To simplify the analysis let 
            r = 1 / ( 1 + interest_rate )
            p = periodic payments
            n = number of periods
        then:
            pv = loan - p*r - p*r**2 - ... -p*r**n
            dpv/dr =  -p -2*p*r - ... -p*n*r**(n-1)
            If r > 0 and p > 0 dpv/dr is negative
            Therefore there is at most one solution to pv == 0 for r > 0.
            pv == loan when r == 0
            For large r -p*r**n will dominate and pv will be negative.
            Therefore there will be one positive real solution to pv == 0
    """
    
    def polynomial_from(nper, loan, pay): 
        """ Create numpy array to represent the polynomial """
        return np.array([-pay]*nper+[loan])
    
    # np.roots returns one root per nper.  Filter to real roots only.
    def real_roots(poly):
        roots_ = np.roots(poly)
        return roots_[np.isclose(roots_.imag, 0)].real
        # return the real part of the roots- with a zero imaginary part
    
    def feasible_rate(nper, loan, pay):
        poly = polynomial_from(nper, loan, pay)
        reals = real_roots(poly)
        r = reals[reals>0][0]   # r is the real root > 0
        return 1/r - 1
    
    def apr(int_rate, nperiods = 12):
        return ( 1 + int_rate ) ** nperiods - 1
    
    mpr = feasible_rate( 24, 50000, 8584 )
    print( 'Monthly rate: {:%}, Annual Rate: {:%}'.format(mpr, apr(mpr)) )
    # Monthly rate: 16.750654%, Annual Rate: 541.374778%
    

    【讨论】:

    • 很好的答案,但你能解释一下提问者如何达到预期的结果吗?
    • @Trilarion 没有数学逻辑可以决定哪个是“正确”答案。通过选择不同的猜测值,有时会产生不同的答案。如果有人想使用 numpy(而不是电子表格),他们可能不希望检查每个结果的可行性。 np.roots 可能有助于确定所有解决方案。一个单独的算法可以在可行的范围内从真实的解决方案(而不是复杂的解决方案)中进行选择。商业客户必须建议什么适合他们。
    • @Trilarion 我已经确定了一个最“商业意识”的解决方案。请参阅我的答案末尾的编辑。
    • @TlsChris 使用了guess 参数以及大多数可能的组合,并选择了适用于所有人的值。
    猜你喜欢
    • 1970-01-01
    • 2020-08-07
    • 1970-01-01
    • 1970-01-01
    • 2021-05-16
    • 2023-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多