Selecting Uniformly From a Collection #
This file defines some computations for selecting uniformly at random from a number
of different collections, using unifSpec to make the random choices.
TODO: A lot of lemmas here could exist at the PMF level instead.
Probably even a lot of the uniform constructions themselves (like uniformOfList)
Typeclass to implement the notation $ xs for selecting an object uniformly from a collection.
The container type is given by cont with the resulting type given by β.
NOTE: This current implementation doesn't impose any "correctness" conditions,
it purely exists to provide the notation, could revisit that in the future.
- uniformSelect : cont → ProbComp β
Instances
Given a selectable object, we can get a random element by indexing into the element vector.
Equations
Instances For
Select a random element from a list by indexing into it with a uniform value. If the list is empty we instead just fail rather than choose a default value. This means selecting from a vector is often preferable, as we can prove at the type level that there is an element in the list, avoiding the defualt case of empty lists.
Equations
Select a random element from a vector by indexing into it with a uniform value. TODO: different types of vectors in mathlib now
Equations
Choose a random element from a finite set, by converting to a list and choosing from that. This is noncomputable as we don't have a canoncial ordering for the resulting list, so generally this should be avoided when possible.
Equations
A SelectableType β instance means that β is a finite inhabited type,
with a computation selectElem that selects uniformly at random from the type.
This generally requires choosing some "canonical" ordering for the type,
so we include this to get a computable version of selection.
We also require that each element has the same probability of being chosen from by selectElem,
see SelectableType.probOutput_selectElem for the reduction when α has a fintype instance.
NOTE: universe polymorphism of β is hard.
- selectElem : ProbComp β
Instances
Select uniformly from the type β using a type-class provided definition.
NOTE: naming is somewhat strange now that Fintype isn't explicitly required.
Equations
Instances For
Equations
Select a uniform element from α × β by independently selecting from α and β.
Equations
Version of Fin selection using the NeZero typeclass, avoiding the need for n + 1.
Equations
Select a uniform element from Vector α n by independently selecting α at each index.
Equations
Select a uniform element from Matrix α n by independently selecting α at each index.
Equations
Given an independent probabilistic computation ob : ProbComp Bool, the probability that its
output b' differs from a uniformly chosen boolean b is the same as the probability that they
are equal. In other words, P(b ≠ b') = P(b = b') where b is uniform.