{"id":35699755,"url":"https://github.com/queelius/compositional.mle","last_synced_at":"2026-03-04T08:01:23.722Z","repository":{"id":274299466,"uuid":"637221012","full_name":"queelius/compositional.mle","owner":"queelius","description":"Composable MLE solvers: a DSL for maximum likelihood estimation where solvers are first-class functions that combine via chaining, racing, and restarts","archived":false,"fork":false,"pushed_at":"2026-02-01T07:18:40.000Z","size":5785,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2026-02-01T16:34:43.868Z","etag":null,"topics":["composable","dsl","estimation","maximum-likelihood","mle","mle-estimation","numerical-methods","optimization","r-package","statistics"],"latest_commit_sha":null,"homepage":"https://queelius.github.io/compositional.mle/","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/queelius.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2023-05-06T22:02:22.000Z","updated_at":"2026-02-01T07:18:45.000Z","dependencies_parsed_at":"2025-12-21T12:01:06.480Z","dependency_job_id":null,"html_url":"https://github.com/queelius/compositional.mle","commit_stats":null,"previous_names":["queelius/numerical.mle","queelius/compositional.mle"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/queelius/compositional.mle","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/queelius%2Fcompositional.mle","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/queelius%2Fcompositional.mle/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/queelius%2Fcompositional.mle/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/queelius%2Fcompositional.mle/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/queelius","download_url":"https://codeload.github.com/queelius/compositional.mle/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/queelius%2Fcompositional.mle/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30075908,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-04T05:31:57.858Z","status":"ssl_error","status_checked_at":"2026-03-04T05:31:38.462Z","response_time":59,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["composable","dsl","estimation","maximum-likelihood","mle","mle-estimation","numerical-methods","optimization","r-package","statistics"],"created_at":"2026-01-06T01:52:21.395Z","updated_at":"2026-03-04T08:01:23.712Z","avatar_url":"https://github.com/queelius.png","language":"R","funding_links":[],"categories":[],"sub_categories":[],"readme":"---\noutput: github_document\n---\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file --\u003e\n\n```{r, include = FALSE}\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\",\n  fig.path = \"man/figures/README-\",\n  out.width = \"100%\"\n)\n```\n\n# compositional.mle\n\n\u003c!-- badges: start --\u003e\n[![R-CMD-check](https://github.com/queelius/compositional.mle/workflows/R-CMD-check/badge.svg)](https://github.com/queelius/compositional.mle/actions)\n\u003c!-- badges: end --\u003e\nAn R package for **composable maximum likelihood estimation**. Solvers are first-class functions that combine via sequential chaining, parallel racing, and random restarts.\n\n## When to Use This Package\n\n**Use compositional.mle when:**\n\n- **Multi-modal likelihoods**: Your likelihood surface has multiple local optima and you need global search strategies (simulated annealing, random restarts)\n- **Coarse-to-fine optimization**: You want to start with a rough global search and progressively refine with local methods\n- **Comparing strategies**: You're unsure which optimizer works best and want to race them automatically\n- **Building robust pipelines**: You need reliable estimation that handles edge cases gracefully\n- **Research/experimentation**: You want to explore optimization strategies and visualize convergence\n\n**Stick with `optim()` when:**\n\n- You have a simple, well-behaved likelihood with a single optimum\n- You know exactly which method works and don't need composition\n\n### Example: Why Composition Matters\n\n```{r why-compose, message=FALSE}\nlibrary(compositional.mle)\n\n# A tricky bimodal likelihood\nset.seed(42)\nbimodal_loglike \u003c- function(theta) {\n  # Two peaks: one at theta=2, one at theta=8\n  log(0.3 * dnorm(theta, 2, 0.5) + 0.7 * dnorm(theta, 8, 0.5))\n}\n\nproblem \u003c- mle_problem(\n loglike = bimodal_loglike,\n  constraint = mle_constraint(support = function(theta) TRUE)\n)\n\n# Single gradient ascent gets trapped at local optimum\nresult_local \u003c- gradient_ascent()(problem, theta0 = 0)\n\n# Simulated annealing + gradient ascent finds global optimum\nstrategy \u003c- sim_anneal(temp_init = 5, max_iter = 200) %\u003e\u003e% gradient_ascent()\nresult_global \u003c- strategy(problem, theta0 = 0)\n\ncat(\"Local search found:\", round(result_local$theta.hat, 2),\n    \"(log-lik:\", round(result_local$loglike, 2), \")\\n\")\ncat(\"Global strategy found:\", round(result_global$theta.hat, 2),\n    \"(log-lik:\", round(result_global$loglike, 2), \")\\n\")\n```\n\n## Installation\n\n```r\n# From CRAN (when available)\ninstall.packages(\"compositional.mle\")\n\n# Development version\ndevtools::install_github(\"queelius/compositional.mle\")\n```\n\n## Design Philosophy\n\nFollowing SICP principles, the package provides:\n1. **Primitive solvers** - `gradient_ascent()`, `newton_raphson()`, `bfgs()`, `sim_anneal()`, etc.\n2. **Composition operators** - `%\u003e\u003e%` (sequential), `%|%` (race), `with_restarts()`\n3. **Closure property** - Combining solvers yields a solver\n\n## Quick Start\n\n```{r example, message=FALSE}\n# Generate sample data\nset.seed(42)\nx \u003c- rnorm(100, mean = 5, sd = 2)\n\n# Define the problem (separate from solver strategy)\nproblem \u003c- mle_problem(\n  loglike = function(theta) {\n    if (theta[2] \u003c= 0) return(-Inf)\n    sum(dnorm(x, theta[1], theta[2], log = TRUE))\n  },\n  score = function(theta) {\n    mu \u003c- theta[1]; sigma \u003c- theta[2]; n \u003c- length(x)\n    c(sum(x - mu) / sigma^2,\n      -n / sigma + sum((x - mu)^2) / sigma^3)\n  },\n  constraint = mle_constraint(\n    support = function(theta) theta[2] \u003e 0,\n    project = function(theta) c(theta[1], max(theta[2], 1e-8))\n  )\n)\n\n# Simple solve\nresult \u003c- gradient_ascent()(problem, theta0 = c(0, 1))\nresult$theta.hat\n```\n\n## Composing Solvers\n\n### Sequential Chaining (`%\u003e\u003e%`)\n\nChain solvers for coarse-to-fine optimization:\n\n```{r sequential}\n# Grid search -\u003e gradient ascent -\u003e Newton-Raphson\nstrategy \u003c- grid_search(lower = c(-10, 0.5), upper = c(10, 5), n = 5) %\u003e\u003e%\n  gradient_ascent(max_iter = 50) %\u003e\u003e%\n  newton_raphson(max_iter = 20)\n\nresult \u003c- strategy(problem, theta0 = c(0, 1))\nresult$theta.hat\n```\n\n### Parallel Racing (`%|%`)\n\nRace multiple methods, keep the best:\n\n```{r race}\n# Try multiple approaches, pick winner by log-likelihood\nstrategy \u003c- gradient_ascent() %|% bfgs() %|% nelder_mead()\n\nresult \u003c- strategy(problem, theta0 = c(0, 1))\nc(result$theta.hat, loglike = result$loglike)\n```\n\n### Random Restarts\n\nEscape local optima with multiple starting points:\n\n```{r restarts}\nstrategy \u003c- with_restarts(\n  gradient_ascent(),\n  n = 10,\n  sampler = uniform_sampler(c(-10, 0.5), c(10, 5))\n)\n\nresult \u003c- strategy(problem, theta0 = c(0, 1))\nresult$theta.hat\n```\n\n## Visualization\n\nTrack and visualize the optimization path:\n\n```{r visualization, fig.height=4, fig.width=8}\n# Enable tracing\ntrace_cfg \u003c- mle_trace(values = TRUE, gradients = TRUE, path = TRUE)\nresult \u003c- gradient_ascent(max_iter = 50)(problem, c(0, 1), trace = trace_cfg)\n\n# Plot convergence\nplot(result, which = c(\"loglike\", \"gradient\"))\n```\n\nExtract trace as data frame for custom analysis:\n\n```{r trace-df}\npath_df \u003c- optimization_path(result)\nhead(path_df)\n```\n\n## Available Solvers\n\n| Factory | Method | Best For |\n|---------|--------|----------|\n| `gradient_ascent()` | Steepest ascent with line search | General purpose, smooth likelihoods |\n| `newton_raphson()` | Second-order Newton | Fast convergence near optimum |\n| `bfgs()` | Quasi-Newton BFGS | Good balance of speed/robustness |\n| `lbfgsb()` | L-BFGS-B with box constraints | High-dimensional, bounded parameters |\n| `nelder_mead()` | Simplex (derivative-free) | Non-smooth or noisy likelihoods |\n| `sim_anneal()` | Simulated annealing | Global optimization, multi-modal |\n| `coordinate_ascent()` | One parameter at a time | Different parameter scales |\n| `grid_search()` | Exhaustive grid | Finding starting points |\n| `random_search()` | Random sampling | High-dimensional exploration |\n\n## Function Transformers\n\n```{r transformers, eval=FALSE}\n# Stochastic gradient (mini-batching for large data)\nloglike_sgd \u003c- with_subsampling(loglike, data = x, subsample_size = 32)\n\n# Regularization\nloglike_l2 \u003c- with_penalty(loglike, penalty_l2(), lambda = 0.1)\nloglike_l1 \u003c- with_penalty(loglike, penalty_l1(), lambda = 0.1)\n```\n\n## Documentation\n\n- Full documentation: \u003chttps://queelius.github.io/compositional.mle/\u003e\n- Vignettes:\n  - [Getting Started](https://queelius.github.io/compositional.mle/articles/getting-started.html)\n  - [Case Studies](https://queelius.github.io/compositional.mle/articles/case-studies.html)\n  - [Theory and Intuition](https://queelius.github.io/compositional.mle/articles/theory-and-intuition.html)\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqueelius%2Fcompositional.mle","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fqueelius%2Fcompositional.mle","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqueelius%2Fcompositional.mle/lists"}