mata programming ii
play

Mata Programming II Christopher F Baum Boston College and DIW - PowerPoint PPT Presentation

Mata Programming II Christopher F Baum Boston College and DIW Berlin NCER, Queensland University of Technology, August 2015 Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 1 / 73 Mata-based likelihood function evaluators


  1. Mata Programming II Christopher F Baum Boston College and DIW Berlin NCER, Queensland University of Technology, August 2015 Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 1 / 73

  2. Mata-based likelihood function evaluators Mata-based likelihood function evaluators We described earlier how a function-evaluator program could be written for estimation via the method of maximum likelihood. That approach is based on the ado-file language. Stata can also use likelihood function evaluators written as Mata functions. This may be particularly attractive when the likelihood function is readily expressed in matrix terms. Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 2 / 73

  3. Mata-based likelihood function evaluators As an example, we rewrite the function evaluator for ordinary linear regression as a Mata function. For comparison, here is the ado-file version of that function evaluator: . type mynormal_lf.ado *! mynormal_lf v1.0.0 CFBaum 08feb2014 program mynormal_lf version 13.1 args lnfj mu sigma quietly replace ` lnfj ´ = ln( normalden( $ML_y1, ` mu ´ , ` sigma ´ ) ) end Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 3 / 73

  4. Mata-based likelihood function evaluators The Mata version of this evaluator receives three arguments: the ‘problem handle’ ML , a parameter vector b and the vector lnfj , with dimension equal to the number of observations, to be computed in the evaluator. In the Mata version, a single parameter vector is used. The moptimize_util_xb() function is then used to extract the parameters pertaining to each equation. The computation of lnfj is essentially the same as the syntax used in the ado-file version. Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 4 / 73

  5. Mata-based likelihood function evaluators . version 13 . mata: mata (type end to exit) : : void mynormal_lf( transmorphic scalar ML, > real rowvector b, > real colvector lnfj) > { > real vector depvar, xb > real scalar sigma > depvar = moptimize_util_depvar(ML, 1) > xb = moptimize_util_xb(ML, b, 1) > sigma = moptimize_util_xb(ML, b, 2) > > lnfj = ln( normalden( depvar, xb, sigma) ) > } : end Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 5 / 73

  6. Mata-based likelihood function evaluators To invoke the Mata-based function evaluator, we need only reference its function name, mynormal_lf() , in the ml model command. Without the following () , Stata looks for an ado-file evaluator with that name. With the () appended, Stata looks for a Mata function with that name. Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 6 / 73

  7. Mata-based likelihood function evaluators . sysuse auto,clear (1978 Automobile Data) . ml model lf mynormal_lf() (mpg = weight displacement) /sigma . ml maximize, nolog initial: log likelihood = -<inf> (could not be evaluated) feasible: log likelihood = -10383.274 rescale: log likelihood = -292.89564 rescale eq: log likelihood = -238.45986 Number of obs = 74 Wald chi2(2) = 139.21 Log likelihood = -195.2398 Prob > chi2 = 0.0000 mpg Coef. Std. Err. z P>|z| [95% Conf. Interval] eq1 weight -.0065671 .0011424 -5.75 0.000 -.0088061 -.0043281 displacement .0052808 .0096674 0.55 0.585 -.0136671 .0242286 _cons 40.08452 1.978738 20.26 0.000 36.20627 43.96278 sigma _cons 3.385282 .2782684 12.17 0.000 2.839886 3.930678 Examination of these results show that they are identical to those displayed earlier. Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 7 / 73

  8. Creating arrays of temporary objects with pointers Creating arrays of temporary objects with pointers In Stata’s ado-file language, it is common to create a set of temporary objects: temporary variables, local macros, or matrices. Sometimes it is necessary to create a different number of temporary items each time we run the program. That is a simple task in Stata’s ado-file language, as we have seen in several examples. In Mata, however, the solution is not immediately clear: we need to declare variables, but the number of variables is not fixed. It is not obvious how to create a set of unknown dimension in Mata code. We consider that problem, using an example from the econometric literature. Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 8 / 73

  9. Creating arrays of temporary objects with pointers Why might we need such a facility? Consider a Stata program that accepts a positive integer argument of lags() , which could take on any number of values, and then computes a matrix from several Stata variables for each lag. In some applications, such as the computation of a heteroskedastic- and autocorrelation-consistent (HAC) covariance matrix, two temporary objects will suffice, because the logic of that computation accumulates the matrices computed for each lag. We create one matrix in which to compute each term in the series and a second to hold the accumulation. In other contexts, however, we might need to use the values contained in each of those matrices in some formula. Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 9 / 73

  10. Creating arrays of temporary objects with pointers In ado-file code, we might write forvalues i = 1/‘lags’ { tempname phi‘i’ matrix ‘phi‘i” = exp } which would define and compute the contents of a sequence of temporary matrices phi1, phi2, . . . , phi‘lags’ . What would we need to do to perform this same task in a Mata context? Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 10 / 73

  11. Creating arrays of temporary objects with pointers The solution involves the use of the sixth element type in Mata: the pointer . A pointer is an object that contains the address of another object. Pointers are commonly used to put a collection of items under one name and to pass functions to functions. A pointer to matrix X contains the address, or location in the computer’s memory, of X . We do not really care to know what that number might be. It suffices to know that if we dereference the pointer, we are referring to the contents of the underlying object to which it points. Note the similarity to Stata’s macros: regardless of the name of the macro, when we dereference it, we access its contents. The dereferencing operator in Mata is * , so that if p is a pointer variable, *p refers to the underlying object. Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 11 / 73

  12. Creating arrays of temporary objects with pointers How do we create a pointer variable? With the operator & , which instructs Mata to store the address (rather than the contents) of its argument in a pointer variable: : X = (1, 2 \3, 4) : p = &X creates a pointer variable p which contains the address of matrix X . If we then refer to the variable: : Z = *p matrix Z will now be set equal to matrix X . Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 12 / 73

  13. Creating arrays of temporary objects with pointers Likewise, : r = (*p)[ 2, .] will cause r to be created as a row vector equal to the second row of X . Note that we use parentheses around *p to clarify that we refer to the second row of the object defined by dereferencing the pointer. Also note that the contents of p itself are not very useful: : p 0x2823b10c Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 13 / 73

  14. Creating arrays of temporary objects with pointers Why, then, are pointers an important programming tool? If pointer variables could only be real scalars, they would not be very useful. But like other Mata objects, pointer variables may be arrayed in vectors or matrices. That now gives us the tools necessary to deal with the problem laid out above. Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 14 / 73

  15. Creating arrays of temporary objects with pointers We would like to set up a set of matrices, phi1 , . . . , phi‘lags’ , within Mata where the Mata function will be called with the argument of lags . Within Mata, we now know how many matrices are to be created, and for ease of computation we want them to be named sequentially as phi1, phi2 . . . . We need to set up an array of pointers that will contain the addresses of each of these matrices. As with other Mata objects, we may fully declare the type of object we create, and it is recommended that you do so. Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 15 / 73

  16. Creating arrays of temporary objects with pointers : pointer(real matrix) rowvector ap : ap = J(1, nlag, NULL) We declare a vector of pointers, named ap , to real matrices. We then declare the vector as having nlag elements, each NULL .null pointer The keyword NULL is the equivalent of a missing value for a pointer variable. It indicates that it currently points to nothing. Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 16 / 73

  17. Creating arrays of temporary objects with pointers Having set up this array, we may now fill in each of its elements: : for(i = 1; i <= nlag; i++) { : ap[i] = &( matrix expression ) : } Within the loop, we compute some matrix expression, enclose it in parentheses, and store its address in the i th element of the ap array. This matrix is anonymous in that it is not given a name; we merely record its address in the appropriate pointer variable. When we want to make use of that matrix, we refer to it as *ap[i] . Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 17 / 73

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend