LibRan  0.1
Pseudo-random number distribution generator
Macros | Typedefs | Functions | Variables
gsn.c File Reference

auxillary program to perform the derivations necessary for computing the CDF and PDF for summing a number of uniform random variates. Can safely handle upto 12 uniform random variates without overflow. More...

#include <stdio.h>
#include <limits.h>
Include dependency graph for gsn.c:

Go to the source code of this file.

Macros

#define NINT   15
 

Typedefs

typedef long LINT
 long integer type used for calculations.
 

Functions

LINT igcd (LINT a, LINT b)
 LINT igcd(LINT a, LINT b) return Greatest Common Denominator of a & b.
 
LINT ifac (int n)
 LINT ifac(LINT n) return the factorial of n (n!)
 
LINT icomb (int n, int k)
 LINT icomb(LINT n, LINT k) return binomial factor n!/[k!*(n-k)!].
 
void iadd (LINT an, LINT ad, LINT bn, LINT bd, LINT x, LINT *on, LINT *od)
 void iadd(an,ad,bn,bd, x, *on, *od) return a + b*x where a,b are represented as rationals More...
 
void imult (LINT an, LINT ad, LINT bn, LINT bd, LINT x, LINT *on, LINT *od)
 void imult(an,ad,bn,bd, x, *on, *od) return a*b*x where a,b are represented as rationals More...
 
void showdf (char *DFstr, int order, LINT xn[][NINT], LINT xd[][NINT])
 void showdf(DFstr, order, xn[][NINT], xd[][NINT]) display the rational values of 'x'. More...
 
void writedf (char *DFstr, int order, LINT xn[][NINT])
 void writedf(DFstr, order, xn[][NINT]) output array as a C declaration More...
 
void newcdf (int nn)
 void newcdf(nn) calculate the new CDF of order nn More...
 
void newpdf (int nn)
 void newpdf(nn) calculate the new PDF of order nn by differentiating the CDF. More...
 
void transint (int nn, LINT xn[][NINT], LINT xd[][NINT])
 void transint(nn, xn[][NINT], xd[]{NINT]) translate each interval to [0,1) More...
 
int main ()
 

Variables

LINT pn [NINT][NINT]
 PDF numerator. More...
 
LINT pd [NINT][NINT]
 PDF denominator. More...
 
LINT cn [NINT][NINT]
 CDF numerator. More...
 
LINT cd [NINT][NINT]
 CDF denominator. More...
 

Detailed Description

auxillary program to perform the derivations necessary for computing the CDF and PDF for summing a number of uniform random variates. Can safely handle upto 12 uniform random variates without overflow.

Adding two random variates will produce a new CDF which is the convolution of the two individual respective PDFs.

The uniform random variate has:

