Numerical Optimization II: Multiple Dimensions

Eugeniy E. Mikhailov and Greg Bentsen

Logistics and Agenda

Homework 4 due Today Oct 19 at 11:59pm

Homework 5 due Monday Oct 26 at 11:59pm


Midterm 2


Agenda

Today

Optimization in multiple dimensions

Optimization in Multiple Dimensions

Last time: optimizing a single variable

Today: what if the protein can bend at two joints θ1,θ2\theta_1, \theta_2?

The multidimensional optimization problem

We choose a vector of adjustable parameters:

θ→=(θ1,θ2,…,θd) \vec{\theta}=(\theta_1,\theta_2,\ldots,\theta_d)

and try to minimize the objective function:

θ→*=argminθ→f(θ→) \boxed{\vec{\theta}_*=\arg\min_{\vec{\theta}} \ f(\vec{\theta})}

Reading a two-dimensional energy landscape

Source: generated with ChatGPT
Source: generated with ChatGPT

Example 1: Fitting Data

Linear regression as an optimization problem

Suppose we have NN fixed datapoints (xi,yi)(x_i,y_i) and want to fit a line:

yfit(x)=mx+b \boxed{y_{\mathrm{fit}}(x)=mx+b}

Adjustable parameters: θ→=(m,b)\vec{\theta}=(m,b) = (slope, offset)

Linear regression as an optimization problem

Suppose we have NN fixed datapoints (xi,yi)(x_i,y_i) and want to fit a line:

yfit(x)=mx+b \boxed{y_{\mathrm{fit}}(x)=mx+b}

Adjustable parameters: θ→=(m,b)\vec{\theta}=(m,b) = (slope, offset)

Objective function: sum of squared residuals

χ2(m,b)=∑i=1N[yi−(mxi+b)]2 \boxed{\chi^2(m,b)=\sum_{i=1}^{N}[y_i-(mx_i+b)]^2}

Goal: Find (m,b)(m,b) that minimize χ2\chi^2.

The Loss Landscape (objective function)

Every choice of (m,b)(m,b) corresponds to a point in the parameter space, with an associated cost χ2(m,b)\chi^2(m,b).

Demo: python linear_fit_gui.py

Next: What happens when the landscape has multiple valleys?

Example 2: Protein Folding

Previously: Three beads, one angle

Source: generated with ChatGPT
Source: generated with ChatGPT

Objective function: folding energy E(θ1,θ2)E(\theta_1,\theta_2)

E(θ1,θ2)=κ(2−cosθ1−cosθ2)+V(r13)+V(r24)+V(r14) \boxed{E(\theta_1,\theta_2)=\kappa(2-\cos\theta_1-\cos\theta_2) +V(r_{13})+V(r_{24})+V(r_{14})}

V(r)=−ϵexp[−(r−r0)22σ2] V(r)=-\epsilon\exp\!\left[-\frac{(r-r_0)^2}{2\sigma^2}\right]

Fixed parameters:

  • κ\kappa: bending stiffness
  • ϵ\epsilon: attractive interaction strength
  • r0r_0: preferred separation
  • σ\sigma: interaction range
Source: generated with ChatGPT
Source: generated with ChatGPT


Adjustable parameters: θ1,θ2\theta_1, \theta_2

The distances rijr_{ij} are trig functions depending on θ1,θ2\theta_1, \theta_2

AI Demo: translate physics into Python

Prompt a local model to generate:

bead_positions(theta1, theta2, a)
energy(theta1, theta2, a, kappa, epsilon, r0, sigma)

Specify the conventions:

AI coding: test before trusting

Check geometry:

Check numerically:

p = bead_positions(0, 0, 1.0)
assert np.allclose(p, [[0,0], [1,0], [2,0], [3,0]])
assert np.isfinite(energy(0., 0., 1., 1., 3., 1., .2))

Takeaway: AI can implement our physical model; we specify and validate it.

Exploring the folding landscape

Demo: folding_2d.py

  1. Move the red marker: how does the chain fold?
  2. Find a low-energy region: what does the chain look like?
  3. Compare two distinct valleys: which fold has lower energy?
  4. Change κ\kappa or ϵ\epsilon: how does the landscape change?

Contrast with linear regression: this energy landscape can have several minima.

Challenge: How can a computer automatically find the valleys?

Gradient-Based Optimization

The gradient: which way is uphill?

For E(θ1,θ2)E(\theta_1,\theta_2), define the gradient:

∇→E=(∂E/∂θ1∂E/∂θ2) \boxed{\vec\nabla E=\begin{pmatrix} \partial E/\partial\theta_1\\[3pt] \partial E/\partial\theta_2 \end{pmatrix}}

Source: generated with ChatGPT
Source: generated with ChatGPT

Gradient descent is an iterated map

Start with θ→0\vec{\theta}_0, then repeatedly step downhill:

θ→n+1=θ→n−η∇→E(θ→n) \boxed{\vec\theta_{n+1}=\vec\theta_n-\eta \ \vec\nabla E(\vec\theta_n)}

This is an iterated map, just like our earlier dynamical-systems examples.

Gradient descent in Python

Start with θ→0\vec{\theta}_0, then repeatedly step downhill:

θ→n+1=θ→n−η∇→E(θ→n) \boxed{\vec\theta_{n+1}=\vec\theta_n-\eta \ \vec\nabla E(\vec\theta_n)}

import numpy as np

# grad_energy(theta) takes a length-2 array and returns the gradient (also length-2)

theta = np.array([1.0, -2.0])   # initial guess (radians)
eta = 0.05                      # learning rate
tol = 1e-6                      # convergence tolerance
max_steps = 1e4                 # maximum number of steps

grad = grad_energy(theta)
step = 0

while (np.linalg.norm(grad) > tol) & (step < max_steps):
    theta -= eta * grad
    grad = grad_energy(theta)
    step = step + 1

In practice: ∇→E\vec\nabla E may be derived analytically or approximated using finite differences.

Demo: learning rate and starting guess

Demo: folding_gradient_descent.py

Source: generated with ChatGPT
Source: generated with ChatGPT

Question: How can we be confident we found the global minimum?

Pitfalls and Takeaways

Common optimization traps

Practical strategy: run the optimizer from many starting configurations.

Takeaways

The goal: minimize E(θ→)E(\vec\theta) in a space with many possible configurations.

  1. Visualize the landscape when you can
  2. Use gradient descent to move downhill systematically
  3. A converged answer is not necessarily the global minimum


AI-generated models and code

  1. Prompts should be exhaustive and specific
  2. Validate AI-generated numerical models against physical checks


Next time: