TorchLean API

NN.Proofs.RL.MDP

Finite-MDP Proofs #

This module proves the foundational theorems for TorchLean's finite discounted MDP layer:

The fixed-point facts are proved once, for any operator on finite value tables that contracts the sup distance (SupContraction). They come from Mathlib's Banach fixed-point theorem (ContractingWith.fixedPoint) after carrying value tables to Fin n → ℝ, whose metric is the sup metric. Proofs.RL.FiniteStochastic reuses the same development for stochastic transitions.

References:

Sup Metric for Finite Value Tables #

The metric is the maximum absolute pointwise difference between two value tables. The finite stochastic development opens these declarations rather than restating them.

Proofs.RL.Markov.valueSupDist measures the same thing on a possibly infinite state space. That one is an sSup rather than a Finset.sup', so it needs a boundedness hypothesis before any of these facts hold.

@[reducible, inline]
abbrev Proofs.RL.MDP.valueEquiv (nStates : ℕ) :
Spec.RL.ValueFunction ℝ nStates ≃ (Fin nStates → ℝ)

Value tables as functions Fin n → ℝ. Mathlib's metric on that type is the sup metric.

Instances For
    noncomputable def Proofs.RL.MDP.valueSupDist {nStates : ℕ} [Fact (0 < nStates)] (values₁ values₂ : Spec.RL.ValueFunction ℝ nStates) :

    Sup distance on finite value functions, using the maximum absolute pointwise difference.

    Instances For
      theorem Proofs.RL.MDP.abs_sub_valueAt_le_valueSupDist {nStates : ℕ} [Fact (0 < nStates)] (values₁ values₂ : Spec.RL.ValueFunction ℝ nStates) (state : Fin nStates) :
      |Spec.RL.valueAt values₁ state - Spec.RL.valueAt values₂ state| ≤ valueSupDist values₁ values₂

      Every pointwise absolute difference is bounded by the sup distance.

      theorem Proofs.RL.MDP.valueSupDist_nonneg {nStates : ℕ} [Fact (0 < nStates)] (values₁ values₂ : Spec.RL.ValueFunction ℝ nStates) :
      0 ≤ valueSupDist values₁ values₂

      The sup distance is nonnegative.

      theorem Proofs.RL.MDP.valueSupDist_le {nStates : ℕ} [Fact (0 < nStates)] {values₁ values₂ : Spec.RL.ValueFunction ℝ nStates} {bound : ℝ} (h : ∀ (state : Fin nStates), |Spec.RL.valueAt values₁ state - Spec.RL.valueAt values₂ state| ≤ bound) :
      valueSupDist values₁ values₂ ≤ bound

      A uniform bound on pointwise differences bounds the sup distance.

      theorem Proofs.RL.MDP.valueSupDist_eq_dist {nStates : ℕ} [Fact (0 < nStates)] (values₁ values₂ : Spec.RL.ValueFunction ℝ nStates) :
      valueSupDist values₁ values₂ = dist ((valueEquiv nStates) values₁) ((valueEquiv nStates) values₂)

      valueSupDist is Mathlib's sup distance on Fin n → ℝ.

      theorem Proofs.RL.MDP.valueSupDist_eq_zero_iff {nStates : ℕ} [Fact (0 < nStates)] (values₁ values₂ : Spec.RL.ValueFunction ℝ nStates) :
      valueSupDist values₁ values₂ = 0 ↔ values₁ = values₂

      valueSupDist = 0 iff two finite value functions are equal.

      Fixed Points of Sup-Norm Contractions #

      Everything here holds for any operator T on finite value tables with valueSupDist (T v) (T w) ≤ γ * valueSupDist v w and 0 ≤ γ < 1. Conjugating T by valueEquiv gives a ContractingWith map on the complete space Fin n → ℝ, and the conclusions are Mathlib's.

      structure Proofs.RL.MDP.SupContraction {nStates : ℕ} [Fact (0 < nStates)] (γ : ℝ) (T : Spec.RL.ValueFunction ℝ nStates → Spec.RL.ValueFunction ℝ nStates) :

      T contracts the finite sup distance by the factor γ ∈ [0, 1).

      • nonneg : 0 ≤ γ

        The factor is nonnegative.

      • lt_one : γ < 1

        The factor is strictly below one.

      • dist_le (values₁ values₂ : Spec.RL.ValueFunction ℝ nStates) : valueSupDist (T values₁) (T values₂) ≤ γ * valueSupDist values₁ values₂

        One application of T shrinks every sup distance by γ.

      Instances For
        theorem Proofs.RL.MDP.SupContraction.contractingWith {nStates : ℕ} [Fact (0 < nStates)] {γ : ℝ} {T : Spec.RL.ValueFunction ℝ nStates → Spec.RL.ValueFunction ℝ nStates} (h : SupContraction γ T) :

        The operator T read on Fin n → ℝ is a Mathlib ContractingWith map.

        noncomputable def Proofs.RL.MDP.SupContraction.fixedPoint {nStates : ℕ} [Fact (0 < nStates)] {γ : ℝ} {T : Spec.RL.ValueFunction ℝ nStates → Spec.RL.ValueFunction ℝ nStates} (h : SupContraction γ T) :

        The fixed point of T given by Banach's fixed-point theorem.

        Instances For
          theorem Proofs.RL.MDP.SupContraction.fixedPoint_isFixed {nStates : ℕ} [Fact (0 < nStates)] {γ : ℝ} {T : Spec.RL.ValueFunction ℝ nStates → Spec.RL.ValueFunction ℝ nStates} (h : SupContraction γ T) :

          fixedPoint is a fixed point.

          theorem Proofs.RL.MDP.SupContraction.eq_fixedPoint {nStates : ℕ} [Fact (0 < nStates)] {γ : ℝ} {T : Spec.RL.ValueFunction ℝ nStates → Spec.RL.ValueFunction ℝ nStates} (h : SupContraction γ T) {values : Spec.RL.ValueFunction ℝ nStates} (hfix : T values = values) :
          values = h.fixedPoint

          Every fixed point of T is fixedPoint.

          theorem Proofs.RL.MDP.SupContraction.existsUnique_fixedPoint {nStates : ℕ} [Fact (0 < nStates)] {γ : ℝ} {T : Spec.RL.ValueFunction ℝ nStates → Spec.RL.ValueFunction ℝ nStates} (h : SupContraction γ T) :
          ∃! values : Spec.RL.ValueFunction ℝ nStates, T values = values

          T has exactly one fixed point.

          theorem Proofs.RL.MDP.SupContraction.iterate_le {nStates : ℕ} [Fact (0 < nStates)] {γ : ℝ} {T : Spec.RL.ValueFunction ℝ nStates → Spec.RL.ValueFunction ℝ nStates} (h : SupContraction γ T) (k : ℕ) (values₁ values₂ : Spec.RL.ValueFunction ℝ nStates) :
          valueSupDist (T^[k] values₁) (T^[k] values₂) ≤ γ ^ k * valueSupDist values₁ values₂

          k applications of T shrink every sup distance by γ ^ k.

          theorem Proofs.RL.MDP.SupContraction.iterate_error {nStates : ℕ} [Fact (0 < nStates)] {γ : ℝ} {T : Spec.RL.ValueFunction ℝ nStates → Spec.RL.ValueFunction ℝ nStates} (h : SupContraction γ T) {values vStar : Spec.RL.ValueFunction ℝ nStates} (hfix : T vStar = vStar) (k : ℕ) :
          valueSupDist (T^[k] values) vStar ≤ γ ^ k * valueSupDist values vStar

          The distance from the k-th iterate to any fixed point decays like γ ^ k.

          theorem Proofs.RL.MDP.SupContraction.apriori_error {nStates : ℕ} [Fact (0 < nStates)] {γ : ℝ} {T : Spec.RL.ValueFunction ℝ nStates → Spec.RL.ValueFunction ℝ nStates} (h : SupContraction γ T) (values : Spec.RL.ValueFunction ℝ nStates) (k : ℕ) :
          valueSupDist (T^[k] values) h.fixedPoint ≤ valueSupDist values (T values) * γ ^ k / (1 - γ)

          A priori error bound for value iteration, computable from the first step alone.

          theorem Proofs.RL.MDP.SupContraction.tendsto_iterate {nStates : ℕ} [Fact (0 < nStates)] {γ : ℝ} {T : Spec.RL.ValueFunction ℝ nStates → Spec.RL.ValueFunction ℝ nStates} (h : SupContraction γ T) (values : Spec.RL.ValueFunction ℝ nStates) :

          Value iteration converges to the fixed point from any starting table.

          Deterministic Bellman Operators #

          theorem Proofs.RL.MDP.valueAt_bellmanPolicy {nStates nActions : ℕ} (mdp : Spec.RL.FiniteMDP ℝ nStates nActions) (policy : Spec.RL.Policy nStates nActions) (values : Spec.RL.ValueFunction ℝ nStates) (state : Fin nStates) :
          Spec.RL.valueAt (Spec.RL.bellmanPolicy mdp policy values) state = Spec.RL.stateActionValue mdp values state (policy state)

          Policy Bellman operators read back exactly the selected state-action value.

          theorem Proofs.RL.MDP.valueAt_bellmanOptimality {nStates nActions : ℕ} [Fact (0 < nActions)] (mdp : Spec.RL.FiniteMDP ℝ nStates nActions) (values : Spec.RL.ValueFunction ℝ nStates) (state : Fin nStates) :
          have x := ⋯; Spec.RL.valueAt (Spec.RL.bellmanOptimality mdp values) state = Finset.univ.sup' ⋯ (Spec.RL.stateActionValue mdp values state)

          Bellman optimality reads back the best state-action value.

          theorem Proofs.RL.MDP.stateActionValue_mono {nStates nActions : ℕ} (mdp : Spec.RL.FiniteMDP ℝ nStates nActions) (values₁ values₂ : Spec.RL.ValueFunction ℝ nStates) (hγ : 0 ≤ mdp.discount) (hValues : ∀ (state : Fin nStates), Spec.RL.valueAt values₁ state ≤ Spec.RL.valueAt values₂ state) (state : Fin nStates) (action : Fin nActions) :
          Spec.RL.stateActionValue mdp values₁ state action ≤ Spec.RL.stateActionValue mdp values₂ state action

          A Bellman state-action value is monotone in the candidate value function when γ ≥ 0.

          theorem Proofs.RL.MDP.bellmanPolicy_mono {nStates nActions : ℕ} (mdp : Spec.RL.FiniteMDP ℝ nStates nActions) (policy : Spec.RL.Policy nStates nActions) (values₁ values₂ : Spec.RL.ValueFunction ℝ nStates) (hγ : 0 ≤ mdp.discount) (hValues : ∀ (state : Fin nStates), Spec.RL.valueAt values₁ state ≤ Spec.RL.valueAt values₂ state) (state : Fin nStates) :
          Spec.RL.valueAt (Spec.RL.bellmanPolicy mdp policy values₁) state ≤ Spec.RL.valueAt (Spec.RL.bellmanPolicy mdp policy values₂) state

          Bellman policy operators are pointwise monotone for nonnegative discounts.

          theorem Proofs.RL.MDP.stateActionValue_le_bellmanOptimality {nStates nActions : ℕ} [Fact (0 < nActions)] (mdp : Spec.RL.FiniteMDP ℝ nStates nActions) (values : Spec.RL.ValueFunction ℝ nStates) (state : Fin nStates) (action : Fin nActions) :
          Spec.RL.stateActionValue mdp values state action ≤ Spec.RL.valueAt (Spec.RL.bellmanOptimality mdp values) state

          Bellman optimality dominates every particular action.

          theorem Proofs.RL.MDP.bellmanPolicy_le_bellmanOptimality {nStates nActions : ℕ} [Fact (0 < nActions)] (mdp : Spec.RL.FiniteMDP ℝ nStates nActions) (policy : Spec.RL.Policy nStates nActions) (values : Spec.RL.ValueFunction ℝ nStates) (state : Fin nStates) :

          Bellman optimality dominates Bellman evaluation under any deterministic policy.

          theorem Proofs.RL.MDP.bellmanOptimality_mono {nStates nActions : ℕ} [Fact (0 < nActions)] (mdp : Spec.RL.FiniteMDP ℝ nStates nActions) (values₁ values₂ : Spec.RL.ValueFunction ℝ nStates) (hγ : 0 ≤ mdp.discount) (hValues : ∀ (state : Fin nStates), Spec.RL.valueAt values₁ state ≤ Spec.RL.valueAt values₂ state) (state : Fin nStates) :

          Bellman optimality is pointwise monotone for nonnegative discounts.

          theorem Proofs.RL.MDP.stateActionValue_abs_sub_le {nStates nActions : ℕ} [Fact (0 < nStates)] (mdp : Spec.RL.FiniteMDP ℝ nStates nActions) (values₁ values₂ : Spec.RL.ValueFunction ℝ nStates) (hγ₀ : 0 ≤ mdp.discount) (state : Fin nStates) (action : Fin nActions) :
          |Spec.RL.stateActionValue mdp values₁ state action - Spec.RL.stateActionValue mdp values₂ state action| ≤ mdp.discount * valueSupDist values₁ values₂

          Deterministic state-action Bellman values are Lipschitz with constant γ.

          theorem Proofs.RL.MDP.bellmanPolicy_contraction {nStates nActions : ℕ} [Fact (0 < nStates)] (mdp : Spec.RL.FiniteMDP ℝ nStates nActions) (policy : Spec.RL.Policy nStates nActions) (values₁ values₂ : Spec.RL.ValueFunction ℝ nStates) (hγ₀ : 0 ≤ mdp.discount) :
          valueSupDist (Spec.RL.bellmanPolicy mdp policy values₁) (Spec.RL.bellmanPolicy mdp policy values₂) ≤ mdp.discount * valueSupDist values₁ values₂

          Bellman evaluation for a deterministic policy is a γ-contraction in the finite sup metric.

          theorem Proofs.RL.MDP.bellmanOptimality_abs_sub_le {nStates nActions : ℕ} [Fact (0 < nStates)] [Fact (0 < nActions)] (mdp : Spec.RL.FiniteMDP ℝ nStates nActions) (values₁ values₂ : Spec.RL.ValueFunction ℝ nStates) (hγ₀ : 0 ≤ mdp.discount) (state : Fin nStates) :
          |Spec.RL.valueAt (Spec.RL.bellmanOptimality mdp values₁) state - Spec.RL.valueAt (Spec.RL.bellmanOptimality mdp values₂) state| ≤ mdp.discount * valueSupDist values₁ values₂

          At a fixed state, Bellman optimality is Lipschitz with constant γ.

          theorem Proofs.RL.MDP.bellmanOptimality_contraction {nStates nActions : ℕ} [Fact (0 < nStates)] [Fact (0 < nActions)] (mdp : Spec.RL.FiniteMDP ℝ nStates nActions) (values₁ values₂ : Spec.RL.ValueFunction ℝ nStates) (hγ₀ : 0 ≤ mdp.discount) :
          valueSupDist (Spec.RL.bellmanOptimality mdp values₁) (Spec.RL.bellmanOptimality mdp values₂) ≤ mdp.discount * valueSupDist values₁ values₂

          Bellman optimality is a γ-contraction in the finite sup metric.

          theorem Proofs.RL.MDP.bellmanPolicy_supContraction {nStates nActions : ℕ} [Fact (0 < nStates)] (mdp : Spec.RL.FiniteMDP ℝ nStates nActions) (policy : Spec.RL.Policy nStates nActions) (hγ₀ : 0 ≤ mdp.discount) (hγ₁ : mdp.discount < 1) :

          For 0 ≤ γ < 1, policy evaluation is a SupContraction: it has a unique fixed point and value iteration converges to it.

          theorem Proofs.RL.MDP.bellmanOptimality_supContraction {nStates nActions : ℕ} [Fact (0 < nStates)] [Fact (0 < nActions)] (mdp : Spec.RL.FiniteMDP ℝ nStates nActions) (hγ₀ : 0 ≤ mdp.discount) (hγ₁ : mdp.discount < 1) :

          For 0 ≤ γ < 1, Bellman optimality is a SupContraction: the optimal value table exists, is unique, and value iteration converges to it.