"""Exact finite checks for Q-var41's scoring and weighted patch estimate. Inputs: var41-prereg.md sections 1, 3--5; var41-price.js fastVariance. Python standard library only. This does not compute Var(41). """ from decimal import Decimal, localcontext from fractions import Fraction as F import json from math import isqrt, prod D = Decimal def rounded(q, digits=12): with localcontext() as ctx: ctx.prec = 70 v = D(q.numerator) / D(q.denominator) if isinstance(q, F) else D(q) return str(v.quantize(D(1).scaleb(-digits))) def predicates(r, b): """Return both literal texts, without choosing or repairing either one.""" r, b = D(r), D(b) assert r.is_finite() and b.is_finite() and b >= 0 lo, hi = r-b, r+b held3 = D('.4013') <= r <= D('.4040') held5 = abs(r-D('.4024')) <= D('.0016') labels = [] if held5: labels.append('HELD') if r < D('.4000'): labels.append('FAILED_LOW') if r > D('.4048'): labels.append('FAILED_HIGH') return dict(r=str(r), b=str(b), interval=[str(lo), str(hi)], section3_member=held3, section5_held=held5, section5_numeric_labels=labels, section5_with_precision_override='INDECISIVE' if b >= D('.0009') else (labels[0] if labels else 'UNSPECIFIED'), section3_whole_interval_inside=D('.4013') <= lo and hi <= D('.4040'), section5_whole_interval_inside=D('.4008') <= lo and hi <= D('.4040')) def primes(n): return [p for p in range(2, n+1) if all(p % d for d in range(2, isqrt(p)+1))] def exact_window(x): W = prod(primes(x)) ps = [p for p in primes(isqrt(W)) if p >= 7] delta = F(2, 30) * prod(F(p-2, p) for p in ps) S2, weighted, unweighted, terms = F(0), F(0), 0, 0 for d in range(1, W): # Enumerate the two allowed mod-30 houses and all local exclusions. local30 = sum((a+d) % 30 in (11, 17) for a in (11, 17)) if not local30: continue rho = F(local30, 30) k = 0 for p in ps: forbidden = {0, (-2) % p, (-d) % p, (-d-2) % p} rho *= F(p-len(forbidden), p) k += d % p in (0, 2, p-2) term = (W-d)*rho S2 += term weighted += k*term unweighted += k terms += 1 E = W*delta V = E+2*S2-E*E kprime = sum((F(3,p) for p in ps), F(0)) kperiod = sum((F(3*p-8,(p-2)**2) for p in ps), F(0)) kactual = weighted/S2 # Compare coefficients under the source's error-expression convention; # this does not certify that floating-point error formula itself. actual_expression = 2*(weighted+3*S2)+2*E*E+2*E projected_expression = 2*(kprime+3)*S2+2*E*E+2*E return dict(x=x, W=W, y=ps[-1], E=rounded(E), variance=rounded(V), mean_unweighted_prime=rounded(kprime), mean_unweighted_window=rounded(F(unweighted,terms)), mean_weighted_period=rounded(kperiod), mean_weighted_window=rounded(kactual), source_expression_over_projection=rounded(actual_expression/projected_expression), projection_underestimates_source_expression=actual_expression>projected_expression), V def brute_level7(): W, M = 210, 30030 allowed = [int(a % 30 in (11, 17) and all(a % p not in (0,p-2) for p in (7,11,13))) for a in range(M)] count = sum(allowed[:W]) total = squares = 0 for t in range(M): total += count squares += count*count count += allowed[(t+W) % M]-allowed[t] return F(squares,M)-F(total,M)**2 def main(): cases = [predicates(r,b) for r,b in [ ('.3999','0'),('.4000','0'),('.4004','0'),('.4008','0'), ('.4010','0'),('.4013','0'),('.4040','0'),('.4044','0'), ('.4048','0'),('.4049','0'),('.4014','.0008'),('.4024','.0009')]] assert predicates('.4010','0')['section5_held'] assert not predicates('.4010','0')['section3_member'] assert predicates('.4004','0')['section5_with_precision_override']=='UNSPECIFIED' assert predicates('.4044','0')['section5_with_precision_override']=='UNSPECIFIED' assert not predicates('.4014','.0008')['section5_whole_interval_inside'] w7,v7=exact_window(7) w11,v11=exact_window(11) brute=brute_level7() assert brute == v7 assert w11['projection_underestimates_source_expression'] # Independently enumerate local weighted means, rather than assuming the # closed form being tested, for every prime 7..997. local_checks=0 for p in primes(997): if p<7: continue weights=[p-len({0,(-2)%p,(-d)%p,(-d-2)%p}) for d in range(p)] enumerated=F(sum(weights[d] for d in (0,2,p-2)),sum(weights)) assert enumerated == F(3*p-8,(p-2)**2) assert enumerated-F(3,p) == F(4*(p-3),p*(p-2)**2)>0 local_checks+=1 with localcontext() as ctx: ctx.prec=60 W=D(304250263527210) ab=lambda v: D(1)/D(v).ln().ln() a7,a13,a37,a41=map(ab,[210,30030,7420738134810,304250263527210]) arith=dict(u41=rounded(W.ln()/D(17442769).ln()), remaining_over_full10_span=rounded(a41/(a7-a41)), remaining_over_x13_to41_span=rounded(a41/(a13-a41)), remaining_over_new_step=rounded(a41/(a37-a41)), extra_lever_arm_fraction=rounded((a37-a41)/(a13-a37)), projected_hours_at_5_cores=str(D('239.7')/5)) result=dict(question='Q-var41', no_var41_measurement=True, literal_scoring_cases=cases, arithmetic=arith, exact_window_checks=[w7,w11], independent_full_rotation_check=dict(x=7,period=30030,window=210,variance=str(brute),matches=True), local_prime_enumerations=local_checks) print(json.dumps(result,indent=2,sort_keys=True)) if __name__=='__main__': main()