{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# TreasureHuntForTwo (BEAR) \u2014 the randomization pool\n", "\n", "Builds the predetermined pool of designs the BEAR dyad study draws from, and checks it.\n", "Same machinery as `randomization.ipynb`: `orders.make_order` does the work. What differs is\n", "where the counter comes from. The SONA and lab samples take it from the pid. BEAR takes it\n", "from a pool entry, so a parent and child can share one pid and still get two designs, and\n", "two siblings can get two of their own.\n", "\n", "See **RANDOMIZATION.md** for the scheme and `randos.py` for the pool.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from collections import Counter\n", "\n", "import orders\n", "import randos\n", "\n", "N_CHILDREN = 100\n", "N_PARENTS = 100\n", "\n", "print(randos.report(N_CHILDREN, N_PARENTS))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The pool\n", "\n", "Ids 1..POOL_SIZE. Children take a consecutive run from the bottom, parents a run `PARENT_GAP` above it." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "kids, parents = randos.allocate(N_CHILDREN, N_PARENTS)\n", "spare = [i for i in randos.pool() if i not in set(kids) | set(parents)]\n", "print(f\"pool {randos.POOL_SIZE} | children {kids[0]}-{kids[-1]} | \"\n", " f\"parents {parents[0]}-{parents[-1]} | {len(spare)} spare: {spare}\")\n", "\n", "rows = ([orders.manifest_row(f\"2{n:03d}\", \"c\", r) for n, r in enumerate(kids, start=1)]\n", " + [orders.manifest_row(f\"2{n:03d}\", \"p\", r) for n, r in enumerate(parents, start=1)])\n", "print(f\"generated {len(rows)} designs\")\n", "try:\n", " import pandas as pd\n", " df = pd.DataFrame(rows)\n", " display(df[[\"pid\", \"role\", \"rando\", \"orderID\", \"layout_id\", \"block_order\"]].head(8))\n", "except ImportError:\n", " pass\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Balance\n", "\n", "The two samples are analysed separately, so each is checked on its own." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def spread(ids, m):\n", " c = Counter(i % m for i in ids)\n", " v = [c.get(k, 0) for k in range(m)]\n", " return f\"{min(v)}-{max(v)}\"\n", "\n", "print(f\"{'':12s}{'block /12':>12s}{'chest /24':>12s}{'layout /5':>12s}\")\n", "for name, ids in ((\"children\", kids), (\"parents\", parents), (\"both\", kids + parents)):\n", " print(f\"{name:12s}\" + \"\".join(f\"{spread(ids, m):>12s}\" for m in (12, 24, 5)))\n", "\n", "# layout, read off the generated designs rather than the arithmetic\n", "for role in (\"c\", \"p\"):\n", " counts = Counter(r[\"layout_id\"] for r in rows if r[\"role\"] == role)\n", " print(f\"layout_id, role {role}:\", dict(sorted(counts.items())))\n", "\n", "# every pair-slot at every entry position, counted over the children\n", "pos = [Counter() for _ in range(orders.N_PLACES)]\n", "for r in rows:\n", " if r[\"role\"] != \"c\":\n", " continue\n", " for i, slot in enumerate(r[\"block_order\"].split(\"_\")):\n", " pos[i][slot.split(\":\")[1]] += 1\n", "widths = {len(p) for p in pos}\n", "print(\"distinct pairs seen at each entry position:\", sorted(widths))\n", "print(\"spread within a position:\", {f\"{min(p.values())}-{max(p.values())}\" for p in pos})\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The dyad\n", "\n", "A child and their parent share a pid and must not share a design." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bad = []\n", "for n, (ck, pk) in enumerate(zip(kids, parents), start=1):\n", " pid = f\"2{n:03d}\"\n", " c = orders.make_order(pid, \"c\", ck)\n", " p = orders.make_order(pid, \"p\", pk)\n", " if (c[\"block_order\"] == p[\"block_order\"] or c[\"layout_id\"] == p[\"layout_id\"]\n", " or np.array_equal(c[\"outcomes\"], p[\"outcomes\"])\n", " or np.array_equal(c[\"stim_map\"], p[\"stim_map\"])):\n", " bad.append(pid)\n", "print(\"dyads sharing any part of a design:\", bad or \"none\")\n", "\n", "# siblings: two children, two pool entries, two designs\n", "a = orders.make_order(\"2001\", \"c\", kids[0])\n", "b = orders.make_order(\"2002\", \"c\", kids[1])\n", "print(\"two siblings share a design:\",\n", " a[\"block_order\"] == b[\"block_order\"] and np.array_equal(a[\"outcomes\"], b[\"outcomes\"]))\n", "\n", "# the pid is only a label: the same pool entry on any pid gives the same design\n", "x = orders.make_order(\"2001\", \"c\", 7)\n", "y = orders.make_order(\"2777\", \"p\", 7)\n", "print(\"design follows the rando and nothing else:\",\n", " x[\"block_order\"] == y[\"block_order\"] and np.array_equal(x[\"outcomes\"], y[\"outcomes\"]))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## One person's full design" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "rando = kids[1]\n", "pid = \"2002\"\n", "o = orders.make_order(pid, \"c\", rando)\n", "m = orders.manifest_row(pid, \"c\", rando)\n", "print(\"pid\", pid, \"| role c | rando\", rando, \"| 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(\"houses by place:\", m[\"house_by_place\"])\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Integrity" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for rando in randos.pool():\n", " o = orders.make_order(\"2001\", \"c\", rando)\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", " # every one of the 24 chests used exactly once\n", " bp = o[\"block_pairs\"]\n", " used = sorted(int(o[\"stim_map\"][b, l]) for b in range(orders.N_PLACES) for l in bp[b].tolist())\n", " assert used == list(range(orders.N_STIM))\n", "\n", "# reproducible\n", "assert np.array_equal(orders.make_order(\"2001\", \"c\", 42)[\"outcomes\"],\n", " orders.make_order(\"2001\", \"c\", 42)[\"outcomes\"])\n", "# no two pool entries give the same design\n", "sigs = {(tuple(orders.make_order(\"2001\", \"c\", r)[\"block_order\"]),\n", " orders.make_order(\"2001\", \"c\", r)[\"layout_id\"],\n", " orders.make_order(\"2001\", \"c\", r)[\"outcomes\"].tobytes()) for r in randos.pool()}\n", "assert len(sigs) == randos.POOL_SIZE, \"two pool entries collide\"\n", "\n", "print(f\"all {randos.POOL_SIZE} pool entries check out, and no two of them are the same\")\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python", "version": "3.12" } }, "nbformat": 4, "nbformat_minor": 5 }