-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvote-duration.js
105 lines (97 loc) · 2.86 KB
/
vote-duration.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const BinomialCDF = require('binomial-cdf');
// Decision rule for simple majority.
function SimpleMajority(y, n) {
if (y < 0 || n < 0) {
throw 'Negative number of votes is not allowed.';
}
return y > n;
}
// Decision rule for supermajority.
function SuperMajority(y, n) {
if (y < 0 || n < 0) {
throw 'Negative number of votes is not allowed.';
}
if (y === 0 && n === 0) {
return false;
}
return y >= 2 * n;
}
// Calculate how many more YES votes are needed to change the outcome.
function HowManyMoreYesVotes(y, n, DecisionRule) {
if (DecisionRule(y, n)) {
// Vote is already passing. Zero extra votes needed.
return 0;
}
const maxExtraVotes = 2 * (y + n) + 2;
for (let i = 0; i < maxExtraVotes; i++) {
const outcome = DecisionRule(y + i, n);
if (outcome) {
return i;
}
}
return 0;
}
// Calculate how many more NO votes are needed to change the outcome.
function HowManyMoreNoVotes(y, n, DecisionRule) {
if (!DecisionRule(y, n)) {
// Vote is already rejected. Zero extra votes needed.
return 0;
}
const maxExtraVotes = y + n + 1;
for (let i = 0; i < maxExtraVotes; i++) {
const outcome = DecisionRule(y, n + i);
if (!outcome) {
return i;
}
}
return 0;
}
// Calculate the minimum number of votes needed to change the outcome.
function VoteMargin(y, n, DecisionRule) {
if (DecisionRule(y, n)) {
return HowManyMoreNoVotes(y, n, DecisionRule);
} else {
return HowManyMoreYesVotes(y, n, DecisionRule);
}
}
// Estimate the probability that a vote outcome will change. This is used to determine when
// a vote can be "called" early, if the probability of an outcome flip is low enough.
function ProbabilityOfVoteOutcomeChange(numVoters, yesVotes, noVotes, daysSinceLastChange, baselineDurationDays, DecisionRule) {
const n = baselineDurationDays;
const d = daysSinceLastChange;
if (d >= n) {
return 0;
}
// Integral of triangle distribution. 0 < p < 1
const p = (n - d) * (n - d) / (n * n);
const undecidedVoters = numVoters - yesVotes - noVotes;
const margin = VoteMargin(yesVotes, noVotes, DecisionRule);
if (margin > undecidedVoters) {
return 0;
}
return 1 - BinomialCDF(margin, undecidedVoters, p);
}
function EstimateVoteDuration(numVoters, yesVotes, noVotes, baselineDurationDays, targetErrorProbability, DecisionRule) {
const oneSecondIsh = 0.00001;
let lo = 0;
let hi = 7;
while (hi - lo > oneSecondIsh) {
const mid = (hi + lo) / 2;
const p = ProbabilityOfVoteOutcomeChange(numVoters, yesVotes, noVotes, mid, baselineDurationDays, DecisionRule);
if (p < targetErrorProbability) {
hi = mid;
} else {
lo = mid;
}
}
return hi;
}
module.exports = {
EstimateVoteDuration,
HowManyMoreNoVotes,
HowManyMoreYesVotes,
ProbabilityOfVoteOutcomeChange,
SimpleMajority,
SuperMajority,
VoteMargin,
};