omega-feel
This project has not described itself yet.
Brought by drakestapleton
omega-feel / tests / wolfram-rules-sim.wl
| 1 | (* wolfram-rules-sim.wl: 2000 simulated duels per case under the full rules of SPEC.md 3.3 (rule 1, then |
| 2 | rule 2 with its halves and the mean rule). Times: incumbent mean 1, challenger mean 1-g. Noise per |
| 3 | launch: lognormal with sigma s. Outside stalls: each launch of either arm, with probability q, takes |
| 4 | 50% longer. Optional stall of the challenger's own: with probability tq its launch takes `tail` longer |
| 5 | (tail is in units of the incumbent's mean launch). |
| 6 | Outcome codes: 1 adopt (count and both halves), 2 adopt by mean, 3 reject, 4 draw. |
| 7 | Each row also gives the one-sided 95% upper bound (Clopper-Pearson) of the adopt rate: a count of 0 |
| 8 | in 2000 bounds the rate by 1 - (1/20)^(1/2000), not by 0. |
| 9 | SeedRandom[20261118]. The final value is one string; written to a file with a final newline it is |
| 10 | tests/wolfram-rules-sim-result.txt. These are rates in this simulation of independent lognormal |
| 11 | times, not rates on a chip. *) |
| 12 | SeedRandom[20261118]; |
| 13 | H = 12; NMIN = 32; NMAX = 256; |
| 14 | one[g_, s_, q_, tq_, tail_] := Module[{tI, tC, d, cI, cC, idx, hI, hC, ok2, h1, h2, m1, m2}, |
| 15 | tI = RandomVariate[LogNormalDistribution[-s^2/2, s], NMAX] (1 + 0.5 UnitStep[q - RandomReal[1, NMAX]]); |
| 16 | tC = (1 - g) RandomVariate[LogNormalDistribution[-s^2/2, s], NMAX] (1 + 0.5 UnitStep[q - RandomReal[1, NMAX]]) + tail UnitStep[tq - RandomReal[1, NMAX]]; |
| 17 | d = Accumulate[Sign[tI - tC]]; cI = Accumulate[tI]; cC = Accumulate[tC]; |
| 18 | ok2 = UnitStep[Range[NMAX] - NMIN] UnitStep[-d - H] UnitStep[cC - cI]; |
| 19 | idx = FirstPosition[ok2, 1, {0}][[1]]; |
| 20 | If[idx > 0, {3, idx}, |
| 21 | hI = cI[[NMAX/2]]; hC = cC[[NMAX/2]]; |
| 22 | h1 = hC < hI; h2 = (cC[[NMAX]] - hC) < (cI[[NMAX]] - hI); |
| 23 | m1 = 64 hC < 63 hI; m2 = 64 (cC[[NMAX]] - hC) < 63 (cI[[NMAX]] - hI); |
| 24 | {Which[h1 && h2 && d[[NMAX]] >= H, 1, m1 && m2, 2, True, 4], NMAX}]]; |
| 25 | upper[k_, m_] := Which[k == 0, 1 - (1/20)^(1/m), k == m, 1, True, Quantile[BetaDistribution[k + 1, m - k], 95/100]]; |
| 26 | trial[g_, s_, q_, tq_, tail_, m_] := Module[{r = Table[one[g, s, q, tq, tail], {m}], c}, |
| 27 | c = Counts[r[[All, 1]]]; |
| 28 | {g, N[Lookup[c, 1, 0]/m], N[Lookup[c, 2, 0]/m], N[Lookup[c, 3, 0]/m], N[Lookup[c, 4, 0]/m], N[Mean[r[[All, 2]]]], N[upper[Lookup[c, 1, 0] + Lookup[c, 2, 0], m]]}]; |
| 29 | m = 2000; |
| 30 | t1 = Table[trial[g, 0.03, 0.01, 0, 0, m], {g, {-0.05, -0.02, -0.01, 0., 0.01, 0.02, 0.05}}]; |
| 31 | t2 = Table[trial[g, 0.10, 0.05, 0, 0, m], {g, {-0.05, -0.02, 0., 0.02, 0.05, 0.10}}]; |
| 32 | (* true mean change of the challenger with its own stalls: (0.95 (1 + q/2) + tail tq) / (1 + q/2) - 1, q = 0.01 *) |
| 33 | t3 = Table[Append[trial[0.05, 0.02, 0.01, tq, 9., m], N[(0.95 (1 + 0.01/2) + 9 tq)/(1 + 0.01/2) - 1]], {tq, {1/8, 1/32, 1/128, 1/512}}]; |
| 34 | f[x_] := ToString[SetPrecision[x, 6], FortranForm]; |
| 35 | StringRiffle[Join[ |
| 36 | {"# 2000 duels per row; columns: g (negative = challenger slower) P(adopt by count) P(adopt by mean) P(reject) P(draw) mean pairs 95% upper bound of P(either adoption)", |
| 37 | "# noise 3%, outside stalls on 1% of launches"}, |
| 38 | Map[StringRiffle[f /@ #, " "] &, t1], |
| 39 | {"# noise 10%, outside stalls on 5% of launches"}, |
| 40 | Map[StringRiffle[f /@ #, " "] &, t2], |
| 41 | {"# challenger 5% faster in the usual case (noise 2%, outside stalls 1%) with a stall of its own that adds 9 incumbent launches, once in 1/tq launches; g = 0.05; last column: its true mean change (+ is slower)", |
| 42 | "# columns: tq P(adopt by count) P(adopt by mean) P(reject) P(draw) mean pairs 95% upper bound of P(either adoption) true mean change"}, |
| 43 | Map[StringRiffle[f /@ #, " "] &, MapThread[Prepend[Rest[#1], #2] &, {t3, {1/8, 1/32, 1/128, 1/512}}]]], "\n"] |