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

Set of LibRan binning functions. More...

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

Go to the source code of this file.

Functions

LR_binLR_bin_new (int n)
 LR_bin_new(LR_data_type d, int n) - create new binning object. More...
 
int LR_bin_rm (LR_bin **b)
 LR_bin_rm(LR_bin **b) - remove binning object. More...
 
int LR_bin_set (LR_bin *b, double x)
 LR_bin_set(LR_bin *b, double x) - add bin boundary. More...
 
int LR_bin_add (LR_bin *b, double x)
 LR_bin_add(LR_bin *b, double x) - collect value to be binned. More...
 

Detailed Description

Set of LibRan binning functions.

This set of LibRan functions will set-up or take-down the LR_bin object and the bins for counting values into bins based on the bin boundaries. Each of the LR_bin objects is independent of any other object such as the LR_obj.

Having such bins are useful for visualizing a random variates distribution. Given the total number of samples is N, then the number of likely samples falling within the bin will be

\[ N \int_{x_i}^{x_{i+1}}f(x)dx = N \left[F(x_{i+1}) - F(x_i)\right] \]

where the bin interval is $ [x_i,x_{i+1}) $ and $ f(x) $ is the probability distribution function (PDF) and $ F(x) $ is the cumulative distribution function (CDF).

The bin object also tallies samples below and above the set of bins. If you define n-1 boundaries then there are n bins. The order the boundaries are set is not important. The method orders the boundaries internally.

The following is some example code fragments for defining the bins, tallying the samples, and viewing the results.

#include <stdio.h>
#include "libran.h"
...
int nbin = 10;
LR_bin *b = LR_bin_new(nbin+2);
...
// set the boundaries
for (int i = 0; i <= nbin; i++) {
LR_bin_set(b, 2.0 + .1 * i);
}
...
// tally samples
for (int i = 0; i < 10000; i++) {
}
...
// view results
fprint("-inf - %f : %f\n", b->bdrs[0], b->bins[0]);
for (int i = 1; i <= nbin; i++) {
fprint("%f - %f : %f\n", b->bdrs[i-1], b->bdrs[i], b->bins[i]);
}
fprint("%f - inf : %f\n", b->bdrs[nbin+1], b->bins[nbin+1]);
...
LR_bin_rm(&b);

Definition in file LRbin.c.

Function Documentation

◆ LR_bin_add()

int LR_bin_add ( LR_bin b,
double  x 
)

LR_bin_add(LR_bin *b, double x) - collect value to be binned.

This is the tallying routine, where the value x is compared to the boundary values and the appropriate bin array element tally is incremented.

Parameters
bLR_bin object
xvalue to count within the given bin.
Returns
0 if successful, else non-zero if failed

Definition at line 178 of file LRbin.c.

◆ LR_bin_new()

LR_bin* LR_bin_new ( int  n)

LR_bin_new(LR_data_type d, int n) - create new binning object.

This routine creates a new LR_bin object and allocates the necessary memory for the boundaries and tally bins. Must give at least the expected number of bins or the number of boundaries plus one.

Parameters
tdata type
nnumber of bins
Returns
LR_bin object if successful, else NULL

Definition at line 81 of file LRbin.c.

◆ LR_bin_rm()

int LR_bin_rm ( LR_bin **  b)

LR_bin_rm(LR_bin **b) - remove binning object.

Since the LR_bin object allocates memory this method needs to be called to deallocate the memory to avoid memory leaks.

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

Definition at line 116 of file LRbin.c.

◆ LR_bin_set()

int LR_bin_set ( LR_bin b,
double  x 
)

LR_bin_set(LR_bin *b, double x) - add bin boundary.

Include another bin boundary, which will be ordered internally. However, it will not identify or flag repeated boundaries.

Parameters
bLR_bin object
xbin boundary to add
Returns
0 if successful, else non-zero if failed

Definition at line 137 of file LRbin.c.