Hi, I was always under the impression SET SEED could be used reproduce random numbers. However I am noticing this not to be true. As I understand it, I was expecting the below code to give me rand1 & rand3 to give me the same random numbers?
How do I produce the same random numbers? dataset close all. new file. output close all. DATA LIST LIST /var1 var2 var3 varn. show seed. preserve. compute rand2=rv.uniform(0,1). preserve. compute rand4=rv.uniform(0,1). exe. |
There are two issues here. First,
the SET SEED command applies to the old random number generator (SET RNG=MC.)
If you are using the superior Mersenne Twister (SET RNG=MT), it does
not apply. You would use SET MTINDEX in that case.
Second, more subtly, the SET command is not a procedure and does not pass the data, so it get executed before the transformation commands and, hence, the starting seed does not get reset. Use the much abused EXECUTE command to force the starting value to be reset before the second computation. HTH, Jon Peck (no "h") aka Kim Senior Software Engineer, IBM [hidden email] new phone: 720-342-5621 From: J Sutar <[hidden email]> To: [hidden email], Date: 11/27/2012 04:59 AM Subject: [SPSSX-L] Reproducing Random Samples Sent by: "SPSSX(r) Discussion" <[hidden email]> Hi, I was always under the impression SET SEED could be used reproduce random numbers. However I am noticing this not to be true. As I understand it, I was expecting the below code to give me rand1 & rand3 to give me the same random numbers? How do I produce the same random numbers? dataset close all. new file. output close all. DATA LIST LIST /var1 var2 var3 varn. show seed. preserve. compute rand2=rv.uniform(0,1). preserve. compute rand4=rv.uniform(0,1). exe.
|
In reply to this post by Jignesh Sutar
This is one of the rare
occasions where you need to have multiple
executes.
There are two sets of syntax. The first tries to stay as close as possible to your syntax using preserve. the second is shorter but does the same thing. new file. DATA LIST LIST /var1 var2 var3 varn. BEGIN DATA 1 2 3 4 1 1 1 1 1 1 2 4 2 3 2 2 1 1 1 1 END DATA. LIST. show seed. set seed 20121127. preserve. show seed. compute rand1=rv.uniform(0,1). execute. restore. preserve. show seed. compute rand2=rv.uniform(0,1). execute. restore. preserve. show seed. compute rand3=rv.uniform(0,1). restore. show seed. formats rand1 rand2 rand3 (f5.4). list. ------------------------ new file. DATA LIST LIST /id (f1). BEGIN DATA 1 2 3 4 END DATA. LIST. show seed. set seed 20121127. compute rand1=rv.uniform(0,1). execute. set seed 20121127. compute rand2=rv.uniform(0,1). execute. set seed 20121127. compute rand3=rv.uniform(0,1). formats rand1 rand2 rand3 (f5.4). list. Art Kendall Social Research ConsultantsOn 11/27/2012 6:47 AM, J Sutar wrote:
===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
Art Kendall
Social Research Consultants |
Dear Art,
I don't get it. Why do you need to set the seed to a value that you already set it to? Does the transformation command change the seed? And why is forcing a data pass with EXECUTE necessary here and how does it affect (setting) the seed? Could you please explain a bit more how this works? TIA, Ruben Date: Tue, 27 Nov 2012 09:25:27 -0500 From: [hidden email] Subject: Re: Reproducing Random Samples To: [hidden email] This is one of the rare
occasions where you need to have multiple
executes.
There are two sets of syntax. The first tries to stay as close as possible to your syntax using preserve. the second is shorter but does the same thing. new file. DATA LIST LIST /var1 var2 var3 varn. BEGIN DATA 1 2 3 4 1 1 1 1 1 1 2 4 2 3 2 2 1 1 1 1 END DATA. LIST. show seed. set seed 20121127. preserve. show seed. compute rand1=rv.uniform(0,1). execute. restore. preserve. show seed. compute rand2=rv.uniform(0,1). execute. restore. preserve. show seed. compute rand3=rv.uniform(0,1). restore. show seed. formats rand1 rand2 rand3 (f5.4). list. ------------------------ new file. DATA LIST LIST /id (f1). BEGIN DATA 1 2 3 4 END DATA. LIST. show seed. set seed 20121127. compute rand1=rv.uniform(0,1). execute. set seed 20121127. compute rand2=rv.uniform(0,1). execute. set seed 20121127. compute rand3=rv.uniform(0,1). formats rand1 rand2 rand3 (f5.4). list. Art Kendall Social Research ConsultantsOn 11/27/2012 6:47 AM, J Sutar wrote:
===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD |
Each
time the PRNG (pseudo-random number
generator) is called it changes the seed that is used
for the next call. It is based on the modulus of the result of
some arithmetic on some long numbers.
see In the example syntax in the previous post the PNRG is called one for every case. Each call or the PRNG changes the seed for the next call. it the same number were put into the formula on each call all of the outputs would be identical. any procedure that causes a data pass would cause the PRNG to be called for each case in this syntax. Execute is the simplest procedure to force a data pass. Transformations are only done when there is a data pass. setting the seed generates the same output in the same order. the syntax below shows some of this. Case I and 2 exactly match. I SWAG that the reason that case 3 in the second set is close and case 4 is very different is that I have a 64-bit machine or it is some numerical analysis problem. I do get identical results across runs on my machine. Try running the syntax below. Please let me know what happens. If somebody on the list has a 32 bit machine would you please try this syntax. new file. set rng = mc. DATA LIST LIST /id (f1). BEGIN DATA 1 END DATA. set seed 20121127. show seed. compute rand1=rv.uniform(0,1). execute. show seed. compute rand1=rv.uniform(0,1). execute. show seed. compute rand1=rv.uniform(0,1). execute. show seed. new file. set rng = mc. DATA LIST LIST /id (f1). BEGIN DATA 1 2 3 4 END DATA. compute xseed =20121127. do if id ge 2. compute a=397204094. compute p = (2**31)-1. compute xseed = mod((a * lag(xseed)), p). end if. formats xseed (f12). list variables = xseed. Art Kendall Social Research ConsultantsOn 11/27/2012 10:34 AM, Ruben van den Berg wrote:
===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
Art Kendall
Social Research Consultants |
Dear Art,
Thanks a lot for clarifying! What I found a bit confusing is this: set seed 1. compute a=rv.ber(.1). sho seed. compute b=rv.ber(.1). sho seed. exe. sho seed. Since there's no data pass after computing "a", the seed does not change. And immediately after computing "b" it is still one. Apparently, both "a" and "b" were computed with a seed of one. But even though the seeds were identical as well as the formulas, "a" and "b" are not identical. Kind regards, Ruben Date: Tue, 27 Nov 2012 13:43:55 -0500 From: [hidden email] Subject: Re: Reproducing Random Samples To: [hidden email] Each
time the PRNG (pseudo-random number
generator) is called it changes the seed that is used
for the next call. It is based on the modulus of the result of
some arithmetic on some long numbers.
see In the example syntax in the previous post the PNRG is called one for every case. Each call or the PRNG changes the seed for the next call. it the same number were put into the formula on each call all of the outputs would be identical. any procedure that causes a data pass would cause the PRNG to be called for each case in this syntax. Execute is the simplest procedure to force a data pass. Transformations are only done when there is a data pass. setting the seed generates the same output in the same order. the syntax below shows some of this. Case I and 2 exactly match. I SWAG that the reason that case 3 in the second set is close and case 4 is very different is that I have a 64-bit machine or it is some numerical analysis problem. I do get identical results across runs on my machine. Try running the syntax below. Please let me know what happens. If somebody on the list has a 32 bit machine would you please try this syntax. new file. set rng = mc. DATA LIST LIST /id (f1). BEGIN DATA 1 END DATA. set seed 20121127. show seed. compute rand1=rv.uniform(0,1). execute. show seed. compute rand1=rv.uniform(0,1). execute. show seed. compute rand1=rv.uniform(0,1). execute. show seed. new file. set rng = mc. DATA LIST LIST /id (f1). BEGIN DATA 1 2 3 4 END DATA. compute xseed =20121127. do if id ge 2. compute a=397204094. compute p = (2**31)-1. compute xseed = mod((a * lag(xseed)), p). end if. formats xseed (f12). list variables = xseed. Art Kendall Social Research ConsultantsOn 11/27/2012 10:34 AM, Ruben van den Berg wrote:
===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD |
SHOW commands are read-interpreted-processed immediately on being encountered in a syntax stream.. COMPUTE
and other transformations are only read-and-interpreted
when encountered in a syntax stream. Transformations are gathered
up and only processed during data pass.
a is not process between In your example syntax. the rv.ber is NOT called between the the two statement in the stream. It would only be called during the data pass when the EXECUTE is encountered. a is not processed between the two SHOW SEEDs. The second snippet of syntax in my previous post approximates the process of generating random numbers. Note that XSEED is the same as the results of SHOW SEED in the first snippet of syntax. Art Kendall Social Research ConsultantsOn 11/28/2012 2:53 AM, Ruben van den Berg wrote:
===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
Art Kendall
Social Research Consultants |
Dear Art,
If I understand correctly, the point here is that a single "execute" command triggers a separate seed reset for each pending transformation that involves the RNG. Great, thanks for explaining! Ruben Date: Wed, 28 Nov 2012 07:42:27 -0500 From: [hidden email] To: [hidden email] CC: [hidden email] Subject: Re: [SPSSX-L] Reproducing Random Samples SHOW commands are read-interpreted-processed immediately on being encountered in a syntax stream.. COMPUTE
and other transformations are only read-and-interpreted
when encountered in a syntax stream. Transformations are gathered
up and only processed during data pass.
a is not process between In your example syntax. the rv.ber is NOT called between the the two statement in the stream. It would only be called during the data pass when the EXECUTE is encountered. a is not processed between the two SHOW SEEDs. The second snippet of syntax in my previous post approximates the process of generating random numbers. Note that XSEED is the same as the results of SHOW SEED in the first snippet of syntax. Art Kendall Social Research ConsultantsOn 11/28/2012 2:53 AM, Ruben van den Berg wrote:
|
I am PTOB Wednesday through Friday, 28-30 November and will not be checking email. I will respond to emails no later than Monday 3 December. Please contact:
·
[hidden email] for questions about or assistance with MIP ProjectPages
(page functionality, content on the pages, i.e. PI name, proejct title, etc.)
·
[hidden email] for questions about charge
numbers
·
[hidden email]
for quesitons or assiatcne with MIP processes or the webiste
·
[hidden email] for assistance with
other Innovation Zone sites, such as CI&T InZone Regards, Mary Lou |
In reply to this post by Ruben Geert van den Berg
I would put it like this: the original version
of the code
set seed 1. compute a=rv.ber(.1). sho seed. compute b=rv.ber(.1). sho seed. exe. sho seed. is actually executed like this. set seed 1. sho seed. sho seed. compute b=rv.ber(.1). compute a=rv.ber(.1). exe. sho seed. The seed changes each time there is a draw. This assumes, though, that you are using the old rng. The MT rng is superior, in which case you would use a different bunch of set and show commands, although the logic is the same. Jon Peck (no "h") aka Kim Senior Software Engineer, IBM [hidden email] new phone: 720-342-5621 From: Ruben van den Berg <[hidden email]> To: [hidden email], Date: 11/28/2012 08:46 AM Subject: Re: [SPSSX-L] Reproducing Random Samples Sent by: "SPSSX(r) Discussion" <[hidden email]> Dear Art, If I understand correctly, the point here is that a single "execute" command triggers a separate seed reset for each pending transformation that involves the RNG. Great, thanks for explaining! Ruben Date: Wed, 28 Nov 2012 07:42:27 -0500 From: [hidden email] To: [hidden email] CC: [hidden email] Subject: Re: [SPSSX-L] Reproducing Random Samples SHOW commands are read-interpreted-processed immediately on being encountered in a syntax stream.. COMPUTE and other transformations are only read-and-interpreted when encountered in a syntax stream. Transformations are gathered up and only processed during data pass. a is not process between In your example syntax. the rv.ber is NOT called between the the two statement in the stream. It would only be called during the data pass when the EXECUTE is encountered. a is not processed between the two SHOW SEEDs. The second snippet of syntax in my previous post approximates the process of generating random numbers. Note that XSEED is the same as the results of SHOW SEED in the first snippet of syntax. Art Kendall Social Research Consultants On 11/28/2012 2:53 AM, Ruben van den Berg wrote: Dear Art, Thanks a lot for clarifying! What I found a bit confusing is this: set seed 1. compute a=rv.ber(.1). sho seed. compute b=rv.ber(.1). sho seed. exe. sho seed. Since there's no data pass after computing "a", the seed does not change. And immediately after computing "b" it is still one. Apparently, both "a" and "b" were computed with a seed of one. But even though the seeds were identical as well as the formulas, "a" and "b" are not identical. Kind regards, Ruben Date: Tue, 27 Nov 2012 13:43:55 -0500 From: Art@... Subject: Re: Reproducing Random Samples To: [hidden email] Each time the PRNG (pseudo-random number generator) is called it changes the seed that is used for the next call. It is based on the modulus of the result of some arithmetic on some long numbers. see Help > Algorithms > Generation of Uniform Random Numbers In the example syntax in the previous post the PNRG is called one for every case. Each call or the PRNG changes the seed for the next call. it the same number were put into the formula on each call all of the outputs would be identical. any procedure that causes a data pass would cause the PRNG to be called for each case in this syntax. Execute is the simplest procedure to force a data pass. Transformations are only done when there is a data pass. setting the seed generates the same output in the same order. the syntax below shows some of this. Case I and 2 exactly match. I SWAG that the reason that case 3 in the second set is close and case 4 is very different is that I have a 64-bit machine or it is some numerical analysis problem. I do get identical results across runs on my machine. Try running the syntax below. Please let me know what happens. If somebody on the list has a 32 bit machine would you please try this syntax. new file. set rng = mc. DATA LIST LIST /id (f1). BEGIN DATA 1 END DATA. set seed 20121127. show seed. compute rand1=rv.uniform(0,1). execute. show seed. compute rand1=rv.uniform(0,1). execute. show seed. compute rand1=rv.uniform(0,1). execute. show seed. new file. set rng = mc. DATA LIST LIST /id (f1). BEGIN DATA 1 2 3 4 END DATA. compute xseed =20121127. do if id ge 2. compute a=397204094. compute p = (2**31)-1. compute xseed = mod((a * lag(xseed)), p). end if. formats xseed (f12). list variables = xseed. Art Kendall Social Research Consultants On 11/27/2012 10:34 AM, Ruben van den Berg wrote: Dear Art, I don't get it. Why do you need to set the seed to a value that you already set it to? Does the transformation command change the seed? And why is forcing a data pass with EXECUTE necessary here and how does it affect (setting) the seed? Could you please explain a bit more how this works? TIA, Ruben Date: Tue, 27 Nov 2012 09:25:27 -0500 From: Art@... Subject: Re: Reproducing Random Samples To: [hidden email] This is one of the rare occasions where you need to have multiple executes. There are two sets of syntax. The first tries to stay as close as possible to your syntax using preserve. the second is shorter but does the same thing. new file. DATA LIST LIST /var1 var2 var3 varn. BEGIN DATA 1 2 3 4 1 1 1 1 1 1 2 4 2 3 2 2 1 1 1 1 END DATA. LIST. show seed. set seed 20121127. preserve. show seed. compute rand1=rv.uniform(0,1). execute. restore. preserve. show seed. compute rand2=rv.uniform(0,1). execute. restore. preserve. show seed. compute rand3=rv.uniform(0,1). restore. show seed. formats rand1 rand2 rand3 (f5.4). list. ------------------------ new file. DATA LIST LIST /id (f1). BEGIN DATA 1 2 3 4 END DATA. LIST. show seed. set seed 20121127. compute rand1=rv.uniform(0,1). execute. set seed 20121127. compute rand2=rv.uniform(0,1). execute. set seed 20121127. compute rand3=rv.uniform(0,1). formats rand1 rand2 rand3 (f5.4). list. Art Kendall Social Research Consultants On 11/27/2012 6:47 AM, J Sutar wrote: Hi, I was always under the impression SET SEED could be used reproduce random numbers. However I am noticing this not to be true. As I understand it, I was expecting the below code to give me rand1 & rand3 to give me the same random numbers? How do I produce the same random numbers? dataset close all. new file. output close all. DATA LIST LIST /var1 var2 var3 varn. BEGIN DATA 1 2 3 4 1 1 1 1 1 1 2 4 2 3 2 2 1 1 1 1 END DATA. LIST. show seed. preserve. set seed 20121127. show seed. compute rand1=rv.uniform(0,1). restore. compute rand2=rv.uniform(0,1). show seed. preserve. set seed 20121127. show seed. compute rand3=rv.uniform(0,1). restore. compute rand4=rv.uniform(0,1). exe. ===================== To manage your subscription to SPSSX-L, send a message to LISTSERV@... (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD ===================== To manage your subscription to SPSSX-L, send a message to LISTSERV@... (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD |
Free forum by Nabble | Edit this page |