{ "cells": [ { "cell_type": "markdown", "id": "c7750cd7", "metadata": {}, "source": [ "# Likelihoods across chromosomes of different lengths\n", "\n", "Real data cover many chromosomes, with map lengths ranging from about 0.7 M (chromosome 22) to\n", "about 2.8 M (chromosome 1, sex-averaged). This example sets up the likelihood for two chromosomes\n", "of different lengths. Each chromosome in each gamete is one \"meiosis\" in `cointf`'s sense, with its\n", "own length $L$, and given the parameters the chromosomes are independent, so their\n", "log-likelihoods add." ] }, { "cell_type": "code", "execution_count": 1, "id": "36857ee7", "metadata": { "execution": { "iopub.execute_input": "2026-11-12T03:19:35.417988Z", "iopub.status.busy": "2026-11-12T03:19:35.417566Z", "iopub.status.idle": "2026-11-12T03:19:35.829675Z", "shell.execute_reply": "2026-11-12T03:19:35.829117Z" } }, "outputs": [], "source": [ "import numpy as np\n", "from scipy.optimize import minimize\n", "from scipy.special import expit\n", "from scipy.stats import chi2\n", "\n", "from cointf import Meioses, loglik_fast, simulate_meioses" ] }, { "cell_type": "markdown", "id": "e57ac509", "metadata": {}, "source": [ "## Data\n", "\n", "1,000 gametes, each with crossovers on chromosome 1 ($L = 2.8$ M) and chromosome 22\n", "($L = 0.7$ M), simulated with the same $\\nu = 4$ and $p = 0.05$." ] }, { "cell_type": "code", "execution_count": 2, "id": "6f0e56e1", "metadata": { "execution": { "iopub.execute_input": "2026-11-12T03:19:35.830876Z", "iopub.status.busy": "2026-11-12T03:19:35.830792Z", "iopub.status.idle": "2026-11-12T03:19:35.842546Z", "shell.execute_reply": "2026-11-12T03:19:35.842098Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "chr1: L = 2.8 M, mean 2.85 crossovers per meiosis, 3% with none\n", "chr22: L = 0.7 M, mean 0.69 crossovers per meiosis, 43% with none\n" ] } ], "source": [ "lengths = {\"chr1\": 2.8, \"chr22\": 0.7} # Morgans\n", "nu_true, p_true, n = 4.0, 0.05, 1000\n", "data = {\n", " \"chr1\": simulate_meioses(L=lengths[\"chr1\"], nu=nu_true, p=p_true, n=n, seed=1),\n", " \"chr22\": simulate_meioses(L=lengths[\"chr22\"], nu=nu_true, p=p_true, n=n, seed=22),\n", "}\n", "for chrom, d in data.items():\n", " counts = np.array([len(x) for x in d])\n", " print(f\"{chrom}: L = {lengths[chrom]} M, mean {counts.mean():.2f} crossovers per meiosis, \"\n", " f\"{np.mean(counts == 0):.0%} with none\")" ] }, { "cell_type": "markdown", "id": "c867ddc6", "metadata": {}, "source": [ "## Two ways to set up the likelihood\n", "\n", "**One `Meioses` per chromosome.** Each carries its own scalar length; the total is the sum." ] }, { "cell_type": "code", "execution_count": 3, "id": "4f97aa9b", "metadata": { "execution": { "iopub.execute_input": "2026-11-12T03:19:35.843525Z", "iopub.status.busy": "2026-11-12T03:19:35.843471Z", "iopub.status.idle": "2026-11-12T03:19:35.860345Z", "shell.execute_reply": "2026-11-12T03:19:35.859901Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "chr1: -2503.896\n", "chr22: -634.477\n", "total: -3138.374\n" ] } ], "source": [ "per_chrom = {chrom: Meioses(d, lengths[chrom]) for chrom, d in data.items()}\n", "ll_by_chrom = {chrom: loglik_fast(m, None, nu=nu_true, p=p_true) for chrom, m in per_chrom.items()}\n", "for chrom, ll in ll_by_chrom.items():\n", " print(f\"{chrom}: {ll:.3f}\")\n", "print(f\"total: {sum(ll_by_chrom.values()):.3f}\")" ] }, { "cell_type": "markdown", "id": "65e9ae02", "metadata": {}, "source": [ "**One `Meioses` for everything.** Concatenate the data and pass one length per entry. This is\n", "convenient when parameters vary between chromosomes or individuals, because `nu` and `p` can then\n", "be arrays aligned with the same entries." ] }, { "cell_type": "code", "execution_count": 4, "id": "72c5d26d", "metadata": { "execution": { "iopub.execute_input": "2026-11-12T03:19:35.861279Z", "iopub.status.busy": "2026-11-12T03:19:35.861215Z", "iopub.status.idle": "2026-11-12T03:19:35.877446Z", "shell.execute_reply": "2026-11-12T03:19:35.876992Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Meioses(M=2000, crossovers=3544, max_count=8)\n", "total: -3138.374\n" ] } ], "source": [ "all_data = data[\"chr1\"] + data[\"chr22\"]\n", "chrom_of = np.repeat([\"chr1\", \"chr22\"], n) # chromosome of each entry\n", "L_all = np.array([lengths[c] for c in chrom_of])\n", "mz = Meioses(all_data, L_all)\n", "print(mz)\n", "print(f\"total: {loglik_fast(mz, None, nu=nu_true, p=p_true):.3f}\")" ] }, { "cell_type": "markdown", "id": "01bc9a2b", "metadata": {}, "source": [ "With `per_meiosis=True` the result has one term per chromosome per gamete. Adding the two\n", "chromosomes of each gamete gives a per-gamete log-likelihood." ] }, { "cell_type": "code", "execution_count": 5, "id": "bd74981f", "metadata": { "execution": { "iopub.execute_input": "2026-11-12T03:19:35.878420Z", "iopub.status.busy": "2026-11-12T03:19:35.878372Z", "iopub.status.idle": "2026-11-12T03:19:35.887333Z", "shell.execute_reply": "2026-11-12T03:19:35.887042Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2000 terms -> 1000 gametes; first three: [-3.581 -1.903 -3.377]\n" ] } ], "source": [ "terms = loglik_fast(mz, None, nu=nu_true, p=p_true, per_meiosis=True)\n", "per_gamete = terms[:n] + terms[n:]\n", "print(f\"{terms.size} terms -> {per_gamete.size} gametes; first three: {per_gamete[:3].round(3)}\")" ] }, { "cell_type": "markdown", "id": "ce85d3ad", "metadata": {}, "source": [ "## Joint fit\n", "\n", "The pooled maximum-likelihood fit of $(\\nu, p)$ uses both chromosomes, on the scale\n", "$(\\log \\nu, \\operatorname{logit} p)$." ] }, { "cell_type": "code", "execution_count": 6, "id": "5fb978d9", "metadata": { "execution": { "iopub.execute_input": "2026-11-12T03:19:35.888312Z", "iopub.status.busy": "2026-11-12T03:19:35.888258Z", "iopub.status.idle": "2026-11-12T03:19:36.755800Z", "shell.execute_reply": "2026-11-12T03:19:36.755444Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "shared fit: nu = 3.72, p = 0.035, log-likelihood -3137.290\n" ] } ], "source": [ "def fit(negloglik, x0):\n", " res = minimize(negloglik, x0=x0, method=\"Nelder-Mead\", options=dict(xatol=1e-6, fatol=1e-8))\n", " return res.x, -res.fun\n", "\n", "\n", "shared, ll_shared = fit(lambda t: -loglik_fast(mz, None, nu=np.exp(t[0]), p=expit(t[1])),\n", " [np.log(2.0), -2.0])\n", "print(f\"shared fit: nu = {np.exp(shared[0]):.2f}, p = {expit(shared[1]):.3f}, log-likelihood {ll_shared:.3f}\")" ] }, { "cell_type": "markdown", "id": "3ef96154", "metadata": {}, "source": [ "## Does interference differ between chromosomes?\n", "\n", "Give each chromosome its own $\\nu$ (sharing $p$) by passing a per-entry array, and compare with the\n", "shared fit by a likelihood-ratio test on one degree of freedom. The data were simulated with a\n", "shared $\\nu$, so the test should usually not reject." ] }, { "cell_type": "code", "execution_count": 7, "id": "c9382bd7", "metadata": { "execution": { "iopub.execute_input": "2026-11-12T03:19:36.756908Z", "iopub.status.busy": "2026-11-12T03:19:36.756847Z", "iopub.status.idle": "2026-11-12T03:19:37.907086Z", "shell.execute_reply": "2026-11-12T03:19:37.906613Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "chromosome-specific fit: nu_chr1 = 3.75, nu_chr22 = 3.60, p = 0.035\n", "LR = 0.10, p-value = 0.75\n" ] } ], "source": [ "is_chr1 = chrom_of == \"chr1\"\n", "\n", "\n", "def negloglik_by_chrom(t):\n", " nu = np.where(is_chr1, np.exp(t[0]), np.exp(t[1]))\n", " return -loglik_fast(mz, None, nu=nu, p=expit(t[2]))\n", "\n", "\n", "separate, ll_separate = fit(negloglik_by_chrom, [shared[0], shared[0], shared[1]])\n", "stat = 2 * (ll_separate - ll_shared)\n", "print(f\"chromosome-specific fit: nu_chr1 = {np.exp(separate[0]):.2f}, nu_chr22 = {np.exp(separate[1]):.2f}, \"\n", " f\"p = {expit(separate[2]):.3f}\")\n", "print(f\"LR = {stat:.2f}, p-value = {chi2.sf(stat, 1):.2f}\")" ] }, { "cell_type": "markdown", "id": "fab05731", "metadata": {}, "source": [ "## Notes\n", "\n", "- **More chromosomes.** The same pattern extends to all autosomes: concatenate the data and give\n", " each entry its chromosome's length (sex-specific lengths if the parents' sexes are known).\n", "- **Short chromosomes.** On a 0.7 M chromosome most meioses have zero or one crossover, which say\n", " little about $\\nu$ beyond the count distribution. Most of the information on interference comes\n", " from the longer chromosomes, which have more pairs of crossovers.\n", "- **Mixing with covariates.** The per-entry arrays combine with the regression in\n", " [the age-effects example](age_effects.ipynb): compute $\\nu_i$ and $p_i$ from each entry's\n", " parent and chromosome, then make one call." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.11" } }, "nbformat": 4, "nbformat_minor": 5 }