LibRan  0.1
Pseudo-random number distribution generator
Functions
LRlspline.c File Reference

Piecewise linear spline distribution. More...

#include <stdlib.h>
#include <math.h>
#include "libran.h"
#include <stdio.h>
Include dependency graph for LRlspline.c:

Go to the source code of this file.

Functions

int LR_lspl_new (LR_obj *o, int n)
 LR_lspl_new(LR_obj *o, int n) - create a new linear spline object. More...
 
int LR_lspl_rm (LR_obj *o)
 LR_lspl_rm(LR_obj *o) - strip out the LR_lspl object part of LR_obj. More...
 
int LR_lspl_set (LR_obj *o, double x, double p)
 LR_lspl_set(LR_obj *o, double x) - add interval boundary (will order internally). More...
 
int LR_lspl_norm (LR_obj *o)
 LR_lspl_norm(LR_obj *o) - normalize the interval scale factors. More...
 
double LRd_lspline_RAN (LR_obj *o)
 LRd_lspline_RAN(LR_obj *o) - double random linear spline distribution random variate. More...
 
double LRd_lspline_PDF (LR_obj *o, double x)
 LRd_lspline_PDF(LR_obj *o, double x) - double linear spline probablity distribution function. More...
 
double LRd_lspline_CDF (LR_obj *o, double x)
 LRd_lspline_CDF(LR_obj *o, double x) - double linear spline cumulative distribution function. More...
 
float LRf_lspline_RAN (LR_obj *o)
 LRf_lspline_RAN(LR_obj *o) - float random linear spline distribution. More...
 
float LRf_lspline_PDF (LR_obj *o, float x)
 LRf_lspline_PDF(LR_obj *o, float x) - float linear spline probablity distribution function. More...
 
float LRf_lspline_CDF (LR_obj *o, float x)
 LRf_lspline_CDF(LR_obj *o, float x) - float linear spline cumulative distribution function. More...
 

Detailed Description

Piecewise linear spline distribution.

The piecewise linear spline distribution is similar to the piecewise uniform distribution except that the probability distribution function looks like the trapezoidal rule for integration, where each segment is interpolated by a straight line (an affine function, i.e. a polynomial of degree 1).

The PDF is pinned to zero at the end points. So at least one point in between must be added with a non-zero value. If this is not done then an error will be raised.

Once the linear spline distribution object is created the set of trapezoidal blocks can be defined with the set of LR_lspl_*() functions. (Note: the LR_aux_*() functions are equivalent and generic procedures and can be used instead.)

This piecewise distribution borrows many of the similar methods as the piecewise uniform distribution (LRpiece.c) auxillary methods since the two have functional equivalency.

#include "libran.h"
...
LR_obj *o = LR_new(lspline, LR_double);
...
// set the endpoints and this pins the probability density height to zero there
LR_set_all(o,"ab", 0., 8.);
// set the number of segments
...
// must set at least one intervening value to a non-zero value.
LR_aux_set(o, 2.0, 1.0);
LR_aux_set(o, 3.0, 3.0);
LR_aux_set(o, 4.0, 0.0);
LR_aux_set(o, 5.0, 5.0);
LR_aux_set(o, 7.0, 2.0);
// normalize the segmented probability density (the total integral = 1)
...
// do your typical processing
...
// remove the segment pieces
// remove the piecewise LR_obj
LR_rm(&o);
...

The probability and cumulative distribution functions as defined by the above code fragment looks like this:

LinearSplineDistribution.png
See also
LRlspline.c LRdf.c

Definition in file LRlspline.c.

Function Documentation

◆ LR_lspl_new()

int LR_lspl_new ( LR_obj o,
int  n 
)

LR_lspl_new(LR_obj *o, int n) - create a new linear spline object.

This routine must be called after the LR_obj object is created and it allocates memory for the number of expected segments.

Parameters
oLR_obj object
nlargest number of intervals
Returns
0 if successful, else non-zero if failed

Definition at line 88 of file LRlspline.c.

◆ LR_lspl_norm()

int LR_lspl_norm ( LR_obj o)

LR_lspl_norm(LR_obj *o) - normalize the interval scale factors.

The LR_lspl_norm() function is absolutely required to be called so the integrated probability density equals one. If this routine is not called subsequent calls to the CDF/PDF/RAN functions will raise an error (returning a NAN).

Parameters
oLR_obj object
Returns
0 if successful, else non-zero if failed

Definition at line 147 of file LRlspline.c.

◆ LR_lspl_rm()

int LR_lspl_rm ( LR_obj o)

LR_lspl_rm(LR_obj *o) - strip out the LR_lspl object part of LR_obj.

Removes the allocated memory for the segment pieces.

Parameters
oLR_obj object address
Returns
0 if successful, else non-zero if failed

Definition at line 100 of file LRlspline.c.

◆ LR_lspl_set()

int LR_lspl_set ( LR_obj o,
double  x,
double  p 
)

LR_lspl_set(LR_obj *o, double x) - add interval boundary (will order internally).

The LR_lspl_set function defines the set of segments for the probability density function. It adds a new boundary and the value of the probability density at that boundary.

Boundary values can be defined in any order and will be ordered internally by this method.

The outer boundaries are defined through the LR_set_all() function and the probability density is pinned to zero at these endpoints.

The set of segments and the probability density is relatively defined with LR_lspl_set(). Once all the segments are defined then the probability density must be normalized with LR_lspl_norm() such that the total integrated value is equal to one!

Parameters
oLR_obj object
xinterval boundary to add
prelative probablity for interval starting at x
Returns
0 if successful, else non-zero if failed

Definition at line 128 of file LRlspline.c.

◆ LRd_lspline_CDF()

double LRd_lspline_CDF ( LR_obj o,
double  x 
)

LRd_lspline_CDF(LR_obj *o, double x) - double linear spline cumulative distribution function.

Parameters
oLR_obj object
xvalue
Returns
double CDF at x

Definition at line 281 of file LRlspline.c.

◆ LRd_lspline_PDF()

double LRd_lspline_PDF ( LR_obj o,
double  x 
)

LRd_lspline_PDF(LR_obj *o, double x) - double linear spline probablity distribution function.

Parameters
oLR_obj object
xvalue
Returns
double PDF at x

Definition at line 247 of file LRlspline.c.

◆ LRd_lspline_RAN()

double LRd_lspline_RAN ( LR_obj o)

LRd_lspline_RAN(LR_obj *o) - double random linear spline distribution random variate.

Parameters
oLR_obj object
Returns
double if OK else NaN

Definition at line 209 of file LRlspline.c.

◆ LRf_lspline_CDF()

float LRf_lspline_CDF ( LR_obj o,
float  x 
)

LRf_lspline_CDF(LR_obj *o, float x) - float linear spline cumulative distribution function.

Parameters
oLR_obj object
xvalue
Returns
float CDF at x

Definition at line 390 of file LRlspline.c.

◆ LRf_lspline_PDF()

float LRf_lspline_PDF ( LR_obj o,
float  x 
)

LRf_lspline_PDF(LR_obj *o, float x) - float linear spline probablity distribution function.

Parameters
oLR_obj object
xvalue
Returns
float PDF at x

Definition at line 356 of file LRlspline.c.

◆ LRf_lspline_RAN()

float LRf_lspline_RAN ( LR_obj o)

LRf_lspline_RAN(LR_obj *o) - float random linear spline distribution.

Parameters
oLR_obj object
Returns
float if OK else NaN

Definition at line 318 of file LRlspline.c.