# Crate test_sampler

source ·## Expand description

Contains tools to perform unit testing of sampling algorithms.

It has been developed particularly to help with the development of Monte Carlo particle transport codes, where a large number of various sampling procedures is required to stochastically simulate physical interactions of radiation with matter.

In general models are described with differential cross-sections $\frac{d\sigma}{dE’d\Omega}(E)$, which provide with the shape of probability density function. In general normalisation is difficult to get without complex integration.

For that reason this package is composed of two parts:

- FunctionSampler which allows to (inefficiently) draw samples from a non-normalised pdf shape function
- Suite of statistical tests crate::stat_tests, which allow to verify that samples from a tested distribution match the one generated with FunctionSampler

Thus to verify sampling one needs to:

- Verify shape function with deterministic unit tests
- Compare sampling procedure against reference values from FunctionSampler using statistical tests

Note that as a result of statistical uncertainty and variable *power* of
statistical tests for different defects and sample populations the sampling
unit tests cannot ever provide with the same level of certainty as the
deterministic one. Also the appropriate number of samples and type(s) of
the test(s) will depend on a particular application.

## Example

Let us verify simple inversion sampling of $f(x) = x$ on $[0;1]$, for which we can generate samples with $\hat{f} = \sqrt{r}$ where $r$ is uniformly distributed random number.

```
use test_sampler::FunctionSampler;
use rand::prelude::*;
// Seed rngs
let mut rng1 = StdRng::seed_from_u64(87674);
let mut rng2 = StdRng::seed_from_u64(87674);
// Draw reference samples from the FunctionSampler
let support = 0.0..1.0;
let num_bins = 30;
let reference_dist = FunctionSampler::new(|x| x, support, num_bins)?;
let s_ref : Vec<f64> = rng1.sample_iter(&reference_dist).take(100).collect();
// Samples to test
let s : Vec<f64> = (0..100).map(|_| rng2.gen()).map(|r: f64| r.sqrt()).collect();
// Perform tests
// Vectors of samples will be moved inside the procedures
// It is necessary since the samples must be sorted (and mutated)
let ks_res = test_sampler::stat_tests::ks2_test(s_ref.clone(), s.clone())?;
let kup_res = test_sampler::stat_tests::kuiper2_test(s_ref.clone(), s.clone())?;
let ad_res = test_sampler::stat_tests::ad2_test(s_ref, s)?;
// Check test results
assert!(ks_res.p_value() > 0.05);
assert!(kup_res.p_value() > 0.05);
assert!(ad_res.p_value() > 0.05);
```

## Modules

- Collection of statistical tests

## Structs

- Distribution described by a non-negative shape function on an interval
- Distribution described by non-normalised histogram

## Enums

- Error raised when the setup of a sampling distribution has failed

## Functions

- Creates linearly spaced grid between
`start`

and`end`

of size`n`