== Listing 4.1: the synthetic task family ==
operations: ['inc', 'add3', 'dbl', 'tri', 'neg', 'sq']; arithmetic modulo 13; program length 4; 1296 programs; 8 contexts
context  target-ops   #correct  #accepted(m=1)  (m=2)  (m=3)  base P(correct)
      0  ['sq', 'tri', 'tri', 'add3']                  5        146        18      5   0.0023
      1  ['add3', 'inc', 'inc', 'inc']                 7         82        10      7   0.0024
      2  ['add3', 'neg', 'tri', 'sq']                  4        146        10      4   0.0006
      3  ['tri', 'tri', 'sq', 'neg']                  12        285        35     12   0.0077
      4  ['tri', 'tri', 'tri', 'sq']                  13        285        41     13   0.0025
      5  ['add3', 'neg', 'neg', 'inc']                 7        104        11      7   0.0027
      6  ['dbl', 'sq', 'tri', 'inc']                   2        139        15      2   0.0020
      7  ['neg', 'neg', 'sq', 'add3']                  6        146        14      6   0.0029
mean base-policy success probability: 0.0029
inc,inc,inc equals add3 on all inputs: True

== Listing 4.2: the policy gradient estimator is unbiased, with or without a baseline ==
J = 0.4983, ||grad J|| = 0.1753
max |finite difference - enumerated gradient| over 20 coordinates: 3.10e-11
max |E[score]| over parameters (should be 0): 1.14e-16
no baseline  : ||estimate - exact|| = 0.0013   (root summed standard error 0.0009)   per-sample variance 0.148
baseline J   : ||estimate - exact|| = 0.0015   (root summed standard error 0.0014)   per-sample variance 0.378
baseline 5.0 : ||estimate - exact|| = 0.0181   (root summed standard error 0.0140)   per-sample variance 39.446

== Listing 4.3: variance of the single-sample estimator for three baselines ==
base policy
  ctx     J      b*     tr Var(b=0)  tr Var(b=J)  tr Var(b=b*)   Var(J)/Var(b*)
    0  0.0023  0.0027        0.008        0.008         0.008     1.0001
    1  0.0024  0.0029        0.008        0.008         0.008     1.0001
    2  0.0006  0.0007        0.002        0.002         0.002     1.0000
    3  0.0077  0.0076        0.022        0.021         0.021     1.0000
after 100 steps
  ctx     J      b*     tr Var(b=0)  tr Var(b=J)  tr Var(b=b*)   Var(J)/Var(b*)
    0  0.4983  0.1082        0.147        0.378         0.128     2.9567
    1  0.0332  0.0214        0.059        0.058         0.058     1.0068
    2  0.0006  0.0007        0.002        0.002         0.002     1.0000
    3  0.2536  0.0584        0.103        0.182         0.095     1.9081

== Listing 4.4: expected maximum of n draws ==
uniform(0,1): n/(n+1) vs numerical integral of 1 - F^n
  n         1        2        4        8       16       64      256
  closed  0.5000   0.6667   0.8000   0.8889   0.9412   0.9846   0.9961
  integral  0.5000   0.6667   0.8000   0.8889   0.9412   0.9846   0.9961
standard normal (unbounded: no saturation), E max vs sqrt(2 ln n)
  E max  0.0000   0.5642   1.0294   1.4236   1.7660   2.3437   2.8269
  sqrt(2 ln n)  0.0000   1.1774   1.6651   2.0393   2.3548   2.8841   3.3302
process-reward distribution of the base policy, context 0:
  values 0.00  0.25  0.50  0.75  1.00
  probs  0.8615  0.1188  0.0161  0.0013  0.0023
  exact E max_n, n = 1, 2, 4, 8, 16, 64, 256: 0.0410, 0.0771, 0.1372, 0.2224, 0.3179, 0.5148, 0.7586
  increasing: True; increments decreasing: True; supremum of reward = 1.00; gap at n=512: 0.1181
estimating E max_n from N=64 samples, 400 repetitions (mean, sd over repetitions)
  n= 4: truth 0.1372   all subsets 0.1385 (sd 0.0421)   disjoint groups 0.1386 (sd 0.0447)
  n=16: truth 0.3179   all subsets 0.3198 (sd 0.0967)   disjoint groups 0.3173 (sd 0.1059)

