Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

Re: st: Input data from keyboard combined with Monte Carlo


From   "Joseph Coveney" <stajc2@gmail.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   Re: st: Input data from keyboard combined with Monte Carlo
Date   Thu, 20 Jun 2013 11:24:08 +0900

John Antonakis wrote:

Thanks for the idea. Thread closed--hope this solutions helps others 
(actually I did not look at your paper yet, Nick, but I will).

So, I basically used mkmat to save the data to a matrix, then I 
retrieved the data with svmat and it works, like this:

--------------------------------------------------------------------------------

As Nick mentioned, the simulator program takes a file name as an argument.
Forming a Stata matrix is fine, but it's unnecessary to go through the extra
steps if you're saving the file, anyway--you can just have your simulator
program -use- the file, itself.   (And if you're not concerned about
flexibility, then you can fix the file name just as you fixed the Stata matrix
name and then omit the file-name option from the simulator program.) 

I illustrate the two approaches below using the flexible approach and temporary
matrix names and files.  That the run times were essentially the same surprised
me a little.  With small enough datasets, the file isn't actually read from disc
each time, but rather from a memory cache, just like the Stata matrix.
Nevertheless, I expected that the Stata matrix approach would be substantially
faster, because it is cached in memory closer to the core, or at least so I
believe.  But the file approach that I suggested is just as fast, even a tad
faster, as seen consistently in repeated runs' timing results.

Joseph Coveney

. do "F:\comparem.do"

. *! comparem.do
. 
. version 12.1

. 
. clear *

. set more off

. set seed `=date("2013-06-20", "YMD")'

. 
. quietly set obs 111

. generate double y = runiform()

. tempfile tmpfil0

. quietly save `tmpfil0'

. 
. tempname Y

. mkmat y, matrix(`Y')

. 
. program define regressem
  1.     version 12.1
  2.     syntax
  3. 
.     foreach var of newlist x1-x17 {
  4.         generate double `var' = rnormal()
  5.     }
  6. 
.     regress y x*
  7. end

. 
. program define usem
  1.     version 12.1
  2.     syntax , file(string)
  3. 
.     drop _all
  4.     use `file'
  5. 
.     regressem
  6. end

. 
. program define svmatem
  1.     version 12.1
  2.     syntax , MATrix(name)
  3. 
.     drop _all
  4.     svmat `matrix', names(y)
  5. 
.     regressem
  6. end

. 
. forvalues i = 1/3 {
  2.     timer clear
  3. 
.     timer on 1
  4.     quietly simulate e(r2) e(F), reps(5000) seed(123): usem ,
file(`tmpfil0')
  5.     timer off 1
  6. 
.     timer on 2
  7.     quietly simulate e(r2) e(F), reps(5000) seed(123): svmatem , mat(`Y')
  8.     timer off 2
  9. 
.     timer list
 10. }
   1:     24.83 /        1 =      24.8300
   2:     25.03 /        1 =      25.0320
   1:     24.72 /        1 =      24.7200
   2:     25.00 /        1 =      25.0010
   1:     24.72 /        1 =      24.7200
   2:     25.00 /        1 =      25.0020

. 
. exit

end of do-file

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


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index