Documentation

The sampler module

racecar.sampler

alias of racecar.sampler.Sampler

class racecar.sampler.Sampler(ic, h, llh, algo='racecar', params={})

A class containing the state information for the current sampler object.

Parameters
  • ic (numpy array) – initial condition for the sampler

  • h (float, positive) – the step size or learning rate to use in the sampler

  • llh (function) –

    The log posterior function to use for the sampler. The function should take the position as input, and output a dictionary. The information required is algorithm-dependent. The algorithms make use of keys:

    • ”llh” (float) : The value of the posterior function at any point

    • ”grad” (numpy array) : The gradient of the posterior

    • ”grad_data” (numpy array) : The gradients of the posterior for each data point

  • algo (string, optional) – The algorithm to use for the sampler. See the Algorithms page for a list of possible methods supported by the package. Uses the racecar algorithm as default.

  • params (dict, optional) – parameters

acceptance_rate()

Returns the acceptance rate of the previously run sample call.

Returns

A – the acceptance rate of the last sampling run, if applicable.

Return type

float

sample(Nsteps, printnum=None, thin=1, output=['pos'])

Runs the sampler and returns a trajectory (if required).

Parameters
  • Nsteps (int) – the number of steps to produce.

  • printnum (int, optional) – if given, prints a summary of the sampling printnum times

  • thin (int, optional) – thins the trajectory by only including one of each thin points

  • output (list, optional) –

    a list of strings giving which arrays should be returned by the function. The output list can contain

    • ”pos” : Outputs the trajectory of position

    • ”grad” : Outputs the gradient at each point

    • ”llh” : Output the log posterior evaluated at each point

    • ”xi” : Gives the auxilliary variable’s value at each step

    • ’mom’ : The sampled momentum points

    The default value is ["pos"].

Returns

  • Q (numpy array) – the trajectory array for sampled points

  • P (numpy array) – the sampled momentum points (if applicable)

  • LLH (numpy array) – the log posterior of sampled points

  • XI (numpy array) – the sampled auxillary variables (if applicable)

  • F (numpy array) – the gradient of the log posterior function at each sampled point (if applicable)

timetaken()

Returns the wall time of the previously run sample call.

Returns

T – the wall time of the last sampling run, in seconds.

Return type

float

The llh module

Contains examples of standard likelihood functions for usage in the package.

racecar.llh.blr(q, data, t, idxs=None, alpha=100)

Bayesian Logistic Regression with a Gaussian prior.

Parameters
  • q (numpy array) – Position parameter

  • data (numpy array) – A (N,d) array, where the d datapoints have dimensionality N.

  • t (numpy array) – A (1,d) binary array of indicator values

  • idxs (list or iterable, optional) – A list of indexes to use in the BLR calculation

  • alpha (float, optional) – The variance of the Gaussian prior, default 100.

Returns

r – returns the log likelihood under the llh key, the gradient under the grad key and the gradients for each data point are given in grad_data.

Return type

dictionary

racecar.llh.isotropic_gaussian(q)

An isotropic Gaussian likelihood function.

Parameters

q (numpy array) – Position parameter

Returns

r – returns the log likelihood under the llh key, and the gradient under the grad key.

Return type

dictionary