Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/faster nix postpred #10

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## [0.19.0] - 2024-12-25

### Changed
- Merry Christmas
- `NormalInvChiSquared`, `NormalGamma`, and `NormalInvGamme` `PpCache` for Gaussian conjugate analysis changed. `ln_pp_with_cache` is much faster.
- `Gamma` `PpCache` for Poisson conjugate analysis has been optimized. `ln_pp_with_cache` is faster.

## [0.18.0] - 2024-06-24

### Added
Expand Down Expand Up @@ -215,6 +222,7 @@
- Remove dependency on `quadrature` crate in favor of hand-rolled adaptive
Simpson's rule, which handles multimodal distributions better.

[0.19.0]: https://github.com/promise-ai/rv/compare/v0.18.0...v0.19.0
[0.18.0]: https://github.com/promise-ai/rv/compare/v0.17.0...v0.18.0
[0.17.0]: https://github.com/promise-ai/rv/compare/v0.16.5...v0.17.0
[0.16.5]: https://github.com/promise-ai/rv/compare/v0.16.4...v0.16.5
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rv"
version = "0.18.0"
version = "0.19.0"
authors = ["Baxter Eaves", "Michael Schmidt", "Chad Scherrer"]
description = "Random variables"
repository = "https://github.com/promised-ai/rv"
Expand All @@ -13,7 +13,7 @@ include = ["README.md", "src/**/*", "benches/*", "Cargo.toml"]
rust-version = "1.72"

[badges]
github = { repository = "promised-ai/rv", tag = "v0.17.0" }
github = { repository = "promised-ai/rv", tag = "v0.19.0" }
maintenance = { status = "actively-developed" }

[dependencies]
Expand Down Expand Up @@ -90,3 +90,11 @@ required-features = ["arraydist"]
[[bench]]
name = "mixture_entropy"
harness = false

[[bench]]
name = "nix"
harness = false

[[bench]]
name = "ng"
harness = false
57 changes: 57 additions & 0 deletions benches/ng.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use criterion::black_box;
use criterion::BatchSize;
use criterion::Criterion;
use criterion::{criterion_group, criterion_main};
use rv::data::GaussianSuffStat;
use rv::dist::Gaussian;
use rv::dist::NormalGamma;
use rv::traits::*;

fn bench_ng_postpred(c: &mut Criterion) {
let mut group = c.benchmark_group("NG ln pp(x)");
let ng = NormalGamma::new_unchecked(0.1, 1.2, 2.3, 3.4);
let mut rng = rand::thread_rng();
let g = Gaussian::standard();

group.bench_function(format!("No cache"), |b| {
b.iter_batched(
|| {
let stat = {
let mut stat = GaussianSuffStat::new();
g.sample_stream(&mut rng).take(10).for_each(|x: f64| {
stat.observe(&x);
});
stat
};
let y: f64 = g.draw(&mut rng);
(y, stat)
},
|(y, stat)| {
black_box(ng.ln_pp(&y, &DataOrSuffStat::SuffStat(&stat)))
},
BatchSize::SmallInput,
);
});

group.bench_function(format!("With cache"), |b| {
b.iter_batched(
|| {
let stat = {
let mut stat = GaussianSuffStat::new();
g.sample_stream(&mut rng).take(10).for_each(|x: f64| {
stat.observe(&x);
});
stat
};
let y: f64 = g.draw(&mut rng);
let cache = ng.ln_pp_cache(&DataOrSuffStat::SuffStat(&stat));
(y, cache)
},
|(y, cache)| black_box(ng.ln_pp_with_cache(&cache, &y)),
BatchSize::SmallInput,
);
});
}

criterion_group!(ng_benches, bench_ng_postpred);
criterion_main!(ng_benches);
99 changes: 99 additions & 0 deletions benches/nix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
use criterion::black_box;
use criterion::BatchSize;
use criterion::Criterion;
use criterion::{criterion_group, criterion_main};
use rv::data::GaussianSuffStat;
use rv::dist::Gaussian;
use rv::dist::NormalInvChiSquared;
use rv::traits::*;

fn bench_nix_postpred(c: &mut Criterion) {
let mut group = c.benchmark_group("NIX ln pp(x)");
let nix = NormalInvChiSquared::new_unchecked(0.1, 1.2, 2.3, 3.4);
let mut rng = rand::thread_rng();
let g = Gaussian::standard();

group.bench_function(format!("No cache"), |b| {
b.iter_batched(
|| {
let stat = {
let mut stat = GaussianSuffStat::new();
g.sample_stream(&mut rng).take(10).for_each(|x: f64| {
stat.observe(&x);
});
stat
};
let y: f64 = g.draw(&mut rng);
(y, stat)
},
|(y, stat)| {
black_box(nix.ln_pp(&y, &DataOrSuffStat::SuffStat(&stat)))
},
BatchSize::SmallInput,
);
});

group.bench_function(format!("With cache"), |b| {
b.iter_batched(
|| {
let stat = {
let mut stat = GaussianSuffStat::new();
g.sample_stream(&mut rng).take(10).for_each(|x: f64| {
stat.observe(&x);
});
stat
};
let y: f64 = g.draw(&mut rng);
let cache = nix.ln_pp_cache(&DataOrSuffStat::SuffStat(&stat));
(y, cache)
},
|(y, cache)| black_box(nix.ln_pp_with_cache(&cache, &y)),
BatchSize::SmallInput,
);
});
}

