0

ログユーティリティと同時依存イベントに基づいて最適な賭け金を計算するプログラムを作成しようとしています。

これを行うために、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はわからないので、このエラーの原因はわかりません。

私のコードは以下の通りです。

付録

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
4

1 に答える 1

1

ここであなたは:

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

タイプを見ると、s[i]is a []f[i]is 0.104528w[i]is が見つかります[5,4]。次に、もう一度インデックスを作成しようとしますf[i]が、これは不可能であり、エラーが発生します。

于 2013-07-30T14:06:16.913 に答える