1

So i am doing this assignment, where i am supposed to minimize the chi squared function. I saw someone doing this on the internet so i just copied it:

Multiple variables in SciPy's optimize.minimize

I made a chi-squared function which is a function in 3 variables (x,y,sigma) where sigma is random gaussian fluctuation random.gauss(0,sigma). I did not print that code here because on first sight it might be confusing (I used a lot of recursion). But i can assure you that this function is correct.

now this code just makes a list of the calculated minimization(Which are different every time because of the random gaussian fluctuation). But here comes the main problem. If i did my calculation correctly, we should get a list with a mean of 2 (since i have 2 degrees of freedom as you can see in this link: https://en.wikipedia.org/wiki/Chi-squared_test).

def Chi2(pos):
    return Chi(pos[0],pos[1],1) 

x_list= []
y_list= []  
chi_list = []
    for i in range(1000):
        result = scipy.optimize.minimize(Chi2,[5,5]).x
        x_list.append(result[0])
        y_list.append(result[1])
        chi_list.append(Chi2(result))

But when i use this code i get a list of mean 4, however if i add the method "Powell" i get a mean of 9!!

So my main question is, how is it possible these means are so different and how do i know which method to use to get the best optimization?

Because i think the error might be in my chisquare function i will show this one as well. The story behind this assignment is that we need to find the position of a mobile device and we have routers on the positions (0,0),(20,0),(0,20) and (20,20). We used a lot of recursion, and the graph of the chi_squared looked fine(it has a minimum on (5,5)

def perfectsignal(x_m,y_m,x_r,y_r):
    return 20*np.log10(c / (4 * np.pi * f)) - 10 * np.log((x_m-x_r)**2 + (y_m-y_r)**2 + 2**2)

def signal(x_m,y_m,x_r,y_r,sigma):
    return perfectsignal(x_m,y_m,x_r,y_r) + random.gauss(0,sigma)

def res(x_m,y_m,x_r,y_r,sigma,sigma2):
    x = (signal(x_m,y_m,x_r,y_r,sigma) -   perfectsignal(x_m,y_m,x_r,y_r))/float(sigma2);
    return x

def Chi(x,y,sigma):
    return(res(x,y,0,0,sigma,1)**2+res(x,y,20,0,sigma,1)**2+res(x,y,0,20,sigma,1)**2+res(x,y,20,20,sigma,1)**2)

Kees

Community
  • 1
  • 1
Kees Til
  • 171
  • 1
  • 17
  • 2
    Could you call optimize with an additional keyword argument : options={'disp': True} ? This turns on debug messaging of the routine. You will be able to see whether there are any hidden complaints. – audio Sep 14 '16 at 09:36
  • Warning: Desired error not necessarily achieved due to precision loss. Current function value: 6.883043 Iterations: 0 Function evaluations: 56 Gradient evaluations: 11 this is what i get – Kees Til Sep 14 '16 at 11:08
  • See : http://scicomp.stackexchange.com/questions/2004/scipy-optimize-fmin-bfgs-desired-error-not-necessarily-achieved-due-to-precisi – audio Sep 14 '16 at 11:39
  • 2
    It would be wrong to compare the results of 4 and 9, when both did not converge (like you indicated for at least one). Keep in mind, that your problem is probably non-convex and all these solvers only can give some locally-optimal solution (no information about global-solution). They are also very dependent on starting-points. I think you should show your ```Chi``` function! There are numerical errors and i think this is very probable if you are optimizing a function with some ```exp``` in it while not bounding the variables. Even more constrained optimizers might struggle there. – sascha Sep 14 '16 at 11:40
  • i will show my Chi_squared code :D – Kees Til Sep 14 '16 at 12:41
  • You should try to step through your computation using a debugger and double check your equations. I have no idea what your environment is, but there are options both for Windows and Linux. – audio Sep 14 '16 at 19:50

0 Answers0