PureOne's picture
Upload Nheixuri III research dataset release
ef1ca93 verified
Raw History Blame Contribute Delete
8.16 kB
#!/usr/bin/env python3
"""Finite illustration of unrestricted private randomness and public quantization.
U is genuinely Uniform[0,1); finite event probabilities are rational interval
lengths, and losses are integrated by closed power antiderivatives. No Monte
Carlo sample is substituted for the continuous experiment. This example is
not a verification of the manuscript's universal theorem.
"""
from fractions import Fraction as F
from pathlib import Path
import json
import mpmath as mp
ROOT = Path(__file__).resolve().parent
mp.mp.dps = 80
mp.iv.dps = 60
SLOPE = F(1, 1000)
D1, D2 = F(1, 4), F(1, 8)
X1, ALPHA = F(1, 2), F(11, 10)
P = mp.mpf('0.5')
def asmp(x):
return mp.mpf(x.numerator) / x.denominator
def asiv(x):
return mp.iv.mpf(x.numerator) / x.denominator
def target(x):
return SLOPE * min(x, 1 - x)
def quantize(a, delta):
return delta * (a // delta)
def policy(published):
# Equality branches make this policy discontinuous. Against raw U,
# the final branch generates a continuum of next inputs.
if published < F(1, 4):
return F(1, 4)
if published == F(1, 4):
return F(3, 4)
if published == F(1, 2):
return F(1)
return min(F(1), max(F(0), published))
def wrapped(u):
# Preserve the raw first action u inside the private simulation.
raw1 = u
public1 = quantize(raw1, D1)
x2 = policy(public1)
raw2 = ALPHA * u + target(X1) + x2 / 5
public2 = quantize(raw2, D2)
return x2, raw1, raw2, public1, public2
def original_with_transformed_policy(u):
raw1 = u
x2 = policy(quantize(raw1, D1))
raw2 = ALPHA * u + target(X1) + x2 / 5
return x2, raw1, raw2
def power_integral(a, b, left, right):
"""Integral |a*u+b|**p, a>0, using its closed antiderivative."""
a, b = asmp(a), asmp(b)
def primitive(u):
z = a * asmp(u) + b
return mp.sign(z) * abs(z) ** (P + 1) / (a * (P + 1))
return primitive(right) - primitive(left)
def atoms():
out = []
for j in range(4):
left, right = F(j, 4), F(j + 1, 4)
b1 = F(j, 4)
x2 = policy(b1)
beta = target(X1) + x2 / 5
k0 = (ALPHA * left + beta) // D2 - 1
k1 = (ALPHA * right + beta) // D2 + 2
cuts = {left, right}
for k in range(k0, k1 + 1):
crossing = (k * D2 - beta) / ALPHA
if left < crossing < right:
cuts.add(crossing)
cuts = sorted(cuts)
for a, b in zip(cuts, cuts[1:]):
mid = (a + b) / 2
x2, raw1, raw2, public1, public2 = wrapped(mid)
out.append((a, b, x2, beta, public1, public2))
assert sum(b - a for a, b, *_ in out) == 1
return out
def polynomial_multiply_linear(coeffs, root):
z = root * 0
result = [z for _ in range(len(coeffs) + 1)]
for k, c in enumerate(coeffs):
result[k] -= root * c
result[k + 1] += c
return result
def clone_certificate(nodes):
"""g=P(sin x), degree<=4, exact Lagrange formula at the nodes."""
values = [target(x) for x in nodes]
zs = [mp.sin(asmp(x)) for x in nodes]
zi = [mp.iv.sin(asiv(x)) for x in nodes]
c = [mp.mpf(0) for _ in nodes]
ci = [mp.iv.mpf(0) for _ in nodes]
for i in range(len(nodes)):
basis, ibasis = [mp.mpf(1)], [mp.iv.mpf(1)]
denominator, idenominator = mp.mpf(1), mp.iv.mpf(1)
for j in range(len(nodes)):
if j == i:
continue
basis = polynomial_multiply_linear(basis, zs[j])
ibasis = polynomial_multiply_linear(ibasis, zi[j])
denominator *= zs[i] - zs[j]
idenominator *= zi[i] - zi[j]
for k in range(len(nodes)):
c[k] += asmp(values[i]) * basis[k] / denominator
ci[k] += asiv(values[i]) * ibasis[k] / idenominator
residual = max(abs(mp.polyval(c[::-1], z) - asmp(v))
for z, v in zip(zs, values))
assert residual < mp.mpf('1e-65')
# |(P(sin x))'| <= sum_{k>=1} k |c_k| on [0,1].
norm_bound = sum(k * abs(ci[k]) for k in range(1, len(ci)))
assert norm_bound.b < mp.iv.mpf('0.212').a
return {
'nodes': [str(x) for x in nodes],
'values': [str(x) for x in values],
'coordinate': 'sin(x)',
'polynomial_degree': len(nodes) - 1,
'exact_definition': 'sum_i v_i product_{j!=i} (sin(x)-sin(u_j))/(sin(u_i)-sin(u_j))',
'coefficient_bound_interval': str(norm_bound),
'proved_coefficient_norm_bound_below': 0.212,
'unit_derivative_budget_certified_by_coefficient_inequality': True,
'node_residual_80_digit_evaluation': mp.nstr(residual, 12),
'note': 'Exact interpolation follows algebraically from the cardinal formula. The displayed interval calculation encloses its coefficient norm bound; this is not a proof-assistant certificate.'
}
def main():
events = atoms()
raw_loss = mp.mpf(0)
rounded_loss = mp.mpf(0)
perturbation = mp.mpf(0)
rows, nodes = [], {F(0), X1}
correspondence_checks = 0
for left, right, x2, beta, b1, b2 in events:
nodes.add(x2)
prob = right - left
y1, y2 = target(X1), target(x2)
rounded_loss += asmp(prob) * (abs(asmp(b1-y1))**P + abs(asmp(b2-y2))**P)
raw_loss += power_integral(F(1), -y1, left, right)
raw_loss += power_integral(ALPHA, beta-y2, left, right)
perturbation += power_integral(F(1), -b1, left, right)
perturbation += power_integral(ALPHA, beta-b2, left, right)
for weight in (F(1, 4), F(1, 2), F(3, 4)):
u = left + weight * (right-left)
w = wrapped(u)
assert w[:3] == original_with_transformed_policy(u)
assert w[3:] == (b1, b2)
correspondence_checks += 1
rows.append({'u_left': str(left), 'u_right': str(right),
'probability': str(prob), 'public_1': str(b1),
'query_2': str(x2), 'public_2': str(b2)})
uniform_bound = asmp(D1)**P + asmp(D2)**P
assert rounded_loss <= raw_loss + perturbation + mp.mpf('1e-60')
assert perturbation <= uniform_bound
# Demonstrate why the transformed policy is required.
witness_u = F(4, 5)
assert policy(witness_u) != policy(quantize(witness_u, D1))
certificate = clone_certificate(sorted(nodes))
report = {
'status': 'all quantized illustration checks passed',
'scope': 'One two-scored-round continuous-seed example; no Monte Carlo and no claim of formal verification of the general theorem.',
'private_seed': 'Uniform[0,1)',
'event_interval_convention': '[left,right); endpoints have zero mass',
'loss_power': 0.5,
'source_target': 'f(x)=0.001 min(x,1-x)',
'source_derivative_sup': 0.001,
'public_grid_steps': [str(D1), str(D2)],
'public_path_event_count': len(events),
'total_event_probability': '1',
'expected_original_loss_with_transformed_policy': mp.nstr(raw_loss, 30),
'expected_quantized_loss': mp.nstr(rounded_loss, 30),
'expected_rounding_perturbation_bound': mp.nstr(perturbation, 30),
'uniform_rounding_bound_sum_delta_to_p': mp.nstr(uniform_bound, 30),
'loss_comparison_passed': True,
'policy_correspondence_rational_checks': correspondence_checks,
'untransformed_policy_witness': {'u': str(witness_u),
'raw_next_input': str(policy(witness_u)),
'rounded_next_input': str(policy(quantize(witness_u,D1)))},
'analytic_clone': certificate,
'cloned_path_probability': '1',
'cloned_public_law_total_variation': 0,
'cloning_justification': 'All possible queried inputs belong to the five interpolation nodes; the same seed therefore reproduces the full public and private trajectory exactly by the cardinal identities.',
'events': rows,
'mpmath_version': mp.__version__
}
path = ROOT / 'verification_quantized.json'
path.write_text(json.dumps(report, indent=2) + '\n')
print(json.dumps({k:v for k,v in report.items() if k!='events'}, indent=2))
if __name__ == '__main__':
main()