{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# TreasureHuntForTwo \u2014 Randomization\n", "\n", "Generates the deterministic per-participant design from `orders.py` for a whole sample and\n", "checks the counterbalancing. The participant ID *is* the order (one order file per ID).\n", "See **RANDOMIZATION.md** for the scheme.\n", "\n", "Run from the repo root (`uv run jupyter ...`, or `jupyter lab`)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from collections import Counter\n", "import orders\n", "\n", "# the sample to generate: 55 per group (group 0 = control, 1 = CU)\n", "N_PER_GROUP = 55\n", "pids = [f\"{g}{n:03d}\" for g in (0, 1) for n in range(1, N_PER_GROUP + 1)]\n", "rows = [orders.manifest_row(p) for p in pids]\n", "print(f\"generated {len(rows)} participant designs\")\n", "try:\n", " import pandas as pd\n", " df = pd.DataFrame(rows)\n", " display(df.head(8))\n", "except Exception:\n", " df = None\n", " for r in rows[:3]:\n", " print(r)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Balance checks across the sample" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 1) location layout: number % 5 -> should be near-equal across the 5 layouts\n", "print('layout_id counts:', dict(sorted(Counter(r['layout_id'] for r in rows).items())))\n", "\n", "# 2) block order is a cyclic Latin square over `number` (not an i.i.d. shuffle), so every\n", "# pair-slot should land at every entry-position EXACTLY floor(N_PER_GROUP/12) or +1 times per\n", "# group -- check the spread is tight (near-equal), which also verifies the rotation is wired up\n", "from collections import defaultdict\n", "pos_counts = defaultdict(Counter) # position -> Counter(pair_slot)\n", "for r in rows:\n", " for chunk in r['block_order'].split('_'):\n", " pos, pair = chunk.split(':')\n", " pos_counts[int(pos)][pair] += 1\n", "spread = [max(c.values()) - min(c.values()) for c in pos_counts.values()]\n", "print('block-order position balance: max-min spread per position (0 = perfectly even):', spread)\n", "\n", "# 3) chest colour vs reward-valence, pooled across the sample. Stim assignment is ALSO now a\n", "# cyclic rotation over `number` (same trick as block order): every chest is guaranteed to land\n", "# under every letter exactly 6 times per complete 24-ID cycle within a group, so this should be\n", "# exactly even for any N_PER_GROUP that's a multiple of 24, and tight otherwise -- this is a\n", "# confirmation of a guarantee, not a hope that a random draw stayed roughly balanced.\n", "self_good, self_bad = Counter(), Counter()\n", "for r in rows:\n", " for chunk in r['stim_by_block'].split('_'):\n", " _, rest = chunk.split(':')\n", " for part in rest.split('-'):\n", " letter, sid = part[0], int(part[1:])\n", " li = orders.LETTERS.index(letter)\n", " colour = sid % orders.N_COLORS\n", " (self_good if orders.LETTER_SELF_GOOD[li] else self_bad)[colour] += 1\n", "print('chest colour vs self-valence (guaranteed even per complete 24-ID cycle):')\n", "for c in sorted(set(self_good) | set(self_bad)):\n", " print(f' colour {c}: self-good {self_good.get(c, 0)}x, self-bad {self_bad.get(c, 0)}x')\n", "\n", "# 4) coin-float order: every participant must be exactly 6 you-first / 6 partner-first\n", "ok = all(r['float_order'].count('you-first') == 6 for r in rows)\n", "print('float order 6/6 for every participant:', ok)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## One participant's full design" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pid = '0002'\n", "o = orders.make_order(pid)\n", "m = orders.manifest_row(pid)\n", "print('pid', pid, '| group', o['group'], '| layout', o['layout_id'])\n", "print('chests, one per (block, letter) slot -- never reused this session:')\n", "print(' ', m['stim_by_block'])\n", "print('block order (experienced sequence of pairs):')\n", "print(' ', o['block_pairs_str'])\n", "print('float order (per block):', o['float_order'].tolist())\n", "print('house positions (physical):', o['house_pos'].tolist())\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Integrity checks" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "o = orders.make_order('0042')\n", "# exact 12 gain / 2 loss / 2 neutral per block, option, recipient\n", "for b in range(orders.N_PLACES):\n", " for opt in range(2):\n", " for rec in range(2):\n", " seq = o['outcomes'][b, opt, rec]\n", " assert (seq != 0).sum() == 14 and (seq == 0).sum() == 2\n", "# reproducible: same ID -> identical outcomes\n", "assert np.array_equal(orders.make_order('0042')['outcomes'], o['outcomes'])\n", "# each of the 6 canonical pairs appears exactly twice in the block order\n", "pairs = [p.split(':')[1] for p in '_'.join([f'{i}:{x}' for i, x in enumerate(o['block_pairs_str'].split('_'))]).split('_')]\n", "print('pair counts in block order:', dict(Counter(o['block_pairs_str'].split('_'))))\n", "print('all integrity checks passed')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python", "version": "3.12" } }, "nbformat": 4, "nbformat_minor": 5 }