# On TB epoch length
Parameters:
* $s$, number of slots (currently 7000)
* $c$, blocks per cycle (8192 or 16384)
* $e$, blocks per epoch
Let $f$ be a delegate's stake fraction.
## Warm-up: current status
How often (in number of levels) will a delegate with one roll have at least one slot?
(VB said that's once every 10 levels. Is this true?)
Currently (at cycle 544), the total active stake is: 718530826543983 mutez. To simplify: `7 * 10^8` tez.
So at given level a baker with 6000 tez has probability: $f = \frac{6* 10^3}{7 * 10^8}$ to be a given slot's owner.
A delegate has no slot at a given level with proba $(1-f)^s$. So the probability to have at least one slot at a given level is $1-(1-f)^s$. In other words, they have on average a slot every $\frac{1}{1-(1-f)^s}$ levels.
For a delegate with one roll we get proba .058236 to have at least one slot. In other words, they have a slot approximatively every $17$ levels. Such a delegate has on average 492 slots per cycle (before M), or 983 slots per cycle (in M).
<!--
WRONG COMPUTATIONS, BUT OK APPROXIMATIONS!!!
So probability $\frac{7 * 10^3 * 6* 10^3}{7 * 10^8} = \frac{6}{100}$ to have at least one slot at a given level. In other words, they have a slot every $\frac{100}{6}=16.6$ levels.
(By the way, such a delegate will have, prior to protocol M, $\frac{8192 * 6}{100} \approx 492$ slots per cycle.)-->
## Variance of number of slots per cycle
Afterthought: this analysis is not very useful, because the rewards are not per included endorsement (slots), but are depending on the expected number of slots per cycle, precisely in order to ignore the variance.
<!-- Given $e$, find $s$ such that we get the same variance as with $e=1$, $s=7000$. -->
NB: the average is the same in all cases: $fsc = fse\frac{c}{e}$.
Let $X$ be the random variable corresponding to the current case. This defines a binomial distribution: we have $sc$ independent draws of a random biased coin with probability $f$.
Let $Y_e$ be the random variable corresponding to the new setting. We have $s\frac{c}{e}$ independent draws, each with output $0$ or $e$.
It is [well-known](https://en.wikipedia.org/wiki/Variance#Commonly_used_probability_distributions) that $Var(X) = scf(1-f)$.
Next, we derive $Var[Y_e]$.
We have $Var(Y_e) := E[(Y_e - E[Y_e])^2] = E[Y_e^2] - E[Y_e]^2$.
Let $Z_i$ be the random variable with
* $Z_i=e$ if draw $i$ is a success,
* $Z_i=0$ if draw $i$ is a failure.
We have $E[Z_i]=fe$. We have $(Z_i-E[Z_i])^2$ is
* $(e-fe)^2$ with proba $f$,
* $(0-fe)^2$ with proba $1-f$.
So $Var(Z_i) = e^2 (1-f)^2 f + f^2 e^2 (1-f) = e^2f(1-f)(1-f + f) = e^2f(1-f)$.
We have $Y_e = \sum_{i=1}^{\frac{sc}{e}}Z_i$. It follows that $E[Y_e] = \frac{sc}{e}fe = fsc$. And $Var(Y_e) = \frac{sc}{e}e^2f(1-f) = scef(1-f)$.
So, to recover the same variance, we'd need $e$ times more slots!
NB: the standard deviation for $e=1$ is $21.5$ slots per cycle. (TODO: what does this mean intuitively?) The standard deviation increases by $\sqrt{e}$ with $e$.
<!--Fixing $s=7000$, here are some standard deviations of the number of slots per cycle, for a delegate with a roll:
| $e$ | SD |
| --- | -- |
| 1 | 21.5 |
-->
## Main question
Given a rate (say 1%) of "accidental endorsement misses" in terms of slots (or levels?!) per cycle, how much can $e$ be increased such that the probability of a delegate with minimal stake losing its rewards is "negligeable" (say once per century)?
Recall that a delegate loses its rewards if it misses one third of its *expected* slots in a cycle.
<!--It seems to me that the question does not make sense if the rate is given in terms of slots per cycle (1% < 33%). Let's thus say it's given in terms of levels per cycle. More precisely, a delegate misses a fraction $r$ of the levels it participates in.
Let $P[Y_e = le]$ be the probability that the delegate has at least one slot in exactly $le$ levels in a cycle. The average number of slots per level is $fs$.-->
What if we ignore the "error late" of 1%, and focus on the variance alone, at least in a first step. We can compute the probability that a delegate does not receive enough slots (just due to bad luck!) to reach 66% of the expected participation, and that depending on $e$. How?
The probability that a delegate has exactly $n$ slots in a cycle is $$P[Y_e=n]=\sum_{n_1+\dots+n_{k}=n}\Pi_{i=1}^kP[Z'_i=n_i]$$
where $k:=\frac{c}{e}$ and $Z'_i$ is the random variable giving the number of slots in epoch $i$.
We have:
* $P[Z=ne] = BD(s,n) := C_s^nf^n(1-f)^{s-n}$,
* $P[Z=m]=0$ for $m$ not a multiple of $e$.
### Case $e=1$ (just to check)
Hm, I don't know how to justify that $P(X=n) = BD(sc,n)$.
### Case $e=c$
This is intuitively the worst case.
We have $P(X=ne) = BD(s,n)$.
Now, we're interested in $$\sum_{i=0}^{b} P(X=i) = \sum_{i=0}^{b} C^i_s f^i(1-f)^{s-i}$$
where $b := \lfloor\frac{fsc}{3c}\rfloor = \lfloor\frac{fs}{3}\rfloor$.
When $e=c$, if a delegate has at least 1 slot, then it has at least $c$ slots! For a delegate with one roll, $b=\lfloor7000\frac{6}{7*10^5}\rfloor=0$. So the sum equals the probability that currently such a delegate has no slot at a given level (when $e=1$). That's actually very high: .941764.
So not an interesting case!
### General case
TODO...
| $e$ | proba | frequency |
| -------- | -------- | --- |
| $1$ | $3.882 * 10^{-15}$ | |
| $2$ | $7.694 * 10^{-8}$ | once every 100000 years |
| $4$ | $1.035 * 10^{-4}$ | once every 75 years |
| $8$ | 0.004346 | once every 2 years |
| $16$ | 0.03178 | once every 3 months |
| $32$ | 0.09478 | once per month |
For the frequency: there are 100 * 365 * 24 * 60 * 2 / 8192 = 12832 cycles per century
---
- Le protocol calcule $E_d$ qui donne le nombre d'endorsement attendus pour le cycle pour le délégué $d$. Du coup, quel est la probabilité qu'effectivement, ce délégué lui soit attribué moins de $\frac{2\times E_d}{3}$? Puisque dans ce cas, il serait dans l'impossibilité de toucher ces rewards.
Dans ce cas là, $E_d=\frac{fsc}{e}$ où $e=1$ pour le moment. Dans le pire cas, on a $f=\frac{6000}{700000000}$
La probabilité d'avoir *au moins* $1$ slot dans un bloc c'est $x=1-(1-f)^s$.
<!-- La probabilité que pour exactement $i$ blocs il a au moins un slot est ... -->
La probabilité que son slot apparaisse dans exactement $i$ blocs c'est
$C^i_c x^i(1-x)^{c-i}$. Donc la probabilité que ce délégué ait moins de $E_d$ blocs est
$$\sum_{i=0}^{\frac{2E_d}{3} - 1} C^i_c x^i(1-x)^{c-i}$$.
ce qui équivalent à
$$\sum_{i=0}^{\frac{E_d}{3}} C^i_c ((1-f)^s)^{c-i}((1-(1-f)^s))^{i}$$.
$$E_d=492$$
---
- 1 % of endorsements is lost
- Given n blocks with at least one slot for a delegate $d$, what is the probability that this delegate lose its rewards because it missed $\frac{n}{3}$ endorsements? It is $\frac{1}{100}^{\frac{n}{3}} < 10^{-6}$.
- Likely for small value of $n$ its unlikely
- If $E(Y_e)$, what is the probability that $Y_e < n$
<!--
Currently the variance is $sc f (1-f)$. (See [wiki](https://en.wikipedia.org/wiki/Variance#Commonly_used_probability_distributions))
The variance is $E[X^2] - E[X]^2$. So the new variance is ...
Hm, but then why in the standard case it's $np(1-p)$ and not $np - n^2p^2$?? Maybe because $E[x^2] \neq E[x]$?
-->
<!-- HM, I'VE GOT CONFUSED!
Given $e$, what's the minimum $s$ such that a delegate with a roll has always at least 1 slot per epoch, except say once every `C`, say 100, cycles? In other words, given that there are `C * \frac{c}{e}` epochs in `C` cycles, we want that the probability that a delegate with a roll has 0 slots in an epoch to be smaller than $\frac{e}{c*C}$.
So the constraint is $(1-f)^s < \frac{e}{c*C}$.
Note: There are currently around 220 endorsements per block. If each delegate has a slot, this means we'd have around 400 endorsements per block.
-->
---
## Attic
<!--
## Current state: when $e=1$
Because the protocol uses the expectation, there is a tiny chance that an endorser actually does not have enough slots with respect to the expectation.
Let's denote $Z$ the random variable that the endorser receives exactly $n$ slots in a cycle.
$$P(Z=n)=C_{cs}^nf^n(1-f)^{cs-n}$$
where $cs$ is the total number of slots in a cycle.
The probability that an endorser does not have enough rights to endorse for this cycle is consequently
$$\sum_{n=0}^{\frac{2}{3}492} P(Z=n)$$
To compute this, we use an approximation with a normal distribution: $\mu = csf=491$ and $\sigma = \sqrt{csf(1-f)} = 22.1$.
The result [given by Wolfram](https://www.wolframalpha.com/input?i=standard+normal+cdf+calculator&assumption=%7B%22F%22%2C+%22NormalProbabilities%22%2C+%22z%22%7D+-%3E%22328%22&assumption=%7B%22F%22%2C+%22NormalProbabilities%22%2C+%22mu%22%7D+-%3E%223337420%22&assumption=%7B%22F%22%2C+%22NormalProbabilities%22%2C+%22sigma%22%7D+-%3E%221773%22) is $< 10^{-15}$.
This is unlikely to happen.
## General case
In the general case we have $\mu_e=\frac{csf}{e}=\frac{\mu}{e}$ and $\sigma_e=\sqrt{\frac{csf(1-f)}{e}}=\frac{\sigma}{\sqrt{e}}$.
Taking $e=32$, we get $\mu_{32}=\frac{491}{32}$, $\sigma_{32}=\frac{\sigma}{\sqrt{32}}$. The probability is $0.09$ following [this computation](https://www.wolframalpha.com/input?i=standard+normal+cdf+calculator&assumption=%7B%22F%22%2C+%22NormalProbabilities%22%2C+%22z%22%7D+-%3E%2210.25%22&assumption=%7B%22F%22%2C+%22NormalProbabilities%22%2C+%22mu%22%7D+-%3E%2215.3%22&assumption=%7B%22F%22%2C+%22NormalProbabilities%22%2C+%22sigma%22%7D+-%3E%223.91%22).
- $e=1 \rightarrow < 10^{-15}$
- $e=2 \rightarrow < 10^{-7}$
- $e=4 \rightarrow < 10^{-4}$
| $e$ | proba | frequency |
| -------- | -------- | --- |
| $1$ | $3.882 * 10^{-15}$ | |
| $2$ | $7.694 * 10^{-8}$ | once every 100000 years |
| $4$ | $1.035 * 10^{-4}$ | once every 75 years |
| $8$ | 0.004346 | once every 2 years |
| $16$ | 0.03178 | once every 3 months |
| $32$ | 0.09478 | once per month |
---
The main question we will try to answer is the following one:
:::info
Given a rate (say 1%) of “accidental endorsement misses” in terms of slots (or levels?!) per cycle, how much can e be increased such that the probability of a delegate with minimal stake losing its rewards is “negligeable” (say once per century)?
:::
The rate of 1 % is an upper bound of what we observed emprically on a indexer so far.
We also recall that a delegate loses its rewards if it miss $\frac{2}{3}$ of its *expected* slots in a cycle.
# Notes
We want a DAL committee to be selected every `n` level. This is called
an `epoch`. This idea could be also extended for the Tenderbake
committee.
This document aims to gather pros and cons of using a notion of `epoch` for the Tenderbake committee. There is no hard constraint to make the `epoch` of the DAL committee the same as the Tenderbake committee, but it would be simpler if it was the same.
Three solutions for DAL :
- Have a DAL committee every `epoch` and have a new `dal_attestation`
- Have a DAL committee every `epoch` and keep the current `endorsement/attestation`
- Have a DAL committee/Tenderbake committee
- Si quelqu'un a un seul roll, avec quelle espérance cette personne pourrait être dans un committé ?
- le `7000` est futur-proof pour éviter qu'un mec tout seul puisse faire un fork. La probabilité que 1/3 du stake est byzantin sont dans le comité soit faible.
- On peut passer de `7000` slots à `20_000`, ce qui impliquerait d'avoir plus d'endorsement
- Tzkt a réimplémenté le sampler
- quid des petits bakers
- RPC endorsing_rights qui prend du temps (encore plus à partir de `15`)
- Échantillonne les endorsers pour `10` blocks avec `1000` blocks pour faire rotater le comité / on peut aussi se rappeler du round précédent
- Trop de réorganisation dans les connexions/déconnexions de changer un comité DAL à tous les niveaux dans un réseau ouvert
-->