{"id":15735701,"url":"https://github.com/simonrw/rust-emcee","last_synced_at":"2025-12-12T11:56:47.334Z","repository":{"id":57624739,"uuid":"91246121","full_name":"simonrw/rust-emcee","owner":"simonrw","description":"A re-implementation of emcee in rust","archived":false,"fork":false,"pushed_at":"2019-06-15T15:21:58.000Z","size":123,"stargazers_count":12,"open_issues_count":3,"forks_count":5,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-09-19T15:36:19.199Z","etag":null,"topics":["analysis","fitting","mcmc","rust","science"],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/simonrw.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-05-14T13:45:46.000Z","updated_at":"2025-08-18T10:50:51.000Z","dependencies_parsed_at":"2022-08-26T22:22:25.458Z","dependency_job_id":null,"html_url":"https://github.com/simonrw/rust-emcee","commit_stats":null,"previous_names":["mindriot101/rust-emcee"],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/simonrw/rust-emcee","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonrw%2Frust-emcee","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonrw%2Frust-emcee/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonrw%2Frust-emcee/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonrw%2Frust-emcee/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/simonrw","download_url":"https://codeload.github.com/simonrw/rust-emcee/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonrw%2Frust-emcee/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":27682359,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-12-12T02:00:06.775Z","response_time":129,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["analysis","fitting","mcmc","rust","science"],"created_at":"2024-10-04T01:14:45.461Z","updated_at":"2025-12-12T11:56:47.272Z","avatar_url":"https://github.com/simonrw.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# emcee\n\n[![Join the chat at https://gitter.im/rust-emcee/Lobby](https://badges.gitter.im/rust-emcee/Lobby.svg)](https://gitter.im/rust-emcee/Lobby?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge)\n[![Build Status](https://travis-ci.org/mindriot101/rust-emcee.svg?branch=master)](https://travis-ci.org/mindriot101/rust-emcee)\n[![Crates version](https://img.shields.io/crates/v/emcee.svg)](https://crates.io/crates/emcee)\n[![Docs](https://img.shields.io/badge/docs-emcee-brightgreen.svg)](https://docs.rs/emcee)\n\nA re-implementation of [Dan Foreman-Mackey's][dfm] [emcee][emcee] in Rust.\n\nSee the [hosted documentation here][docs]\n\nThe [`fitting_a_model_to_data` example][fitting-model-to-data] is a re-creation of the [\"fitting a model to\ndata\"][fitting-model-to-data-python] example from the `emcee` documentation.\n\n## Attribution\n\nIf you make use of emcee in your work, please cite Dan's paper ([arXiv](http://arxiv.org/abs/1202.3665), [ADS](http://adsabs.harvard.edu/abs/2013PASP..125..306F), [BibTeX](http://adsabs.harvard.edu/cgi-bin/nph-bib_query?bibcode=2013PASP..125..306F\u0026data_type=BIBTEX)).\n\nA copy of the original MIT license is given under [DFM-LICENSE][dfm-license].\n\n## Basic usage\n\n### Implementing models\n\nThe sampler requires a struct that implements [`emcee::Prob`][emcee-prob], for example:\n\n```rust\nuse emcee::{Guess, Prob};\n\nstruct Model;\n\nimpl Prob for Model {\n    fn lnlike(\u0026self, params: \u0026Guess) -\u003e f32 {\n        // Insert actual implementation here\n        0f32\n    }\n\n    fn lnprior(\u0026self, params: \u0026Guess) -\u003e f32 {\n        // Insert actual implementation here\n        0f32\n    }\n}\n```\n\nThe trait has a default implementation for [`lnprob`][emcee-lnprob] which computes the product\nof the likelihood and prior probability (sum in log space) as per Bayes' rule.  Invalid prior\nvalues are marked by returning -[`std::f32::INFINITY`][std-infinity] from the priors function.\nNote your implementation is likely to need external data. This data should be included with\nyour `Model` class, for example:\n\n```rust\nstruct Model\u003c'a\u003e {\n    x: \u0026'a [f32],\n    y: \u0026'a [f32],\n}\n\n// Linear model y = m * x + c\nimpl\u003c'a\u003e Prob for Model\u003c'a\u003e {\n    fn lnlike(\u0026self, params: \u0026Guess) -\u003e f32 {\n        let m = params[0];\n        let c = params[1];\n\n        -0.5 * self.x.iter().zip(self.y)\n            .map(|(xval, yval)| {\n                let model = m * xval + c;\n                let residual = (yval - model).powf(2.0);\n                residual\n            }).sum::\u003cf32\u003e()\n    }\n\n    fn lnprior(\u0026self, params: \u0026Guess) -\u003e f32 {\n        // unimformative priors\n        0.0f32\n    }\n}\n\n```\n\n### Initial guess\n\nNext, construct an initial guess. A [`Guess`][emcee-guess] represents a proposal parameter\nvector:\n\n```rust\nuse emcee::Guess;\n\nlet initial_guess = Guess::new(\u0026[0.0f32, 0.0f32]);\n```\n\nThe sampler implemented by this create uses multiple *walkers*, and as such the initial\nguess must be replicated once per walker, and typically dispersed from the initial position\nto aid exploration of the problem parameter space. This can be achieved with the\n[`create_initial_guess`][emcee-create-initial-guess] method:\n\n```rust\nlet nwalkers = 100;\nlet perturbed_guess = initial_guess.create_initial_guess(nwalkers);\nassert_eq!(perturbed_guess.len(), nwalkers);\n```\n\n### Constructing a sampler\n\nThe sampler generates new parameter vectors, assess the probability using a user-supplied\nprobability model, accepts more likely parameter vectors and iterates for a number of\niterations.\n\nThe sampler needs to know the number of walkers to use, which must be an even number\nand at least twice the size of your parameter vector. It also needs the size of your\nparameter vector, and your probability struct (which implements [`Prob`][emcee-prob]):\n\n```rust\nlet nwalkers = 100;\nlet ndim = 2;  // m and c\n\n// Build a linear model y = m * x + c (see above)\n\nlet initial_x = [0.0f32, 1.0f32, 2.0f32];\nlet initial_y = [5.0f32, 7.0f32, 9.0f32];\n\nlet model = Model {\n    x: \u0026initial_x,\n    y: \u0026initial_y,\n};\n\nlet sampler = emcee::EnsembleSampler::new(nwalkers, ndim, \u0026model)\n    .expect(\"could not create sampler\");\n```\n\nThen run the sampler:\n\n```rust\nlet niterations = 100;\nsampler.run_mcmc(\u0026perturbed_guess, niterations).expect(\"error running sampler\");\n```\n\n#### Iterative sampling\n\nIt is sometimes useful to get the internal values proposed and evaluated\nduring each proposal step of the sampler. In the Python version, the\nmethod `sample` is a generator which can be iterated over to evaluate\nthe sample steps.\n\nIn this Rust version, we provide this feature by exposing the\n[`sample`][emcee-sample] method, which takes a callback, which is called\nonce per iteration with a single [`Step`][emcee-step] object. For\nexample:\n\n```rust\nsampler.sample(\u0026perturbed_guess, niterations, |step| {\n    println!(\"Current iteration: {}\", step.iteration);\n    println!(\"Current guess vectors: {:?}\", step.pos);\n    println!(\"Current log posterior probabilities: {:?}\", step.lnprob);\n});\n```\n\n### Studying the results\n\nThe samples are stored in the sampler's `flatchain` which is constructed through the\n[`flatchain`][emcee-flatchain] method on the sampler:\n\n```rust\nlet flatchain = sampler.flatchain().unwrap();\n\nfor (i, guess) in flatchain.iter().enumerate() {\n    // Skip possible \"burn-in\" phase\n    if i \u003c 50 * nwalkers {\n        continue;\n    }\n\n    println!(\"Iteration {}; m={}, c={}\", i, guess[0], guess[1]);\n}\n```\n\n[emcee]: http://dan.iel.fm/emcee/current/\n[emcee-prob]: https://docs.rs/emcee/0.3.0/emcee/trait.Prob.html\n[emcee-guess]: https://docs.rs/emcee/0.3.0/emcee/struct.Guess.html\n[emcee-lnprob]: https://docs.rs/emcee/0.3.0/emcee/trait.Prob.html#method.lnprob\n[std-infinity]: https://doc.rust-lang.org/std/f32/constant.INFINITY.html\n[emcee-create-initial-guess]: https://docs.rs/emcee/0.3.0/emcee/struct.Guess.html#method.create_initial_guess\n[emcee-flatchain]: https://docs.rs/emcee/0.3.0/emcee/struct.EnsembleSampler.html#method.flatchain\n[docs]: https://docs.rs/emcee\n[fitting-model-to-data]: https://github.com/mindriot101/rust-emcee/blob/master/examples/fitting_a_model_to_data.rs\n[fitting-model-to-data-python]: http://dan.iel.fm/emcee/current/user/line/\n[dfm]: http://dan.iel.fm/\n[dfm-license]: https://github.com/mindriot101/rust-emcee/blob/master/DFM-LICENSE\n[emcee-sample]: https://docs.rs/emcee/0.3.0/emcee/struct.EnsembleSampler.html#method.sample\n[emcee-step]: https://docs.rs/emcee/0.3.0/emcee/struct.Step.html\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimonrw%2Frust-emcee","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsimonrw%2Frust-emcee","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimonrw%2Frust-emcee/lists"}