rwolst
rwolst

Reputation: 13662

Unknown numpy.optimize.fmin error

I am trying to write a program that calculates the optimum amount to bet based on log utility and simultaneous dependent events.

In order to do this I am trying to use the numpy.optimize.fmin function. The function anon that I am passing to it works and produces (hopefully) correct output but when numpy tries to optimise the function I get the following error

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

Since I have no idea about fmin, I have no idea what is causing this error.

My code is below, hopefully not tl;dr but I wouldn't blame you.

APPENDIX

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

Upvotes: 0

Views: 148

Answers (1)

Eiyrioü von Kauyf
Eiyrioü von Kauyf

Reputation: 4725

Here where you do:

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

if you look at the types, you'll find s[i] is a [], f[i] is 0.104528 and w[i] is [5,4]. You then try to index f[i] a second time - which is not possible and causes the error.

Upvotes: 1

Related Questions