<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://invarians.org/index.php?action=history&amp;feed=atom&amp;title=The_Principia_Machina%2FTPC-PY%2Ftpc_beta.py</id>
	<title>The Principia Machina/TPC-PY/tpc beta.py - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://invarians.org/index.php?action=history&amp;feed=atom&amp;title=The_Principia_Machina%2FTPC-PY%2Ftpc_beta.py"/>
	<link rel="alternate" type="text/html" href="https://invarians.org/index.php?title=The_Principia_Machina/TPC-PY/tpc_beta.py&amp;action=history"/>
	<updated>2026-10-21T09:19:36Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.46.2</generator>
	<entry>
		<id>https://invarians.org/index.php?title=The_Principia_Machina/TPC-PY/tpc_beta.py&amp;diff=4199&amp;oldid=prev</id>
		<title>BFP Rauch: 1 revision imported</title>
		<link rel="alternate" type="text/html" href="https://invarians.org/index.php?title=The_Principia_Machina/TPC-PY/tpc_beta.py&amp;diff=4199&amp;oldid=prev"/>
		<updated>2026-09-28T04:37:36Z</updated>

		<summary type="html">&lt;p&gt;1 revision imported&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw-interface=&quot;&quot;&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 04:37, 28 September 2026&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-notice&quot; lang=&quot;en&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(No difference)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>BFP Rauch</name></author>
	</entry>
	<entry>
		<id>https://invarians.org/index.php?title=The_Principia_Machina/TPC-PY/tpc_beta.py&amp;diff=4198&amp;oldid=prev</id>
		<title>BFP Rauch: TPC-PY source tpc_beta.py (0.1.0), SHA-256 4f047f1c7c0a6891; conformed TPS 0.3.0; PROPOSED, not declared (CO-8)</title>
		<link rel="alternate" type="text/html" href="https://invarians.org/index.php?title=The_Principia_Machina/TPC-PY/tpc_beta.py&amp;diff=4198&amp;oldid=prev"/>
		<updated>2026-09-27T15:43:18Z</updated>

		<summary type="html">&lt;p&gt;TPC-PY source tpc_beta.py (0.1.0), SHA-256 4f047f1c7c0a6891; conformed TPS 0.3.0; PROPOSED, not declared (CO-8)&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;quot;&amp;quot;&amp;quot;tpc_beta 0.1.0 (DRAFT): the Beta layer&amp;#039;s arithmetic, shared by the&lt;br /&gt;
user score (tpc_sponsor, D160) and the agent score (CR11, another&lt;br /&gt;
module).&lt;br /&gt;
&lt;br /&gt;
Reference implementation, Python standard library only. DATA for landing&lt;br /&gt;
round 8 of branch tpm_project_closeout; not a declared TPC-PY version and&lt;br /&gt;
not TPX code. Every function is a pure function of its arguments and is&lt;br /&gt;
written with only +, -, *, /, comparison, sqrt, log and exp, in a fixed&lt;br /&gt;
operation order, so that the PHP residence (tpc_beta.php) reproduces every&lt;br /&gt;
double bit for bit on one libm and every rendered value byte for byte&lt;br /&gt;
through canon() (the tree&amp;#039;s %.12g rendering, tpc_strike.py, copied here as&lt;br /&gt;
canon_float). Every refused argument raises ValueError with a short fixed&lt;br /&gt;
message. Ints are never bools.&lt;br /&gt;
&lt;br /&gt;
Rule sources: CR11 RELIABILITY (D122) &amp;quot;E the mean of Beta(a0 + F, b0 + A)&lt;br /&gt;
with the 95 per cent equal-tailed interval and n always shown&amp;quot;; CR12 and&lt;br /&gt;
D160.1 (b) &amp;quot;E_u = (a0 + F_u) / (a0 + b0 + F_u + A_u), with the 95 per cent&lt;br /&gt;
interval and n_u&amp;quot;; PRIOR a0, b0 = 1, 1 (DECLARED PARAMETERS). The text&lt;br /&gt;
fixes the distribution, its mean and the interval&amp;#039;s coverage and shape;&lt;br /&gt;
the numerical method is this module&amp;#039;s (taken by delegation 2026-09-26,&lt;br /&gt;
BT-1 to BT-4 below).&lt;br /&gt;
&lt;br /&gt;
beta_mean(a, b) -&amp;gt; float                         a / (a + b); a, b &amp;gt; 0&lt;br /&gt;
beta_interval(a, b, level) -&amp;gt; (lo, hi)           equal-tailed: the level&lt;br /&gt;
    is split half and half; lo = betainc_inv(a, b, (1 - level) / 2),&lt;br /&gt;
    hi = betainc_inv(a, b, 1 - (1 - level) / 2); 0 &amp;lt; level &amp;lt; 1&lt;br /&gt;
