diff --git a/AGENTS.md b/AGENTS.md index a92f3f0..4f0f8c7 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -141,10 +141,17 @@ how it paces the work, not which workflows exist: the user asks for a long or multi-session run, scale up: clarify the goal, plan in phases, execute with checkpoints — proactive but not silent; state in `wiki/project/`. The dial is in [`modes/assistant.md`](./modes/assistant.md) "The autonomy dial". - -Select (first match): explicit instruction → `.mode` file → `profile.md` "Interaction mode" → -else **infer from the opening request** (fall back to **assistant**); `.maintainer` outranks -`.mode`. State an inferred mode in one line and invite correction; acknowledge an explicit +- **Start here** — *the guided tour*: the six-step natural-language workflow of the RTD + Natural Language Inference page run on the bundled 1D Gaussian, then the same six steps on + the user's own project. One step per turn; the user types each prompt themselves. It adds + no capability — it scripts the existing skills at newcomer depth — and hands off to + assistant (or teacher) mode when it ends. See [`modes/start_here.md`](./modes/start_here.md). + +Select (first match): the **published start-here prompt** (`README.md` "Getting Started"), or +"start here" / "start_here" → explicit instruction → `.mode` file → `profile.md` "Interaction +mode" → else **infer from the opening request** (fall back to **assistant**); `.maintainer` +outranks all of them. The start-here trigger outranks inference from the rest of the opening +request. State an inferred mode in one line and invite correction; acknowledge an explicit one only if it changes behavior. Read `modes/.md`; depth still follows `skills/_style.md` "Adaptive depth". diff --git a/README.md b/README.md index bb17ea4..6c294e4 100644 --- a/README.md +++ b/README.md @@ -12,26 +12,18 @@ Simply open your AI coding agent (`codex` or `Claude Code` are recommended) and > > Begin the "start here" guide for a new user. -### Bring Your Own Likelihood +**What happens when you type it.** The assistant starts a guided tour that runs in two tracks. First the **bundled +1D Gaussian**: six steps — compose a model, define a likelihood, choose a search, fit and read the result, save and +reload it, then extend the workflow — one step per turn, with **you** typing each prompt, so you find out for +yourself that the whole workflow is natural language. Then **your own science**: a paper or a plain-language +description of what you measure, then your model, your likelihood, a search chosen for your problem, the fit, and +the results. You can stop and ask "what is a prior?" at any step without losing your place, and saying +**teacher mode** turns on full explanations for the rest of the tour. -Already have a likelihood function for your science problem? **Point the assistant at your existing code and it can set it up with PyAutoFit** — defining the model, choosing priors with you, configuring a search and organising the results: +The tour is scripted in [`modes/start_here.md`](modes/start_here.md); the sections below are the same six steps, +written out so you can read ahead or work through them on your own. -> Set up PyAutoFit with my existing science project. An example likelihood -> function can be found at [GitHub link or local directory]. -> -> First, give me an overview of my project and likelihood function. Compose -> an appropriate model, explain it to me, and recommend a non-linear search -> (for example MCMC, nested sampling or maximum-likelihood estimation). -> -> Do not begin inference until we have discussed the setup and I give you -> the go-ahead. -> -> Once inference is running, explain how the results are written to disk and -> show me how to inspect and interpret them with PyAutoFit. - -Your existing science code remains the source of the likelihood. With PyAutoFit built around it, you can perform inference through natural language while gaining access to features such as flexible priors and model composition, MCMC and nested sampling, automated result handling, model comparison and scalable workflows. - -### Setting up the assistant +## Setting up the assistant The assistant runs inside an **AI coding agent** — a tool that reads this repository, executes Python on your computer and inspects the results. That is what lets it install **PyAutoFit**, wrap your likelihood code, run @@ -53,8 +45,9 @@ priors, planning an analysis or learning in Teacher Mode all happen inside the s claude # or: codex ``` -3. **Submit the starting prompt** at the top of this README (or the "Bring Your Own Likelihood" prompt if you already - have likelihood code). +3. **Submit the starting prompt — the tour begins.** Paste the prompt at the top of this README and the assistant + opens the guided tour at step 1. (If you already have likelihood code, the "Bring Your Own Likelihood" prompt + under [Your own project](#your-own-project) starts the same workflow from your code instead.) **Experimental alternative.** [OpenCode](docs/setup/opencode_cli.md) is an open-source coding agent whose client is free; the model you connect it to is a separate choice with its own cost and capability, free offerings are often @@ -62,7 +55,7 @@ time-limited, and no provider/model configuration has yet been validated against compatible, not tested. Browser chats with a GitHub connector are **no longer supported** (retired 2026-09-10; the old pages are archived with a notice under [`docs/archive/`](docs/archive/README.md)). -### Using PyAutoFit Assistant +## The tour, step by step Every step below can be requested in natural language — you do not need to write Python to follow it. The workflow is: **model → priors → likelihood → search → results → scientific workflow**. @@ -72,7 +65,7 @@ To try the bundled example, ask: > Fit the bundled dataset in dataset/gaussian_x1/ with a 1D Gaussian. > Explain the model, priors, likelihood, search and results as we go. -#### Contents +### Contents - **Compose the Model**: Describe model components and assign priors to named parameters. - **Define the Likelihood**: Specify how the model is compared with your data, or supply existing likelihood code. @@ -81,7 +74,7 @@ To try the bundled example, ask: - **Saving and Loading**: Save results and images during fitting, then reload completed runs for further analysis. - **Scientific Workflows**: Compose more complex models, compare inference algorithms and investigate saved results. -#### Example +### Example We will fit a 1D Gaussian profile to noisy data and infer its centre, normalization and width. The data points and their uncertainties are shown @@ -100,7 +93,7 @@ Here, $x$ is the coordinate, $c$ is the centre, $N$ is the normalization is to infer $c$, $N$ and $\sigma$ from the data, together with their uncertainties. -#### Compose the model +### Compose the model > Create a 1D Gaussian model with free centre, normalization and sigma. > Use uniform priors from 0 to 100 for centre, 0 to 100 for normalization, @@ -113,9 +106,9 @@ Total Free Parameters = 3 model Gaussian (N=3) -centre UniformPrior [1], lower_limit = 0.0, upper_limit = 100.0 -normalization LogUniformPrior [2], lower_limit = 1e-06, upper_limit = 1000000.0 -sigma UniformPrior [3], lower_limit = 0.0, upper_limit = 25.0 +centre UniformPrior [0], lower_limit = 0.0, upper_limit = 100.0 +normalization UniformPrior [1], lower_limit = 0.0, upper_limit = 100.0 +sigma UniformPrior [2], lower_limit = 0.1, upper_limit = 30.0 ``` Models are highly customizable: you can ask to fix a parameter, @@ -124,7 +117,11 @@ science, simply ask the assistant to compose your model for you. **AI First Design.** Internally, PyAutoFit composes the model with a name (`Gaussian`), named parameters (`centre`, `normalization`, `sigma`) and an expressive naming convention (e.g. `model.gaussian.sigma`) which ensure the AI can easily map natural language descriptions of the model to changes in its internal representation. -#### Define the likelihood +**Try it:** reword it — "make it two Gaussians", "fix sigma to 10", "put a Gaussian prior on centre, mean 50, sigma 10" — and watch `model.info` change. + +*Ask anything at this step — "what is a prior?", "why nested sampling?" — or say "teacher mode" for full explanations.* + +### Define the likelihood > Load the 1D Gaussian data and noise map, define a likelihood function which uses > independent Gaussian errors to compare the model with the data and for a random @@ -148,7 +145,11 @@ You can also give the assistant papers and descriptions of your data, parameters and assumptions. This supplies the scientific context so you can use domain-specific natural language while keeping it separate from the inference code. -#### Choose a search +**Try it:** ask for the likelihood at the true parameters (`centre=50`, `normalization=25`, `sigma=10`) and compare it with the random draw. + +*Ask anything at this step — "what is a prior?", "why nested sampling?" — or say "teacher mode" for full explanations.* + +### Choose a search > Show me the available non-linear searches, including those which support > gradient based inference using JAX. For this example fit, our likelihood @@ -173,7 +174,11 @@ goal. **AI First Design.** All PyAutoFit searches share a common interface, so the agent can switch between them while retaining the model and likelihood, making it easy to compare inference across different searches. -#### Fit and inspect the result +**Try it:** ask "use Emcee instead" — or "what changes with 50 live points?" — and have the trade-off explained before you commit to it. + +*Ask anything at this step — "what is a prior?", "why nested sampling?" — or say "teacher mode" for full explanations.* + +### Fit and inspect the result > Run the model fit. Show the parameter estimates and uncertainties, and plot > the maximum-likelihood Gaussian over the data. @@ -187,7 +192,11 @@ a plot of the fitted profile. You can then explore the result: **AI First Design.** Results preserve the model's named parameters (e.g. `result.instance.gaussian.sigma`), so the agent can connect the scientific quantities you specify via language to the numerical results. -#### Save and revisit the analysis +**Try it:** ask "what is the Bayesian evidence, and what is it for?" or "how many likelihood evaluations did that take?". + +*Ask anything at this step — "what is a prior?", "why nested sampling?" — or say "teacher mode" for full explanations.* + +### Save and revisit the analysis > Save the run to disk, with fit and residual images updated during > sampling. Afterwards, reload the saved samples and inspect the fit @@ -205,7 +214,11 @@ search, model or result properties. For example: **AI First Design.** Structured, persistent outputs give the agent a history of experiments it can reload, query and compare as your analysis grows. -#### Extend the workflow +**Try it:** run the same script again — a completed search reloads instead of re-fitting, which surprises everyone once. + +*Ask anything at this step — "what is a prior?", "why nested sampling?" — or say "teacher mode" for full explanations.* + +### Extend the workflow The same building blocks support more involved requests: @@ -240,29 +253,70 @@ ones successfully find the best-fit reliably. The assistant uses saved samples and the original data to reinspect an already completed fit. -#### HowToFit / Teacher Mode +**Try it:** pick one of the three prompts above; you do not have to run all of them — and note the three-Gaussian fit is the first that takes real time, so say "use Nautilus instead" if you would rather not wait on Dynesty. -For users less familiar with Bayesian inference and scientific analysis you may wish to read through -the **HowToFits** lectures. These teach you the basic principles of Bayesian inference, with the -content pitched at undergraduate level and above. +*Ask anything at this step — "what is a prior?", "why nested sampling?" — or say "teacher mode" for full explanations.* -The lectures are available in the [standalone HowToFit repository](https://github.com/PyAutoLabs/HowToFit). +## Your own project -If you're new to statistical inference and are not totally sure what concepts like a model, likelihood or -sampling are, you can use **teacher mode** to have the assistant explain concepts in more detail. Simply -start a prompt with "Teacher mode." and ask questions: +The tour does not stop at the Gaussian. The same six steps run on your science, in this order — ask for any of them +directly if you would rather skip ahead. -> Teacher mode. +**P0. Science context.** Start from a paper or from a plain-language description of what you measure and what you +want to infer, plus where your data lives and in what format. The assistant restates your problem in inference terms +— parameters, data, the shape of the likelihood — for you to correct. + +PyAutoFit is domain agnostic: **you bring the scientific context**. The assistant ships with a statistics wiki +covering Bayesian inference, priors, searches and model comparison. Its **literature wiki** at `wiki/literature/` +is yours to populate with the papers that define your field and analysis. + +Adding papers lets the assistant connect your natural language scientific descriptions to its inference: what +parameters mean, which assumptions are conventional, how previous studies approached the problem, and what might +complicate the interpretation of a result. + +To add a paper, simply ask: + +> Ingest this paper into the literature wiki: [arXiv ID, link or local PDF]. +> Summarise its model, likelihood, priors and main conclusions, and explain +> how it relates to the analysis we are developing. + +The wiki builds a lasting reference for your project, so scientific context is available alongside your code and +results. As you add relevant papers, the assistant can draw on them to frame decisions, cite prior work and identify +caveats worth investigating. + +**P1. Model.** Your parametrisation, described in words, becomes a PyAutoFit model with explicit priors, and +`model.info` shows you exactly what will be fitted. Describe it differently and the model changes with it. + +**P2. Likelihood.** If you already have likelihood code, it stays yours: the assistant wraps it and checks it +returns the same numbers, never re-parametrising or "fixing" your science. If you do not, it builds a simple one +from your description. Either way your real data is plotted and discussed — unmodelled data pathologies are the +first source of biased inference, so this step is not optional. + +Already have a likelihood function for your science problem? **Point the assistant at your existing code and it can set it up with PyAutoFit** — defining the model, choosing priors with you, configuring a search and organising the results: + +> Set up PyAutoFit with my existing science project. An example likelihood +> function can be found at [GitHub link or local directory]. > -> I'm new to PyAutoFit and want to learn the basic workflow end-to-end. Fit the -> bundled 1D Gaussian dataset in dataset/gaussian_x1/ and recover its input -> parameters. +> First, give me an overview of my project and likelihood function. Compose +> an appropriate model, explain it to me, and recommend a non-linear search +> (for example MCMC, nested sampling or maximum-likelihood estimation). > -> Explain what each step is doing and why as we go: composing the model, choosing -> the priors, picking the non-linear search, and how to read the posterior. So I -> come away understanding the workflow, not just the commands. +> Do not begin inference until we have discussed the setup and I give you +> the go-ahead. +> +> Once inference is running, explain how the results are written to disk and +> show me how to inspect and interpret them with PyAutoFit. + +Your existing science code remains the source of the likelihood. With PyAutoFit built around it, you can perform inference through natural language while gaining access to features such as flexible priors and model composition, MCMC and nested sampling, automated result handling, model comparison and scalable workflows. -## Science Project +**P3. Search.** Runtime budget, whether your likelihood is JAX-differentiable, and whether you need the Bayesian +evidence pick the sampler between them. You get a recommendation with the reason, not just a name. + +**P4. Fit and results.** Nothing is fitted until you give the go-ahead. Then the search runs, the output folder is +toured while it does, and the results are plotted and read back to you. There is no truth to check against on real +data, so the check becomes the residuals and the shape of the posterior. + +**P5. Save and organise.** When the analysis is worth keeping, it can become a **science project** of its own. When you begin a scientific study, `autofit_assistant` can create a dedicated **science project**: a structured folder linked to a GitHub repository containing your data, model and likelihood code, prior and search configurations, results, plotting scripts, and a record of your work with the assistant. @@ -277,25 +331,38 @@ To begin, include a request like this in your prompt: > the project so we can compare alternative models and share the results > with collaborators. -## Scientific Context +**P6. Extend.** Model comparison, sampler comparison, chaining searches into a pipeline, hierarchical models over +many datasets — the PyAutoFit +[Scientific Workflow](https://pyautofit.readthedocs.io/en/latest/overview/scientific_workflow.html) and +[Statistical Methods](https://pyautofit.readthedocs.io/en/latest/overview/statistical_methods.html) pages describe +what is available, and you can ask for any of it the same way. -PyAutoFit is domain agnostic: **you bring the scientific context**. The assistant ships with a statistics wiki -covering Bayesian inference, priors, searches and model comparison. Its **literature wiki** at `wiki/literature/` -is yours to populate with the papers that define your field and analysis. +*Ask anything at this step — "what is a prior?", "why nested sampling?" — or say "teacher mode" for full explanations.* -Adding papers lets the assistant connect your natural language scientific descriptions to its inference: what -parameters mean, which assumptions are conventional, how previous studies approached the problem, and what might -complicate the interpretation of a result. +Everything above was natural language. You did not write a line of Python — though the assistant saved you the +scripts anyway, one per step, so you can read, re-run and modify them yourself. -To add a paper, simply ask: +## Teacher mode and HowToFit -> Ingest this paper into the literature wiki: [arXiv ID, link or local PDF]. -> Summarise its model, likelihood, priors and main conclusions, and explain -> how it relates to the analysis we are developing. +For users less familiar with Bayesian inference and scientific analysis you may wish to read through +the **HowToFits** lectures. These teach you the basic principles of Bayesian inference, with the +content pitched at undergraduate level and above. -The wiki builds a lasting reference for your project, so scientific context is available alongside your code and -results. As you add relevant papers, the assistant can draw on them to frame decisions, cite prior work and identify -caveats worth investigating. +The lectures are available in the [standalone HowToFit repository](https://github.com/PyAutoLabs/HowToFit). + +If you're new to statistical inference and are not totally sure what concepts like a model, likelihood or +sampling are, you can use **teacher mode** to have the assistant explain concepts in more detail. Simply +start a prompt with "Teacher mode." and ask questions: + +> Teacher mode. +> +> I'm new to PyAutoFit and want to learn the basic workflow end-to-end. Fit the +> bundled 1D Gaussian dataset in dataset/gaussian_x1/ and recover its input +> parameters. +> +> Explain what each step is doing and why as we go: composing the model, choosing +> the priors, picking the non-linear search, and how to read the posterior. So I +> come away understanding the workflow, not just the commands. ## License diff --git a/docs/setup/claude_code.md b/docs/setup/claude_code.md index 947fd40..64fea0e 100644 --- a/docs/setup/claude_code.md +++ b/docs/setup/claude_code.md @@ -38,7 +38,8 @@ Explain the model, priors, likelihood, search and results as we go. ``` New to Bayesian inference? Start the prompt with `Teacher mode.` and the assistant explains -the concepts as it goes. You do not have to let it run anything: ask it to plan the analysis and -discuss the model and priors before any search is submitted. +the concepts as it goes — or just ask, the start-here tour takes questions at every step. You do +not have to let it run anything: ask it to plan the analysis and discuss the model and priors +before any search is submitted. Experimental alternative if you cannot use Claude Code or Codex: [OpenCode](opencode_cli.md). diff --git a/docs/setup/codex_cli.md b/docs/setup/codex_cli.md index 1dc79e7..1162e59 100644 --- a/docs/setup/codex_cli.md +++ b/docs/setup/codex_cli.md @@ -41,7 +41,8 @@ Explain the model, priors, likelihood, search and results as we go. ``` New to Bayesian inference? Start the prompt with `Teacher mode.` and the assistant explains -the concepts as it goes. You do not have to let it run anything: ask it to plan the analysis and -discuss the model and priors before any search is submitted. +the concepts as it goes — or just ask, the start-here tour takes questions at every step. You do +not have to let it run anything: ask it to plan the analysis and discuss the model and priors +before any search is submitted. Experimental alternative if you cannot use Claude Code or Codex: [OpenCode](opencode_cli.md). diff --git a/modes/start_here.md b/modes/start_here.md new file mode 100644 index 0000000..d5a3988 --- /dev/null +++ b/modes/start_here.md @@ -0,0 +1,458 @@ +# Start here mode + +The guided first session. It runs the six-step natural-language workflow of the RTD +[Natural Language Inference](https://pyautofit.readthedocs.io/en/latest/overview/natural_language.html) +page on the bundled 1D Gaussian dataset — **one step per turn, with the user typing each prompt +themselves** — and then replays the same six steps on the user's own science. + +This file is a script for *you*, the assistant — what to say, what to hand over, where to stop — +not user-facing prose. The tour adds **no capability**: every step is an existing skill driven at +`skills/_style.md` "Newcomer mode" depth. + +## What changes + +- **One step per turn.** Explain, hand over the next prompt, then **stop and wait**. Never run + ahead, batch two steps, or type the user's prompt for them — the point is that they discover + the workflow *is* natural language by using it. +- **Newcomer-mode pacing:** one concept at a time, statistical framing before code, a check-in + beat closing every step, and the two-line footer below on every step of both parts. +- **Generated Python is kept**, one script per step under `scripts/start_here/` — they leave with + runnable files, not a transcript. + +## What stays the same + +- Every `AGENTS.md` safety invariant: the data-inspection gate (mandatory on real data in Part 2; + the bundled dataset is simulated and exempt), the code gate, "the likelihood code is the + user's", the `output/` write-ban, the source-edit boundary. +- Saved Python is at full `skills/_style.md` "Generated script style" quality — mode-invariant. +- The skills are unchanged. This mode composes `af_compose_model`, `af_custom_analysis`, + `af_configure_search`, `af_run_search`, `af_plot_fit`, `af_load_results`, `af_adapt_to_domain`, + `af_ingest_paper`, `af_wrap_likelihood` and `start-new-project`; read the skill rather than + restating it here. + +## Composition + +Depth follows `skills/_style.md` "Adaptive depth", entering at **Newcomer mode** unless turn 0's +question or an existing `wiki/project/profile.md` says otherwise — a recorded expert level is not +overridden, so an expert gets the *workflow*, not Bayes from first principles. "Teacher mode" +mid-tour switches `modes/teacher.md` on for the rest of the tour without leaving it. + +## What triggers inference + +The published start-here prompt (below), "start here", "start_here", "begin the start here +guide", "walk me through PyAutoFit from the beginning" — outranking inference from the rest of +the opening request; `.maintainer` outranks it. + +--- + +## Trigger and posture + +The published prompt — on the PyAutoFit README, this repo's README and the RTD Quick Start — is: + +> I want to perform scientific inference with PyAutoFit (https://github.com/PyAutoLabs/PyAutoFit) and the +> autofit_assistant (https://github.com/PyAutoLabs/autofit_assistant). +> +> Begin the "start here" guide for a new user. + +Before turn 0, run the session-start drift check (`AGENTS.md` "Session start"): +`python autoassistant/audit_skill_apis.py --check-version`. Exit 0 — continue. Exit 2 or 3 — the +stack is absent or broken: run `af_setup_environment`, say so in one line, resume at turn 0. Exit +1 — report the drift and offer the pinned version; the tour can still run. + +Honour **"skip to step N"** and **"skip to my project"** immediately, always saying in one line +what is skipped — never skip silently. "Back up" returns to the previous step. + +--- + +## Turn 0 — the intro + +One turn, in this order: + +1. **What PyAutoFit is**, in a paragraph: a framework for fitting *your* model to *your* data + with *your* likelihood. It supplies the inference — priors, samplers, results, model + comparison — and stays out of the science, which is why it works across fields. +2. **The workflow line**, verbatim: + `model -> priors -> likelihood -> search -> results -> scientific workflow` — one tour step + per link, all of it done by typing English. +3. **The dataset.** `dataset/gaussian_x1/`: 100 pixels of a 1D Gaussian plus noise, simulated with + `centre=50.0`, `normalization=25.0`, `sigma=10.0` and per-pixel noise sigma 2.0. Quote those + truth values — they make step 4 judgeable rather than merely reportable + (`dataset/gaussian_x1/README.md`). +4. **The equation**: `g(x) = N exp[-1/2 ((x - c) / sigma)^2]`, with `c` the centre, `N` the peak + amplitude and `sigma` the width. These three are what gets inferred. +5. **Plot the data and announce the absolute path**, offering to open it (`skills/_style.md` + "Plot output and path announcement"). The data are simulated so the gate does not formally + apply, but a newcomer should see what is being fitted before it is fitted. +6. **The footer, once, in full** (see "Teacher mode and questions"), naming + [HowToFit](https://github.com/PyAutoLabs/HowToFit) as the lecture-notes companion. +7. **At most one question**: *"Are you new to Bayesian inference, or just new to PyAutoFit?"* — it + sets depth, nothing more. Write `wiki/project/profile.md` only if they volunteer durable + context unprompted (`AGENTS.md` "First-interaction protocol"). +8. **Hand over the step-1 prompt and STOP.** + +--- + +## Part 1 — the six steps + +Every step has the same five beats: **Explain** (2-4 sentences of statistical then domain framing +plus one `wiki/core/` link — not a lecture) → **Prompt to type** (the RTD page's prompt for that +section, verbatim, in a blockquote, with "type this as your next message") → **Run and show** (do +the work through the named skill, save the script, show the *real* output) → **Try a variation** +(two or three rewordings they could type instead, making the point that the description *is* the +model) → **check-in** (the footer, then stop). + +### Step 1 — Compose the Model + +*Skill:* `af_compose_model`. *Script:* `scripts/start_here/step_1_compose_model.py`. + +**Explain.** A model is a plain Python class; its `__init__` arguments become the free parameters, +each with a prior — a statement of what is believed before the data are seen. Point at +`wiki/core/concepts/model_composition_and_priors.md`. + +The tour's Gaussian is **peak-normalised**, as the equation says, and lives in +`scripts/start_here/gaussian.py`; PyAutoFit's example class `af.ex.Gaussian` is *area*-normalised +(dividing by `sigma * sqrt(2 pi)`), which here would put the normalization near 620 — outside the +0-100 prior this step prescribes. If the user asks, explain the difference: the convention belongs +to the model class, and the model class is theirs. + +**Prompt to type.** + +> Create a 1D Gaussian model with free centre, normalization and sigma. +> Use uniform priors from 0 to 100 for centre, 0 to 100 for normalization, +> and 0.1 to 30 for sigma. Show me the model and its priors. + +**Run and show.** `model.info` — the real output: + +```bash +Total Free Parameters = 3 + +model Gaussian (N=3) + +centre UniformPrior [0], lower_limit = 0.0, upper_limit = 100.0 +normalization UniformPrior [1], lower_limit = 0.0, upper_limit = 100.0 +sigma UniformPrior [2], lower_limit = 0.1, upper_limit = 30.0 +``` + +Read it back: three free parameters, each with an explicit prior, named the way they named them. + +**Try a variation.** *"Make it two Gaussians"* becomes an `af.Collection` with six free parameters +and nothing else about the workflow changed: + +```bash +Total Free Parameters = 6 + +model Collection (N=6) + gaussian_0 - gaussian_1 Gaussian (N=3) + +gaussian_0 + centre UniformPrior [0], lower_limit = 0.0, upper_limit = 100.0 + normalization UniformPrior [1], lower_limit = 0.0, upper_limit = 100.0 + sigma UniformPrior [2], lower_limit = 0.1, upper_limit = 30.0 +gaussian_1 + centre UniformPrior [3], lower_limit = 0.0, upper_limit = 100.0 + normalization UniformPrior [4], lower_limit = 0.0, upper_limit = 100.0 + sigma UniformPrior [5], lower_limit = 0.1, upper_limit = 30.0 +``` + +Also offer *"fix sigma to 10"* (one dimension gone) and *"put a Gaussian prior on centre, mean 50, +sigma 10"* (a belief rather than a flat range). + +### Step 2 — Define the Likelihood + +*Skills:* `af_custom_analysis`, and `af_wrap_likelihood` for the user's own code. +*Scripts:* `step_2_define_likelihood.py`, with the likelihood in `analysis.py`. + +**Explain.** The likelihood turns parameter values into a number: here independent Gaussian +errors, so `ln L = -1/2 sum ((d - m)/sigma)^2`. It lives in an `Analysis` class holding the data +and implementing one method. Point at `wiki/core/concepts/bayesian_inference.md`. + +**Prompt to type.** + +> Load the 1D Gaussian data and noise map, define a likelihood function which uses +> independent Gaussian errors to compare the model with the data and for a random +> set of parameters calculate the likelihood. Produce an image comparing the fit +> to the data + +**Run and show.** The log likelihood at one random draw from the priors, plus the model-vs-data +figure with residuals, path announced with an offer to open it. Say why the number is so bad: a +random draw is *meant* to be a bad fit, and seeing a bad fit score badly is how you check the +likelihood is wired up at all. The likelihood at the simulation truth alongside gives the scale of +the gradient the search will climb. + +**Try a variation.** *"Evaluate it at the true parameters instead"*, and — the pointer to +`af_wrap_likelihood`, which is why Part 2 works at all — the page's bring-your-own-likelihood +prompt: + +> Use my existing likelihood code for this analysis [point to code]. Connect +> it to PyAutoFit and check that it returns the same likelihood values at +> the same parameter values. + +### Step 3 — Searches + +*Skill:* `af_configure_search`. *Script:* `step_3_choose_search.py`. + +**Explain.** The search explores the parameter space, and the choice is statistical before it is +computational: evidence needed (nested), posterior only (MCMC), or a best-fit point (optimiser). +Point at `wiki/core/concepts/non_linear_search.md`. + +**Prompt to type.** + +> Show me the available non-linear searches, including those which support +> gradient based inference using JAX. For this example fit, our likelihood +> function is not implemented using JAX, so lets use Dynesty nested sampling +> with 100 live points to estimate the posterior and evidence. + +**Run and show.** The installed roster, read from the library rather than from memory, in the +page's four families — **nested sampling** (`Nautilus`, `DynestyStatic`, `DynestyDynamic`), +**MCMC** (`Emcee`, `Zeus`), **optimisation** (`LBFGS`, `Drawer`), **gradient-based JAX** +(`BlackJAXNUTS`, the `MultiStart*` optimisers) — then the configured +`af.DynestyStatic(path_prefix="start_here", name="gaussian_x1", nlive=100)`. Say why Dynesty and +not a JAX sampler (this likelihood is plain NumPy, so there are no automatic gradients to take), +and why nested and not MCMC (step 6 compares evidences). + +**Try a variation.** *"Use Emcee instead"* (posterior but no evidence, so step 6's comparison would +be unavailable) and *"what changes with 50 live points?"* (the accuracy/runtime dial). + +### Step 4 — Model Fit and Results + +*Skills:* `af_run_search`, `af_plot_fit`, `af_load_results`. *Script:* `step_4_fit_and_results.py`. + +**Explain.** Model, likelihood and search meet in one call — seconds on this dataset. Say the +expected wall time before launching: on their own data that estimate is the difference between a +coffee and an HPC job. + +**Prompt to type.** + +> Run the model fit. Show the parameter estimates and uncertainties, and plot +> the maximum-likelihood Gaussian over the data. + +**Run and show.** While it runs, do the **output-folder tour** — not a path, a tour +(`skills/_style.md` "Output folder announcement"): the absolute +`output/start_here/gaussian_x1//`, `model.results` first, then `model.info`, +`search.summary`, `files/` and `image/`, and the fact that they update *as the search runs*. Then +the parameter table against turn 0's truth — the real numbers from this dataset: + +```bash +parameter inferred (median, 1 sigma) truth +-------------------------------------------------------------- +centre 50.00 +0.28 / -0.28 50.0 +normalization 24.89 +0.67 / -0.58 25.0 +sigma 9.82 +0.28 / -0.26 10.0 +``` + +with `log evidence: -48.95` — plus the maximum-likelihood profile over the data with residuals, +path announced. Then the sentence that matters: every parameter is recovered within about one sigma +of its input, so the machinery works, and that check is what you do first on real data too. + +Then hand over the page's second prompt, in the same step: + +> Plot the posterior distributions. How well is sigma constrained, and +> is it correlated with normalization? + +and show the corner plot, reading it for them: sigma is constrained to about 3%, and its +correlation with normalization is the visible one — a wider Gaussian needs a lower peak to keep the +same total signal. + +**Try a variation.** *"What is the Bayesian evidence, and what is it for?"*, *"How many likelihood +evaluations did that take?"* (`search.summary`). + +### Step 5 — Saving and Loading + +*Skill:* `af_load_results`. *Script:* `step_5_save_and_load.py`. + +**Explain.** The fit already saved itself; this step reads it back. A result that outlives its +Python session is what makes a hundred fits manageable instead of terrifying. + +**Prompt to type.** + +> Save the run to disk, with fit and residual images updated during +> sampling. Afterwards, reload the saved samples and inspect the fit +> without rerunning it. + +**Run and show.** Walk the tree they saw mid-fit, now complete, and point out the `.zip` beside it. +Then reload through the aggregator **without re-running the search**: same evidence, same medians. +Two things to say out loud — re-running the script reloads rather than re-fits (this surprises +everyone once), and paths are never composed by hand because the unique identifier changes with the +model. One line on `af_inspect_results_mcp`, which exposes the same folder to a chat client with no +code execution. + +**Try a variation.** The page's aggregator prompt, where one fit becomes a catalogue: + +> Find the completed Gaussian fits and make a table of the inferred widths +> and their uncertainties, labelled by dataset and search algorithm. + +### Step 6 — Scientific Workflows + +*Skills:* `af_compose_model`, `af_chain_searches`, `af_load_results`. +*Scripts:* `step_6_three_gaussians.py`, and `step_6_three_gaussians_nautilus.py` for the faster +sampler. + +**Explain.** Everything so far was one model and one fit. A scientific workflow is what happens +when there are several — competing models, competing samplers, a backlog of saved runs. Offer the +page's three extensions; let the user pick one **or more**, and never run all three unasked. + +**Extension 1 — model comparison.** + +> Extend the model to three Gaussians and sum their profiles in the +> likelihood. Assert that their centres are in ascending order, show me +> the priors, perform inference with Dynesty again and compare the Bayesian +> evidence with the single-Gaussian fit. + +**Warn before you launch it.** This is the first fit that costs real time, and they should hear +why before watching a silent terminal: nine free parameters instead of three, and the +ascending-centre assertion rejects most prior draws, so Dynesty's ellipsoidal bounds spend almost +every proposal on points the assertion throws away. The dry run measured **0.21% sampling +efficiency** — 346,992 likelihood calls for 733 accepted samples — and after **30 minutes** was +still at `dlogz` 339 against a 0.109 target, nowhere near converged and slowing (7.7 s per +accepted sample by the end). Say that out loud rather than letting them find it. + +**Offer the faster sampler.** *"If you'd rather not wait, say 'use Nautilus instead'"* — +`step_6_three_gaussians_nautilus.py` is the same model, priors, assertion and likelihood with +`af.Nautilus(n_live=100)` in place of Dynesty, and finishes in **about 2.5 minutes**. That swap is +the lesson `af_configure_search` teaches: every search shares one interface, so the sampler is the +cheapest thing in an analysis to change. Both fits under Nautilus: + +```bash +model N max log L log evidence +-------------------------------------------------------- +one Gaussian 3 -36.13 -49.41 +three Gaussians 9 -33.65 -60.23 +``` + +**Read it for them.** The three-Gaussian model reaches a *higher* maximum likelihood — with six +more parameters it always will — and a log evidence **11 lower**, strong preference for the single +Gaussian on the usual Jeffreys-style reading. The data came from one Gaussian, two components have +nothing to fit and wander over their whole prior range, and the evidence charges for that unused +prior volume automatically: nobody added a penalty term, and Occam's razor arrived on its own. +Mention that the assertion is what removes the six-fold relabelling degeneracy of three identical +components, and that it is a prior statement like any other — so this is the evidence *under these +priors*. + +If they run the Dynesty version anyway, do not wait in silence: suggest extension 2 or 3, or +reading `output/start_here/gaussian_x3//model.results`, while it runs. Quote evidence +numbers only from a run that finished; step 4's single-Gaussian Dynesty reference is +`log evidence: -48.95`. + +**Extension 2 — sampler comparison.** + +> Fit the same model using Emcee, Dynesty and an optimiser for maximum +> likelihood estimation. Keep the likelihood and parameter bounds fixed, +> and use the same priors for both samplers. Compare runtime, likelihood +> evaluations and best-fit values. For the samplers, also assess convergence +> and agreement of posterior constraints. + +**Extension 3 — revisiting a saved result.** + +> Load the saved Gaussian fit. Report the median and 68% credible interval +> for sigma, plot its correlation with normalization, and inspect the +> residuals for structure the model may have missed. + +--- + +## Part 2 — your own project + +Enter after step 6, or the moment the user says "now my project" / "skip to my project". Say in one +line that the same six steps now run on their science, and that this is the part that persists. + +The engine is [`af_adapt_to_domain`](../skills/af_adapt_to_domain.md) — **read it and run its +interview from there; do not restate it here.** This mode supplies the running order and the pacing +only. Note as you go that their model class defines its own normalisation convention, parameter +names and units — precisely why step 1 used a plain Python class rather than a framework object. + +**P0 — Science context.** Ask for **either** a paper (arXiv ID, link or local PDF → +`af_ingest_paper`, into `wiki/literature/`) **or** a plain-language description of what they measure +and want to infer, plus where their data lives and in what format. Run the `af_adapt_to_domain` +interview lightly; write `wiki/project/profile.md` and `state.md` per `AGENTS.md`. **Restate their +problem in inference terms in one paragraph** — parameters, data, the shape of the likelihood — and +ask them to correct it; that paragraph is the contract for everything below. Any selection cut made +while loading their data (a redshift floor, a quality flag) is a scientific decision: state it back +and record it. + +**P1 — Model.** Their parametrisation, in words, becomes `af.Model` / `af.Collection` via +`af_compose_model`; show `model.info` and check the parameter count is what they expected. Then step +1's nudge again: *"describe it differently and watch the model change"*. + +**P2 — Likelihood.** **They have code:** `af_wrap_likelihood` — wrap it into an `Analysis`, validate +at hand-built instances against their own function's values, **never alter its numerics** +(`AGENTS.md`). **No code:** build a small `Analysis` from their description via +`af_custom_analysis`. Either way the **data-inspection gate is mandatory here** — real data: plot it +(or summarise it numerically), show the absolute path, and ask one question about known artefacts, +outliers or selection effects before anything is fitted. + +**P3 — Search.** Ask three things — runtime budget, whether the likelihood is JAX-differentiable, +whether they need the evidence — then `af_configure_search` gives a recommendation **with the +reason**, not just a name. + +**P4 — Fit and results.** Ask for an explicit go-ahead first; the page's bring-your-own-likelihood +prompt sets the default posture ("Do not begin inference until we have discussed the setup and I +give you the go-ahead"). Then `af_run_search`, the output-folder tour on *their* output, +`af_plot_fit` and `af_load_results`. Real data has no truth to check against, so the check becomes +the residuals and the shape of the posterior — say so explicitly. + +**P5 — Save and organise.** Offer [`start-new-project`](../skills/start-new-project.md) for a +standalone science-project repo — offer it, do not force it; quick exploration is fine here. + +**P6 — Extend.** Model comparison, sampler comparison, search chaining (`af_chain_searches`), +hierarchical models — the RTD +[Scientific Workflow](https://pyautofit.readthedocs.io/en/latest/overview/scientific_workflow.html) +and +[Statistical Methods](https://pyautofit.readthedocs.io/en/latest/overview/statistical_methods.html) +pages, and the matching skills. + +--- + +## Teacher mode and questions + +Append this footer to **every** step of both parts, verbatim: + +> Ask anything here - "what is a prior?", "why nested sampling?" - and I'll explain before we +> move on. Say "teacher mode" for full explanations at every step. + +Turn 0 says it once in full, adding that [HowToFit](https://github.com/PyAutoLabs/HowToFit) is the +lecture-notes companion for the statistics itself. + +- **A question never advances the step.** Answer it, then re-offer the same prompt. Only "next", or + the user typing the step's prompt, moves the tour on. +- **"Teacher mode" mid-tour** switches `modes/teacher.md` on for the rest of the tour without + leaving it: more *why*, explicit assumptions, wiki and HowToFit pointers. The step script is + unchanged; the depth around it grows. +- **Three or more concept questions in a row** is a signal — offer teacher mode, or offer to pause + and read the relevant HowToFit tutorial together. + +--- + +## Hand-off + +When both parts are done, or the user stops: + +- One line: the session now runs in **assistant mode** (or **teacher mode** if chosen), and the + tour's pacing rules no longer apply. +- Name what is on disk — scripts under `scripts/start_here/`, fits under `output/`, and any + `wiki/project/` or `wiki/literature/` entries Part 2 wrote. +- Where to go next: [`start-new-project`](../skills/start-new-project.md) for a project repo, + [`af_ingest_paper`](../skills/af_ingest_paper.md) for more domain context, the RTD Scientific + Workflow and Statistical Methods pages for scale, + [HowToFit](https://github.com/PyAutoLabs/HowToFit) for the statistics. + +--- + +## Rules + +- **Part-1 prompts are byte-identical** to `PyAutoFit:docs/overview/natural_language.md` — never + paraphrase, reflow or "improve" one; the tour, that page and this repo's `README.md` are one + artefact in three places. +- **Never fabricate output.** Every number, table, path and `model.info` the tour shows comes from + running the script. If a run fails, say so and debug it (`af_debug_fit_failure`); a + plausible-looking invented result is this mode's one unrecoverable failure. +- **All `AGENTS.md` safety invariants apply**, code gate included: on a harness without hooks, + self-enforce with `python autoassistant/audit_skill_apis.py --file scripts/start_here/