Skip to content

Commit 8006227

Browse files
committed
Work in progress on probability of quorum
1 parent 8af831b commit 8006227

File tree

3 files changed

+54
-26
lines changed

3 files changed

+54
-26
lines changed

analysis/markov/Linleios/Evolve.lean

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,40 @@ open Std (HashMap)
88
abbrev Probability := Float
99

1010

11+
def approxCommittee (committeeSize : Float) : Float × Float :=
12+
let nPools : Nat := 2500
13+
let stakes : List Float := (List.range nPools).map (fun k => ((k + 1).toFloat / nPools.toFloat)^10 - (k.toFloat / nPools.toFloat)^10)
14+
let ps : List Float := stakes.map (fun s => 1 - Float.exp (- committeeSize * s))
15+
let μ : Float := ps.sum
16+
let σ := (ps.map (fun p => p * (1 - p))).sum.sqrt
17+
⟨ μ , σ ⟩
18+
19+
1120
structure Environment where
1221
activeSlotCoefficient : Probability
1322
Lheader : Nat
1423
Lvote : Nat
1524
Ldiff : Nat
1625
pSpacingOkay : Probability
26+
pRbHeaderArrives : Probability
27+
pEbValidates : Probability
1728

18-
def makeEnvironment (activeSlotCoefficient : Float) (Lheader Lvote Ldiff : Nat) : Environment :=
29+
def makeEnvironment (activeSlotCoefficient pRbHeaderArrives pEbValidates : Float) (Lheader Lvote Ldiff : Nat) : Environment :=
1930
{
2031
activeSlotCoefficient := activeSlotCoefficient
2132
Lheader := Lheader
2233
Lvote := Lvote
2334
Ldiff := Ldiff
2435
pSpacingOkay := (1 - activeSlotCoefficient).pow (3 * Lheader + Lvote + Ldiff - 1).toFloat
36+
pRbHeaderArrives := pRbHeaderArrives
37+
pEbValidates := pEbValidates
2538
}
2639

2740

2841
structure State where
2942
rbCount : Nat
3043
ebCount : Nat
44+
canCertify : Bool
3145
deriving Repr, BEq, Hashable, Inhabited
3246

3347
example : (default : State).rbCount = 0 := rfl
@@ -42,27 +56,34 @@ instance : Inhabited Probabilities where
4256
default := (∅ : Probabilities).insert Inhabited.default 1
4357

4458

45-
def forge {env : Environment} (state : State) : List (State × Probability) :=
59+
abbrev Outcomes := List (State × Probability)
60+
61+
62+
def forge {env : Environment} (state : State) : Outcomes :=
63+
let state' :=
64+
{
65+
state with
66+
rbCount := state.rbCount + 1
67+
canCertify := true
68+
}
69+
if state.canCertify
70+
then [
71+
⟨{state' with ebCount := state.ebCount + 1}, env.pSpacingOkay⟩
72+
, ⟨state', 1 - env.pSpacingOkay⟩
73+
]
74+
else [(state', 1)]
75+
76+
def validate {env : Environment} (state : State) : Outcomes :=
4677
[
47-
48-
{
49-
state with
50-
rbCount := state.rbCount + 1
51-
}
52-
, 1 - env.pSpacingOkay
53-
54-
, ⟨
55-
{
56-
state with
57-
rbCount := state.rbCount + 1
58-
ebCount := state.ebCount + 1
59-
}
60-
, env.pSpacingOkay
61-
78+
(state, env.pRbHeaderArrives * env.pEbValidates)
79+
, ({state with canCertify := false}, 1 - env.pRbHeaderArrives * env.pEbValidates)
6280
]
6381

82+
def step {env : Environment} : List (State → Outcomes) :=
83+
[@forge env, @validate env]
84+
6485

65-
def evolve (transition : State → List (State × Probability)) : Probabilities → Probabilities :=
86+
def evolve (transition : State → Outcomes) : Probabilities → Probabilities :=
6687
HashMap.fold
6788
(
6889
fun acc state p =>
@@ -73,12 +94,18 @@ def evolve (transition : State → List (State × Probability)) : Probabilities
7394
)
7495
7596

76-
def simulate (transition : State → List (State × Probability)) (start : Probabilities) : Nat → Probabilities
77-
| 0 => start
78-
| n + 1 => simulate transition (evolve transition start) n
97+
def chain (transitions : List (State → Outcomes)) : Probabilities → Probabilities :=
98+
match transitions with
99+
| [] => id
100+
| (t :: ts) => chain ts ∘ evolve t
101+
102+
def simulate (env : Environment) (start : Probabilities) : Nat → Probabilities
103+
| 0 => start
104+
| n + 1 => let state' := chain (@step env) start
105+
simulate env state' n
79106

80107
def prune (ε : Float) : Probabilities → Probabilities :=
81-
HashMap.filter (fun _ p => p ε)
108+
HashMap.filter (fun _ p => p > ε)
82109

83110
def totalProbability (states : Probabilities) : Probability :=
84111
states.values.sum

analysis/markov/Main.lean

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
import Linleios
33

44

5-
def env : Environment := makeEnvironment 0.05 1 4 7
5+
def env : Environment := makeEnvironment 0.05 0.95 0.90 1 4 7
66

77
def s0 : Probabilities := default
8-
def sn := simulate (@forge env) s0 50
8+
def sn := simulate env s0 10
99
def pn := totalProbability sn
1010

1111
def main : IO Unit :=
1212
do
13-
let print {α : Type} [Repr α] (x : α) : IO Unit := IO.println $ (reprPrec x 0).pretty
14-
print sn
13+
let print {α : Type} [Repr α] (x : α) : IO Unit := IO.println (reprPrec x 0).pretty
14+
print $ prune 0 sn
1515
print pn

analysis/sims/kernels.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ in
3333
igraph
3434
lubridate
3535
mongolite
36+
poibin
3637
quantreg
3738
RPostgres
3839
R_utils

0 commit comments

Comments
 (0)