add comments

This commit is contained in:
Michael Zhu
2019-12-18 11:07:26 -08:00
parent bb3ec970a9
commit 701ba3902c
6 changed files with 28 additions and 7 deletions

View File

@@ -120,7 +120,7 @@ export function StakerMixin<TBase extends Constructor>(Base: TBase): TBase & Con
? StakeStatus.Undelegated
: (Pseudorandom.sample(
[StakeStatus.Undelegated, StakeStatus.Delegated],
[0.2, 0.8],
[0.2, 0.8], // 20% chance of `Undelegated`, 80% chance of `Delegated`
) as StakeStatus);
const from = new StakeInfo(fromStatus, fromPoolId);
@@ -133,7 +133,7 @@ export function StakerMixin<TBase extends Constructor>(Base: TBase): TBase & Con
? StakeStatus.Undelegated
: (Pseudorandom.sample(
[StakeStatus.Undelegated, StakeStatus.Delegated],
[0.2, 0.8],
[0.2, 0.8], // 20% chance of `Undelegated`, 80% chance of `Delegated`
) as StakeStatus);
const to = new StakeInfo(toStatus, toPoolId);

View File

@@ -8,8 +8,10 @@ class PRNGWrapper {
public readonly rng = seedrandom(this.seed);
/*
* Pseudorandom version of _.sample. Picks an element of the given array with uniform probability.
* Return undefined if the array is empty.
* Pseudorandom version of _.sample. Picks an element of the given array. If an array of weights
* is provided, elements of `arr` are weighted according to the value in the corresponding index
* of `weights`. Otherwise, the samples are chosen uniformly at random. Return undefined if the
* array is empty.
*/
public sample<T>(arr: T[], weights?: number[]): T | undefined {
if (arr.length === 0) {
@@ -29,7 +31,9 @@ class PRNGWrapper {
/*
* Pseudorandom version of _.sampleSize. Returns an array of `n` samples from the given array
* (with replacement), chosen with uniform probability. Return undefined if the array is empty.
* (with replacement). If an array of weights is provided, elements of `arr` are weighted
* according to the value in the corresponding index of `weights`. Otherwise, the samples are
* chosen uniformly at random. Return undefined if the array is empty.
*/
public sampleSize<T>(arr: T[], n: number, weights?: number[]): T[] | undefined {
if (arr.length === 0) {
@@ -59,11 +63,14 @@ class PRNGWrapper {
/*
* Returns a function that produces samples from the Kumaraswamy distribution parameterized by
* the given alpha and beta. The Kumaraswamy distribution is like the beta distribution, but
* with a nice closed form.
* with a nice closed form. More info:
* https://en.wikipedia.org/wiki/Kumaraswamy_distribution
* https://www.johndcook.com/blog/2009/11/24/kumaraswamy-distribution/
* Alpha and beta default to 0.2, so that the distribution favors the extremes of the domain.
* The PDF for alpha=0.2, beta=0.2:
* https://www.wolframalpha.com/input/?i=0.2*0.2*x%5E%280.2-1%29*%281-x%5E0.2%29%5E%280.2-1%29+from+0+to+1
*/
public kumaraswamy(this: PRNGWrapper, alpha: Numberish, beta: Numberish): () => BigNumber {
public kumaraswamy(this: PRNGWrapper, alpha: Numberish = 0.2, beta: Numberish = 0.2): () => BigNumber {
const ONE = new BigNumber(1);
return () => {
const u = new BigNumber(this.rng()).modulo(ONE); // u ~ Uniform(0, 1)