Liknorm’s documentation

Estimates

\[\int_{l}^r \text{ExpFam}(\theta, g(x)) \mathcal{N} (x | \mu, \sigma^2) \mathrm d x\]

via deterministic numerical integration.

The values \(l\) and \(r\) are necessarily the lower and upper bound of the likelihood domain. (The user is not allowed to set it.) \(g(\cdot)\) is a link function and \(\theta\) determines the exponential-family distribution of interest:

Install

You can install it via [conda](http://conda.pydata.org/docs/index.html)

conda install -c conda-forge liknorm

or by cloning this repository and building it

git clone https://github.com/glimix/liknorm.git
cd liknorm
mkdir build
cd build
cmake ..
make
sudo make install

Usage example

Suppose you have the file

/* example.c */
#include "liknorm/liknorm.h"

#include <stdio.h>

int main()
{
  double log_zeroth, mean, variance;
  double prior_var = 2.5;
  double prior_mean = -2.0;
  double nsuccesses = 2;
  double ntrials = 15;

  LikNormMachine *machine = liknorm_create_machine(500);

  liknorm_set_binomial(machine, nsuccesses, ntrials);
  liknorm_set_prior(machine, 1 / prior_var, prior_mean / prior_var);

  liknorm_integrate(machine, &log_zeroth, &mean, &variance);

  printf("%f\n", log_zeroth);
  printf("%f\n", mean);
  printf("%f\n", variance);

  liknorm_destroy_machine(machine);
}

Compiling, linking, and running it via

cc libliknorm.a example.c -o example
./example

should print:

-2.049961
-2.038184
0.524308

Functions description

LikNormMachine* liknorm_create_machine(int size)

Create a Machine instance capable of doing numerical integration.

Parameters:
  • size (int) – Number of integration points. 500 points should be enough. 300 is usually fine too.
Returns:

Machine instance to perform integration.

Return type:

LikNormMachine*

void liknorm_integrate(LikNormMachine *machine, double *log_zeroth, double *mean, double *variance)

Perform numerical integration.

Parameters:
  • machine (LikNormMachine*) – Machine to perform integration.
  • log_zeroth (double*) – Zeroth moment.
  • log_mean (double*) – First moment of the normalized distribution.
  • log_variance (double*) – Variance of the normalized distribution.
void liknorm_destroy_machine(LikNormMachine *machine)

Destroy a Machine instance.

Parameters:
  • machine (LikNormMachine*) – Machine to be destroyed. Always call it before exiting your program, otherwise it will leak memory.
void liknorm_set_bernoulli(LikNormMachine *machine, double k)

Set a Bernoulli likelihood.

Parameters:
  • machine (LikNormMachine*) – Machine to perform integration.
  • k (double) – 0 or 1 indicating a Bernoulli outcome.
void liknorm_set_binomial(LikNormMachine *machine, double k, double n)

Set a Binomial likelihood.

Parameters:
  • machine (LikNormMachine*) – Machine to perform integration.
  • k (double) – Number of successes.
  • n (double) – Number of trials.
void liknorm_set_poisson(LikNormMachine *machine, double k)

Set a Poisson likelihood.

Parameters:
  • machine (LikNormMachine*) – Machine to perform integration.
  • k (double) – Number of successes.
void liknorm_set_exponential(LikNormMachine *machine, double x)

Set a Exponential likelihood.

Parameters:
  • machine (LikNormMachine*) – Machine to perform integration.
  • x (double) – Time span.
void liknorm_set_gamma(LikNormMachine *machine, double x, double a)

Set a Gamma likelihood.

Parameters:
  • machine (LikNormMachine*) – Machine to perform integration.
  • x (double) – Positive outcome.
  • a (double) – Shape parameter.
void liknorm_set_geometric(LikNormMachine *machine, double x)

Set a Geometric likelihood.

Parameters:
  • machine (LikNormMachine*) – Machine to perform integration.
  • x (double) – Number of trials to success.
void liknorm_set_prior(LikNormMachine *machine, double tau, double eta)

Set the natural parameters of Normal prior.

Parameters:
  • machine (LikNormMachine*) – Machine to perform integration.
  • tau (double) – It equals to \(\sigma^{-2}\).
  • eta (double) – It equals to \(\mu \sigma^{-2}\).