tor  0.4.2.1-alpha-dev
Data Structures | Macros | Functions | Variables
prob_distr.h File Reference
#include "lib/cc/compat_compiler.h"
#include "lib/cc/torint.h"
#include "lib/testsupport/testsupport.h"

Go to the source code of this file.

Data Structures

struct  dist
 
struct  dist_ops
 
struct  geometric
 
struct  genpareto
 
struct  weibull
 
struct  log_logistic
 
struct  logistic
 
struct  uniform
 

Macros

#define DIST_BASE(OPS)   { .ops = (OPS) }
 
#define TYPE_CHECK_OBJ(OPS, OBJ, TYPE)   (0*sizeof(&(OBJ) - (const TYPE *)&(OBJ)))
 
#define DIST_BASE_TYPED(OPS, OBJ, TYPE)   DIST_BASE((OPS) + TYPE_CHECK_OBJ(OPS,OBJ,TYPE))
 
#define GEOMETRIC(OBJ)   DIST_BASE_TYPED(&geometric_ops, OBJ, struct geometric)
 
#define GENPARETO(OBJ)   DIST_BASE_TYPED(&genpareto_ops, OBJ, struct genpareto)
 
#define WEIBULL(OBJ)   DIST_BASE_TYPED(&weibull_ops, OBJ, struct weibull)
 
#define LOG_LOGISTIC(OBJ)   DIST_BASE_TYPED(&log_logistic_ops, OBJ, struct log_logistic)
 
#define LOGISTIC(OBJ)   DIST_BASE_TYPED(&logistic_ops, OBJ, struct logistic)
 
#define UNIFORM(OBJ)   DIST_BASE_TYPED(&uniform_ops, OBJ, struct uniform)
 

Functions

const char * dist_name (const struct dist *)
 
double dist_sample (const struct dist *)
 
double dist_cdf (const struct dist *, double x)
 
double dist_sf (const struct dist *, double x)
 
double dist_icdf (const struct dist *, double p)
 
double dist_isf (const struct dist *, double p)
 

Variables

const struct dist_ops geometric_ops
 
const struct dist_ops genpareto_ops
 
const struct dist_ops weibull_ops
 
const struct dist_ops log_logistic_ops
 
const struct dist_ops logistic_ops
 
const struct dist_ops uniform_ops
 

Detailed Description

Header for prob_distr.c.

Definition in file prob_distr.h.

Macro Definition Documentation

◆ DIST_BASE

#define DIST_BASE (   OPS)    { .ops = (OPS) }

Untyped initializer element for struct dist using the specified struct dist_ops pointer. Don't actually use this directly – use the type-specific macro built out of DIST_BASE_TYPED below – but if you did use this directly, it would be something like:

struct weibull mydist = { DIST_BASE(&weibull_ops), .lambda = ..., .k = ..., };

Note there is NO COMPILER FEEDBACK if you accidentally do something like

struct geometric mydist = { DIST_BASE(&weibull_ops), ... };

Definition at line 42 of file prob_distr.h.

◆ DIST_BASE_TYPED

#define DIST_BASE_TYPED (   OPS,
  OBJ,
  TYPE 
)    DIST_BASE((OPS) + TYPE_CHECK_OBJ(OPS,OBJ,TYPE))

Typed initializer element for struct dist using the specified struct dist_ops pointer. Don't actually use this directly – use a type-specific macro built out of it – but if you did use this directly, it would be something like:

struct weibull mydist = { DIST_BASE_TYPED(&weibull_ops, mydist, struct weibull), .lambda = ..., .k = ..., };

If you want to define a distribution type, define a canonical set of operations and define a type-specific initializer element like so:

struct foo { struct dist base; int omega; double tau; double phi; };

struct dist_ops foo_ops = ...;

#define FOO(OBJ) DIST_BASE_TYPED(&foo_ops, OBJ, struct foo)

Then users can do:

struct foo mydist = { FOO(mydist), .omega = ..., .tau = ..., .phi = ..., };

If you accidentally write

struct bar mydist = { FOO(mydist), ... };

then the compiler will report a type mismatch in the sizeof expression, which otherwise evaporates at runtime.

Definition at line 108 of file prob_distr.h.

◆ TYPE_CHECK_OBJ

#define TYPE_CHECK_OBJ (   OPS,
  OBJ,
  TYPE 
)    (0*sizeof(&(OBJ) - (const TYPE *)&(OBJ)))

A compile-time type-checking macro for use with DIST_BASE_TYPED.

This macro works by checking that &OBJ is a pointer type that is the same type (except for qualifiers) as (const TYPE *)&OBJ. It's a C constraint violation (which requires a diagnostic) if two pointers are different types and are subtracted. The sizeof() forces compile-time evaluation, and the multiplication by zero is to discard the result of the sizeof() from the expression.

We define this conditionally to suppress false positives from Coverity, which gets confused by the sizeof business.

Definition at line 59 of file prob_distr.h.

Function Documentation

◆ dist_cdf()

double dist_cdf ( const struct dist dist,
double  x 
)

Compute the CDF of dist at x.

Definition at line 1341 of file prob_distr.c.

◆ dist_icdf()

double dist_icdf ( const struct dist dist,
double  p 
)

Compute the iCDF (Inverse CDF) of dist at x.

Definition at line 1355 of file prob_distr.c.

◆ dist_isf()

double dist_isf ( const struct dist dist,
double  p 
)

Compute the iSF (Inverse Survival function) of dist at x.

Definition at line 1362 of file prob_distr.c.

◆ dist_name()

const char* dist_name ( const struct dist dist)

Generic operations on distributions. These simply defer to the corresponding dist_ops function. In the parlance of C++, these call virtual member functions.

Public API for probability distributions:

These are wrapper functions on top of the various probability distribution operations using the generic dist structure.

These are the functions that should be used by consumers of this API.Returns the name of the distribution in dist.

Definition at line 1327 of file prob_distr.c.

◆ dist_sf()

double dist_sf ( const struct dist dist,
double  x 
)

Compute the SF (Survival function) of dist at x.

Definition at line 1348 of file prob_distr.c.