我目前正在使用 scipy.optimise.minimise() 方法来最小化 -log 似然表达式:
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)
当我想要最小化单个数据帧上的单个可能性并正确确定 beta1 和 beta2 的最佳估计时,该方法非常有效。
但是,我想同时拟合多个数据帧并获得一组 beta1 和 beta2 的总体最佳估计。
我不知道如何做到这一点,但我确信这一定是一个已解决的问题。任何指导将不胜感激!
最佳答案
您可以使用“args”参数将其他参数传递给要最小化的函数。
引用:https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.optimize.minimize.html
对于您的示例,您必须重新定义 f(...) 才能获取您想要更改的参数。然后更改对 scipy.minimize 的调用以提供您想要作为元组传递给 f(...) 的附加参数。这里唯一的限制是需要定义 f(...) ,以便您要最小化的参数数组是第一个参数。
以下是对您的代码的(未经测试的)更改。我在您的参数中添加了前缀“some_”,以表明可以在此处更改这些参数。
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)
关于python - 使用 scipy.optimise.minimise() 执行同时拟合/最小化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40769244/