{"id":29,"date":"2013-07-18T12:30:00","date_gmt":"2013-07-18T12:30:00","guid":{"rendered":"https:\/\/www.root42.de\/blog\/?p=29"},"modified":"2013-07-18T12:30:00","modified_gmt":"2013-07-18T12:30:00","slug":"how-to-use-scipy-least-squares-to-minimize-multiple-functions-at-once","status":"publish","type":"post","link":"https:\/\/www.root42.de\/blog\/?p=29","title":{"rendered":"How to use SciPy Least Squares to minimize multiple functions at once"},"content":{"rendered":"<p>SciPy <a href=\"http:\/\/docs.scipy.org\/doc\/scipy\/reference\/generated\/scipy.optimize.leastsq.html\">comes with a least squares<\/a> <a href=\"http:\/\/en.wikipedia.org\/wiki\/Levenberg%E2%80%93Marquardt_algorithm\">Levenberg-Marquardt<\/a> implementation. This allows you to minimize functions. By defining your function as the difference between some measurements and your model function, you can fit a model to those measurements. <\/p>\n<p>Sometimes your model contains multiple functions. You can also minimize for all functions using this approach: <\/p>\n<ul>\n<li>Define your functions that you like to minimize A(p0), B(P1), &#8230;<br \/>    their cumulative paramaters will be a tuple (p0, p1, &#8230;).<\/li>\n<li>Define your function to be minimized as f(x0), where x0 is expanded to the parameter tuple.<\/li>\n<li>The function f returns a vector of differences between discrete measured sample and the individual functions A, B etc.<\/li>\n<li>Let SciPy minimize this function, starting with a reasonably selected initial parameter vector.<\/li>\n<\/ul>\n<p>This is an example implementation: <\/p>\n<pre><br \/>import math<br \/>import scipy.optimize<br \/><br \/>measured = { <br \/>    1: [ 0, 0.02735, 0.47265 ],<br \/>    6: [ 0.0041, 0.09335, 0.40255 ],<br \/>    10: [ 0.0133, 0.14555, 0.34115 ],<br \/>    20: [ 0.0361, 0.205, 0.2589 ],<br \/>    30: [ 0.06345, 0.23425, 0.20225 ],<br \/>    60: [ 0.132, 0.25395, 0.114 ],<br \/>    90: [ 0.2046, 0.23445, 0.06095 ],<br \/>    120: [ 0.2429, 0.20815, 0.04895 ],<br \/>    180: [ 0.31755, 0.1618, 0.02065 ],<br \/>    240: [ 0.3648, 0.121, 0.0142 ],<br \/>    315: [ 0.3992, 0.0989, 0.00195 ]<br \/>}<br \/><br \/>def A( x, a, k ):<br \/>    return a * math.exp( -x * k )<br \/><br \/>def B( x, a, k, l ):<br \/>    return k * a \/ ( l - k ) * ( math.exp( -k * x ) - math.exp( -l * x ) )<br \/><br \/>def C( x, a, k, l ):<br \/>    return a * ( 1 - l \/ ( l - k ) * math.exp( -x * k ) + k \/ ( l - k ) * math.exp( -x * l ) )<br \/><br \/>def f( x0 ):<br \/>    a, k, l = x0<br \/>    error = []<br \/>    for x in measured:<br \/>        error += [ C( x, a, k, l ) - measured[ x ][ 0 ],<br \/>                   B( x, a, k, l ) - measured[ x ][ 1 ],<br \/>                   A( x, a, k ) - measured[ x ][ 2 ]<br \/>               ]<br \/>    return error<br \/><br \/>def main():<br \/>    x0 = ( 0.46, 0.01, 0.001 ) # initial parameters for a, k and l<br \/>    x, cov, infodict, mesg, ier = scipy.optimize.leastsq( f, x0, full_output = True, epsfcn = 1.0e-2 )<br \/>    print x<br \/><br \/>if __name__ == \"__main__\":<br \/>    main()<br \/><\/pre>\n<p> SciPy returns a lot more information, not only the final parameters. See their documentation for details. You also may want to tweak epsfcn for a better fit. This depends on your functions shape and properties.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SciPy comes with a least squares Levenberg-Marquardt implementation. This allows you to minimize functions. By defining your function as the difference between some measurements and your model function, you can fit a model to those measurements. Sometimes your model contains multiple functions. You can also minimize for all functions using this approach: Define your functions &hellip; <a href=\"https:\/\/www.root42.de\/blog\/?p=29\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to use SciPy Least Squares to minimize multiple functions at once&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7,26,15],"tags":[],"_links":{"self":[{"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/29"}],"collection":[{"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=29"}],"version-history":[{"count":0,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/29\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=29"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=29"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}