lines 8-90 of file: example/python/mixed/ran_likelihood_xam.py {xrst_begin mixed_ran_likelihood_xam.py} {xrst_spell laplace } ran_likelihood: Example and Test ################################ p(y|theta, u) ************* In this example math:`y` given :math:`( \theta , u )` is distributed normally with mean :math:`\bar{y} + u` and variance :math:`\theta`; i.e. .. math:: - \log [ \B{p} ( y | \theta , u ) ] = \log \left[ \sqrt{ 2 \pi \theta } \right] + \frac{1}{2} ( y - \bar{y} - u )^2 / \theta p(u|theta) ********** In this example, the prior for :math:`u` given :math:`\theta` is a normal with mean zero and standard deviation :math:`\sigma`; i.e. .. math:: - \log [ \B{p} ( u | \theta ) ] = \log \left[ \sqrt{ 2 \pi \sigma^2 } \right] + \frac{1}{2} u^2 / \sigma^2 p(y|theta) ********** For this example, Laplace approximation is equal to :math:`\B{p}(y|\theta)` i.e, it is exact. Furthermore, .. math:: - \log[ \B{p}(y|\theta) ] = \log \left[ \sqrt{ 2 \pi ( \theta + \sigma^2) } \right] + \frac{1}{2} ( y - \bar{y} )^2 / ( \theta + \sigma^2 ) Optimal Fixed Effects ********************* For this example there is no fixed effects likelihood or constraints. Hence the optimal fixed effects minimizes the following expression w.r.t :math:`\theta`: .. math:: \frac{1}{2} \log \left[ \theta + \sigma^2 \right] + \frac{1}{2} ( y - \bar{y} )^2 / ( \theta + \sigma^2 ) Taking the derivative w.r.t. :math:`\theta` and setting it equal to zero, the optimal fixed effects :math:`\hat{\theta}` solves the equations .. math:: 0 & = ( \hat{\theta} + \sigma^2 )^{-1} - ( y - \bar{y} )^2 ( \hat{\theta} + \sigma^2 )^{-2} \\ 1 & = ( y - \bar{y} )^2 ( \hat{\theta} + \sigma^2 )^{-1} \\ \hat{\theta} & = (y - \bar{y})^2 - \sigma^2 {xrst_literal # BEGIN SOURCE # END SOURCE } {xrst_end mixed_ran_likelihood_xam.py}