{"id":28946236,"url":"https://github.com/pymc-devs/nuts-rs","last_synced_at":"2025-06-23T08:05:17.526Z","repository":{"id":38361777,"uuid":"123021212","full_name":"pymc-devs/nuts-rs","owner":"pymc-devs","description":"A implementation of NUTS in rust","archived":false,"fork":false,"pushed_at":"2025-05-27T15:31:58.000Z","size":314,"stargazers_count":77,"open_issues_count":2,"forks_count":7,"subscribers_count":21,"default_branch":"main","last_synced_at":"2025-06-13T02:31:50.639Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/pymc-devs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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},"funding":{"github":"numfocus","custom":["https://numfocus.org/donate-to-pymc"]}},"created_at":"2018-02-26T19:43:11.000Z","updated_at":"2025-06-10T21:52:32.000Z","dependencies_parsed_at":"2023-02-02T18:00:50.776Z","dependency_job_id":"8fa74e32-ab2b-4894-ad3c-be2ae2580120","html_url":"https://github.com/pymc-devs/nuts-rs","commit_stats":{"total_commits":151,"total_committers":4,"mean_commits":37.75,"dds":0.0331125827814569,"last_synced_commit":"c416c4206dfb8c7d737d709e2720c7e8a6f228ca"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"purl":"pkg:github/pymc-devs/nuts-rs","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pymc-devs%2Fnuts-rs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pymc-devs%2Fnuts-rs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pymc-devs%2Fnuts-rs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pymc-devs%2Fnuts-rs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pymc-devs","download_url":"https://codeload.github.com/pymc-devs/nuts-rs/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pymc-devs%2Fnuts-rs/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260368621,"owners_count":22998465,"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","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":[],"created_at":"2025-06-23T08:05:16.616Z","updated_at":"2025-06-23T08:05:17.488Z","avatar_url":"https://github.com/pymc-devs.png","language":"Rust","funding_links":["https://github.com/sponsors/numfocus","https://numfocus.org/donate-to-pymc"],"categories":[],"sub_categories":[],"readme":"![Workflow Status](https://github.com/pymc-devs/nuts-rs/actions/workflows/test.yml/badge.svg)\n[![dependency status](https://deps.rs/repo/github/pymc-devs/nuts-rs/status.svg)](https://deps.rs/repo/github/pymc-devs/nuts-rs)\n\n\u003c!-- cargo-rdme start --\u003e\n\nSample from posterior distributions using the No U-turn Sampler (NUTS).\nFor details see the original [NUTS paper](https://arxiv.org/abs/1111.4246)\nand the more recent [introduction](https://arxiv.org/abs/1701.02434).\n\nThis crate was developed as a faster replacement of the sampler in PyMC,\nto be used with the new numba backend of PyTensor. The python wrapper\nfor this sampler is [nutpie](https://github.com/pymc-devs/nutpie).\n\n## Usage\n\n```rust\nuse nuts_rs::{CpuLogpFunc, CpuMath, LogpError, DiagGradNutsSettings, Chain, SampleStats,\nSettings};\nuse thiserror::Error;\nuse rand::thread_rng;\n\n// Define a function that computes the unnormalized posterior density\n// and its gradient.\n#[derive(Debug)]\nstruct PosteriorDensity {}\n\n// The density might fail in a recoverable or non-recoverable manner...\n#[derive(Debug, Error)]\nenum PosteriorLogpError {}\nimpl LogpError for PosteriorLogpError {\n    fn is_recoverable(\u0026self) -\u003e bool { false }\n}\n\nimpl CpuLogpFunc for PosteriorDensity {\n    type LogpError = PosteriorLogpError;\n\n    // Only used for transforming adaptation.\n    type TransformParams = ();\n\n    // We define a 10 dimensional normal distribution\n    fn dim(\u0026self) -\u003e usize { 10 }\n\n    // The normal likelihood with mean 3 and its gradient.\n    fn logp(\u0026mut self, position: \u0026[f64], grad: \u0026mut [f64]) -\u003e Result\u003cf64, Self::LogpError\u003e {\n        let mu = 3f64;\n        let logp = position\n            .iter()\n            .copied()\n            .zip(grad.iter_mut())\n            .map(|(x, grad)| {\n                let diff = x - mu;\n                *grad = -diff;\n                -diff * diff / 2f64\n            })\n            .sum();\n        return Ok(logp)\n    }\n}\n\n// We get the default sampler arguments\nlet mut settings = DiagGradNutsSettings::default();\n\n// and modify as we like\nsettings.num_tune = 1000;\nsettings.maxdepth = 3;  // small value just for testing...\n\n// We instanciate our posterior density function\nlet logp_func = PosteriorDensity {};\nlet math = CpuMath::new(logp_func);\n\nlet chain = 0;\nlet mut rng = thread_rng();\nlet mut sampler = settings.new_chain(0, math, \u0026mut rng);\n\n// Set to some initial position and start drawing samples.\nsampler.set_position(\u0026vec![0f64; 10]).expect(\"Unrecoverable error during init\");\nlet mut trace = vec![];  // Collection of all draws\nfor _ in 0..2000 {\n    let (draw, info) = sampler.draw().expect(\"Unrecoverable error during sampling\");\n    trace.push(draw);\n}\n```\n\nUsers can also implement the `Model` trait for more control and parallel sampling.\n\n## Implementation details\n\nThis crate mostly follows the implementation of NUTS in [Stan](https://mc-stan.org) and\n[PyMC](https://docs.pymc.io/en/v3/), only tuning of mass matrix and step size differs\nsomewhat.\n\n\u003c!-- cargo-rdme end --\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpymc-devs%2Fnuts-rs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpymc-devs%2Fnuts-rs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpymc-devs%2Fnuts-rs/lists"}