physics.policy_gradient
physics.policy_gradient
Policy-gradient training on verifiable rewards: coefficients, forks, and clipping.
Closed forms behind reinforcement learning of a policy from rewards that an automated check computes (added 2026-10-20). Each takes stated quantities: the rewards of one group of rollouts, a trajectory’s turns and the boundaries at which continuations are forked, sampled continuation pass counts, or one token’s old probability under a ratio clip. None models a trained network’s learning dynamics. The solvers in engine/solvers/reinforcement.py compose them with the shared helpers in physics/agents.py (calc_pass_posterior for the false-accept share, calc_mixed_outcome_fraction for reward contrast, calc_independent_success and calc_short_circuit_cost for repeated verification).
Functions
| Name | Description |
|---|---|
| calc_binary_mean_se_bound | Bound the standard error of an independent binary sample mean. |
| calc_continuation_fork_tokens | Count generated tokens for independent continuation forks. |
| calc_group_relative_coefficients | Normalize rollout rewards by their within-group dispersion. |
| calc_ratio_clip_room | Return the upper ratio-clip threshold and its absolute increment. |
calc_binary_mean_se_bound
physics.policy_gradient.calc_binary_mean_se_bound(samples)Bound the standard error of an independent binary sample mean.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| samples | int | Number of independent Bernoulli samples; at least one. | required |
Returns
| Name | Type | Description |
|---|---|---|
| float | Maximum standard error in probability units. |
Notes
Equation: SE <= 1 / (2*sqrt(samples)).
Assumptions: Samples are independent and identically distributed Bernoulli outcomes.
Excludes: Dependence, selection bias, confidence-interval coverage, and finite-population correction.
The difference of two independent means with equal sample counts has a standard-error bound sqrt(2) times this value.
References
Derivation: Bernoulli variance p*(1 - p) is maximized at p = 1/2, then divide that variance by the sample count.
Examples
>>> calc_binary_mean_se_bound(4) == calc_binary_mean_se_bound(1) / 2
Truecalc_continuation_fork_tokens
physics.policy_gradient.calc_continuation_fork_tokens(
turns,
tokens_per_turn,
branches,
boundaries=None,
)Count generated tokens for independent continuation forks.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| turns | int | Turns in the base trajectory; at least two. | required |
| tokens_per_turn | int | Generated tokens per turn, assumed fixed and nonnegative. | required |
| branches | int | Continuations sampled at each selected boundary; nonnegative. | required |
| boundaries | iterable of int or None | Distinct interior turn indices between 1 and turns - 1 inclusive; None selects every interior boundary. |
None |
Returns
| Name | Type | Description |
|---|---|---|
| tuple | Base generated-token count, total fork count, and total generated-token count. |
Notes
Equation: total = H*T + m*T*sum_k(H - k); all boundaries give H*T*(1 + m*(H - 1)/2).
Assumptions: Each branch independently generates every remaining turn, and all turns have the same token cost.
Excludes: Shared generated suffixes, early termination, input prefill, and caching.
References
Derivation: Add the H*T base trajectory to m continuations of length H - k at every selected boundary k.
Examples
>>> base, forks, total = calc_continuation_fork_tokens(3, 10, 2)
>>> (base, forks, total) == (3 * 10, 2 * 2, 3 * 10 + 2 * 10 * (2 + 1))
Truecalc_group_relative_coefficients
physics.policy_gradient.calc_group_relative_coefficients(
rewards,
stabilizer=0.0,
bessel=False,
)Normalize rollout rewards by their within-group dispersion.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| rewards | iterable of float | Dimensionless scalar rewards for one group; at least one value. | required |
| stabilizer | float | Nonnegative denominator offset in reward units; defaults to zero. | 0.0 |
| bessel | bool | Use the sample standard deviation with divisor G - 1; requires G >= 2. | False |
Returns
| Name | Type | Description |
|---|---|---|
| tuple | Mean reward, standard deviation, and list of dimensionless normalized coefficients. |
Notes
Equation: A_i = (r_i - mean(r)) / (std(r) + stabilizer).
Assumptions: Rewards share one scale; the population divisor is G unless bessel is true.
Excludes: Policy updates, clipping, learning dynamics, and any claim that reward normalization improves quality.
Tied rewards have zero dispersion. With zero stabilizer the division is undefined and raises ZeroDivisionError; a positive stabilizer produces zero coefficients.
References
Derivation: Center each reward and divide by its group dispersion; the sample variant replaces G with G - 1 in the variance denominator.
Examples
>>> mean, std, coefficients = calc_group_relative_coefficients([0, 1])
>>> coefficients == [-1.0, 1.0]
True
>>> calc_group_relative_coefficients([1, 1], stabilizer=1)[2] == [0.0, 0.0]
Truecalc_ratio_clip_room
physics.policy_gradient.calc_ratio_clip_room(probability, epsilon)Return the upper ratio-clip threshold and its absolute increment.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| probability | float | Old token probability between zero and one. | required |
| epsilon | float | Nonnegative dimensionless upper-ratio allowance. | required |
Returns
| Name | Type | Description |
|---|---|---|
| tuple | Unclamped probability threshold (1 + epsilon)p and increment epsilonp. |
Notes
Equation: ceiling = (1 + epsilon)*probability; rise = epsilon*probability.
Assumptions: The upper ratio threshold is supplied as a hypothetical bound on a probability update.
Excludes: Renormalization, clipping at probability one, and a guarantee that PPO clipping constrains actual updates.
A clipped policy objective is not a hard probability constraint. This function computes the ratio threshold only, and the threshold can exceed one.
References
Derivation: Multiply the old probability by an allowed ratio and subtract the old value to express the increment.
Examples
>>> ceiling, rise = calc_ratio_clip_room(0.5, 0.25)
>>> ceiling - 0.5 == rise
True