pub struct Grid { /* private fields */ }Expand description
A distribution on the points 0, h, 2h, …, (n - 1)h.
Grids are what FFT and Panjer aggregation work on. They are exact for
sums, layers and stop-loss on the grid, but a grid made from a continuous
distribution is an approximation: see Grid::local_moment,
Grid::rounding and Grid::lower, which return a
DiscretizationReport with the error introduced.
The probabilities always sum to 1. Discretization lumps whatever lies beyond the last point onto it and reports that mass.
§Example
use prospicio_prob::{Distribution, Grid, Lognormal, Severity};
let sev = Lognormal::new(7.0, 0.5).unwrap();
let (grid, report) = Grid::local_moment(&sev, 100.0, 200).unwrap();
// Local moment matching preserves the limited mean up to the last point.
assert!((grid.mean() - sev.lev(199.0 * 100.0)).abs() < 1e-9);
// About 3e-9 of the mass lies beyond the last point (19,900).
assert!(report.tail_mass < 1e-8);Implementations§
Source§impl Grid
impl Grid
Sourcepub fn new(step: f64, probs: Vec<f64>) -> Result<Self>
pub fn new(step: f64, probs: Vec<f64>) -> Result<Self>
A grid with step step and probabilities probs at 0, step, ….
Fails if step is not finite and positive, probs is empty or holds a
negative or non-finite value, or the probabilities do not sum to 1
within 1e-9.
Sourcepub fn local_moment<D: Severity>(
source: &D,
step: f64,
points: usize,
) -> Result<(Self, DiscretizationReport)>
pub fn local_moment<D: Severity>( source: &D, step: f64, points: usize, ) -> Result<(Self, DiscretizationReport)>
Discretizes source by local moment matching on the mean, using its
limited expected values:
f_0 = 1 - LEV(h) / h
f_j = (2 LEV(jh) - LEV((j-1)h) - LEV((j+1)h)) / h, 0 < j < n - 1
f_{n-1} = (LEV((n-1)h) - LEV((n-2)h)) / h (the rest)The grid’s mean is exactly LEV((n - 1)h).
Sourcepub fn rounding<D: Distribution>(
source: &D,
step: f64,
points: usize,
) -> Result<(Self, DiscretizationReport)>
pub fn rounding<D: Distribution>( source: &D, step: f64, points: usize, ) -> Result<(Self, DiscretizationReport)>
Discretizes source by rounding each loss to the nearest point:
f_0 = F(h/2), f_j = F((j + 1/2)h) - F((j - 1/2)h), and the last
point takes everything above (n - 3/2)h.
Sourcepub fn lower<D: Distribution>(
source: &D,
step: f64,
points: usize,
) -> Result<(Self, DiscretizationReport)>
pub fn lower<D: Distribution>( source: &D, step: f64, points: usize, ) -> Result<(Self, DiscretizationReport)>
Discretizes source by moving each cell’s mass to its left end:
f_j = F((j + 1)h) - F(jh), with the last point taking everything
above (n - 1)h. The result is a stochastic lower bound of source.
Sourcepub fn distortion(&self, d: &Distortion) -> f64
pub fn distortion(&self, d: &Distortion) -> f64
Distortion risk measure of the grid, exact for the grid; see
Distortion::apply_discrete.
use prospicio_prob::{Distortion, Grid};
let g = Grid::new(1.0, vec![0.5, 0.25, 0.25]).unwrap();
// TVaR at 50%: the top half of the mass, at 1 and 2.
assert_eq!(g.distortion(&Distortion::tvar(0.5).unwrap()), 1.5);Sourcepub fn map(&self, f: impl FnMut(f64) -> f64) -> Result<(Self, bool)>
pub fn map(&self, f: impl FnMut(f64) -> f64) -> Result<(Self, bool)>
The distribution of f(X) on the same step, and whether it is
exact.
Each point’s mass moves to f(x_j). A value on a grid point (within
1e-9 of a step) keeps its mass there; one between points k and
k + 1 is split between them so its mean is kept, as local moment
matching does. The mean is therefore always exact, and the whole
distribution is exact when the flag is true. The result is as long
as the largest value needs.
For a layer with boundaries on grid points the map is exact:
use prospicio_prob::{Distribution, Grid};
let x = Grid::new(1.0, vec![0.2, 0.3, 0.3, 0.2]).unwrap();
// 1 xs 1: values 0, 0, 1, 1.
let (layer, exact) = x.map(|v| (v - 1.0).clamp(0.0, 1.0)).unwrap();
assert!(exact);
assert_eq!(layer.probs(), [0.5, 0.5]);
// 1.5 xs 0.5 puts 0.5 and 1.5 between points: the mean is kept.
let (layer, exact) = x.map(|v| (v - 0.5).clamp(0.0, 1.5)).unwrap();
assert!(!exact);
assert!((layer.mean() - (0.3 * 0.5 + 0.5 * 1.5)).abs() < 1e-15);Fails if f returns a negative or non-finite value at a point with
mass.
Trait Implementations§
Source§impl Distribution for Grid
impl Distribution for Grid
Source§fn survival(&self, x: f64) -> f64
fn survival(&self, x: f64) -> f64
Summed from the top, so small tail probabilities keep their precision.
Source§fn sample(&self, rng: &mut StreamRng, n: usize) -> Vec<f64>
fn sample(&self, rng: &mut StreamRng, n: usize) -> Vec<f64>
n draws from stream rng, by inverse transform unless the family
overrides it (crate::Gamma draws by Marsaglia and Tsang). Read moreSource§fn is_parallel_safe(&self) -> bool
fn is_parallel_safe(&self) -> bool
crate::Custom
whose callbacks must stay on the calling thread (an R function), so
the parallel simulations run single-threaded when they meet one.Source§impl Severity for Grid
impl Severity for Grid
Source§fn stop_loss(&self, retention: f64) -> f64
fn stop_loss(&self, retention: f64) -> f64
E[max(X - retention, 0)].Source§fn layer_second_moment(&self, limit: f64, attachment: f64) -> f64
fn layer_second_moment(&self, limit: f64, attachment: f64) -> f64
limit xs attachment,
E[min(max(X - attachment, 0), limit)^2]. limit = +inf gives the
unlimited layer (infinite if the second moment is).impl StructuralPartialEq for Grid
Auto Trait Implementations§
impl Freeze for Grid
impl RefUnwindSafe for Grid
impl Send for Grid
impl Sync for Grid
impl Unpin for Grid
impl UnsafeUnpin for Grid
impl UnwindSafe for Grid
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more