Sam Gregson - 8 days ago 6

Python Question

I am currently using the scipy.optimise.minimise() method to minimise a -log likelihood expression:

`param_array = np.array([`

0.5, # beta1

0.5 # beta2

])

def f(param_array):

great_bayesian = -sum( np.log(binomial.pmf(t_g_c, t_g_t, (param_array[0] + historical_t_g_c)/(param_array[0] + param_array[1] + historical_t_g_t) ) ) )

return great_bayesian

beta_opt = optimize.minimize(f,param_array,method='SLSQP', constraints=cons)

The method works great when I want to minimise a single likelihood over a single dataframe and correctly determines best estimates of beta1 and beta2.

However, I would like to fit multiple dataframes simultaneously and obtain one set of overall best estimates for beta1 and beta2.

I cannot see how to do this, but I'm sure it must be a solved problem. Any guidance would be greatly appreciated!

Answer

You can pass additional arguments to the function you want to minimize using the "args" argument.

Reference: https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.optimize.minimize.html

For your example you would have to redefine f(...) to take the arguments you want to change. Then change the call to scipy.minimize to provide the additional arguments you want to pass to f(...) as a tuple. The only restriction here is that f(...) needs to be defined so that the array of params you are minimizing over is the first argument.

Here are (untested) changes to your code. I added the prefix "some_" to your arguments to indicate that these can be changed here.

```
param_array = np.array([
0.5, # beta1
0.5 # beta2
])
def f(param_array, team_great_conv, team_great_tot, historical_team_great_conv, historical_team_great_tot):
great_bayesian = -sum( np.log(binomial.pmf(team_great_conv, team_great_tot, (param_array[0] + historical_team_great_conv)/(param_array[0] + param_array[1] + historical_team_great_tot) ) ) )
return great_bayesian
beta_opt = optimize.minimize(f, param_array,
args=(some_team_great_conv,
some_team_great_tot,
some_historical_team_great_conv,
some_historical_team_great_tot,),
method='SLSQP', constraints=cons)
```

Source (Stackoverflow)

Comments