%autosave 10
Autosaving every 10 seconds
scipy.optimize
: mathematical optimisation, reducing functions, etc.f(x)
. Or multiple variables.scipy.optimize
, statsmodels
.If data is random, using np.argmin(f)
(brute force) is the best you can do because there is no structure.
Completely linear, or quadratic, functions are easy to solve without numeric methods, let alone brute force, because the structure allows us to do it analytically.
Tend to be semi-structured, and often can't be expressed symbolically. Maybe there isn't even a symbolic representation.
f = lambda x: np.exp((x-4)**2)
return scipy.optimize.minimize(f, 5)
But this is extremely customisable, lots of methods. Each with strengths and weaknesses. Default is BFGS. Just change a 'method' keyword in scipy.optimize.minimize
.
How to choose solver?
Characteristics of each method
Check error messages! Might say it isn't sure about solution.
from pulp import *
x = LpVariable('x', -10, 10)
y = LpVariable('y', -10, 10)
prob = LpProblem("Toy problem", LpMinimize)
prob += 3*x - y
prob.solve()
1
prob
Toy problem: MINIMIZE 3*x + -1*y + 0 VARIABLES -10 <= x <= 10 Continuous -10 <= y <= 10 Continuous
(x.value(), y.value())
(-10.0, 10.0)
numdifftools
: numerical approximation. scipy.optimize has some methods.import sympy as S
from math import *
S.var("x mu sigma", real=True)
(x, mu, sigma)
f = 1/(sqrt(2*S.pi)*sigma)*S.exp(-(x-mu)**2/(2*sigma**2))
f
0.398942280401433*exp(-(-mu + x)**2/(2*sigma**2))/sigma
ll = log(f)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-9-569f5e1c5974> in <module>() ----> 1 ll = log(f) /Users/ai/Programming/.envs/default/lib/python2.7/site-packages/sympy/core/expr.pyc in __float__(self) 205 if result.is_number and result.as_real_imag()[1]: 206 raise TypeError("can't convert complex to float") --> 207 raise TypeError("can't convert expression to float") 208 209 def __complex__(self): TypeError: can't convert expression to float
cvxpy
and ask it if it's convex