fn bench_gauss_stat(c: &mut Criterion) {
let mut group = c.benchmark_group("Gaussian Suffstat");

let mut rng = rand::thread_rng();
let g = Gaussian::standard();

group.bench_function(format!("Forget"), |b| {
b.iter_batched(
|| {
let mut stat = GaussianSuffStat::new();
for _ in 0..3 {
let x: f64 = g.draw(&mut rng);
stat.observe(&x);
}
let x: f64 = g.draw(&mut rng);
stat.observe(&x);
(x, stat)
},
|(x, mut stat)| {
black_box(stat.forget(&x));
},
BatchSize::SmallInput,
);
});

group.bench_function(format!("Observe"), |b| {
b.iter_batched(
|| {
let mut stat = GaussianSuffStat::new();
let x: f64 = g.draw(&mut rng);
stat.observe(&x);
let x: f64 = g.draw(&mut rng);
(x, stat)
},
|(x, mut stat)| {
black_box(stat.observe(&x));
},
BatchSize::SmallInput,
);
});
}

criterion_group!(nix_benches, bench_nix_postpred, bench_gauss_stat);
criterion_main!(nix_benches);
28 changes: 13 additions & 15 deletions src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub use stat::UnitPowerLawSuffStat;
use crate::dist::{
Bernoulli, Categorical, Gaussian, InvGamma, InvGaussian, Poisson,
};
use crate::traits::ConjugatePrior;
use crate::traits::HasDensity;
use crate::traits::{HasSuffStat, SuffStat};

pub type BernoulliData<'a, X> = DataOrSuffStat<'a, X, Bernoulli>;
Expand Down Expand Up @@ -159,7 +161,7 @@ where
pub fn n(&self) -> usize {
match &self {
DataOrSuffStat::Data(data) => data.len(),
DataOrSuffStat::SuffStat(s) => s.n(),
DataOrSuffStat::SuffStat(s) => <Fx::Stat as SuffStat<X>>::n(s),
}
}

Expand Down Expand Up @@ -212,39 +214,35 @@ where

/// Convert a `DataOrSuffStat` into a `Stat`
#[inline]
pub fn extract_stat<Fx, X, Ctor>(
x: &DataOrSuffStat<X, Fx>,
stat_ctor: Ctor,
) -> Fx::Stat
pub fn extract_stat<X, Fx, Pr>(pr: &Pr, x: &DataOrSuffStat<X, Fx>) -> Fx::Stat
where
Fx: HasSuffStat<X>,
Fx: HasSuffStat<X> + HasDensity<X>,
Fx::Stat: Clone,
Ctor: Fn() -> Fx::Stat,
Pr: ConjugatePrior<X, Fx>,
{
match x {
DataOrSuffStat::SuffStat(s) => (*s).clone(),
DataOrSuffStat::Data(xs) => {
let mut stat = stat_ctor();
xs.iter().for_each(|y| stat.observe(y));
let mut stat = pr.empty_stat();
stat.observe_many(xs);
stat
}
}
}

/// Convert a `DataOrSuffStat` into a `Stat` then do something with it
#[inline]
pub fn extract_stat_then<Fx, X, Ctor, Fnx, Y>(
pub fn extract_stat_then<X, Fx, Pr, Fnx, Y>(
pr: &Pr,
x: &DataOrSuffStat<X, Fx>,
stat_ctor: Ctor,
f_stat: Fnx,
) -> Y
where
Fx: HasSuffStat<X>,
Fx: HasSuffStat<X> + HasDensity<X>,
Fx::Stat: Clone,
Ctor: Fn() -> Fx::Stat,
Pr: ConjugatePrior<X, Fx>,
Fnx: Fn(Fx::Stat) -> Y,
{
let stat = extract_stat(x, stat_ctor);
let stat = extract_stat(pr, x);
f_stat(stat)
}

Expand Down
5 changes: 5 additions & 0 deletions src/data/stat/bernoulli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ impl<X: Booleable> SuffStat<X> for BernoulliSuffStat {
self.k -= 1
}
}

fn merge(&mut self, other: Self) {
self.n += other.n;
self.k += other.k;
}
}

#[cfg(test)]
Expand Down
6 changes: 6 additions & 0 deletions src/data/stat/beta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ macro_rules! impl_suffstat {
self.sum_ln_1mx = 0.0;
}
}

fn merge(&mut self, other: Self) {
self.n += other.n;
self.sum_ln_x += other.sum_ln_x;
self.sum_ln_1mx += other.sum_ln_1mx;
}
}
};
}
Expand Down
10 changes: 10 additions & 0 deletions src/data/stat/categorical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ impl<X: CategoricalDatum> SuffStat<X> for CategoricalSuffStat {
self.n -= 1;
self.counts[ix] -= 1.0;
}

fn merge(&mut self, other: Self) {
self.n += other.n;
self.counts
.iter_mut()
.zip(other.counts.iter().copied())
.for_each(|(ct, ct_o)| {
*ct += ct_o;
});
}
}

#[cfg(test)]
Expand Down
Loading
Loading