betainc(a, b, x) -&amp;gt; float                        the regularized incomplete&lt;br /&gt;
    beta function I_x(a, b); a, b &amp;gt; 0; 0 &amp;lt;= x &amp;lt;= 1&lt;br /&gt;
betainc_inv(a, b, p) -&amp;gt; float                    the quantile: the x with&lt;br /&gt;
    I_x(a, b) = p; 0 &amp;lt;= p &amp;lt;= 1&lt;br /&gt;
lgamma(x) -&amp;gt; float                               log Gamma(x), x &amp;gt; 0&lt;br /&gt;
canon_float(x) -&amp;gt; str                            the tree&amp;#039;s %.12g rendering&lt;br /&gt;
&lt;br /&gt;
Method (taken by delegation 2026-09-26; the alternatives in the spec note):&lt;br /&gt;
  BT-1 lgamma by the Lanczos approximation, g = 7, nine coefficients&lt;br /&gt;
       (Godfrey&amp;#039;s set), reflection below 0.5. Relative error about 1e-15.&lt;br /&gt;
  BT-2 betainc by the continued fraction of Numerical Recipes (betacf),&lt;br /&gt;
       evaluated by the modified Lentz method with FPMIN 1e-300, EPS&lt;br /&gt;
       1e-15 and a fixed iteration cap of 300 (the loop stops early only&lt;br /&gt;
       on the tolerance and never differs between residences); the&lt;br /&gt;
       symmetry I_x(a, b) = 1 - I_(1-x)(b, a) used when x &amp;gt; (a + 1) /&lt;br /&gt;
       (a + b + 2); the prefactor by exp(lgamma(a + b) - lgamma(a) -&lt;br /&gt;
       lgamma(b) + a log x + b log(1 - x)). Not converged at the cap:&lt;br /&gt;
       ValueError(&amp;quot;betainc&amp;quot;).&lt;br /&gt;
  BT-3 betainc_inv by bisection on [0, 1], exactly 100 halvings, the&lt;br /&gt;
       midpoint returned; p = 0 gives 0 and p = 1 gives 1.&lt;br /&gt;
  BT-4 Every value is rendered through canon_float: the two residences&lt;br /&gt;
       are byte-identical on one host (MEASURED by diff_beta.py); across&lt;br /&gt;
       hosts a last-ulp libm difference in log or exp can move a twelfth&lt;br /&gt;
       digit at a rounding boundary (VERIFY on the Windows run).&lt;br /&gt;
&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
import math&lt;br /&gt;
&lt;br /&gt;
VERSION = &amp;quot;0.1.0&amp;quot;&lt;br /&gt;
LANCZOS_G = 7.0&lt;br /&gt;
LANCZOS = (&lt;br /&gt;
    0.99999999999980993, 676.5203681218851, -1259.1392167224028,&lt;br /&gt;
    771.32342877765313, -176.61502916214059, 12.507343278686905,&lt;br /&gt;
    -0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7)&lt;br /&gt;
FPMIN = 1e-300&lt;br /&gt;
EPS = 1e-15&lt;br /&gt;
MAXIT = 300&lt;br /&gt;
INV_ITER = 100&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# --------------------------------------------------------------- canon&lt;br /&gt;
&lt;br /&gt;
def canon_float(x):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;C &amp;quot;%.12g&amp;quot; of a double, as tpc_strike.py&amp;#039;s _canon_float builds it:&lt;br /&gt;
    one rounding to 12 significant digits from &amp;quot;%.11e&amp;quot;, fixed notation for&lt;br /&gt;
    exponents -4..11, exponent notation otherwise, trailing zeros removed;&lt;br /&gt;
    0 for 0.0 and -0.0.&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    if x != x:&lt;br /&gt;
        raise ValueError(&amp;quot;canon&amp;quot;)&lt;br /&gt;
    if x in (float(&amp;quot;inf&amp;quot;), float(&amp;quot;-inf&amp;quot;)):&lt;br /&gt;
        return &amp;quot;inf&amp;quot; if x &amp;gt; 0 else &amp;quot;-inf&amp;quot;&lt;br /&gt;
    if x == 0:&lt;br /&gt;
        return &amp;quot;0&amp;quot;&lt;br /&gt;
    mant, exp = (&amp;quot;%.11e&amp;quot; % abs(x)).split(&amp;quot;e&amp;quot;)&lt;br /&gt;
    digits = mant.replace(&amp;quot;.&amp;quot;, &amp;quot;&amp;quot;)&lt;br /&gt;
    e = int(exp)&lt;br /&gt;
    if -4 &amp;lt;= e &amp;lt; 12:&lt;br /&gt;
        if e &amp;gt;= 0:&lt;br /&gt;
            ip, fp = digits[:e + 1], digits[e + 1:]&lt;br /&gt;
        else:&lt;br /&gt;
            ip, fp = &amp;quot;0&amp;quot;, &amp;quot;0&amp;quot; * (-e - 1) + digits&lt;br /&gt;
        fp = fp.rstrip(&amp;quot;0&amp;quot;)&lt;br /&gt;
        out = ip + (&amp;quot;.&amp;quot; + fp if fp else &amp;quot;&amp;quot;)&lt;br /&gt;
    else:&lt;br /&gt;
        rest = digits[1:].rstrip(&amp;quot;0&amp;quot;)&lt;br /&gt;
        out = (digits[0] + (&amp;quot;.&amp;quot; + rest if rest else &amp;quot;&amp;quot;) + &amp;quot;e&amp;quot;&lt;br /&gt;
               + (&amp;quot;-&amp;quot; if e &amp;lt; 0 else &amp;quot;+&amp;quot;) + &amp;quot;%02d&amp;quot; % abs(e))&lt;br /&gt;
    return (&amp;quot;-&amp;quot; if x &amp;lt; 0 else &amp;quot;&amp;quot;) + out&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def _num(x, name):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;A finite float or int (never a bool), returned as float.&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    if type(x) is bool or not isinstance(x, (int, float)):&lt;br /&gt;
        raise ValueError(name)&lt;br /&gt;
    x = float(x)&lt;br /&gt;
    if not math.isfinite(x):&lt;br /&gt;
        raise ValueError(name)&lt;br /&gt;
    return x&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# -------------------------------------------------------------- lgamma&lt;br /&gt;
&lt;br /&gt;
def lgamma(x):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;BT-1. log Gamma(x) for x &amp;gt; 0.&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    x = _num(x, &amp;quot;lgamma&amp;quot;)&lt;br /&gt;
    if x &amp;lt;= 0.0:&lt;br /&gt;
        raise ValueError(&amp;quot;lgamma&amp;quot;)&lt;br /&gt;
    if x &amp;lt; 0.5:&lt;br /&gt;
        # reflection: Gamma(x) Gamma(1 - x) = pi / sin(pi x)&lt;br /&gt;
        return math.log(math.pi / math.sin(math.pi * x)) - lgamma(1.0 - x)&lt;br /&gt;
    x = x - 1.0&lt;br /&gt;
    s = LANCZOS[0]&lt;br /&gt;
    for i in range(1, 9):&lt;br /&gt;
        s = s + LANCZOS[i] / (x + float(i))&lt;br /&gt;
    t = x + LANCZOS_G + 0.5&lt;br /&gt;
    return (0.5 * math.log(2.0 * math.pi) + (x + 0.5) * math.log(t) - t&lt;br /&gt;
            + math.log(s))&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# ------------------------------------------------------------- betainc&lt;br /&gt;
&lt;br /&gt;
def _betacf(a, b, x):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;The continued fraction for I_x(a, b) by the modified Lentz method.&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    qab = a + b&lt;br /&gt;
    qap = a + 1.0&lt;br /&gt;
    qam = a - 1.0&lt;br /&gt;
    c = 1.0&lt;br /&gt;
    d = 1.0 - qab * x / qap&lt;br /&gt;
    if abs(d) &amp;lt; FPMIN:&lt;br /&gt;
        d = FPMIN&lt;br /&gt;
    d = 1.0 / d&lt;br /&gt;
    h = d&lt;br /&gt;
    m = 1&lt;br /&gt;
    while m &amp;lt;= MAXIT:&lt;br /&gt;
        fm = float(m)&lt;br /&gt;
        m2 = 2.0 * fm&lt;br /&gt;
        aa = fm * (b - fm) * x / ((qam + m2) * (a + m2))&lt;br /&gt;
        d = 1.0 + aa * d&lt;br /&gt;
        if abs(d) &amp;lt; FPMIN:&lt;br /&gt;
            d = FPMIN&lt;br /&gt;
        c = 1.0 + aa / c&lt;br /&gt;
        if abs(c) &amp;lt; FPMIN:&lt;br /&gt;
            c = FPMIN&lt;br /&gt;
        d = 1.0 / d&lt;br /&gt;
        h = h * d * c&lt;br /&gt;
        aa = -(a + fm) * (qab + fm) * x / ((a + m2) * (qap + m2))&lt;br /&gt;
        d = 1.0 + aa * d&lt;br /&gt;
        if abs(d) &amp;lt; FPMIN:&lt;br /&gt;
            d = FPMIN&lt;br /&gt;
        c = 1.0 + aa / c&lt;br /&gt;
        if abs(c) &amp;lt; FPMIN:&lt;br /&gt;
            c = FPMIN&lt;br /&gt;
        d = 1.0 / d&lt;br /&gt;
        de = d * c&lt;br /&gt;
        h = h * de&lt;br /&gt;
        if abs(de - 1.0) &amp;lt; EPS:&lt;br /&gt;
            return h&lt;br /&gt;
        m += 1&lt;br /&gt;
    raise ValueError(&amp;quot;betainc&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def betainc(a, b, x):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;BT-2. The regularized incomplete beta function I_x(a, b).&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a = _num(a, &amp;quot;shape&amp;quot;)&lt;br /&gt;
    b = _num(b, &amp;quot;shape&amp;quot;)&lt;br /&gt;
    x = _num(x, &amp;quot;x&amp;quot;)&lt;br /&gt;
    if a &amp;lt;= 0.0 or b &amp;lt;= 0.0:&lt;br /&gt;
        raise ValueError(&amp;quot;shape&amp;quot;)&lt;br /&gt;
    if x &amp;lt; 0.0 or x &amp;gt; 1.0:&lt;br /&gt;
        raise ValueError(&amp;quot;x&amp;quot;)&lt;br /&gt;
    if x == 0.0:&lt;br /&gt;
        return 0.0&lt;br /&gt;
    if x == 1.0:&lt;br /&gt;
        return 1.0&lt;br /&gt;
    bt = math.exp(lgamma(a + b) - lgamma(a) - lgamma(b)&lt;br /&gt;
                  + a * math.log(x) + b * math.log(1.0 - x))&lt;br /&gt;
    if x &amp;lt; (a + 1.0) / (a + b + 2.0):&lt;br /&gt;
        return bt * _betacf(a, b, x) / a&lt;br /&gt;
    return 1.0 - bt * _betacf(b, a, 1.0 - x) / b&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def betainc_inv(a, b, p):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;BT-3. The x with I_x(a, b) = p, by 100 halvings of [0, 1].&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a = _num(a, &amp;quot;shape&amp;quot;)&lt;br /&gt;
    b = _num(b, &amp;quot;shape&amp;quot;)&lt;br /&gt;
    p = _num(p, &amp;quot;p&amp;quot;)&lt;br /&gt;
    if a &amp;lt;= 0.0 or b &amp;lt;= 0.0:&lt;br /&gt;
        raise ValueError(&amp;quot;shape&amp;quot;)&lt;br /&gt;
    if p &amp;lt; 0.0 or p &amp;gt; 1.0:&lt;br /&gt;
        raise ValueError(&amp;quot;p&amp;quot;)&lt;br /&gt;
    if p == 0.0:&lt;br /&gt;
        return 0.0&lt;br /&gt;
    if p == 1.0:&lt;br /&gt;
        return 1.0&lt;br /&gt;
    lo = 0.0&lt;br /&gt;
    hi = 1.0&lt;br /&gt;
    for _ in range(INV_ITER):&lt;br /&gt;
        mid = (lo + hi) / 2.0&lt;br /&gt;
        if betainc(a, b, mid) &amp;lt; p:&lt;br /&gt;
            lo = mid&lt;br /&gt;
        else:&lt;br /&gt;
            hi = mid&lt;br /&gt;
    return (lo + hi) / 2.0&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# ----------------------------------------------------- mean and interval&lt;br /&gt;
&lt;br /&gt;
def beta_mean(a, b):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;The mean of Beta(a, b): a / (a + b).&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a = _num(a, &amp;quot;shape&amp;quot;)&lt;br /&gt;
    b = _num(b, &amp;quot;shape&amp;quot;)&lt;br /&gt;
    if a &amp;lt;= 0.0 or b &amp;lt;= 0.0:&lt;br /&gt;
        raise ValueError(&amp;quot;shape&amp;quot;)&lt;br /&gt;
    return a / (a + b)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def beta_interval(a, b, level=0.95):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;The equal-tailed interval of Beta(a, b) at the level (0.95: the&lt;br /&gt;
    2.5 and 97.5 per cent quantiles).&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a = _num(a, &amp;quot;shape&amp;quot;)&lt;br /&gt;
    b = _num(b, &amp;quot;shape&amp;quot;)&lt;br /&gt;
    level = _num(level, &amp;quot;level&amp;quot;)&lt;br /&gt;
    if a &amp;lt;= 0.0 or b &amp;lt;= 0.0:&lt;br /&gt;
        raise ValueError(&amp;quot;shape&amp;quot;)&lt;br /&gt;
    if level &amp;lt;= 0.0 or level &amp;gt;= 1.0:&lt;br /&gt;
        raise ValueError(&amp;quot;level&amp;quot;)&lt;br /&gt;
    alpha = (1.0 - level) / 2.0&lt;br /&gt;
    return (betainc_inv(a, b, alpha), betainc_inv(a, b, 1.0 - alpha))&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# -------------------------------------------------------------- corpus&lt;br /&gt;
&lt;br /&gt;
FNS = (&amp;quot;mean&amp;quot;, &amp;quot;interval&amp;quot;, &amp;quot;betainc&amp;quot;, &amp;quot;betainc_inv&amp;quot;, &amp;quot;lgamma&amp;quot;, &amp;quot;canon&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def run_case(fn, args):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;One corpus case: the render, or &amp;quot;ERROR:&amp;quot; and the message.&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    if fn not in FNS:&lt;br /&gt;
        raise ValueError(&amp;quot;fn&amp;quot;)&lt;br /&gt;
    try:&lt;br /&gt;
        if fn == &amp;quot;mean&amp;quot;:&lt;br /&gt;
            return canon_float(beta_mean(args[&amp;quot;a&amp;quot;], args[&amp;quot;b&amp;quot;]))&lt;br /&gt;
        if fn == &amp;quot;interval&amp;quot;:&lt;br /&gt;
            lo, hi = beta_interval(args[&amp;quot;a&amp;quot;], args[&amp;quot;b&amp;quot;], args[&amp;quot;level&amp;quot;])&lt;br /&gt;
            return canon_float(lo) + &amp;quot;|&amp;quot; + canon_float(hi)&lt;br /&gt;
        if fn == &amp;quot;betainc&amp;quot;:&lt;br /&gt;
            return canon_float(betainc(args[&amp;quot;a&amp;quot;], args[&amp;quot;b&amp;quot;], args[&amp;quot;x&amp;quot;]))&lt;br /&gt;
        if fn == &amp;quot;betainc_inv&amp;quot;:&lt;br /&gt;
            return canon_float(betainc_inv(args[&amp;quot;a&amp;quot;], args[&amp;quot;b&amp;quot;], args[&amp;quot;p&amp;quot;]))&lt;br /&gt;
        if fn == &amp;quot;lgamma&amp;quot;:&lt;br /&gt;
            return canon_float(lgamma(args[&amp;quot;x&amp;quot;]))&lt;br /&gt;
        return canon_float(_num(args[&amp;quot;x&amp;quot;], &amp;quot;canon&amp;quot;))&lt;br /&gt;
    except ValueError as ex:&lt;br /&gt;
        return &amp;quot;ERROR:&amp;quot; + str(ex)&lt;br /&gt;
&lt;/div&gt;</summary>
		<author><name>BFP Rauch</name></author>
	</entry>
</feed>