# tfp.experimental.substrates.numpy.experimental.inference_gym.targets.BayesianModel

Base class for Bayesian models in the Inference Gym.

Inherits From: `Model`

Given a Bayesian model described by a prior `P(x)` which we can sample from, and a likelihood `P(x | y)` with evidence `y` we construct the posterior by multiplying the two terms. The posterior distribution `P(x | y)` is represented as a product of the inverse normalization constant and the un-normalized density: `1/Z tilde{P}(x | y)`.

#### Examples

A simple 2-variable Bayesian model:

``````class SimpleModel(gym.targets.BayesianModel):

def __init__(self):
self._prior_val = tfd.Exponential(0.)
self._evidence = 1.

def log_likelihood_fn(x):
return tfd.Normal(x, 1.).log_prob(self._evidence)

self._log_likelihood_fn = log_likelihood_fn

super(SimpleModel, self).__init__(
default_event_space_bijector=tfb.Exp(),
event_shape=self._prior_val.event_shape,
dtype=self._prior_val.dtype,
name='simple_model',
pretty_name='SimpleModel',
sample_transformations=dict(
identity=gym.targets.Model.SampleTransformation(
fn=lambda x: x,
pretty_name='Identity',
),),
)

def _prior_distribution(self):
return self._prior_val

def _log_likelihood(self, value):
return self._log_likelihood_fn(value)
``````

Note how we first constructed a prior distribution, and then used its properties to specify the Bayesian model. Note that we don't need to define an explicit `_unnormalized_log_prob`, as that's automatically constructed from the defined `_prior_distribution` and `_log_likelihood` methods.

We don't specify the ground truth values for the `identity` sample transformation as they're not known analytically. See `GermanCreditNumericLogisticRegression` Bayesian model for an example of how to incorporate Monte-Carlo derived values for ground truth into a sample transformation.

`default_event_space_bijector` A (nest of) bijectors that take unconstrained `R**n` tensors to the event space of the posterior.
`event_shape` A (nest of) shapes describing the samples from the posterior.
`dtype` A (nest of) dtypes describing the dtype of the posterior.
`name` Python `str` name prefixed to Ops created by this class.
`pretty_name` A Python `str`. The pretty name of this model.
`sample_transformations` A dictionary of Python strings to `SampleTransformation`s.

`default_event_space_bijector` Bijector mapping the reals (R**n) to the event space of this model.
`dtype` The `DType` of `Tensor`s handled by this model.
`event_shape` Shape of a single sample from as a `TensorShape`.

May be partially defined or unknown.

`name` Python `str` name prefixed to Ops created by this class.
`sample_transformations` A dictionary of names to `SampleTransformation`s.

## Child Classes

`class SampleTransformation`

## Methods

### `log_likelihood`

View source

Evaluates the log_likelihood at `value`.

### `prior_distribution`

View source

The prior distribution over the model parameters.

### `unnormalized_log_prob`

View source

The un-normalized log density of evaluated at a point.

This corresponds to the target distribution associated with the model, often its posterior.

Args
`value` A (nest of) `Tensor` to evaluate the log density at.
`name` Python `str` name prefixed to Ops created by this method.

Returns
`unnormalized_log_prob` A floating point `Tensor`.