在Python中使用递归实现二分法

时间:2018-07-14 18:24:49

标签: python recursion bisection

在一个玩具示例中,我已经在Python中实现了带有递归的Bisection算法,在该示例中,我的目标是找到我每月应支付的每月最低分期付款,为期12个月,以便在12个月结束时我的余额为零或更少。

假设我在期初的余额为320000,年利率为0.2。正确答案是29157.09。

我的代码由于无法收敛而遇到异常。

代码如下(我已插入打印语句以方便调试)

balance = 320000
annualInterestRate = 0.2

Balance = balance

Annual_interest_rate = annualInterestRate



Monthly_interest_rate = (Annual_interest_rate) / 12.0
Monthly_payment_lower_bound = Balance / 12
Monthly_payment_upper_bound = (Balance * (1 + Monthly_interest_rate)) / 12.0

def bisect(Balance, Annual_interest_rate, a, b ):



    Previous_balance = Balance

    for i in range(12):



            Monthly_unpaid_balance = (Previous_balance) - a
            Updated_balance_each_month = (Monthly_unpaid_balance) + (Monthly_interest_rate * Monthly_unpaid_balance)
            Previous_balance =  Updated_balance_each_month

    f_a = Previous_balance

    for i in range(12):



            Monthly_unpaid_balance = (Previous_balance) - b
            Updated_balance_each_month = (Monthly_unpaid_balance) + (Monthly_interest_rate * Monthly_unpaid_balance)
            Previous_balance =  Updated_balance_each_month

    f_b = Previous_balance

    c = (a + b)/2


    for i in range(12):



            Monthly_unpaid_balance = (Previous_balance) - c
            Updated_balance_each_month = (Monthly_unpaid_balance) + (Monthly_interest_rate * Monthly_unpaid_balance)
            Previous_balance =  Updated_balance_each_month

    f_c = Previous_balance

    print('a is {0}, b is {1}, c = {2}, f_a = {3}, f_b = {4}, f_c = {5}'.format(a, b, c, f_a, f_b, f_c))

    if abs(f_c) <=0.01:
        return(c)

    elif f_c * f_a >0:

        a =c

        print('a is {0}, b is {1}, c = {2}, f_a = {3}, f_b = {4}, f_c = {5}'.format(a, b, c, f_a, f_b, f_c))

        return(bisect(Balance, Annual_interest_rate, a,  b ))

    else:

        b = c

        print('a is {0}, b is {1}, c = {2}, f_a = {3}, f_b = {4}, f_c = {5}'.format(a, b, c, f_a, f_b, f_c))

        return(bisect(Balance, Annual_interest_rate, a,  b ))

运行该函数时,我得到以下打印输出:

bisect(Balance, Annual_interest_rate, Monthly_payment_lower_bound, Monthly_payment_upper_bound )
a is 26666.666666666668, b is 27111.11111111111, c = 26888.88888888889, f_a = 33328.98239049623, f_b = -322183.0368628964, f_c = -752717.255677314
a is 26666.666666666668, b is 26888.88888888889, c = 26888.88888888889, f_a = 33328.98239049623, f_b = -322183.0368628964, f_c = -752717.255677314
a is 26666.666666666668, b is 26888.88888888889, c = 26777.77777777778, f_a = 33328.98239049623, f_b = -319209.06882307003, f_c = -747603.8415428438
a is 26666.666666666668, b is 26777.77777777778, c = 26777.77777777778, f_a = 33328.98239049623, f_b = -319209.06882307003, f_c = -747603.8415428438
a is 26666.666666666668, b is 26777.77777777778, c = 26722.222222222226, f_a = 33328.98239049623, f_b = -317722.08480315685, f_c = -745047.1344756087
a is 26666.666666666668, b is 26722.222222222226, c = 26722.222222222226, f_a = 33328.98239049623, f_b = -317722.08480315685, f_c = -745047.1344756087

您的建议将不胜感激。

1 个答案:

答案 0 :(得分:0)

以下内容使用前端功能,因此用户不必处理“魔术”参数,例如您的 from sklearn.model_selection import GridSearchCV from sklearn import svm params_svm = { 'kernel' : ['linear','rbf','poly'], 'C' : [0.1,0.5,1,10,100], 'gamma' : [0.001,0.01,0.1,1,10] } svm_clf = svm.SVC() estimator_svm = GridSearchCV(svm_clf,param_grid=params_svm,cv=4,verbose=1,scoring='accuracy') estimator_svm.fit(data,labels) print(estimator_svm.best_params_) estimator_svm.best_score_ /* data.shape is (891,9) labels.shape is (891) both are numeric 2-D and 1-D arrays. */ mymethod in dir(package) 。它将年利率重新调整为月利率,并通过将递归输入缩放100来将计算结果转换为整数,然后在返回之前重新缩放解决方案。

a