HarvestForTwo / analysis /night2_structures.py
princessamygdala's picture
Claude Opus 5
Night-condition design simulations and the options document
92ae8e8
Raw History Blame Contribute Delete
5.25 kB
"""Three payoff structures for Night 2, compared on what each can identify.
Effort is matched across the two options in all three, which is what makes
Night 2 Night 2: the effort terms cancel and only their difference dk survives.
What differs is the PARTNER's side of the ledger.
A allocation keep: +a to me, 0 to them leave: 0 to me, +b to them
B symmetric transfer keep: +a to me, -a from them leave: -b from me, +b to them
C asymmetric keep: +a to me, -l from them leave: 0 to me, +b to them
The model weights a partner GAIN and a partner LOSS separately, because there is
no reason to assume they are the same:
V = d_self + w * (partner gain) - h * (partner loss) - k * E^2
A never moves the partner downward, so h has nothing to attach to and only w is
estimable there. B and C both create gains and losses, so both parameters are in
play; the question is whether the design can pull them apart.
"""
import numpy as np
from pyem.api import EMModel
from pyem.utils.math import calc_fval, norm2beta
N, SEED = 100, 20260925
W_MAX, H_MAX, BETA_MAX = 2.0, 2.0, 3.0
EFF = np.array([.35, .50, .65, .80])
# each entry: (self_keep, other_keep, self_leave, other_leave)
STRUCTURES = {
"A allocation (current)": [
(a, 0, 0, b) for a in (4, 6, 8) for b in (4, 8)],
"B symmetric transfer": [
(a, -a, -b, b) for a in (4, 6, 8) for b in (4, 8)],
"C asymmetric (harm vs gift)": [
(a, -l, 0, b) for a, l in ((4, 4), (6, 4), (8, 8)) for b in (4, 8)],
}
def value_diff(par, eff, sk, ok, sl, ol, has_h):
"""V_keep - V_leave. Partner gains weighted by w, losses by h."""
if has_h:
w, h, dk, beta = par
else:
w, dk, beta = par; h = 0.0
def partner_term(x):
return w * np.maximum(x, 0) - h * np.maximum(-x, 0)
v = (sk - sl) + (partner_term(ok) - partner_term(ol)) + dk * eff ** 2
return v, beta
def p_keep(par, eff, sk, ok, sl, ol, has_h):
v, beta = value_diff(par, eff, sk, ok, sl, ol, has_h)
return 1 / (1 + np.exp(-beta * v))
def make_obj(has_h):
def obj(par, eff, sk, ok, sl, ol, ch, prior=None, output="npl"):
if has_h:
nat = np.array([norm2beta(par[0], W_MAX), norm2beta(par[1], H_MAX),
par[2], norm2beta(par[3], BETA_MAX)])
else:
nat = np.array([norm2beta(par[0], W_MAX), par[1],
norm2beta(par[2], BETA_MAX)])
p = np.clip(p_keep(nat, eff, sk, ok, sl, ol, has_h), 1e-10, 1 - 1e-10)
nll = -np.sum(ch * np.log(p) + (1 - ch) * np.log(1 - p))
return {"nll": float(nll)} if output == "all" else calc_fval(nll, par, prior=prior, output=output)
return obj
def design(n, rng, cells):
grid = [(e, *c) for e in EFF for c in cells]
d = np.array(grid * int(np.ceil(n / len(grid))), float)
rng.shuffle(d); d = d[:n]
return [d[:, i] for i in range(5)]
def rank_of(cells, has_h):
"""Can the regressors be told apart at all, before any fitting?"""
rows = []
for e in EFF:
for sk, ok, sl, ol in cells:
pg = max(ok, 0) - max(ol, 0) # net partner gain
pl = max(-ok, 0) - max(-ol, 0) # net partner loss
rows.append([pg, -pl, e ** 2] if has_h else [pg - pl, e ** 2])
X = np.array(rows, float)
return np.linalg.matrix_rank(X), np.linalg.cond(X), X.shape[1]
def run(cells, has_h, n_trials, seed):
rng = np.random.default_rng(seed)
if has_h:
true = np.column_stack([rng.uniform(.2, .9, N), rng.uniform(.2, .9, N),
rng.normal(3., 1.5, N), rng.uniform(.7, 2.2, N)])
names = ["w", "h", "dk", "beta"]
else:
true = np.column_stack([rng.uniform(.2, .9, N), rng.normal(3., 1.5, N),
rng.uniform(.7, 2.2, N)])
names = ["w", "dk", "beta"]
data = []
for i in range(N):
cols = design(n_trials, rng, cells)
p = p_keep(true[i], *cols, has_h)
data.append([*cols, (rng.random(len(p)) < p).astype(int)])
est = np.asarray(EMModel(all_data=data, fit_func=make_obj(has_h),
param_names=names).fit(verbose=0, seed=1).m)
if has_h:
nat = np.column_stack([norm2beta(est[0], W_MAX), norm2beta(est[1], H_MAX),
est[2], norm2beta(est[3], BETA_MAX)])
else:
nat = np.column_stack([norm2beta(est[0], W_MAX), est[1],
norm2beta(est[2], BETA_MAX)])
return names, [np.corrcoef(true[:, j], nat[:, j])[0, 1] for j in range(len(names))]
print(f"{N} participants. Effort is matched across options in all three, so only")
print("dk is estimable for effort, never k_self and k_other separately.\n")
for name, cells in STRUCTURES.items():
has_h = any(ok < 0 or ol < 0 for _, ok, _, ol in cells)
r, c, ncol = rank_of(cells, has_h)
print(f"{name}")
print(f" partner can lose: {'yes' if has_h else 'no':3s} "
f"design rank {r} of {ncol} cond {c:.1f}")
for nt in (24, 48):
names, rs = run(cells, has_h, nt, SEED + nt)
print(f" {nt:3d} trials " + " ".join(f"{n} {v:+.2f}" for n, v in zip(names, rs)),
flush=True)
print()