## gov.sandia.cognition.statistics.bayesian Class BayesianUtil

```java.lang.Object gov.sandia.cognition.statistics.bayesian.BayesianUtil
```

`public class BayesianUtilextends Object`

Contains generally useful utilities for Bayesian statistics.

Since:
3.0
Author:
Kevin R. Dixon

Constructor Summary
`BayesianUtil()`

Method Summary
```static <ObservationType> double ``` ```deviance(ComputableDistribution<ObservationType> conditional, Iterable<? extends ObservationType> observations)```
Computes the deviance of the model, which is -2log(p(observations|parameter)).
```static <ObservationType,ParameterType> UnivariateGaussian ``` ```expectedDeviance(BayesianParameter<ParameterType,? extends ComputableDistribution<ObservationType>,?> predictiveDistribution, Iterable<? extends ObservationType> observations, Random random, int numSamples)```
Computes the expected deviance of the model by sampling parameters from the posterior and then computing the deviance using the conditional distribution.
`static UnivariateGaussian` `getMean(Collection<? extends Double> samples)`
Computes the Monte Carlo distribution of the given samples.
```static <ObservationType> double ``` ```logLikelihood(ComputableDistribution<? super ObservationType> distribution, Iterable<? extends ObservationType> observations)```
Computes the log likelihood of the i.i.d.
```static <ObservationType,ParameterType> ArrayList<ObservationType> ``` ```sample(BayesianParameter<ParameterType,? extends Distribution<ObservationType>,? extends Distribution<ParameterType>> parameter, Random random, int numSamples)```
Samples from the given BayesianParameter by first sampling the prior distribution, then updating the conditional distribution, then sampling from the updated conditional distribution.
```static <ObservationType,ParameterType> ArrayList<? extends ObservationType> ``` ```sample(ClosedFormDistribution<ObservationType> conditional, String parameterName, Distribution<ParameterType> prior, Random random, int numSamples)```
Samples from the given BayesianParameter by first sampling the prior distribution, then updating the conditional distribution, then sampling from the updated conditional distribution.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Constructor Detail

### BayesianUtil

`public BayesianUtil()`
Method Detail

### logLikelihood

```public static <ObservationType> double logLikelihood(ComputableDistribution<? super ObservationType> distribution,
Iterable<? extends ObservationType> observations)```
Computes the log likelihood of the i.i.d. data using the given probability function.

Type Parameters:
`ObservationType` - Type of observations to consider
Parameters:
`distribution` - Computable distribution from which to get the probability function (a pdf or pmf) used to compute the likelihood.
`observations` - Observations to compute the log likelihood of.
Returns:
Log likelihood of the i.i.d. data using the given probability function.

### sample

```public static <ObservationType,ParameterType> ArrayList<? extends ObservationType> sample(ClosedFormDistribution<ObservationType> conditional,
String parameterName,
Distribution<ParameterType> prior,
Random random,
int numSamples)```
Samples from the given BayesianParameter by first sampling the prior distribution, then updating the conditional distribution, then sampling from the updated conditional distribution.

Type Parameters:
`ObservationType` - Type of observations generated by the conditional distribution
`ParameterType` - Type of parameters generated by the prior distribution, used to update the conditional distribution
Parameters:
`conditional` - Conditional distribution that generates observations
`parameterName` - Name of the parameter in the conditional distribution that is generated according to the prior distribution
`prior` - Prior distribution of parameter values
`random` - Random number generator
`numSamples` - Number of samples to generate
Returns:
Samples from the given BayesianParameter by first sampling the prior distribution, then updating the conditional distribution, then sampling from the updated conditional distribution.

### sample

```public static <ObservationType,ParameterType> ArrayList<ObservationType> sample(BayesianParameter<ParameterType,? extends Distribution<ObservationType>,? extends Distribution<ParameterType>> parameter,
Random random,
int numSamples)```
Samples from the given BayesianParameter by first sampling the prior distribution, then updating the conditional distribution, then sampling from the updated conditional distribution.

Type Parameters:
`ObservationType` - Type of observations generated by the conditional distribution
`ParameterType` - Type of parameters generated by the prior distribution, used to update the conditional distribution
Parameters:
`parameter` - BayesianParameter that links the conditional distribution via a parameter to a prior distribution.
`random` - Random number generator
`numSamples` - Number of samples to generate
Returns:
Samples from the given BayesianParameter by first sampling the prior distribution, then updating the conditional distribution, then sampling from the updated conditional distribution.

### deviance

```@PublicationReference(author={"Andrew Gelman","John B. Carlin","Hal S. Stern","Donald B. Rubin"},
title="Bayesian Data Analysis, Second Edition",
type=Book,
year=2004,
pages={180,181},
notes="Equation 6.6")
public static <ObservationType> double deviance(ComputableDistribution<ObservationType> conditional,
Iterable<? extends ObservationType> observations)```
Computes the deviance of the model, which is -2log(p(observations|parameter)). This is proportional to the mean squared error if the model is normal with constant variance.

Type Parameters:
`ObservationType` -
Parameters:
`conditional` - Conditional distribution that generates observations.
`observations` - Observations to consider
Returns:
Deviance of the model.

### expectedDeviance

```@PublicationReference(author={"Andrew Gelman","John B. Carlin","Hal S. Stern","Donald B. Rubin"},
title="Bayesian Data Analysis, Second Edition",
type=Book,
year=2004,
pages={180,181},
notes="Equation 6.9")
public static <ObservationType,ParameterType> UnivariateGaussian expectedDeviance(BayesianParameter<ParameterType,? extends ComputableDistribution<ObservationType>,?> predictiveDistribution,
Iterable<? extends ObservationType> observations,
Random random,
int numSamples)```
Computes the expected deviance of the model by sampling parameters from the posterior and then computing the deviance using the conditional distribution. This is also proportional to the Kullback-Leibler divergence of the posterior up to an additive constant. In the limit of infinite data, the model with the lowest expected deviance will have the highest posterior probability.

Type Parameters:
`ObservationType` - Type of observations generated by the conditional distribution.
`ParameterType` - Type of parameters generated by the posterior distribution.
Parameters:
`predictiveDistribution` - Relationship between the posterior (parameters) and the conditional (observations).
`observations` - Observations to consider when computing the deviance
`random` - Random number generator
`numSamples` - Number of samples to use in the expectation.
Returns:
Expected deviance of the model.

### getMean

`public static UnivariateGaussian getMean(Collection<? extends Double> samples)`
Computes the Monte Carlo distribution of the given samples.

Parameters:
`samples` - Samples to consider.
Returns:
Distribution describing the mean and estimated variance of the mean of the samples.