import Mathlib /-! # Verification theorem for a deterministic finite-horizon control problem (HJB) **Setting.** State space `E` (a real normed space, e.g. `EuclideanSpace ℝ (Fin d)`), control set `A` (any type), dynamics `b : E → A → E`, running reward `r : E → A → ℝ`, terminal reward `g : E → ℝ`, horizon `T > 0`, initial state `x₀`. An *admissible pair* `(a, x)` is a control `a : ℝ → A` together with a trajectory `x : ℝ → E` such that `x 0 = x₀`, `x' t = b (x t) (a t)` for every `t ∈ [0, T]` (derivative within `[0, T]`), and `t ↦ r (x t) (a t)` is integrable on `[0, T]`. Its payoff is `J(a, x) = g (x T) + ∫₀ᵀ r (x t) (a t) dt`. **Stated.** Let `u : ℝ → E → ℝ` be `C¹` on `ℝ × E` (jointly), with time derivative `∂ₜu (t, x) = Du(t, x) (1, 0)` and spatial derivative `Dₓu (t, x) v = Du(t, x) (0, v)`, both *defined* below from `fderiv` of `Function.uncurry u`. Assume the terminal condition `u T = g` and the HJB inequality `∂ₜu + Dₓu · b(x, α) + r(x, α) ≤ 0` for all `t ∈ [0, T]`, `x`, `α`. Then 1. every admissible pair has `J(a, x) ≤ u 0 x₀`; 2. every admissible pair along which the HJB inequality is an equality for a.e. `t ∈ [0, T]` has `J(a, x) = u 0 x₀`. In particular, if such an optimal pair exists, `u 0 x₀` is the greatest payoff (the value). **Not stated.** This is the *deterministic* verification theorem. The stochastic version (controlled SDEs, Itô's formula / Dynkin's formula, second-order HJB) is out of reach: Mathlib currently has no Itô calculus. Nothing is said about existence or uniqueness of (viscosity) solutions of the HJB equation, nor about the dimension dependence of anything. -/ open MeasureTheory namespace NSHJB.HJBVerification variable {E : Type*} [NormedAddCommGroup E] [NormedSpace ℝ E] {A : Type*} /-- Time partial derivative `∂ₜu (t, x)`, defined from the joint Fréchet derivative. -/ noncomputable def timeDeriv (u : ℝ → E → ℝ) (t : ℝ) (x : E) : ℝ := fderiv ℝ (Function.uncurry u) (t, x) (1, 0) /-- Spatial derivative `Dₓu (t, x) v`, defined from the joint Fréchet derivative. -/ noncomputable def spaceDeriv (u : ℝ → E → ℝ) (t : ℝ) (x : E) (v : E) : ℝ := fderiv ℝ (Function.uncurry u) (t, x) (0, v) /-- `(a, x)` is admissible: `x 0 = x₀`, `x` solves `x' = b (x, a)` on `[0, T]`, and the running reward along the pair is integrable on `[0, T]`. -/ def IsAdmissible (b : E → A → E) (r : E → A → ℝ) (T : ℝ) (x₀ : E) (a : ℝ → A) (x : ℝ → E) : Prop := x 0 = x₀ ∧ (∀ t ∈ Set.Icc 0 T, HasDerivWithinAt x (b (x t) (a t)) (Set.Icc 0 T) t) ∧ IntervalIntegrable (fun t => r (x t) (a t)) volume 0 T /-- Payoff `J(a, x) = g (x T) + ∫₀ᵀ r (x t) (a t) dt`. -/ noncomputable def payoff (r : E → A → ℝ) (g : E → ℝ) (T : ℝ) (a : ℝ → A) (x : ℝ → E) : ℝ := g (x T) + ∫ t in (0 : ℝ)..T, r (x t) (a t) /-- Deterministic verification theorem. -/ theorem hjb_verification (b : E → A → E) (r : E → A → ℝ) (g : E → ℝ) (T : ℝ) (x₀ : E) (u : ℝ → E → ℝ) (hT : 0 < T) (hu : ContDiff ℝ 1 (Function.uncurry u)) (hterm : ∀ x, u T x = g x) (hHJB : ∀ t ∈ Set.Icc 0 T, ∀ (x : E) (α : A), timeDeriv u t x + spaceDeriv u t x (b x α) + r x α ≤ 0) : (∀ (a : ℝ → A) (x : ℝ → E), IsAdmissible b r T x₀ a x → payoff r g T a x ≤ u 0 x₀) ∧ (∀ (a : ℝ → A) (x : ℝ → E), IsAdmissible b r T x₀ a x → (∀ᵐ t ∂volume, t ∈ Set.Icc 0 T → timeDeriv u t (x t) + spaceDeriv u t (x t) (b (x t) (a t)) + r (x t) (a t) = 0) → payoff r g T a x = u 0 x₀) := by sorry end NSHJB.HJBVerification