user135520
user135520

Reputation: 139

Positional argument missing error in scipy optimize

I wanted to get a maximum estimate for the following function in mu and sigma using scipy where s is defined as the sample

s = np.random.normal(mu, sigma, 100)

def f(mu, sigma):
  x = norm.pdf(s,mu,sigma)
  x = np.prod(x)
  x = np.log(x)
  return x

I've tried

x0 = [0,1]


bounds = [(-5,5),(-5,5)]

print(optimize.minimize(f, x0, method='SLSQP', bounds=bounds))

but keep getting the error:

f() missing 1 required positional argument: 'sigma'

I'm not sure what's going on here

Upvotes: 1

Views: 255

Answers (1)

Learning is a mess
Learning is a mess

Reputation: 8277

You need to pass the arguments to f as a vector:

def f(z):
  mu, sigma = z
  x = norm.pdf(s,mu,sigma)
  x = np.prod(x)
  x = np.log(x)
  return x

Upvotes: 3

Related Questions