0

I have a problem with minimizing a likelihood function(LLF). I wrote everything and my code works without any error. but my problem is the scipy. minimize only change 1 parameter and the other ones remain the same as the initial inputs. (i changed the method but it didn't work)

this is my LLf:

1

and these are the LLf parameters:

2

3

i wrote each parameter in a seperate function and LLF is the objective function. here you can see my code:

def Rt(r1):
    return (np.ones([n, n])) * r1 + (np.eye(n)) * (1 - r1)

def absoluteRt(r1):
    return ((1 - r1) ** (n - 1)) * (1 + (n - 1) * r1)

def inverseRt(r1):
    return (np.ones([n, n]) * (1 / (1 - r1))) - ((np.eye(n)) * (r1 / ((1 - r1) * (1 + (n - 1) * r1))))

def Dt(t1):
    return np.diag(np.asarray(ht.iloc[:, t1]))

def ytilde(t1):
    return np.dot((Dt(t1)), (np.subtract(np.asarray(returns.iloc[:-1, t1]), (np.subtract(np.asarray(m), (
        np.multiply(np.asarray(phi.iloc[:, 0]), np.asarray(returns.iloc[:-1, t1 - 1]))))))).T)

def constraint1(r1):
    return np.abs(r1) - epsilon

def objective(r1):
    for i in range(t):
        z = np.log(absoluteRt(r1[i])) + np.dot(ytilde(i).T, (np.dot(inverseRt(r1[i]), ytilde(i))))
    return (1) / 2 * z


w = [0.1 , 0.5, 0.5, 0.5]

con1 = {'type': 'ineq', 'fun': constraint1}
cons = ([con1])
b = (((-1)/131)+epsilon , 1-epsilon)
bnds=tuple( b  for e in range(t))

solution= minimize(objective,w,method='SLSQP', constraints=cons , bounds=bnds)

and the result is:

 fun: 23.564446928934615
     jac: array([ 0.        ,  0.        ,  0.        , 17.42714119])
 message: 'Optimization terminated successfully.'
    nfev: 12
     nit: 2
    njev: 2
  status: 0
 success: True
       x: array([ 1.00000000e-01,  5.00000000e-01,  5.00000000e-01, -2.44142484e-11])

where is my mistake? what am I doing wrong?

NimDang
  • 11
  • 4
  • In `objective` the return value only depends on the last input value, because of how you iterate. – hpaulj Jun 25 '21 at 07:53
  • tnx. how can I fix it? – NimDang Jun 25 '21 at 08:16
  • Please [don’t post images of code or error messages.](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question/285557#285557) – tripleee Jun 25 '21 at 08:33

0 Answers0