Statalist


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

st: Possible bug with Mata binomialp and variable argument


From   Bob Hammond <[email protected]>
To   [email protected]
Subject   st: Possible bug with Mata binomialp and variable argument
Date   Tue, 03 Feb 2009 16:29:27 -0500

All,

Claiming to have found a bug is always risky but here goes. The binomialp() function in Mata results in a missing value when one of the arguments is a variable name:

mata: binomialp(3,2,0.25)

works fine but:

mata: binomialp(n,2,0.25)

results in missing when the variable n equals 3 in observation 1. In contrast, binomial() works as expected in both cases above. Also, binomialp() works as expected in Stata (as opposed to in Mata) in both cases. If it is not clear why one would need to use a variable name within binomialp(), the code at the end of this message illustrates one such situation (with a simplified function). The work-around is clear (use (binomial(n,2,0.25)-binomial(n,1,0.25)) instead of binomialp(n,2,0.25)) but I thought I should point out the bug (if it is one) anyway.

Thanks,

Bob

Works:
>>>>>>>>>>>>>>>>>>>>>>>
clear
set obs 10
gen byte n = 2 + int(5*runiform())
gen double x = .
cap mata: mata drop myfunc()

*the following requires moremata to be installed (findit moremata)
mata:
function myfunc(x,n) return( x^2 - (binomial(n,2,0.25)-binomial(n,1,0.25)))
   for(i=1;i<=10;i++) {
       st_view(n = .,i,"n")
       mm_root(x = .,&myfunc(),0,1,0,1000,n)
       st_store(i,"x",x)
   }
end
list
>>>>>>>>>>>>>>>>>>>>>>>

Does not work:
>>>>>>>>>>>>>>>>>>>>>>>
clear
set obs 10
gen byte n = 2 + int(5*runiform())
gen double x = .
cap mata: mata drop myfunc()

*the following requires moremata to be installed (findit moremata)
mata:
   function myfunc(x,n) return( x^2 - binomialp(n,2,0.25))
   for(i=1;i<=10;i++) {
       st_view(n = .,i,"n")
       mm_root(x = .,&myfunc(),0,1,0,1000,n)
       st_store(i,"x",x)
   }
end
list
>>>>>>>>>>>>>>>>>>>>>>>

--
------------------------------------------------------------------------
Bob Hammond
Department of Economics
North Carolina State University
Office: (919) 513-2871
Fax: (919) 515-7873
http://www4.ncsu.edu/~rghammon/
*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index