== Listing 4.5: best-of-n with an imperfect scorer (hypothetical simulation) ==
true quality q ~ N(0,1); scorer = q + 0.5 e, e ~ N(0,1); with probability 0.02 a sample
carries an exploit that adds 4 to the scorer but not to q
exploit probability 0.0
   n    scorer(selected)   true q (selected)   share of selected samples exploited
    1        0.027          0.035 +- 0.050      0.000
    4        1.129          0.909 +- 0.040      0.000
   16        1.970          1.558 +- 0.034      0.000
   64        2.620          2.086 +- 0.030      0.000
  256        3.165          2.536 +- 0.029      0.000
 1024        3.628          2.894 +- 0.029      0.000
exploit probability 0.02
   n    scorer(selected)   true q (selected)   share of selected samples exploited
    1        0.101          0.013 +- 0.051      0.025
    4        1.333          0.840 +- 0.043      0.070
   16        2.578          1.239 +- 0.049      0.257
   64        3.965          1.005 +- 0.058      0.679
  256        5.229          1.083 +- 0.044      0.969
 1024        6.101          1.660 +- 0.033      1.000

== Listing 4.6: group-mean and leave-one-out estimators ==
J = 0.4983, ||grad J||^2 = 0.0307
   G  | factor (mean-incl-self)  | tr Var: formula  empirical  oracle V/G | MSE loo   MSE mean
   2  |   0.512 (theory 0.500)     |     0.4098    0.4120    0.1890  |   0.4121    0.1119
   4  |   0.750 (theory 0.750)     |     0.1313    0.1324    0.0945  |   0.1324    0.0755
   8  |   0.875 (theory 0.875)     |     0.0551    0.0555    0.0473  |   0.0555    0.0429
  16  |   0.940 (theory 0.938)     |     0.0255    0.0255    0.0236  |   0.0255    0.0228
V = tr Var of g (R - J) = 0.378, zeta2 = 0.410 (G=16 call)

== Listing 4.7: standard-deviation normalisation ==
ctx      p      ||normalised est|| / ||grad p||   predicted 1/sqrt(p(1-p))   cosine   all-equal groups at G=8 (empirical, theory)
  0  0.4983          1.995                         2.000            0.9999   0.008  0.008
  1  0.0332          5.519                         5.585            1.0000   0.766  0.764
  2  0.0006         19.709                        42.546            0.9982   0.995  0.996
  3  0.2536          2.304                         2.299            1.0000   0.098  0.096
  4  0.0518          4.478                         4.514            1.0000   0.651  0.654
  5  0.0120          8.791                         9.177            0.9997   0.908  0.908
  6  0.8983          3.329                         3.309            0.9997   0.424  0.424
  7  0.8455          2.747                         2.767            0.9997   0.264  0.261

== Listing 4.8: training against a complete verifier, 20 seeds ==
true success probability (mean +- 95% CI over seeds) after the given number of steps
estimator      step 0        step 100      step 200      step 300
none       0.006+-0.002  0.509+-0.068  0.840+-0.042  0.895+-0.040
ema        0.006+-0.002  0.524+-0.071  0.835+-0.045  0.889+-0.042
mean       0.006+-0.002  0.485+-0.065  0.825+-0.045  0.884+-0.041
loo        0.006+-0.002  0.485+-0.065  0.825+-0.045  0.884+-0.041
std        0.006+-0.002  0.479+-0.064  0.825+-0.045  0.884+-0.041
(G = 8 samples per context per step, Adam step size 0.05, 8 contexts, batch of 64 programs)

== Listing 4.9: reward hacking with a weak verifier (loo advantage, 20 seeds) ==
m = number of test inputs the verifier runs (0..m-1); true reward needs all 13 inputs
  m   proxy@300         true@300          true@100          pass@1 true  base->trained   pass@16 true  base->trained
  1  0.988+-0.001   0.049+-0.029   0.048+-0.023     0.006 -> 0.049          0.084 -> 0.123
  2  0.990+-0.000   0.400+-0.057   0.304+-0.042     0.006 -> 0.400          0.084 -> 0.444
  3  0.884+-0.041   0.884+-0.041   0.485+-0.065     0.006 -> 0.884          0.084 -> 0.903
 13  0.884+-0.041   0.884+-0.041   0.485+-0.065     0.006 -> 0.884          0.084 -> 0.903
