Tor  0.4.4.0-alpha-dev
Data Structures | Macros | Functions | Variables
prob_distr.h File Reference

Header for prob_distr.c. More...

#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_t
 
struct  dist_ops_t
 
struct  geometric_t
 
struct  genpareto_t
 
struct  weibull_t
 
struct  log_logistic_t
 
struct  logistic_t
 
struct  uniform_t
 

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_t)
 
#define GENPARETO(OBJ)   DIST_BASE_TYPED(&genpareto_ops, OBJ, struct genpareto_t)
 
#define WEIBULL(OBJ)   DIST_BASE_TYPED(&weibull_ops, OBJ, struct weibull_t)
 
#define LOG_LOGISTIC(OBJ)   DIST_BASE_TYPED(&log_logistic_ops, OBJ, struct log_logistic_t)
 
#define LOGISTIC(OBJ)   DIST_BASE_TYPED(&logistic_ops, OBJ, struct logistic_t)
 
#define UNIFORM(OBJ)   DIST_BASE_TYPED(&uniform_ops, OBJ, struct uniform_t)
 

Functions

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

Variables

const struct dist_ops_t geometric_ops
 
const struct dist_ops_t genpareto_ops
 
const struct dist_ops_t weibull_ops
 
const struct dist_ops_t log_logistic_ops
 
const struct dist_ops_t logistic_ops
 
const struct dist_ops_t 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_t using the specified struct dist_ops_t 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_t using the specified struct dist_ops_t 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_t),
  .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_t {
  struct dist_t base;
  int omega;
  double tau;
  double phi;
};

struct dist_ops_t foo_ops = ...;

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

Then users can do:

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

If you accidentally write

struct bar_t 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_t dist,
double  x 
)

Compute the CDF of dist at x.

Definition at line 1343 of file prob_distr.c.

◆ dist_icdf()

double dist_icdf ( const struct dist_t dist,
double  p 
)

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

Definition at line 1357 of file prob_distr.c.

◆ dist_isf()

double dist_isf ( const struct dist_t dist,
double  p 
)

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

Definition at line 1364 of file prob_distr.c.

◆ dist_name()

const char* dist_name ( const struct dist_t dist)

Generic operations on distributions. These simply defer to the corresponding dist_ops_t 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 1329 of file prob_distr.c.

◆ dist_sf()

double dist_sf ( const struct dist_t dist,
double  x 
)

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

Definition at line 1350 of file prob_distr.c.