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

Piecewise uniform distribution. More...

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

Go to the source code of this file.

Functions

int LR_pcs_new (LR_obj *o, int n)
 LR_pcs_new(LR_obj *o, int n) - create a new piecewise uniform object set of segments. More...
 
int LR_pcs_rm (LR_obj *o)
 LR_pcs_rm(LR_obj *o) - strip out the LR_pcs object part of LR_obj. More...
 
int LR_pcs_set (LR_obj *o, double x, double p)
 LR_pcs_set(LR_obj *o, double x) - add interval boundary (will order internally). More...
 
int LR_pcs_norm (LR_obj *o)
 LR_pcs_norm(LR_obj *o) - normalize the interval scale factors. More...
 
double LRd_piece_RAN (LR_obj *o)
 LRd_piece_RAN(LR_obj *o) - double random piecewise uniform distribution random variate. More...
 
double LRd_piece_PDF (LR_obj *o, double x)
 LRd_piece_PDF(LR_obj *o, double x) - double piecewise uniform probablity distribution function. More...
 
double LRd_piece_CDF (LR_obj *o, double x)
 LRd_piece_CDF(LR_obj *o, double x) - double piecewise uniform cumulative distribution function. More...
 
float LRf_piece_RAN (LR_obj *o)
 LRf_piece_RAN(LR_obj *o) - float random piecewise uniform distribution. More...
 
float LRf_piece_PDF (LR_obj *o, float x)
 LRf_piece_PDF(LR_obj *o, float x) - float piecewise uniform probablity distribution function. More...
 
float LRf_piece_CDF (LR_obj *o, float x)
 LRf_piece_CDF(LR_obj *o, float x) - float piecewise uniform cumulative distribution function. More...
 

Detailed Description

Piecewise uniform distribution.

The Piecewise uniform distribution is a probability distribution function that looks like a histogram, where each block is of some width $ x_n $ and height of $ p_n $.

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

The auxilliary methods found here are also used by the linear spline distribution method.

#include "libran.h"
...
LR_obj *o = LR_new(piece, LR_double);
...
// set the end points and the probability density height of the first segment
LR_set_all(o,"abx", 0., 8., 4.);
// set the number of pieces
// now set the probability density height of the subsequent segments
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:

PiecewiseUniformDistribution.png
See also
LRlspline.c LRdf.c

Definition in file LRpiece.c.

Function Documentation

◆ LR_pcs_new()

int LR_pcs_new ( LR_obj o,
int  n 
)

LR_pcs_new(LR_obj *o, int n) - create a new piecewise uniform object set of segments.

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 78 of file LRpiece.c.

◆ LR_pcs_norm()

int LR_pcs_norm ( LR_obj o)

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

The LR_pcs_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 220 of file LRpiece.c.

◆ LR_pcs_rm()

int LR_pcs_rm ( LR_obj o)

LR_pcs_rm(LR_obj *o) - strip out the LR_pcs 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 122 of file LRpiece.c.

◆ LR_pcs_set()

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

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

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

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

The first segment (and boundary) is defined through the LR_set_all() function, which also defines the last segment end point.

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

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

Definition at line 158 of file LRpiece.c.

◆ LRd_piece_CDF()

double LRd_piece_CDF ( LR_obj o,
double  x 
)

LRd_piece_CDF(LR_obj *o, double x) - double piecewise uniform cumulative distribution function.

Parameters
oLR_obj object
xvalue
Returns
double CDF at x, else a NAN if an error

Definition at line 332 of file LRpiece.c.

◆ LRd_piece_PDF()

double LRd_piece_PDF ( LR_obj o,
double  x 
)

LRd_piece_PDF(LR_obj *o, double x) - double piecewise uniform probablity distribution function.

Parameters
oLR_obj object
xvalue
Returns
double PDF at x, else a NAN if an error

Definition at line 305 of file LRpiece.c.

◆ LRd_piece_RAN()

double LRd_piece_RAN ( LR_obj o)

LRd_piece_RAN(LR_obj *o) - double random piecewise uniform distribution random variate.

Parameters
oLR_obj object
Returns
double if OK, else NaN

Definition at line 277 of file LRpiece.c.

◆ LRf_piece_CDF()

float LRf_piece_CDF ( LR_obj o,
float  x 
)

LRf_piece_CDF(LR_obj *o, float x) - float piecewise uniform cumulative distribution function.

Parameters
oLR_obj object
xvalue
Returns
float CDF at x, else a NAN if an error

Definition at line 415 of file LRpiece.c.

◆ LRf_piece_PDF()

float LRf_piece_PDF ( LR_obj o,
float  x 
)

LRf_piece_PDF(LR_obj *o, float x) - float piecewise uniform probablity distribution function.

Parameters
oLR_obj object
xvalue
Returns
float PDF at x, else a NAN if an error

Definition at line 388 of file LRpiece.c.

◆ LRf_piece_RAN()

float LRf_piece_RAN ( LR_obj o)

LRf_piece_RAN(LR_obj *o) - float random piecewise uniform distribution.

Parameters
oLR_obj object
Returns
float if OK, else NaN

Definition at line 360 of file LRpiece.c.