Skip to content

Commit

Permalink
Benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
ekoutanov committed Jan 4, 2024
1 parent 6179ba8 commit 688bf66
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 14 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/master.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Cargo build

on:
push:
branches: [ master ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
toolchain: 1.70.0
- run: cargo test -- --include-ignored
- run: cargo test --examples
- run: cargo doc --no-deps
- run: cargo bench --no-run --profile dev
benchmark:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
- run: cargo install cargo-criterion
- run: cargo criterion
27 changes: 27 additions & 0 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Cargo build

on:
pull_request:
branches: [ master ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
toolchain: 1.70.0
- run: cargo test -- --include-ignored
- run: cargo test --examples
- run: cargo doc --no-deps
- run: cargo bench --no-run --profile dev
benchmark:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
- run: cargo install cargo-criterion
- run: cargo criterion
9 changes: 7 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "mcmc"
name = "metromc"
version = "0.1.0"
edition = "2021"
authors = ["Emil Koutanov"]
Expand All @@ -14,4 +14,9 @@ statrs = "0.16.0"
tinyrand = "0.5.0"

[dev-dependencies]
assert_float_eq = "1.1.3"
assert_float_eq = "1.1.3"
criterion = { version = "0.5.1", features = ["html_reports"] }

[[bench]]
name = "cri_gaussian"
harness = false
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
`mcmc`
`metromc`
===
Markov chain Monte Carlo sampling using the _Independence Metropolis-Hastings_ algorithm with uniform transition kernel.

Uses the [tinyrand](https://github.com/obsidiandynamics/tinyrand) RNG.
[![Crates.io](https://img.shields.io/crates/v/mcmc?style=flat-square&logo=rust)](https://crates.io/crates/mcmc)
[![docs.rs](https://img.shields.io/badge/docs.rs-mcmc-blue?style=flat-square&logo=docs.rs)](https://docs.rs/mcmc)
[![Build Status](https://img.shields.io/github/actions/workflow/status/obsidiandynamics/mcmc/master.yml?branch=master&style=flat-square&logo=github)](https://github.com/obsidiandynamics/mcmc/actions/workflows/master.yml)

Uses the [tinyrand](https://github.com/obsidiandynamics/tinyrand) RNG to sample at rate of ~50M samples/sec.

# Example
Draw samples from the Gaussian distribution.

```rust
use std::ops::RangeInclusive;
use tinyrand::Wyrand;
use mcmc::gaussian::Gaussian;
use mcmc::sampler::{Config, Sampler};
use metromc::gaussian::Gaussian;
use metromc::sampler::{Config, Sampler};

// sample from Gaussian with µ=0.0 and σ=1.0, in the interval [-5.0, 5.0]
let sampler = Sampler::new(Config {
Expand Down
29 changes: 29 additions & 0 deletions benches/cri_gaussian.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use std::ops::RangeInclusive;

use criterion::{Criterion, criterion_group, criterion_main};
use tinyrand::Wyrand;

use metromc::gaussian::Gaussian;
use metromc::sampler::{Config, Sampler};

fn criterion_benchmark(c: &mut Criterion) {
const MEAN: f64 = 0.0;
const STD_DEV: f64 = 1.0;
const RANGE: RangeInclusive<f64> = -5.0..=5.0;

let mut sampler = Sampler::new(Config {
rand: Wyrand::default(),
dist: Gaussian::new(MEAN, STD_DEV),
range: RANGE,
});

// sanity check
assert!(RANGE.contains(&sampler.next()));

c.bench_function("cri_gaussian", |b| {
b.iter(|| sampler.next());
});
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
4 changes: 2 additions & 2 deletions examples/gamma_pdf.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use mcmc::gamma::Gamma;
use mcmc::Pdf;
use metromc::gamma::Gamma;
use metromc::Pdf;

fn main() {
const SHAPE: f64 = 2.0;
Expand Down
4 changes: 2 additions & 2 deletions examples/gaussian_pdf.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use mcmc::Pdf;
use mcmc::gaussian::Gaussian;
use metromc::Pdf;
use metromc::gaussian::Gaussian;

fn main() {
const MEAN: f64 = 3.0;
Expand Down
4 changes: 2 additions & 2 deletions examples/sample_gamma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::ops::RangeInclusive;

use tinyrand::Wyrand;

use mcmc::gamma::Gamma;
use mcmc::sampler::{Config, Sampler};
use metromc::gamma::Gamma;
use metromc::sampler::{Config, Sampler};

fn main() {
const SHAPE: f64 = 2.0;
Expand Down
4 changes: 2 additions & 2 deletions examples/sample_gaussian.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::ops::RangeInclusive;
use tinyrand::Wyrand;
use mcmc::gaussian::Gaussian;
use mcmc::sampler::{Config, Sampler};
use metromc::gaussian::Gaussian;
use metromc::sampler::{Config, Sampler};

fn main() {
const MEAN: f64 = 0.0;
Expand Down

0 comments on commit 688bf66

Please sign in to comment.