Last active
January 10, 2018 01:10
-
-
Save Dorus/f6534f4a340438331c7a4dfca1342249 to your computer and use it in GitHub Desktop.
Test overshoot
This file contains 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
function LL (x) { | |
return 1/(1+10**(-x/400)); | |
} | |
function LLR(W, L, elo0, elo1) { | |
//if (W==0 || L==0) return 0; | |
if (!W) W=1; | |
if (!L) L=1; | |
var N = W + L; | |
var w = W/N, l = L/N; | |
var s = w; | |
var m2 = w; | |
var variance = m2-s**2; | |
var variance_s = variance / N; | |
var s0 = LL(elo0); | |
var s1 = LL(elo1); | |
return (s1-s0)*(2*s-s0-s1)/variance_s/2.0; | |
} | |
//function SPRT(W,L,elo0,elo1) | |
function SPRT(W,L) | |
{ | |
var elo0 = 0, elo1 = 35; | |
var alpha = .05, beta = .05; | |
var LLR_ = LLR(W,L,elo0,elo1); | |
var LA = Math.log(beta/(1-alpha)); | |
var LB = Math.log((1-beta)/alpha); | |
//console.log(LLR_ + " " + LA + " " + LB); | |
if (LLR_ > LB) { | |
return true; | |
} else if (LLR_ < LA) { | |
return false; | |
} else { | |
return null; | |
} | |
} | |
var check = (w, l, r) => { | |
return Math.max(w*2-l, 5) > r | |
} | |
function test(success) { | |
var w = 0, l = 0, r = 0, i | |
var arr = Array(100) | |
arr.fill(null) | |
for (i = 0; i < 3000; i++) { | |
if (w === 220 || SPRT(w, l) !== null || w + l + r > 399) { | |
// console.log("Finished!", w,l,r,i) | |
return ({ w, l, r, i}); | |
} | |
if (check(w,l,r)) { | |
arr.push(Math.random() < success) | |
r++; | |
} else { | |
arr.push(null) | |
} | |
var res = arr.shift() | |
if (res !== null) { | |
if (res) { | |
w++; | |
} else { | |
l++; | |
} | |
r--; | |
} | |
} | |
// console.log("no result!", w,l,r,i) | |
return ({ w, l, r, i}); | |
} | |
function testAvg(success) { | |
var w = 0, ws = 0; // wins and win steps | |
var l = 0, ls = 0, lo = 0; // losses, loss steps and loss overshoot | |
var inc = 0; // inconclusive | |
for (i = 0; i < 2500; i++) { | |
var r = test(success) | |
if (r.w + r.l + r.r > 399) { | |
inc++; | |
} else if (r.w === 220 || SPRT(r.w, r.l)) { | |
w++; ws += r.i | |
} else { | |
l++; ls += r.i; lo += r.r; | |
} | |
} | |
return ({success, w, ws, l, ls, lo, inc}); | |
} | |
function testAvgPrint(success) { | |
var r = testAvg(success); | |
console.log(`wins: ${r.w}`); | |
console.log(`avg win steps: ${r.ws/r.w}`); | |
console.log(`losses: ${r.l}`); | |
console.log(`avg loss steps: ${r.ls/r.l}`); | |
console.log(`avg loss overshoot: ${r.lo/r.l}`); | |
console.log(`inconclusive: ${r.inc}`); | |
} | |
function makeTable() { | |
var arr = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9].map(e => testAvg(e)) | |
console.log("success|wins|losses|inconclusive|avg win steps|avg loss steps|avg loss overshoot") | |
console.log("-- | -- | -- | -- | -- | -- | --") | |
arr.forEach((e) => | |
console.log(`${e.success}|${e.w}|${e.l}|${e.inc}|${(e.ws/e.w).toFixed(2)}|${(e.ls/e.l).toFixed(2)}|${(e.lo/e.l).toFixed(2)}|`) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment