|
| 1 | +# Examples |
| 2 | + |
| 3 | +The [`examples/`](https://github.com/thisiscam/acopula/tree/main/examples) |
| 4 | +directory has runnable, self-contained scripts. Each uses small synthetic / |
| 5 | +copula-scale data — no external datasets required. |
| 6 | + |
| 7 | +Run them from the repo root: |
| 8 | + |
| 9 | +```bash |
| 10 | +uv run python examples/01_quickstart.py |
| 11 | +uv run python examples/02_fit_mle.py |
| 12 | +uv run python examples/03_censored_survival.py |
| 13 | +uv run --extra examples python examples/04_sampling_and_visualize.py [output_dir] |
| 14 | +``` |
| 15 | + |
| 16 | +## 01 — Quickstart |
| 17 | + |
| 18 | +Build a two-level nested copula, evaluate the log-likelihood and its exact |
| 19 | +`jax.grad`. See [Quickstart](quickstart.md). |
| 20 | + |
| 21 | +## 02 — Maximum-likelihood fitting |
| 22 | + |
| 23 | +Simulate 1000 observations from a known `d=10` Clayton copula (two sectors of |
| 24 | +five), then recover the parameters by gradient-descent MLE with `optax`. The |
| 25 | +whole likelihood is differentiable, so the fit is plain |
| 26 | +`jax.value_and_grad` + Adam: |
| 27 | + |
| 28 | +``` |
| 29 | +parameter true fitted |
| 30 | + theta_root 2.00 1.958 |
| 31 | + theta_sector 5.00 4.840 |
| 32 | +``` |
| 33 | + |
| 34 | +## 03 — Censored survival |
| 35 | + |
| 36 | +A bivariate Frank copula over two Weibull event times, where the second is |
| 37 | +right-censored. A censored leaf still enters the copula argument (through its |
| 38 | +survival function) but is not differentiated in the density — `acopula` |
| 39 | +assembles the correct mixed partial over only the observed dimensions. |
| 40 | + |
| 41 | +## 04 — Sampling and visualization |
| 42 | + |
| 43 | +Draw samples from a Clayton-over-Frank copula (one root, three sectors of two |
| 44 | +leaves) with the Marshall-Olkin sampler, and render the copula tree. |
| 45 | + |
| 46 | +### Copula tree |
| 47 | + |
| 48 | +`CompiledModel.visualize()` draws the structural graph — root and sector |
| 49 | +copulas in blue, leaves in orange: |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | +### Sampled margins |
| 54 | + |
| 55 | +Pairwise scatter of the six sampled margins. Within-sector pairs |
| 56 | +(`u[i,0]`–`u[i,1]`) are visibly more dependent than across-sector pairs, |
| 57 | +reflecting the nesting: |
| 58 | + |
| 59 | + |
0 commit comments