Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: mata optimize problem


From   "Michael Ralph M. Abrigo" <[email protected]>
To   [email protected]
Subject   Re: st: mata optimize problem
Date   Sun, 4 Jul 2010 10:11:08 +0800

Many thanks Professors Austin and Stas. Points well taken. I already
have found the bug, thanks to your help, and did the necessary
tweaking. I was planning to set the externals as parameters later
after I set it running, but as Prof Stas as pointed out I should set
them as parameters outright. I was able to run it (yey!), but now I
was prompted

could not calculate numerical derivatives -- discontinuous region with missing
values encountered

I don't now whether this is what Lambert, Brown and Florax's (2010)
are saying that numerical solutions using various search algorithms
were difficult to obtain, or because of my codes. In any case thank
you very much. This is my very first code in Mata.

Cheers,
Michael

On Sun, Jul 4, 2010 at 2:08 AM, Stas Kolenikov <[email protected]> wrote:
> set matalnum on
>
> to see which line of your procedure generates the error. To make your
> program well defended against bad inputs, you can code something like
>
> if ( cols(A_inv) != rows( X ) ) {
>  printf( "{err}A_inv is not conformant to X"\n)
>  lndensity[,] = J( rows(lndensity), 1, . )
>  return
> }
>
> Note also that your observations lie on the straight line x==y. A more
> sensible set of values might have been
>
> gen xcoor = uniform()
> gen ycoor = uniform()
>
> producing points distribution U[0,1]^2.
>
> From programming style, I would try to get rid of the -external-
> matrices in your routine. Any -external- statement read like "I don't
> bother explaining to you what's going on here, and what these matrices
> are", which makes it more difficult to debug. You can transfer them as
> parameters of -optimize()-. And of course transferring a known matrix
> I is kinda silly, you can generate it within the routine.
>
> On Sat, Jul 3, 2010 at 8:44 AM, Austin Nichols <[email protected]> wrote:
>> Michael Ralph M. Abrigo <[email protected]> :
>>
>> I have not read through this, but you should put the dimensions of
>> every matrix in as a comment, and also print out the matrices once,
>> and then maybe you will see where the error is.
>>
>> On Sat, Jul 3, 2010 at 2:08 AM, Michael Ralph M. Abrigo
>> <[email protected]> wrote:
>>> Good day statalisters!
>>> I am practically new in programming, especially in Mata. I'm trying to
>>> implement Lambert, Brown and Florax's (2010) poisson spatial lag model
>>> ("A two-step estimator for a spatial lag model for counts: Theory,
>>> small sample performance and an application", Urban and Regional
>>> Economics) in Stata using full-information ML (instead of the two-step
>>> LIML). However, I got stuck because of a conformability error which I
>>> cannot trace (I was able to generate the matrices interactively using
>>> ad hoc values). I was hoping I can do the FIML first before I venture
>>> to their two-step estimator.
>>> Any hint will be greatly appreciated.
>>> Below is my code and the error messages.
>>> Cheers,
>>> Michael
>>> drop _all
>>> ** (1) Set version; Open file
>>> version 11.0
>>> webuse "dollhill3.dta", clear
>>> ** (2) Generate constant; Local variables
>>> gen cons = 1
>>> local y deaths
>>> local xlist smokes pyears cons
>>> ** (3) Generate weight matrix
>>> sort pyears
>>> gen xcoor = _n
>>> gen ycoor = _n
>>> spwmatrix gecon xcoor ycoor, wname(W) wtype(bin) ///
>>>    dband(0 1000) rowstand mata replace
>>> ** (4) Optimize using Mata
>>> mata
>>>   void spmlpoisson (
>>>     real scalar todo,
>>> real rowvector rho_b,
>>> real colvector lndensity,
>>> real matrix g,
>>> real matrix H)
>>> {
>>>       external y, X, W, I
>>>  transmorphic A_inv
>>>  real colvector Xb_sar, mu_sar
>>>  real rowvector b
>>>  real scalar rho
>>>       rho_b = rho, b
>>> A_inv = cholinv(I - rho * W)
>>> Xb_sar = A_inv * X * b'
>>>         mu_sar = exp(Xb_sar)
>>> lndensity = (y :* Xb_sar) - mu_sar - lnfactorial(y)
>>> }
>>>   st_view(y = ., ., "`y'")
>>>   st_view(X = ., ., tokens("`xlist'"))
>>>   fh = fopen("W", "r")
>>>     W = fgetmatrix(fh)
>>>    fclose(fh)
>>>   I = I(cols(W))
>>>   S = optimize_init()
>>>   optimize_init_evaluator(S, &spmlpoisson())
>>>   optimize_init_which(S, "max")
>>>   optimize_init_evaluatortype(S, "d0")
>>>   optimize_init_params(S, J(1, cols(X) + 1, 0))
>>>   rho_b = optimize(S)
>>>                        *:  3200  conformability error
>>>            spmlpoisson():     -  function returned error
>>>       opt__calluser0_d():     -  function returned error
>>>       opt__d0_calluser():     -  function returned error
>>>     deriv__call1user_d():     -  function returned error
>>>  _deriv__compute_value():     -  function returned error
>>>                 _deriv():     -  function returned error
>>>        opt__eval_nr_d0():     -  function returned error
>>>              opt__eval():     -  function returned error
>>> opt__looputil_iter0_common():     -  function returned error
>>> opt__looputil_iter0_nr():     -  function returned error
>>>           opt__loop_nr():     -  function returned error
>>>               optimize():     -  function returned error
>>>                  <istmt>:     -  function returned error
>>> r(3200);
>>>
>>>
>>> end
>>
>> *
>> *   For searches and help try:
>> *   http://www.stata.com/help.cgi?search
>> *   http://www.stata.com/support/statalist/faq
>> *   http://www.ats.ucla.edu/stat/stata/
>>
>
>
>
> --
> Stas Kolenikov, also found at http://stas.kolenikov.name
> Small print: I use this email account for mailing lists only.
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
>



-- 
"I am most anxious for liberties for our country... but I place as a
prior condition the education of the people so that our country may
have an individuality of its own and make itself worthy of
liberties... " Jose Rizal,1896

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index