generated from privacycg/template
-
Notifications
You must be signed in to change notification settings - Fork 7
Miscellaneous simulator improvements #238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
apasel422
wants to merge
16
commits into
w3c:main
Choose a base branch
from
apasel422:tidy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+219
−35
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
cb347df
Add note about deployed simulator
apasel422 cdccaf0
Add helper for checking random range
apasel422 d56f83c
Make some arrays readonly
apasel422 37ee08a
Export fairlyAllocateCredit for testing
apasel422 a182c22
Add basic tests for fairly allocate credit
apasel422 7f5cb0c
Check expected values of fairlyAllocateCredit with confidence intervals
apasel422 556b8a6
Update impl/src/backend.test.ts
apasel422 c798060
Update impl/src/backend.test.ts
apasel422 88f1936
s/ppf/normalPpf/
apasel422 2f85c2a
Strict less-than for diff
apasel422 9140435
Update impl/src/backend.test.ts
apasel422 e1e482a
Update impl/src/backend.test.ts
apasel422 7e0c78f
Update impl/src/backend.test.ts
apasel422 0dcd754
Update impl/src/backend.test.ts
apasel422 8a58ac0
Manual fixes
apasel422 7e1093e
Import
apasel422 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
import { fairlyAllocateCredit } from "./backend"; | ||
|
||
import { strict as assert } from "assert"; | ||
import test from "node:test"; | ||
import { inverseErrorFunction } from "simple-statistics"; | ||
|
||
interface FairlyAllocateCreditTestCase { | ||
name: string; | ||
credit: number[]; | ||
value: number; | ||
needsRand?: boolean; | ||
} | ||
|
||
function noRand(): number { | ||
throw new Error("no rand expected"); | ||
} | ||
|
||
type Interval = [min: number, max: number]; | ||
|
||
// https://en.wikipedia.org/wiki/Probit | ||
function normalPpf(q: number, stdDev: number): number { | ||
return stdDev * Math.sqrt(2) * inverseErrorFunction(2 * q - 1); | ||
} | ||
|
||
const minNForIntervalApprox = 1000; | ||
|
||
function getIntervalApprox(n: number, p: number, alpha: number): Interval { | ||
if (n < minNForIntervalApprox) { | ||
throw new RangeError(`n must be >= ${minNForIntervalApprox}`); | ||
} | ||
|
||
// Approximates a binomial distribution with a normal distribution which is a bit | ||
// simpler as it is symmetric. | ||
const mean = n * p; | ||
const variance = mean * (1 - p); | ||
const diff = normalPpf(1 - alpha / 2, Math.sqrt(variance)); | ||
return [mean - diff, mean + diff]; | ||
} | ||
|
||
function getAllIntervals( | ||
n: number, | ||
creditFractions: readonly number[], | ||
alphaTotal: number, | ||
): Interval[] { | ||
// We are testing one hypothesis per dimension, so divide `alphaTotal` by | ||
// the number of dimensions: https://en.wikipedia.org/wiki/Bonferroni_correction | ||
const alpha = alphaTotal / creditFractions.length; | ||
apasel422 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return creditFractions.map((cf) => getIntervalApprox(n, cf, alpha)); | ||
} | ||
|
||
function runFairlyAllocateCreditTest( | ||
tc: Readonly<FairlyAllocateCreditTestCase>, | ||
): void { | ||
// TODO: replace with precise sum | ||
const sumCredit = tc.credit.reduce((a, b) => a + b, 0); | ||
const normalizedFloatCredit = tc.credit.map((item) => item / sumCredit); | ||
|
||
const [rand, k] = tc.needsRand ? [Math.random, 1000] : [noRand, 1]; | ||
|
||
const totals = new Array<number>(tc.credit.length).fill(0); | ||
|
||
for (let n = 0; n < k; ++n) { | ||
const actualCredit = fairlyAllocateCredit(tc.credit, tc.value, rand); | ||
|
||
assert.equal(actualCredit.length, tc.credit.length); | ||
|
||
for (const [j, actual] of actualCredit.entries()) { | ||
assert.ok(Number.isInteger(actual)); | ||
|
||
const normalized = normalizedFloatCredit[j]! * tc.value; | ||
const diff = Math.abs(actual - normalized); | ||
assert.ok( | ||
diff < 1, | ||
`credit error >= 1: actual=${actual}, normalized=${normalized}`, | ||
); | ||
|
||
totals[j]! += actual / tc.value; | ||
} | ||
|
||
assert.equal( | ||
// TODO: replace with precise sum | ||
apasel422 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
actualCredit.reduce((a, b) => a + b, 0), | ||
tc.value, | ||
`actual credit does not sum to value: ${actualCredit.join(", ")}`, | ||
); | ||
} | ||
|
||
const alpha = 0.00001; // Probability of test failing at random. | ||
|
||
const intervals: Interval[] = | ||
k > 1 | ||
? getAllIntervals( | ||
k, | ||
normalizedFloatCredit.map((c) => c - Math.floor(c)), | ||
alpha, | ||
) | ||
: normalizedFloatCredit.map((c) => [c, c]); | ||
|
||
for (const [j, total] of totals.entries()) { | ||
const [min, max] = intervals[j]!; | ||
assert.ok( | ||
total >= min && total <= max, | ||
`total for credit[${j}] ${total} not in ${1 - alpha} confidence interval [${min}, ${max}]`, | ||
); | ||
} | ||
} | ||
|
||
const testCases: FairlyAllocateCreditTestCase[] = [ | ||
{ | ||
name: "credit-equal-to-value", | ||
credit: [1], | ||
value: 1, | ||
needsRand: false, | ||
}, | ||
{ | ||
name: "credit-less-than-value", | ||
credit: [2], | ||
value: 3, | ||
needsRand: false, | ||
}, | ||
{ | ||
name: "credit-less-than-1", | ||
credit: [0.25], | ||
value: 4, | ||
needsRand: false, | ||
}, | ||
{ | ||
name: "2-credit-divides-value-evenly", | ||
credit: [3, 1], | ||
value: 8, | ||
needsRand: false, | ||
}, | ||
{ | ||
name: "3-credit-divides-value-evenly", | ||
credit: [2, 1, 1], | ||
value: 8, | ||
needsRand: false, | ||
}, | ||
{ | ||
name: "2-credit-divides-value-unevenly", | ||
credit: [1, 1], | ||
value: 5, | ||
needsRand: true, | ||
}, | ||
{ | ||
name: "3-credit-divides-value-unevenly", | ||
credit: [2, 1, 1], | ||
value: 5, | ||
needsRand: true, | ||
}, | ||
]; | ||
|
||
void test("fairlyAllocateCredit", async (t) => { | ||
await Promise.all( | ||
testCases.map((tc) => | ||
t.test(tc.name, () => runFairlyAllocateCreditTest(tc)), | ||
), | ||
); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.