[英]Understanding output from recursive function
我試圖運行功能obtainingparams
遞歸5倍。 但是,目前我程序的輸出如下,我實際上無法理解為什么在32323232
MATRIX
, PARAMS
, VALUES
輸出之后,代碼末尾的while
循環中的32323232
行沒有打印出來。
MATRIX [[ 1. 7.53869055 7.10409234 -0.2867544 ]
[ 1. 7.53869055 7.10409234 -0.2867544 ]
[ 1. 7.53869055 7.10409234 -0.2867544 ]
...,
[ 1. 0.43010753 0.43010753 0.09642396]]
PARAMS [ 5.12077446 8.89859946 -10.26880411 -9.58965259]
VALUES [(0.5, 1.5, 206.59958540866882, array([ 5.12077446, 8.89859946, -10.26880411, -9.58965259]))]
MATRIX [[ 1. 3.14775472 2.54122406 -0.43709966]
[ 1. 3.14775472 2.54122406 -0.43709966]
[ 1. 3.14775472 2.54122406 -0.43709966]
...,
[ 1. 0.25806447 0.25806428 0.07982733]]
PARAMS [ 4.90731466 4.41623398 -7.65250737 -6.01128351]
VALUES [(0.5, 1.5, 206.59958540866882, array([ 5.12077446, 8.89859946, -10.26880411, -9.58965259])), (0.7, 1.7, 206.46228694927203, array([ 4.90731466, 4.41623398, -7.65250737, -6.01128351]))]
等等。 df
是一個Dataframe。
values = []
def counted(fn):
def wrapper(*args, **kwargs):
wrapper.called+= 1
return fn(*args, **kwargs)
wrapper.called= 0
wrapper.__name__= fn.__name__
return wrapper
@counted
def obtainingparams(self, df, tau_1, tau_2, residuals):
global values
no_of_bonds = df.shape[0]
yields = df['coupon'].values
matrix_of_params = np.empty(shape=[1, 4])
months_to_maturity_matrix = df.months_to_maturity.values
count = 0
for x, value in np.ndenumerate(months_to_maturity_matrix):
if count < months_to_maturity_matrix.shape[0]:
months_to_maturity_array = months_to_maturity_matrix[count]
years_to_maturity_array = months_to_maturity_array/12
newrow = [1, ((1-np.exp(-years_to_maturity_array/tau_1))/years_to_maturity_array/tau_1), ((1-np.exp(-years_to_maturity_array/tau_1))/years_to_maturity_array/tau_1)-np.exp(-years_to_maturity_array/tau_1), ((1-np.exp(-years_to_maturity_array/tau_2))/years_to_maturity_array/tau_2)-np.exp(-years_to_maturity_array/tau_2)]
count = count + 1
matrix_of_params = np.vstack([matrix_of_params, newrow])
matrix_of_params = np.delete(matrix_of_params, (0), axis=0)
print('MATRIX', matrix_of_params)
params = np.linalg.lstsq(matrix_of_params,yields)[0]
print('PARAMS', params)
residuals = np.sqrt(((yields - matrix_of_params.dot(params))**2).sum())
tau_1 = tau_1 + 0.2
tau_2 = tau_2 + 0.2
values.append((tau_1, tau_2, residuals, params))
print('VALUES', values)
while self.obtainingparams(df, tau_1, tau_2, residuals).called < 5:
print('32323232')
self.obtainingparams(df, tau_1, tau_2, residuals)
編輯 :調用obtainingparams
這是內部的功能BondClass
類:
tau_1 = 0.3
tau_2 = 1.3
BOND_OBJECT = BondClass.GeneralBondClass(price, coupon, coupon_frequecy, face_value, monthstomaturity, issue_date)
residuals = [0, 0, 0, 0, 0]
df1 = Exc.ExcelFileReader() #Read the Dataframe in from an Excel File
BOND_OBJECT.obtainingparams(df1, tau_1, tau_2, residuals)
問題是你永遠不會進入while循環,因為為了輸入它,你進行遞歸調用。 因此,在對called
的測試進行評估之前,您已經在遞歸。 此代碼不是您想要的:
while self.obtainingparams(df, tau_1, tau_2, residuals).called < 5:
它查找called
在函數調用的結果,而不是函數本身。 只需將其替換為:
while self.obtainingparams.called < 5:
而你應該就在那里。
while self.obtainingparams(df, tau_1, tau_2, residuals).called < 5:
print('32323232')
self.obtainingparams(df, tau_1, tau_2, residuals)
我的第一直覺是, self.obtainingparams().called
具有調用self.obtainingparams()
來獲得.called
財產。 通過這種方式,您以遞歸方式調用函數,但在調用函數之前無法傳入while循環,從而解釋了輸出的不足。
我建議不要使用包含的變量來計算遞歸實例,而是在封閉范圍內使用一個可以在每次調用時遞增的變量,並使基本情況檢查此變量並在達到所需的遞歸步數后return
。
例:
count = 0
def recurse():
count += 1
# Base case
if count >= 5:
return
else:
recurse()
最后,您需要再看看這行代碼實際上做了什么:
self.obtainingparams(df, tau_1, tau_2, residuals).called
你的函數obtainingparams
實際上並沒有實際返回一個值,而是說它返回了一個int
。 這行真的會檢查int.called
,但是int
沒有名為called
的屬性。 如果你想檢查函數對象的屬性,你可能想檢查self.obtainingparams.called
,雖然在我看來有更好的方法來做你正在嘗試用這個代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.