-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
127 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use std::ops::RangeInclusive; | ||
|
||
use tinyrand::Wyrand; | ||
use metromc::beta::Beta; | ||
|
||
use metromc::sampler::{Config, Sampler}; | ||
|
||
fn main() { | ||
const ALPHA: f64 = 0.5; | ||
const BETA: f64 = 0.5; | ||
const RANGE: RangeInclusive<f64> = 0.0..=1.0; | ||
const NUM_BUCKETS: usize = 1_000; | ||
|
||
let sampler = Sampler::new(Config { | ||
rand: Wyrand::default(), | ||
dist: Beta::new(ALPHA, BETA), | ||
range: RANGE, | ||
}); | ||
|
||
let mut buckets = [0_usize; NUM_BUCKETS]; | ||
let span = RANGE.end() - RANGE.start(); | ||
for sample in sampler.skip(100).take(10_000_000) { | ||
// println!("{rand:.6}"); | ||
let bucket = ((sample - RANGE.start()) / span * NUM_BUCKETS as f64) as usize; | ||
buckets[bucket] += 1; | ||
} | ||
|
||
println!("bucket start, count"); | ||
println!("------------, -----"); | ||
|
||
for (bucket, count) in buckets.iter().enumerate() { | ||
let bucket_start = RANGE.start() + bucket as f64 / NUM_BUCKETS as f64 * span; | ||
println!("{bucket_start:.3}, {}", count); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use std::ops::RangeInclusive; | ||
|
||
use tinyrand::Wyrand; | ||
|
||
use metromc::pareto::Pareto; | ||
use metromc::sampler::{Config, Sampler}; | ||
|
||
fn main() { | ||
const SHAPE: f64 = 2.0; | ||
const RATE: f64 = 1.0; | ||
const RANGE: RangeInclusive<f64> = 0.0..=16.0; | ||
const NUM_BUCKETS: usize = 1_000; | ||
|
||
let sampler = Sampler::new(Config { | ||
rand: Wyrand::default(), | ||
dist: Pareto::new(SHAPE, RATE), | ||
range: RANGE, | ||
}); | ||
|
||
let mut buckets = [0_usize; NUM_BUCKETS]; | ||
let span = RANGE.end() - RANGE.start(); | ||
for sample in sampler.skip(100).take(10_000_000) { | ||
// println!("{rand:.6}"); | ||
let bucket = ((sample - RANGE.start()) / span * NUM_BUCKETS as f64) as usize; | ||
buckets[bucket] += 1; | ||
} | ||
|
||
println!("bucket start, count"); | ||
println!("------------, -----"); | ||
|
||
for (bucket, count) in buckets.iter().enumerate() { | ||
let bucket_start = RANGE.start() + bucket as f64 / NUM_BUCKETS as f64 * span; | ||
println!("{bucket_start:.3}, {}", count); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use statrs::distribution::Continuous; | ||
use crate::Pdf; | ||
|
||
pub struct Beta { | ||
dist: statrs::distribution::Beta, | ||
} | ||
impl Beta { | ||
#[inline(always)] | ||
pub fn new(alpha: f64, beta: f64) -> Self { | ||
Self { | ||
dist: statrs::distribution::Beta::new(alpha, beta).unwrap(), | ||
} | ||
} | ||
} | ||
|
||
impl Pdf for Beta { | ||
#[inline(always)] | ||
fn prob(&self, x: f64) -> f64 { | ||
self.dist.pdf(x) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
pub mod beta; | ||
pub mod gamma; | ||
pub mod gaussian; | ||
pub mod pareto; | ||
pub mod sampler; | ||
|
||
pub trait Pdf { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use statrs::distribution::Continuous; | ||
use crate::Pdf; | ||
|
||
pub struct Pareto { | ||
dist: statrs::distribution::Pareto, | ||
} | ||
impl Pareto { | ||
#[inline(always)] | ||
pub fn new(shape: f64, rate: f64) -> Self { | ||
Self { | ||
dist: statrs::distribution::Pareto::new(shape, rate).unwrap(), | ||
} | ||
} | ||
} | ||
|
||
impl Pdf for Pareto { | ||
#[inline(always)] | ||
fn prob(&self, x: f64) -> f64 { | ||
self.dist.pdf(x) | ||
} | ||
} |