#NGroups 4 Likelihood Example Calculation Begin Matrices; X Full 2 1 ! Data Vector M Full 2 1 ! Vector of Means S Symm 2 2 ! Covariance Matrix P Full 1 1 ! Scalar Pi T Full 1 1 ! Scalar 2 H Full 1 1 ! Scalar .5 End Matrices; Matrix X .5 -.3 Matrix M 0 0 Matrix S 1 .5 1 Matrix P \pi ! 3.141592 Matrix T 2 Matrix H -.5 Begin Algebra; A= (X-M)'; ! Deviations of X Values from Means B= S~; ! Inverse of Covariance Matrix C= A&B; ! Mahalanobis Distance (x-m)'S~(x-m) D=\exp(H@C); ! e^(-.5 (x-m)'S~(x-m)) E=(T.P)@\sqrt(\det(S)); ! 2pi sqrt|S| F= E~*D; ! At last, the likelihood G= -T*\ln(F); ! Now -2ln L End Algebra; End Title Another few ways to look at the likelihood Data NInput=2 ! This is a data group and we're going to read 2 variables per line Rectangular .5 -.3 End Rectangular ! That was the data, folks, just one miserable case ! Note that we usually read it in from a file, e.g., rectangular file=myfile.rec ! Next put together some matrices for the model Begin Matrices; S Symm 2 2 ! Covariance matrix, usually would be free M Full 2 1 ! Vector of means, usually would be free End Matrices; ! Now put some values into the matrices M and S Matrix M 0 0 Matrix S 1 .5 1 ! Then specify the matrix formulae for the predicted means and covariances Means M; Covariance S; ! Lastly, we will dump the individual likelihood vectors to a file Option mx%p=indiv.lik End Title Another fascinating way to check things out Calculation Begin Matrices; F Full 1 1 =%F2 ! Function value = -2lnL from group 2 H full 1 1 End Matrices; Matrix H -.5 Begin Algebra; V = H*F; ! -.5 times -2lnL equals lnL W =\exp(V); ! e^(lnL) gives L itself End Algebra; End Title As if we hadn't had enough, we now do it via \pdfnor Calculation Begin Matrices = Group 1; ! Note how we get all the matrices from group 1 X full 2 1 ! But because the data were separate, we need a vector for them End Matrices; Matrix X .5 -.3 Begin Algebra; F = \pdfnor(X'_M'_S); ! Here's the height of the bivariate normal at that point G = \ln(F); ! Here's the logarithm of the height (aka likelihood) J = -G-G; ! And finally, -2lnL again End Algebra; End