best-of-n limit under a weak verifier: P(correct | accepted) at the base policy, mean over contexts
  m=1: mean accept 0.1074, mean correct 0.0029, mean of p/accept 0.028
  m=2: mean accept 0.0080, mean correct 0.0029, mean of p/accept 0.450
  m=3: mean accept 0.0029, mean correct 0.0029, mean of p/accept 1.000

== Listing 4.10: gradient ascent on a flat softmax over programs, verifier m=2 ==
tasks: 150; initial leader of the accepted set is correct in 0.453 (uniform-leader prediction: mean |T|/|A| = 0.445)
final proxy reward: mean 1.0000, minimum 1.0000
final true reward when the leader is wrong (82 tasks): mean 1.21e-06, max 5.11e-05
final true reward when the leader is correct (68 tasks): mean 1.0000
Theorem 4.6 cap |T|/(|T|+1) * P(accepted): largest ratio true/cap over wrong-leader tasks 5.53e-05

== Listing 4.11: UCB1 regret on a 4-armed Bernoulli bandit ==
arm means [0.7, 0.6, 0.5, 0.3], 200 repetitions, horizon 5000
      t   expected regret   bound of Theorem 4.7   regret / ln t
     10             1.51              323.35           0.66
    100            11.59              645.71           2.52
    500            39.85              871.03           6.41
   1000            63.17              968.07           9.14
   5000           140.74             1193.39          16.52
arm   gap   mean pulls   bound 8 ln T/gap^2 + 1.41
  1  0.10       607.2         6815.2
  2  0.20       244.0         1704.8
  3  0.40        78.0          427.3
slope of regret against ln t between t=500 and t=5000: 43.82
Lai-Robbins constant sum gap/KL (coefficient of ln T no consistent policy can beat): 7.90; Theorem 4.7 coefficient sum 8/gap: 140.0

== Listing 4.12: accuracy against verifier calls (60 tasks of length 5, 8 repetitions) ==
base prior P(correct) per task: mean 0.0054, median 0.0030, min 0.00017, max 0.0483
budget (verifier calls)               1         2         4         8        16        32        64       128       256
closed-form best-of-n             0.005     0.011     0.021     0.041     0.077     0.139     0.235     0.370     0.533
sampled best-of-n                 0.010     0.019     0.031     0.052     0.087     0.156     0.237     0.383     0.519
   95% bootstrap CI           0.00-0.02 0.01-0.03 0.02-0.05 0.03-0.08 0.05-0.12 0.11-0.21 0.18-0.30 0.31-0.46 0.44-0.60
UCT outcome, any c                0.002     0.013     0.019     0.040     0.071     0.123     0.206     0.335     0.483
   95% bootstrap CI           0.00-0.01 0.00-0.03 0.01-0.03 0.02-0.06 0.05-0.10 0.09-0.16 0.17-0.25 0.28-0.39 0.41-0.55
UCT process, c=0.1                0.002     0.013     0.019     0.044     0.173     1.000     1.000     1.000     1.000
   95% bootstrap CI           0.00-0.01 0.00-0.03 0.01-0.03 0.02-0.07 0.12-0.22 1.00-1.00 1.00-1.00 1.00-1.00 1.00-1.00
UCT process, c=0.3                0.002     0.013     0.019     0.044     0.081     0.373     1.000     1.000     1.000
   95% bootstrap CI           0.00-0.01 0.00-0.03 0.01-0.03 0.02-0.07 0.05-0.11 0.31-0.44 1.00-1.00 1.00-1.00 1.00-1.00
UCT process, c=0.7                0.002     0.013     0.019     0.040     0.065     0.135     0.256     0.694     1.000
   95% bootstrap CI           0.00-0.01 0.00-0.03 0.01-0.03 0.02-0.06 0.04-0.10 0.10-0.18 0.21-0.31 0.63-0.76 1.00-1.00
UCT process, c=1.4                0.002     0.013     0.019     0.040     0.065     0.104     0.204     0.400     0.623
   95% bootstrap CI           0.00-0.01 0.00-0.03 0.01-0.03 0.02-0.06 0.04-0.10 0.07-0.14 0.15-0.25 0.33-0.46 0.56-0.68
