LApprox Functions¶
Calculate the Laplace Approximation
- LApprox.Calculate_Hessian(func, vals, **kwargs)¶
Function to calculate the Hessian Matrix of a generic function
- Parameters
func (function) – Python function to estimate the Hessian matrix of, N dimensional
vals (array) – numpy array of length N. The point at which to estimate the matrix of second derivatives
kwargs – keyword arguments
- Returns
numpy array, Hessian of the function func. Shape NxN.
- Return type
array
- LApprox.Laplace_Approximation(func, x0, **kwargs)¶
Calculate the Laplace Approximation of an integral of specific form.
- A challenging integral, when possible to write in terms of an exponent:
- \[Z = \int(exp(f(x))dx)\]
Can be estimated as approximately:
\[[\frac{(2\pi)^{2}}{det|H(x_{0})|}]^{\frac{1}{2}} * exp(f(x_{0}))\]where H is the functions Hessian matrix, and x0 is a region of high probability.
- Parameters
func (function) – python function, f(x), in the exponent of the term we wish to estimate. Note that this is NOT the total function that we are trying to integrate, but f(x), in the exponent.
x0 (array) – numpy array of N values, where the function is N-dimensional. This is the local maximum around which to compute the approxmation. When done correctly, one should optimize a function first before finding the Laplace Approximation.
- Returns
logA (float) and logB (float). logA = f(x0), where exp(f(x0)) is A. We return the logarithm because, in practice, many functions we wish to calculate the Laplace Approximation for return very small or very large values that can overflow a computer’s floating point precision. The term added by Laplace’s approximation involving the Hessian Matrix, [(2pi)^2/(det|H(x_{0})|)]^1/2 where we take the logarithm for consistency with A.
- Return type
(tuple)
- LApprox.NDeriv_2(func, x0, dim1, dim2, **kwargs)¶
Numerically calculate the second partial derivative of a function
- Parameters
func (function) – python function to calculate the derivitive of with dimension N
x0 (array) – point at which to calculate the derivative, N dimensional
dim1 (int) – which dimension to take the first partial derivative of
dim2 (int) – which dimension to take the second partial derivative of
kwargs – keyword arguments
- Returns
numerical second partial derivative with respect to dimensions one and two
- Return type
float