math / data /lean /OAI /Probability /MemoryPrecision /KernelModel.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
6.47 kB
import Mathlib
namespace OAI
open MeasureTheory ProbabilityTheory Set Filter EuclideanGeometry
open scoped ENNReal NNReal RealInnerProductSpace Topology Pointwise
noncomputable section
universe u v
open MeasureTheory ProbabilityTheory Set Filter
open scoped ENNReal NNReal RealInnerProductSpace Topology Pointwise
namespace MemoryPrecision.Kernel
abbrev Vector (d : β„•) := EuclideanSpace ℝ (Fin d)
abbrev Sphere (d : β„•) := Metric.sphere (0 : Vector d) 1
abbrev State (M : β„•) := Fin (2^M)
abbrev Action (M : β„•) := Bool Γ— State M
abbrev Observations (d k : β„•) := Fin k β†’ (Vector d Γ— ℝ)
abbrev Rows (d k : β„•) := Fin k β†’ Vector d
def UniformSphere (d : β„•) : Measure (Sphere d) :=
let euclideanGroup : NormedAddCommGroup (Vector d) :=
PiLp.normedAddCommGroup
(@OfNat.ofNat ℝβ‰₯0∞ 2
(@instOfNatAtLeastTwo ℝβ‰₯0∞ 2 ENNReal.instAddCommMonoidWithOne.toNatCast inferInstance))
(fun _ : Fin d => ℝ)
let euclideanInner := PiLp.innerProductSpace (π•œ := ℝ) (fun _ : Fin d => ℝ)
let Οƒ := @Measure.toSphere (Vector d) euclideanGroup
(@InnerProductSpace.toNormedSpace ℝ (Vector d) _
euclideanGroup.toSeminormedAddCommGroup euclideanInner) inferInstance
(@volume (Vector d) (@measureSpaceOfInnerProductSpace (Vector d)
euclideanGroup euclideanInner inferInstance inferInstance inferInstance))
(Οƒ Set.univ)⁻¹ β€’ Οƒ
def RowLaw (d k : β„•) : @Measure (Rows d k)
(@MeasurableSpace.pi (Fin k) (fun _ => Vector d)
(fun _ => @WithLp.measurableSpace
(@OfNat.ofNat ℝβ‰₯0∞ 2
(@instOfNatAtLeastTwo ℝβ‰₯0∞ 2 ENNReal.instAddCommMonoidWithOne.toNatCast inferInstance))
(Fin d β†’ ℝ) inferInstance)) :=
let euclideanMeasurable := @WithLp.measurableSpace
(@OfNat.ofNat ℝβ‰₯0∞ 2
(@instOfNatAtLeastTwo ℝβ‰₯0∞ 2 ENNReal.instAddCommMonoidWithOne.toNatCast inferInstance))
(Fin d β†’ ℝ) inferInstance
@Measure.pi (Fin k) (fun _ => Vector d) inferInstance (fun _ => euclideanMeasurable)
(fun _ => @stdGaussian (Vector d)
(PiLp.normedAddCommGroup
(@OfNat.ofNat ℝβ‰₯0∞ 2
(@instOfNatAtLeastTwo ℝβ‰₯0∞ 2 ENNReal.instAddCommMonoidWithOne.toNatCast inferInstance))
(fun _ : Fin d => ℝ))
(PiLp.innerProductSpace (π•œ := ℝ) (fun _ : Fin d => ℝ)) inferInstance euclideanMeasurable)
def ObservationReference (d : β„•) : Measure (Vector d Γ— ℝ) :=
(stdGaussian (Vector d)).prod volume
structure Policy (d M T : β„•) where
initial : Action M β†’ ℝβ‰₯0∞
initial_sum : βˆ‘ a, initial a = 1
transition : β„• β†’ State M β†’ (Vector d Γ— ℝ) β†’ Action M β†’ ℝβ‰₯0∞
transition_sum : βˆ€ t j obs, βˆ‘ a, transition t j obs a = 1
output : β„• β†’ State M β†’ Measure (Sphere d)
output_probability : βˆ€ t j, IsProbabilityMeasure (output t j)
namespace Policy
instance {d M T : β„•} (K : Policy d M T) (t : β„•) (j : State M) :
IsProbabilityMeasure (K.output t j) := K.output_probability t j
def Completed {d M T : β„•} (K : Policy d M T) : Prop :=
βˆ€ t j a, AEMeasurable (fun obs => K.transition t j obs a) (ObservationReference d)
def Borel {d M T : β„•} (K : Policy d M T) : Prop :=
βˆ€ t j a, Measurable (fun obs => K.transition t j obs a)
def eval {d M T : β„•} {X : Type*} (K : Policy d M T)
(w : β„• β†’ State M β†’ X β†’ ℝβ‰₯0∞) (n : β„•) :
(k : β„•) β†’ State M β†’ X β†’ Observations d k β†’ ℝβ‰₯0∞
| 0, j, s, _ => w n j s
| k+1, j, s, obs => βˆ‘ a : Action M, K.transition (n+1) j (obs 0) a *
if a.1 then w (n+1) a.2 s else K.eval w (n+1) k a.2 s (Fin.tail obs)
def pathValue {d M T : β„•} {X : Type*} (K : Policy d M T)
(w : β„• β†’ State M β†’ X β†’ ℝβ‰₯0∞) (s : X) (obs : Observations d T) : ℝβ‰₯0∞ :=
βˆ‘ a : Action M, K.initial a * if a.1 then w 0 a.2 s else K.eval w 0 T a.2 s obs
def records {d T : β„•} (s : Sphere d) (A : Rows d T) : Observations d T :=
fun index => (A index, @inner ℝ (EuclideanSpace ℝ (Fin d))
(@InnerProductSpace.toInner ℝ (EuclideanSpace ℝ (Fin d)) _
(PiLp.seminormedAddCommGroup
(@OfNat.ofNat ℝβ‰₯0∞ 2
(@instOfNatAtLeastTwo ℝβ‰₯0∞ 2 ENNReal.instAddCommMonoidWithOne.toNatCast inferInstance))
(fun _ : Fin d => ℝ))
(PiLp.innerProductSpace (π•œ := ℝ) (fun _ : Fin d => ℝ))) s.val (A index))
def outputTest {d M T : β„•} (K : Policy d M T) (f : Sphere d Γ— Sphere d β†’ ℝβ‰₯0∞)
(t : β„•) (j : State M) (s : Sphere d) : ℝβ‰₯0∞ :=
∫⁻ v, f (s,v) βˆ‚K.output t j
def angleTest {d : β„•} (Ξ΅ : ℝ)
(point : MemoryPrecision.Kernel.Sphere d Γ— MemoryPrecision.Kernel.Sphere d) : ℝβ‰₯0∞ :=
@ite ℝβ‰₯0∞ (Real.arccos (@inner ℝ (EuclideanSpace ℝ (Fin d))
(@InnerProductSpace.toInner ℝ (EuclideanSpace ℝ (Fin d)) _
(PiLp.seminormedAddCommGroup
(@OfNat.ofNat ℝβ‰₯0∞ 2
(@instOfNatAtLeastTwo ℝβ‰₯0∞ 2 ENNReal.instAddCommMonoidWithOne.toNatCast inferInstance))
(fun _ : Fin d => ℝ))
(PiLp.innerProductSpace (π•œ := ℝ) (fun _ : Fin d => ℝ)))
point.2.val point.1.val) ≀ Ξ΅) (Classical.propDecidable _) 1 0
def chordTest {d : β„•} (Ξ΅ : ℝ) (x : Sphere d Γ— Sphere d) : ℝβ‰₯0∞ :=
{x : Sphere d Γ— Sphere d | dist x.2.val x.1.val ≀ Ξ΅}.indicator (fun _ => 1) x
def experiment {d M T : β„•} {Ξ© : Type u} (K : Ξ© β†’ Policy d M T)
(f : Sphere d Γ— Sphere d β†’ ℝβ‰₯0∞) (z : Sphere d Γ— (Ξ© Γ— Rows d T)) : ℝβ‰₯0∞ :=
(K z.2.1).pathValue ((K z.2.1).outputTest f) z.1 (records z.1 z.2.2)
structure Admissible {d M T : β„•} {Ξ© : Type u} [MeasurableSpace Ξ©]
(K : Ξ© β†’ Policy d M T) (ρ : Measure Ξ©) (Ο€ : Measure (Sphere d)) : Prop where
completed : βˆ€α΅ Ο‰ βˆ‚Ο, (K Ο‰).Completed
experiment : βˆ€ f : Sphere d Γ— Sphere d β†’ ℝβ‰₯0∞, Measurable f β†’
(βˆ€ x, f x ≀ 1) β†’ AEMeasurable (experiment K f) (Ο€.prod (ρ.prod (RowLaw d T)))
def Success {d M T : β„•} {Ξ© : Type u} [MeasurableSpace Ξ©]
(K : Ξ© β†’ Policy d M T) (ρ : Measure Ξ©) (Ο€ : Measure (Sphere d)) (Ξ΅ : ℝ) : ℝβ‰₯0∞ :=
∫⁻ z, experiment K (angleTest Ξ΅) z βˆ‚Ο€.prod (ρ.prod (RowLaw d T))
def ChordSuccess {d M T : β„•} {Ξ© : Type u} [MeasurableSpace Ξ©]
(K : Ξ© β†’ Policy d M T) (ρ : Measure Ξ©) (Ο€ : Measure (Sphere d)) (Ξ΅ : ℝ) : ℝβ‰₯0∞ :=
∫⁻ z, experiment K (chordTest Ξ΅) z βˆ‚Ο€.prod (ρ.prod (RowLaw d T))
end Policy
end MemoryPrecision.Kernel
end
end OAI