SSM-AI – Appendix I — Lens Builder (I7–I9)

Auto-pick gamma, sanity-check c, and ship a drop-in lens helper — bounded, reproducible, values untouched.

I7) Auto-suggest gamma in w := |m|^gamma (one pass)
Pick a strength-aware weighting once, using stable weekly slices. Goal: minimize variance and segment bias while keeping the simplest gamma.

# I7 — gamma scan (copy-paste)
def rsi_port(rows, eps_a=1e-6, eps_w=1e-12):
    from math import atanh, tanh
    U=W=0.0
    for a,w in rows:
        x = max(-1+eps_a, min(1-eps_a, a))
        U += w * atanh(x); W += w
    return 0.0 if W<=0 else tanh(U / max(W, eps_w))

def pick_gamma(m_vals, a_vals, weeks, gammas=(0.0,0.5,1.0,1.5,2.0)):
    import math
    best = (None, 1e30, None)     # (gamma, score, ports)
    for g in gammas:
        ports = []
        for wk, idxs in weeks.items():
            rows = [(a_vals[i], (abs(m_vals[i])**g if g>0 else 1.0)) for i in idxs]
            ports.append(rsi_port(rows))
        mu = sum(ports)/len(ports)
        sigma = math.sqrt(sum((x-mu)**2 for x in ports)/len(ports))  # stability
        # fairness: deviation from global
        glob = rsi_port([(a_vals[i], (abs(m_vals[i])**g if g>0 else 1.0)) for i in range(len(a_vals))])
        bias = sum(abs(x-glob) for x in ports)/len(ports)
        score = 0.7*sigma + 0.3*bias
        if score < best[1] - 1e-12 or (abs(score-best[1])<=1e-12 and (best[0] is None or g<best[0])):
            best = (g, score, ports)
    return best[0]

Publish: the chosen gamma in the manifest and freeze it for all comparable runs.


I8) Quick metrics to validate c (saturation / dead-zone)
Keep alignments informative (not saturated, not inert). Targets: sat < 0.10, dead < 0.70.

# I8 — saturation & dead-zone checks (copy-paste)
def sat_dead_rates(e_vals, Unit, c):
    import math
    z = [abs(c * (e / max(Unit, 1e-12))) for e in e_vals]
    sat  = sum(1 for v in z if v > 3.0) / max(1, len(z))       # |a| > ~0.995
    dead = sum(1 for v in z if v < 0.10) / max(1, len(z))      # |a| < tanh(0.10)
    return sat, dead

# Tune rule:
# - If sat high  -> increase Unit (or slightly lower c)
# - If dead high -> decrease Unit (or slightly increase c)

Invariant: classical values remain untouched by any of these choices: phi((m,a)) = m.


I9) Pseudocode (drop-in, copy-paste-ready)
End-to-end helpers to choose Unit, c, map alignments, and compute RSI.

# I9 — tiny lens builder kit
def percentile_abs(vals, p):
    xs = sorted(abs(v) for v in vals)
    i = int(max(0, min(len(xs)-1, round((p/100.0)*(len(xs)-1)))))
    return xs[i]

def atanh_safe(x):
    x = max(-0.999999, min(0.999999, x))
    from math import log
    return 0.5 * log((1+x)/(1-x))

def tanh_fast(x):
    from math import tanh
    return tanh(x)

# Unit and c
def choose_unit(e_vals):                     # robust default
    return percentile_abs(e_vals, 95)

def choose_c(e_vals, target=0.60):
    e95 = max(percentile_abs(e_vals, 95), 1e-12)
    # map p95(|e|) to boundary target (e.g., 0.60 for A+, 0.90 for A++)
    from math import atanh
    return atanh(target) / e95

# Map (single-channel)
def map_alignment(e, Unit, c):
    return tanh_fast(c * (e / max(Unit, 1e-12)))

# Two-channel (support vs penalty)
def two_channel(e_out, e_in, Unit_out, Unit_in, c):
    a_out = tanh_fast(+c * (e_out / max(Unit_out, 1e-12)))
    a_in  = tanh_fast(-c * (e_in  / max(Unit_in,  1e-12)))
    return a_in, a_out

# Order/shard-invariant fuse
def fuse_alignments(a_list, w_list=None, eps_a=1e-6, eps_w=1e-12):
    if w_list is None: w_list = [1.0]*len(a_list)
    U=W=0.0
    for a,w in zip(a_list, w_list):
        x = max(-1+eps_a, min(1-eps_a, a))
        U += w * atanh_safe(x); W += w
    return 0.0 if W<=0 else tanh_fast(U / max(W, eps_w))

# Chooser (single-lane or pooled)
def rsi_from(a_in_list, a_out_list, w_in=None, w_out=None, eps_a=1e-6, eps_w=1e-12):
    if w_in  is None: w_in  = [1.0]*len(a_in_list)
    if w_out is None: w_out = [1.0]*len(a_out_list)
    U_in=W_in=0.0; V_out=0.0
    for a,w in zip(a_in_list, w_in):
        x = max(-1+eps_a, min(1-eps_a, a)); U_in += w * atanh_safe(x); W_in += w
    for a,w in zip(a_out_list, w_out):
        x = max(-1+eps_a, min(1-eps_a, a)); V_out += w * atanh_safe(x)
    return 0.0 if W_in<=0 else tanh_fast((V_out - U_in) / max(W_in, eps_w))

Publish once: Unit, c, weights (w := |m|^gamma or w := 1), eps_a, eps_w, bands, and a knobs_hash. From that point, all runs are comparable. Decision fusion remains bounded and order/shard invariant, and phi((m,a)) = m guarantees your classical numbers stay pristine.


Navigation
Previous: SSM-AI – Appendix I — Lens Builder (I4–I6)
Next: SSM-AI – Appendix I — Lens Builder (I10–I12)


Directory of Pages
SSM-AI — Table of Contents