bs_opt module¶
Interface to scipy.optimize
:
ScalarFunctionAndGradient
,ProximalFunction
type aliases- an
OptimizeParams
class check_gradient_scalar_function
checks whether an analytical gradient is correctacc_grad_descent
: accelerated gradient descent for convex, possibly non-smooth functionsminimize_some_fixed
: minimizes a function with some parameter values possibly fixed and some possibly within bounds, using L-BFGS-Bminimize_free
: minimizes a function with some parameter values possibly within boundsdfp_update, bfgs_update
: compute updates to the inverese Hessianarmijo_alpha, barzilai_borwein_alpha
: two ways of computing the step lengthprint_optimization_results
,print_constrained_optimization_results
format the results.
ProximalFunction = Callable[[np.ndarray, float, Iterable], np.ndarray]
module-attribute
¶
Type of h(x, t, pars)
that returns a scalar value.
ScalarFunctionAndGradient = Callable[[np.ndarray, Iterable, Optional[bool]], Union[float, tuple[float, np.ndarray]]]
module-attribute
¶
Type of f(v, args, gr)
that returns a scalar value and also a gradient if gr
is True
.
OptimizeParams
dataclass
¶
used for optimization; combines values, bounds and initial values for a parameter vector
Source code in bs_python_utils/bs_opt.py
37 38 39 40 41 42 43 44 |
|
acc_grad_descent(grad_f, x_init, other_params, prox_h=None, print_result=False, verbose=False, tol=1e-09, alpha=1.01, beta=0.5, maxiter=10000)
¶
Minimizes (f+h)
by Accelerated Gradient Descent where f
is smooth and convex and h
is convex.
By default h
is zero.
The convergence criterion is that the largest component of the absolute value of the gradient must be smaller than tol
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
grad_f |
Callable
|
grad_f of |
required |
x_init |
ndarray
|
initial guess, shape |
required |
prox_h |
ProximalFunction | None
|
proximal projector of |
None
|
other_params |
Iterable
|
an iterable with additional parameters |
required |
verbose |
bool
|
if |
False
|
tol |
float
|
convergence criterion on grad_f |
1e-09
|
alpha |
float
|
ceiling on step multiplier |
1.01
|
beta |
float
|
floor on step multiplier |
0.5
|
maxiter |
int
|
max number of iterations |
10000
|
Returns:
Type | Description |
---|---|
tuple[ndarray, int]
|
the candidate solution, and a convergence code (0 if successful, 1 if not). |
Source code in bs_python_utils/bs_opt.py
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
|
armijo_alpha(f, x, d, args, alpha_init=1.0, beta=0.5, max_iter=100, tol=0.0)
¶
Given a function f
we are minimizing, computes the step size alpha
to take in the direction d
using the Armijo rule.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
f |
Callable
|
the function |
required |
x |
ndarray
|
the current point |
required |
d |
ndarray
|
the direction we are taking |
required |
args |
Iterable
|
other arguments passed to |
required |
alpha_init |
float
|
the initial step size |
1.0
|
beta |
float
|
the step size reduction factor |
0.5
|
max_iter |
int
|
the maximum number of iterations |
100
|
tol |
float
|
a tolerance |
0.0
|
Returns:
Type | Description |
---|---|
float
|
the step size |
Source code in bs_python_utils/bs_opt.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
|
barzilai_borwein_alpha(grad_f, x, args)
¶
Given a function f
we are minimizing, computes the step size alpha
to take in the opposite direction of the gradient using the Barzilai-Borwein rule.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
grad_f |
Callable
|
the gradient of the function |
required |
x |
ndarray
|
the current point |
required |
args |
Iterable
|
other arguments passed to |
required |
Returns:
Type | Description |
---|---|
tuple[float, ndarray]
|
the step size |
Source code in bs_python_utils/bs_opt.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
|
bfgs_update(hess_inv, gradient_diff, x_diff)
¶
Runs a BFGS update for the inverse Hessian.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hess_inv |
ndarray
|
the current inverse Hessian |
required |
gradient_diff |
ndarray
|
the update in the gradient |
required |
x_diff |
ndarray
|
the update in x |
required |
Returns:
Type | Description |
---|---|
ndarray
|
the updated inverse Hessian. |
Source code in bs_python_utils/bs_opt.py
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 |
|
check_gradient_scalar_function(fg, p, args, mode='central', EPS=1e-06)
¶
Checks the gradient of a scalar function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fg |
ScalarFunctionAndGradient
|
should return the scalar value, and the gradient if its |
required |
p |
ndarray
|
where we are checking the gradient |
required |
args |
Iterable
|
other arguments passed to |
required |
mode |
str
|
"central" or "forward" derivatives |
'central'
|
EPS |
float
|
the step for forward or central derivatives |
1e-06
|
Returns:
Type | Description |
---|---|
TwoArrays
|
the analytic and numeric gradients. |
Source code in bs_python_utils/bs_opt.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
|
dfp_update(hess_inv, gradient_diff, x_diff)
¶
Runs a DFP update for the inverse Hessian.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hess_inv |
ndarray
|
the current inverse Hessian |
required |
gradient_diff |
ndarray
|
the update in the gradient |
required |
x_diff |
ndarray
|
the update in x |
required |
Returns:
Type | Description |
---|---|
ndarray
|
the updated inverse Hessian. |
Source code in bs_python_utils/bs_opt.py
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 |
|
minimize_free(obj, grad_obj, x_init, args, options=None, bounds=None)
¶
Minimize a function on all of its variables, using BFGS or L-BFGS-B.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj |
Callable
|
the original function |
required |
grad_obj |
Callable
|
its gradient function |
required |
x_init |
ndarray
|
the initial values of all variables |
required |
args |
Iterable
|
other parameters |
required |
options |
dict | None
|
any options passed on to |
None
|
bounds |
list[tuple[float, float]] | None
|
the bounds on all variables, if any |
None
|
Returns:
Type | Description |
---|---|
Any
|
the result of optimization, on all variables. |
Source code in bs_python_utils/bs_opt.py
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 |
|
minimize_some_fixed(obj, grad_obj, x_init, args, fixed_vars, fixed_vals, options=None, bounds=None, time_execution=False)
¶
Minimize a function with some variables fixed, using L-BFGS-B.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj |
Callable
|
the original function |
required |
grad_obj |
Callable
|
its gradient function |
required |
fixed_vars |
list[int] | None
|
a list if the indices of variables whose values are fixed |
required |
fixed_vals |
ndarray | None
|
their fixed values |
required |
x_init |
ndarray
|
the initial values of all variables (those on fixed variables are not used) |
required |
args |
Iterable
|
other parameters |
required |
options |
dict | None
|
any options passed on to |
None
|
bounds |
list[tuple[float, float]] | None
|
the bounds on all variables (those on fixed variables are not used) |
None
|
time_execution |
bool
|
if |
False
|
Returns:
Type | Description |
---|---|
Any
|
the result of optimization, on all variables. |
Source code in bs_python_utils/bs_opt.py
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 |
|
print_constrained_optimization_results(resus, title='Minimizing', print_constr=False, print_multipliers=False)
¶
print results from constrained optimization.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
resus |
OptimizeResult
|
results from optimization |
required |
title |
str
|
a title |
'Minimizing'
|
print_constr |
bool
|
if |
False
|
print_multipliers |
bool
|
if |
False
|
Returns:
Type | Description |
---|---|
None
|
just prints. |
Source code in bs_python_utils/bs_opt.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
|
print_optimization_results(resus, title='Minimizing')
¶
print results from unconstrained optimization.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
resus |
OptimizeResult
|
results from optimization |
required |
title |
str
|
a title |
'Minimizing'
|
Returns:
Type | Description |
---|---|
None
|
just prints. |
Source code in bs_python_utils/bs_opt.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
|