Created
January 18, 2021 22:06
-
-
Save ColdTeapot273K/60976d8ba18e5f157862e48ffa68d0c3 to your computer and use it in GitHub Desktop.
Scipy optimizer doesn't properly converge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""constrained LLSQ""" | |
"""scipy.minimize""" | |
import numpy as np | |
from scipy import optimize | |
from scipy import linalg | |
# Generate random problem data. | |
m = 30 | |
n = 20 | |
np.random.seed(1) | |
X = np.random.randn(m, n) | |
y = np.random.randn(m) | |
def obj(theta, X, y): | |
return linalg.norm(a=(np.dot(X, theta0) - y), ord=2) | |
args = (X, y) | |
bounds = [(0.0, 1.0)] * n | |
theta0 = np.full((n), 0.9) # try 0.1, 0.2, 0.5, 0.7, etc. | |
method = 'SLSQP' | |
tol = 1e-8 | |
opt = optimize.minimize(obj, theta0, method=method, args=args, bounds=bounds, tol=tol) | |
opt | |
""" | |
fun: 21.6995803953911 | |
jac: array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., | |
0., 0., 0.]) | |
message: 'Optimization terminated successfully' | |
nfev: 21 | |
nit: 1 | |
njev: 1 | |
status: 0 | |
success: True | |
x: array([0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, | |
0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9]) | |
""" | |
"""cvxpy""" | |
import cvxpy as cp | |
import numpy as np | |
# Generate random problem data. | |
m = 30 | |
n = 20 | |
np.random.seed(1) | |
X = np.random.randn(m, n) | |
y = np.random.randn(m) | |
# Construct the problem. | |
theta = cp.Variable(n) | |
objective = cp.Minimize(cp.sum_squares(X*theta - y)) | |
constraints = [0 <= theta, theta <= 1] | |
prob = cp.Problem(objective, constraints) | |
# The optimal objective value is returned by `prob.solve()`. | |
result = prob.solve() | |
# The optimal value for theta is stored in `theta.value`. | |
print(result) | |
print(theta.value) | |
""" | |
19.83126370644502 | |
[-1.79109255e-19 2.85112420e-02 2.79973443e-19 3.37658729e-20 | |
-2.72802663e-19 1.49285011e-01 -9.94082533e-20 8.35373900e-20 | |
2.46718649e-01 5.78224144e-01 -4.03739463e-19 1.01242860e-03 | |
-9.28486180e-20 2.26767464e-01 -1.58813678e-19 -8.97232272e-20 | |
-1.22145729e-19 -1.51509428e-19 1.12060672e-19 -3.48318635e-19] | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment