Skip to main content

Grid

Struct Grid 

Source
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

Source

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.

Source

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).

Source

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.

Source

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.

Source

pub fn step(&self) -> f64

Grid step h.

Source

pub fn probs(&self) -> &[f64]

Probabilities at 0, h, 2h, ….

Source

pub fn len(&self) -> usize

Number of grid points.

Source

pub fn is_empty(&self) -> bool

Always false: a grid has at least one point.

Source

pub fn x(&self, j: usize) -> f64

Loss at point j, j * h.

Source

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);
Source

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 Clone for Grid

Source§

fn clone(&self) -> Grid

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Grid

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Distribution for Grid

Source§

fn survival(&self, x: f64) -> f64

Summed from the top, so small tail probabilities keep their precision.

Source§

fn mean(&self) -> f64

Expected value.
Source§

fn variance(&self) -> f64

Variance.
Source§

fn cdf(&self, x: f64) -> f64

P(X <= x).
Source§

fn quantile(&self, p: f64) -> Result<f64>

Smallest x with cdf(x) >= p. Read more
Source§

fn std_dev(&self) -> f64

Standard deviation.
Source§

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 more
Source§

fn is_parallel_safe(&self) -> bool

Whether the distribution may be evaluated on several threads at once. True for every native family; false for a 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 From<Grid> for Dist

Source§

fn from(d: Grid) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Grid

Source§

fn eq(&self, other: &Grid) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Severity for Grid

Source§

fn lev(&self, limit: f64) -> f64

Limited expected value E[min(X, limit)]. Read more
Source§

fn stop_loss(&self, retention: f64) -> f64

Expected excess over a retention, E[max(X - retention, 0)].
Source§

fn layer_second_moment(&self, limit: f64, attachment: f64) -> f64

Second moment of the loss to the layer limit xs attachment, E[min(max(X - attachment, 0), limit)^2]. limit = +inf gives the unlimited layer (infinite if the second moment is).
Source§

fn layer(&self, limit: f64, attachment: f64) -> f64

Expected loss to the layer limit xs attachment, E[min(max(X - attachment, 0), limit)].
Source§

fn layer_variance(&self, limit: f64, attachment: f64) -> f64

Variance of the loss to the layer limit xs attachment.
Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V