0

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for the response, how did you manage to look into the types? I wasn't able to step into the scipy.optimize.fmin when debuggging.
if you use the debugger on the actual function ... or in this case you could even use the dreaded print debugging
Ok, I think I need to understand what is going on inside the function to get to the root of the problem. The f that I initially pass is a 2D list however it seems the optimisation is returning a 1D list at each iteration, hence why f[i] is not indexable a second time.
you might want to submit another question? also my post on how to use leastsq might be relevant since it has similar parameters: stackoverflow.com/questions/17934198/…
Right, will do that now.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.