2013-07-29 68 views
0

我正在嘗試編寫一個程序,根據日誌實用程序和同時發生的相關事件來計算最佳下注量。未知numpy.optimize.fmin錯誤

爲了做到這一點,我試圖使用numpy.optimize.fmin函數。該功能anon,我傳遞給它的工作原理和生產(希望)正確的輸出,但是當numpy試圖優化我收到以下錯誤功能

s[i].append(f[i][0]*w[i][0] + f[i][1]*w[i][1]) 
IndexError: invalid index to scalar variable. 

因爲我不知道有關fmin,我不知道是什麼導致這個錯誤。

我的代碼在下面,希望不是tl;博士,但我不會責怪你。

附錄

def main(): 
    p = [[0.1,0.1,0.2, 0.2,0.1,0, 0.1,0.1,0.1]] 
    w = [[5,4]] 
    MaxLU(p,w,True) 

def MaxLU(p, w, Push = False, maxIter = 10): 
    #Maximises LU, using Scipy in built function 
    if Push == True: 
     anon = lambda f: -PushLogUtility(p, w, f) 
    else: 
     anon = lambda f: -LogUtility(p, w, f) 
    #We use multiple random starts 
    f = [] 
    LU = [] 
    for i in range(0,maxIter): 
     start = np.random.rand(len(p)) 
     start = start/5 * np.sum(start) 
     f.append(optimize.fmin(anon, start)) #Error occurs in here! 
     if Push == True: 
      LU.append(PushLogUtility(p, w, f[-1])) 
     else: 
      LU.append(LogUtility(p, w, f[-1])) 

    #Now find the index of the max LU and return that same index of f 
    return f[LU.index(np.max(LU))] 

def PushLogUtility(p,w,f): 
    #Outputs log utility incoroporating pushes and dependent totals, money data 
    #p : 9xk length vector of joint probabilities for each of the k games, p = [[p_(W_T W_M), p_(W_T P_M), p_(W_T L_M), p_(P_T W_M) ... ]] 
    #w : 2xk matrix of odds where w = [[total odds, money odds] ... ] 
    #f : 2xk matrix of bankroll percentages to bet, f = [[f_T, f_M] ... ] 
    utility = 0 
    k = len(p) 
    s = k*[[]] 
    for i in range(0,k): 
     s[i].append(f[i][0]*w[i][0] + f[i][1]*w[i][1]) 
     s[i].append(f[i][0]*w[i][0]) 
     s[i].append(f[i][0]*w[i][0] - f[i][1]) 
     s[i].append(f[i][1]*w[i][1]) 
     s[i].append(0) 
     s[i].append(-f[i][1]) 
     s[i].append(-f[i][0] - f[i][1]) 
     s[i].append(-f[i][0] - f[i][1]) 
     s[i].append(-f[i][0] - f[i][1]) 

    for i in range(0,9 ** k): 
     l = de2ni(i) #Converts number to base 9 
     if i == 0: 
      l += int(math.ceil(k - 1 - math.log(i + 1,9))) * [0] 
     else: 
      l += int(math.ceil(k - 1 - math.log(i,9))) * [0] 
     productTerm = np.prod([p[i][l[i]] for i in range(0,k)]) 
     sumTerm = np.sum([s[i][l[i]] for i in range(0,k)]) 
     utility = utility + productTerm * np.log(1 + sumTerm) 
    return utility 

回答

1

在這兒,你做的事:

s[i].append(f[i][0]*w[i][0] + f[i][1]*w[i][1]) 

,如果你看的類型,你會發現s[i][]f[i]0.104528w[i][5,4]。然後您嘗試再次索引f[i] - 這是不可能的並導致錯誤。

+0

感謝您的回覆,您是如何設法研究這些類型的?調試時我無法進入'scipy.optimize.fmin'。 – rwolst

+0

如果你在實際功能上使用調試器......或者在這種情況下,你甚至可以使用可怕的''print''調試 –

+0

好吧,我想我需要理解函數內部到底是怎麼回事的問題。我最初傳遞的'f'是一個2D列表,但是似乎優化在每次迭代時都返回一個1D列表,因此'f [i]'不能再次索引。 – rwolst