Skip to content

auto_math_trust_regions_and_optimization_1

github-actions[bot] edited this page Nov 14, 2025 · 1 revision

Snippet from Math-Trust-Regions-and-Optimization.md

import numpy as np

def trust_step(g: np.ndarray, B: np.ndarray, Delta: float,
               eta1: float = 0.1, gamma1: float = 2.0) -> tuple:
    """
    Execute one trust region step with adaptive radius adjustment.
    
    Args:
        g: Gradient vector (d,).
        B: Hessian approximation (d x d).
        Delta: Current trust radius.
        eta1: Acceptance threshold for rho.
        gamma1: Expansion factor.
    
    Returns:
        Tuple of (step p, quality ratio rho, new radius Delta).
    """
    
    # Compute Cauchy point (steepest descent approximation)
    g_norm = np.linalg.norm(g)
    if g_norm == 0:
        return np.zeros_like(g), 1.0, Delta
    
    # Scale gradient to trust boundary
    p = -(Delta / g_norm) * g
    
    # Predicted reduction (quadratic model)
    m0 = 0
    m_p = g @ p + 0.5 * p @ B @ p
    pred_red = m0 - m_p
    
    # Actual reduction (in practice: route and measure energy change)
    # Here using toy: 70% match for demonstration
    actual_red = 0.7 * pred_red
    
    # Compute quality ratio
    rho = actual_red / pred_red if pred_red > 0 else -1
    
    # Accept/reject and adjust radius
    accept = rho > eta1
    if accept:
        Delta_new = min(gamma1 * Delta, 2 * np.linalg.norm(p))
    else:
        Delta_new = Delta / 2
    
    return p, rho, Delta_new

# Example: Gradient descent with trust region control
g = np.array([1.0, 0.5])  # Gradient
B = np.eye(2) * 0.1       # Hessian approximation
Delta = 1.0               # Initial trust radius

p, rho, Delta_new = trust_step(g, B, Delta)

print("Trust Region Step Results")
print("=" * 50)
print(f"Step direction: {p}")
print(f"Quality ratio rho: {rho:.2f}")
print(f"Accept step: {rho > 0.1}")
print(f"New radius Delta: {Delta_new:.2f}")
print(f"\nInterpretation:")
print(f"  rho > 0.75: Excellent (expand aggressively)")
print(f"  0.1 ≤ rho ≤ 0.75: Good (maintain or expand)")
print(f"  rho < 0.1: Poor (reject and shrink)")

Clone this wiki locally