Hi all,
this is be a bit of a long and complicated post, but I am confused a little bit about a finding I cannot explain. Maybe (hopefully) my error is very basic and obvious (but not to me at the moment....) The post is interesting for Stata 15 and not relevant for Stata 14.
I am using a program to set the type of the random number generator, seed and seed stream. My expectation was, that this sets the random number generator, stream and seed. However it does not. What I found is, that Stata "returns" to the default random number generator type after the program is finished. This implies that if I draw the same number of random numbers outside and within the program, the numbers differ.
The following example shows it:
The program seed_checker set a type of the random number generator, the stream and a seed. Then it saves those in mata matrices and - if requested - draws 10 random numbers. It results in the following
x is drawn at the very beginning. Only x_2 is drawn within the program. x_1 is drawn after the program is called and x_3 is drawn doing the same as the program does, but outside of it.
I do understand that the results in x and x_1 differ, because I set a different random number generator type and stream (mt64s instead of mt64). What I don't understand is, that x and x_2 are the same, but not x_1 and x_2. The difference between x_1 and x_2 is, that x_2 is drawn inside the program, x_1 after the program. Also I don't understand that x and x_2 are the same as I changed from mt64 to mt64s. Then I though I check the type of the random number generator:
The first column refers to the very beginning, column 2 is from the first example after calling the program and column 3 within the program. Thus it looks like the random number generator within and after the program is different. Why is this the case? The same is true for the 2nd example. The numbers drawn in example 2 are the same as in the starting example. They are the same (and the same as in the "by hand case"), but the type of the generators are different. What is the rational behind this?
When checking the random number states after drawing, it turns out that only the ones after example 1 and 2 are the same and the internal one from example 2 and the one for example 3 ("by hand") are the same.
Once again the rng_state after 1 and 2 are the same, but the random numbers are different? The only bit which makes sense to me is that the states for example 2 (drawn inside the program) and for the "by hand" example are the same.
To sum up, my questions are:
this is be a bit of a long and complicated post, but I am confused a little bit about a finding I cannot explain. Maybe (hopefully) my error is very basic and obvious (but not to me at the moment....) The post is interesting for Stata 15 and not relevant for Stata 14.
I am using a program to set the type of the random number generator, seed and seed stream. My expectation was, that this sets the random number generator, stream and seed. However it does not. What I found is, that Stata "returns" to the default random number generator type after the program is finished. This implies that if I draw the same number of random numbers outside and within the program, the numbers differ.
The following example shows it:
Code:
clear all clear rngstream capture program drop seed_checker program seed_checker syntax anything [,draw ] clear rngstream set rng mt64s set rngstream 1 set seed `anything' mata rng_int =( "`c(rng)'" \ "`c(rng_current)'") if "`draw'" == "draw" { mata x_sc = rnormal(10,1,0,1) } mata rngstate_int = "`c(rngstate)'" end * Starting Example clear rngstream set seed 123 mata x = rnormal(10,1,0,1) mata rng_start = ( "`c(rng)'" \ "`c(rng_current)'") mata state_start = "`c(rngstate)'" * Example 1 seed_checker 123 mata x_1 = rnormal(10,1,0,1) mata rng_after_1 =( "`c(rng)'" \ "`c(rng_current)'") mata rng_int_1 = rng_int mata state_int_1 = rngstate_int mata state_after_1 = "`c(rngstate)'" * Example 2 seed_checker 123, draw mata rng_after_2 =( "`c(rng)'" \ "`c(rng_current)'") mata state_after_2 = "`c(rngstate)'" mata rng_int_2 = rng_int mata state_int_2 = rngstate_int mata x_2 = x_sc *by hand clear rngstream set rng mt64s set rngstream 1 set seed 123 mata x_3 = rnormal(10,1,0,1) mata rng_3 =( "`c(rng)'" \ "`c(rng_current)'") mata state_after_3 = "`c(rngstate)'" **Check random draws mata (x , x_1, x_2, x_3) ** Check nrg and rng_current mata (rng_start , rng_after_1 , rng_int_1 , rng_after_2, rng_int_2,rng_3) **compare states: mata states = J(0,5,.) foreach st in state_start state_after_1 state_after_2 state_int_2 state_after_3 { mata states = (states \ (`st':== (state_start ,state_after_1 ,state_after_2 ,state_int_2 ,state_after_3))) } mata names = ("state_start" , "state_after_1" ,"state_after_2" ,"state_int_2" ,"state_after_3") ** RNG States: mata (("", names )\ (names' , strofreal(states)))
Code:
. **Check random draws . mata (x , x_1, x_2, x_3) 1 2 3 4 +-------------------------------------------------------------+ 1 | .4907676602 -.3268441812 .4907676602 .4907676602 | 2 | -1.405081998 -1.213826865 -1.405081998 -1.405081998 | 3 | -.2040506142 -.0088207356 -.2040506142 -.2040506142 | 4 | 2.10972645 1.626667208 2.10972645 2.10972645 | 5 | .4460774109 -1.296418109 .4460774109 .4460774109 | 6 | -.3231870725 1.052054144 -.3231870725 -.3231870725 | 7 | .7594250051 .9918897139 .7594250051 .7594250051 | 8 | -1.257190365 .1931405811 -1.257190365 -1.257190365 | 9 | -.7144663077 -.0311804972 -.7144663077 -.7144663077 | 10 | .11753014 -.2283643257 .11753014 .11753014 | +-------------------------------------------------------------+
I do understand that the results in x and x_1 differ, because I set a different random number generator type and stream (mt64s instead of mt64). What I don't understand is, that x and x_2 are the same, but not x_1 and x_2. The difference between x_1 and x_2 is, that x_2 is drawn inside the program, x_1 after the program. Also I don't understand that x and x_2 are the same as I changed from mt64 to mt64s. Then I though I check the type of the random number generator:
Code:
** Check nrg and rng_current . mata (rng_start , rng_after_1 , rng_int_1 , rng_after_2, rng_int_2,rng_3) 1 2 3 4 5 6 +-------------------------------------------------------------+ 1 | default default mt64s default mt64s mt64s | 2 | mt64 mt64 mt64s mt64 mt64s mt64s | +-------------------------------------------------------------+
When checking the random number states after drawing, it turns out that only the ones after example 1 and 2 are the same and the internal one from example 2 and the one for example 3 ("by hand") are the same.
Code:
. ** RNG States should be the same: . mata (("", names )\ (names' , strofreal(states))) [symmetric] 1 2 3 4 5 6 +-------------------------------------------------------------------------------------------------+ 1 | | 2 | state_start 1 | 3 | state_after_1 0 1 | 4 | state_after_2 0 1 1 | 5 | state_int_2 0 0 0 1 | 6 | state_after_3 0 0 0 1 1 | +-------------------------------------------------------------------------------------------------+
To sum up, my questions are:
- Why does the type of the random number generator cannot be set with a program?
- Why are the random numbers the same if I switch from mt64 to mt64s, but different if I set the type within a program, despite using the same rng type?
- Why does this matter for the rng states?
Comment