\begin{eqnarray*} \mbox{PDF } u(x) &= \left\{ \begin{array}{ll} 0, & x < 0 \mbox{ or } x \ge 1, \\ 1, & 0 \le x < 1 . \end{array} \right. \\ \mbox{CDF } U(x) &= \left\{ \begin{array}{ll} 0, & x < 0 , \\ x, & 0 \le x < 1 , \\ 1, & x \ge 1 . \end{array} \right. \end{eqnarray*}

Look at page 22 of "Non-Uniform Random Variate Generation" by Luc Devroye.

The CDF of $ n $ uniform random variates summed together is

\[ F(x) = \frac{1}{n!} \left[H(x)^n - \left(\begin{array}{c} {n} \\ {1} \end{array}\right) H(x-1)^n + \left(\begin{array}{c} {n} \\ {2} \end{array}\right) H(x-2)^n - \cdots \right] \]

\[ \mbox{where } H(x) = \left\{ \begin{array}{ll} 0, & x < 0 \\ x, & 0 \le x . \end{array} \right. \]

\[ \mbox{and the combination factor } \left(\begin{array}{c} {n} \\ {m} \end{array}\right) = \frac{n!}{m!(n-m)!} \]

This code will handle each of the intervals individually and expressly handle the polynomial coefficients for that interval as rational values (ratio of integers) and performing basic mathematic operations exactly . The result will be the new CDF and when differentiated the new PDF.

Finally, translate the polynomial coefficients to the local interval where $ 0 \le x < 1 $ to prevent inaccuracies and overflows when evaluating a large polynomial.

Finally the coefficients pairs are output into a format such the text could be included into C code with minimal editing. This was how the coefficients were generated for LRgsn.c.

The invoking functions must initialize the floating point coefficients by calculating the ratio of coefficient pairs. This only needs to be done once if the floating point values are stored in an array.

See also
{LRgsn.c}

Definition in file gsn.c.

Function Documentation

◆ iadd()

void iadd ( LINT  an,
LINT  ad,
LINT  bn,
LINT  bd,
LINT  x,
LINT on,
LINT od 
)

void iadd(an,ad,bn,bd, x, *on, *od) return a + b*x where a,b are represented as rationals

Parameters
LINTan 'a' numerator
LINTad 'a' denominator
LINTbn 'b' numerator
LINTbd 'b' denominator
LINTx multiplication factor
LINT*on output numerator
LINT*od output denominator

Definition at line 242 of file gsn.c.

◆ imult()

void imult ( LINT  an,
LINT  ad,
LINT  bn,
LINT  bd,
LINT  x,
LINT on,
LINT od 
)

void imult(an,ad,bn,bd, x, *on, *od) return a*b*x where a,b are represented as rationals

Parameters
LINTan 'a' numerator
LINTad 'a' denominator
LINTbn 'b' numerator
LINTbd 'b' denominator
LINTx multiplication factor
LINT*on output numerator
LINT*od output denominator

Definition at line 272 of file gsn.c.

◆ newcdf()

void newcdf ( int  nn)

void newcdf(nn) calculate the new CDF of order nn

This is the main driver routine that generates the rational coefficients for the new Gaussian-like culmulative distributions functions of order n

Parameters
intnn order of gsn.

Definition at line 346 of file gsn.c.

◆ newpdf()

void newpdf ( int  nn)

void newpdf(nn) calculate the new PDF of order nn by differentiating the CDF.

This is the other main driver routine that generates the rational coefficients for the new Gaussian-like probability distributions functions of order n

Parameters
intnn order of gsn.

Definition at line 399 of file gsn.c.

◆ showdf()

void showdf ( char *  DFstr,
int  order,
LINT  xn[][NINT],
LINT  xd[][NINT] 
)

void showdf(DFstr, order, xn[][NINT], xd[][NINT]) display the rational values of 'x'.

Parameters
char* DFstr descriptive string
intorder maximum order number
LINTxn[][NINT] output numerator
LINTxd[][NINT] output denominator

Definition at line 298 of file gsn.c.

◆ transint()

void transint ( int  nn,
LINT  xn[][NINT],
LINT  xd[][NINT] 
)

void transint(nn, xn[][NINT], xd[]{NINT]) translate each interval to [0,1)

This translates the polynomial coefficients from [i,i+1) ot [0,1) where the coefficients are represented by rational values.

\[ \mbox{Given } f(x) = \sum_{i = 0}^{n} a_i x^i \]

\[ f(x+c) = \sum_{m = 0}^{n} a'_m x^m \]

\[ \mbox{Where } a'_m = \sum_{k = m}^{n} \left(\begin{array}{c} {k} \\ {k-m} \end{array}\right) a_k c^{k-m} \]

Parameters
intnn order of gsn.
LINTxn[][NINT] input/output numerator
LINTxd[][NINT] input/output denominator

Definition at line 440 of file gsn.c.

◆ writedf()

void writedf ( char *  DFstr,
int  order,
LINT  xn[][NINT] 
)

void writedf(DFstr, order, xn[][NINT]) output array as a C declaration

Parameters
char* DFstr descriptive string
intorder maximum order number
LINTon[][NINT] output array

Definition at line 317 of file gsn.c.

Variable Documentation

◆ cd

LINT cd[NINT][NINT]
Initial value:
= {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
}

CDF denominator.

Definition at line 177 of file gsn.c.

◆ cn

LINT cn[NINT][NINT]
Initial value:
= {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0,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