Note that there are some explanatory texts on larger screens.

plurals
  1. POCorrect usage of fmin_l_bfgs_b for fitting model parameters
    primarykey
    data
    text
    <p>I have a some experimental data (for y, x, t_exp, m_exp), and want to find the "optimal" model parameters (A, B, C, D, E) for this data using the <a href="http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.fmin_l_bfgs_b.html#scipy.optimize.fmin_l_bfgs_b" rel="nofollow">constrained multivariate BFGS method</a>. Parameter E must be greater than 0, the others are unconstrained.</p> <pre><code>def func(x, A, B, C, D, E, *args): return A * (x ** E) * numpy.cos(t_exp) * (1 - numpy.exp((-2 * B * x) / numpy.cos(t_exp))) + numpy.exp((-2 * B * x) / numpy.cos(t_exp)) * C + (D * m_exp) initial_values = numpy.array([-10, 2, -20, 0.3, 0.25]) mybounds = [(None,None), (None,None), (None,None), (None,None), (0, None)] x,f,d = scipy.optimize.fmin_l_bfgs_b(func, x0=initial_values, args=(m_exp, t_exp), bounds=mybounds) </code></pre> <p>A few questions:</p> <ol> <li>Should my model formulation <code>func</code> include my independent variable <code>x</code> or should it be provided from the experimental data <code>x_exp</code> as part of <code>*args</code>?</li> <li>When I run the above code, I get an error <code>func() takes at least 6 arguments (3 given)</code>, which I assume are x, and my two *args... How should I define <code>func</code>?</li> </ol> <p>EDIT: Thanks to @zephyr's answer, I now understand that the goal is to minimize the sum of squared residuals, not the actual function. I got to the following working code:</p> <pre><code>def func(params, *args): l_exp = args[0] s_exp = args[1] m_exp = args[2] t_exp = args[3] A, B, C, D, E = params s_model = A * (l_exp ** E) * numpy.cos(t_exp) * (1 - numpy.exp((-2 * B * l_exp) / numpy.cos(t_exp))) + numpy.exp((-2 * B * l_exp) / numpy.cos(theta_exp)) * C + (D * m_exp) residual = s_exp - s_model return numpy.sum(residual ** 2) initial_values = numpy.array([-10, 2, -20, 0.3, 0.25]) mybounds = [(None,None), (None,None), (None,None), (None,None), (0,None)] x, f, d = scipy.optimize.fmin_l_bfgs_b(func, x0=initial_values, args=(l_exp, s_exp, m_exp, t_exp), bounds=mybounds, approx_grad=True) </code></pre> <p>I am not sure that the bounds are working correctly. When I specify (0, None) for E, I get a run flag 2, abnormal termination. If I set it to (1e-6, None), it runs fine, but selects 1e-6 as E. Am I specifying the bounds correctly?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload