0
price = pd.read_csv('C:\\Users\\mypath\\price.csv', index_col= [0,1], usecols=[0,5,6])
yt = price.loc['AUS']
yt = yt.pct_change().dropna().values


def p(u, sigma, pi):
    
    d = pi / (2*np.pi*sigma)**0.5 * np.exp(-(yt-u)**2 / (2*sigma**2))
    
    return d

def Lf(u, sigma, pi):
    
    prob = p(u[0], sigma[0], pi[0]) + p(u[1], sigma[1], pi[1])
    L = np.sum(-np.log(prob))
    
    return L

pi_init = (0.5,0.5)
sigma_init = (0.1,0.1)
u_init = (0.1,0.1)

res = opt.minimize(Lf, (u_init, sigma_init, pi_init), method='L-BFGS-B')

If i run Lf() i get a real number but when i run minimize i get the following error message:

TypeError: Lf() missing 2 required positional arguments: 'sigma' and 'pi'

This message doesn't make sense to me...

matteo611
  • 21
  • 1
  • Please read the documentation for scipy.optimize.minimize; then use the correct set of arguments. Depending on your goal, you may even want to use a different function, i.e., scipy.optimize.minimize_scalar instead. – 9769953 Nov 28 '22 at 11:35
  • If you're optimizing against `yt`, you should pass that as an argument to your functions (and in your call to `opt.minimize`); don't use it as a global variable. – 9769953 Nov 28 '22 at 11:37

1 Answers1

0
x0 = np.array([...]) # suitably shaped numpy array of your init values
res = opt.minimize(Lf, x0, args=(u_init, sigma_init, pi_init), method='L-BFGS-B')

May be you can try calling like this

Shen
  • 136
  • 4
  • That will not work, since the `x0` argument is still missing. – 9769953 Nov 28 '22 at 11:36
  • yes matteo611, can put a numpy array of suitable shape there. I am just telling to add args because as per the documents it is a keyword argument and before that x0 is a positional argument, so basically he is passing the arguments for fucntion as x0 currently – Shen Nov 28 '22 at 11:47
  • Yes, but your example also lacks an `x0`, and will just fail. Without an explanation what the actual problem is (missing argument, hence the args will be used as such), or clarification from the OP, your solution is incorrect. – 9769953 Nov 28 '22 at 11:49
  • @9769953 I have edited my answer. Thanks But I also expect users to check documentation – Shen Nov 28 '22 at 11:57
  • Well, yes, rtfm is usually the original problem, and 90% of the time, that is the actual answer to these kind of questions. – 9769953 Nov 28 '22 at 12:02
  • thank you, Shen you are right. I just needed to rewrite Lf as a function of a single vector. – matteo611 Nov 28 '22 at 18:25