Title: | Streams of Random Numbers |
---|---|
Description: | Unified object oriented interface for multiple independent streams of random numbers from different sources. |
Authors: | Josef Leydold <[email protected]> |
Maintainer: | Josef Leydold <[email protected]> |
License: | GPL-2 |
Version: | 1.3.7 |
Built: | 2024-11-08 03:41:28 UTC |
Source: | https://github.com/cran/rstream |
Unified interface to uniform random number generators
Package: | rstream |
Type: | Package |
Version: | 1.3.7 |
Date: | 2022-10-18 |
License: | GPL 2 or later |
This package provides a unified interface to uniform random number Objects of its classes act as a source of streams for random numbers that can be handled by a set of methods and which can be used as arguments of functions that require sources of randomness, e.g. discrete event models, Monte Carlo integration or other stochastic simulations.
The instances of objects are independent, i.e., sampling random numbers from one instance or reseting and changing its state does not effect any other instance. (However, different streams may not be stochastically independent if they are not carefully seeded!)
Additionally there is a mechanismus interface to save and restore such streams, e.g. for the next R session, for a reruning some stochastic calculations with identical input, or for sending the stream object to a slave node in parallel computing via MPI.
The interface to these classes is inspired by Pierre L'Ecuyers RngStreams package.
For an overview of classes and methods, see
rstream
.
Josef Leydold [email protected]
P. L'Ecuyer and J. Leydold (2005): rstream: Streams of Random Numbers for Stochastic Simulation, R News 5(2), 16–20.
The virtual class "rstream" provides a unified interface to uniform random number generators. Objects of its subclasses act as a source of streams for random numbers that can be handled by a set of methods and which can be used as arguments of functions that require sources of randomness, e.g. discrete event models, Monte Carlo integration or other stochastic simulations.
The instances of objects of this class and its subclasses are independent, i.e., sampling random numbers from one instance or reseting and changing its state does not effect any other instance. (However, different streams may not be stochastically independent if they are not carefully seeded!)
Additionally there is a mechanismus interface to save and restore such streams, e.g. for the next R session, for a reruning some stochastic calculations with identical input, or for sending the stream object to a slave node in parallel computing via MPI.
The interface to these classes is inspired by Pierre L'Ecuyers RngStreams package.
A virtual Class: No objects may be created from it.
The virtual class "rstream" prepares the following methods for
handling random stream objects. Some methods that return parameters of
the stream object have a variant that uses <-
to change the
respective parameters. See the man pages for the respective methods
for details.
Notice: Some subclasses only implement a subset of these interfaces. The methods that do not work produce an error message.
Methods to use the stream (available for all subclasses):
signature(object = "rstream")
:
Get a random sample from the stream object.
signature(object = "rstream")
:
Same as rstream.sample
.
signature(object = "rstream")
:
Reset stream into initial state.
Some subclasses have implemented the concept of substreams. This is especially usefull if two or more streams should be synchronized:
signature(object = "rstream")
:
Set state of stream to next substream.
signature(object = "rstream")
:
Reset current substream into starting state.
Antithetic random streams return numbers which have smallest possible
correlation (i.e. ) to their respective counterparts:
signature(object = "rstream")
:
Whether or not the stream object returns antithetic random
numbers.
signature(object = "rstream")
:
Change antithetic flag (TRUE
or FALSE
).
Most sources of pseudo random numbers generate random numbers of
precision .
For some streams this can be increased to machine epsilon (i.e.
) by combining two random numbers of
lower precision:
signature(object = "rstream")
:
Whether or not the stream object returns random numbers with
increased precision.
signature(object = "rstream")
:
Change flag for increased precision (TRUE
or FALSE
).
Handling "rstream" objects:
signature(x = "rstream")
:
Print state of the stream object.
signature(x = "rstream")
:
Same as print
.
signature(object = "rstream")
:
The name of the stream object.
signature(object = "rstream")
:
Change the name of the stream object.
signature(object = "rstream")
:
Make a copy (clone) of stream object.
When a "rstream" object should be used in another R session or saved for some kind of later reuse all information about the object must be packed. Notice no method other than unpacking can be applied to a packed object. It must be unpacked before.
signature(object = "rstream")
:
Whether or not the stream object is packed.
signature(object = "rstream")
:
Pack or unpack object: set packed to TRUE
or FALSE
.
When "rstream" objects should be used in later R sessions they must be packed before the current R session is stopped and unpacked after the new R session has been started and the workspace image has been reloaded.
"rstream" objects cannot simply be copied by <-
. The new
variable does not hold a copy of an "rstream" object but just points
to the old object which might not be the wanted result (similar to R
environments). Use rstream.clone
instead.
The actual interface is implemented in subclasses, one for each source (package/library) of random numbers. The slots of this class and of all its subclasses must not be accessed directly. Use the above methods instead.
One may miss a method for reseeding a random stream. However, there is no need for such a method as there is a method for resetting the stream to its initial state. I one needs a stream with a different stream, then a new rstream object should be created at all.
Packed objects must be unpacked before any other method can be applied.
Josef Leydold [email protected]
L'Ecuyer, P., Simard, R., Chen, E. J., and Kelton, W. D. (2002) An object-oriented random-number package with many long streams and substreams. Operations Research 50(6), 1073-1075.
rstream.mrg32k3a
,
rstream.runif
.
rstream.antithetic-methods
,
rstream.clone-methods
,
rstream.incprecision-methods
,
rstream.name-methods
,
rstream.packed-methods
,
rstream.reset-methods
,
rstream.sample-methods
,
rstream.nextsubstream-methods
,
rstream.RNG
.
Get and change the antithetic flag for an "rstream" object in package rstream.
If the antithetic flag is on (TRUE
) the "rstream" object
returns antithetic random numbers.
Antithetic random streams return numbers which have smallest possible
correlation (i.e. ) to their respective counterparts.
Thus instead of a number
the number
is
returned.
This is useful for variance reduction techniques in Monte Carlo computations.
## S4 method for signature 'rstream' rstream.antithetic(stream) rstream.antithetic(stream) <- value
## S4 method for signature 'rstream' rstream.antithetic(stream) rstream.antithetic(stream) <- value
stream |
an "rstream" object. |
value |
a boolean ( |
Methods available for the following "rstream" subclasses:
rstream.mrg32k3a
,
rstream.runif
.
Josef Leydold [email protected]
## create a new rstream object (of subclass rstream.mrg32k3a) s <- new("rstream.mrg32k3a") ## set antithetic flag of rstream object rstream.antithetic(s) <- TRUE ## get antithetic flag of rstream object rstream.antithetic(s)
## create a new rstream object (of subclass rstream.mrg32k3a) s <- new("rstream.mrg32k3a") ## set antithetic flag of rstream object rstream.antithetic(s) <- TRUE ## get antithetic flag of rstream object rstream.antithetic(s)
Make a copy (clone) of an "rstream" object in package rstream.
"rstream" objects cannot simply be copied by <-
. The new
variable does not hold a copy of an "rstream" object but just points
to the old object which might not be the wanted result (similar to R
environments). To get a copy of the whole "rstream" object the clone
method must be used.
## S4 method for signature 'rstream' rstream.clone(stream)
## S4 method for signature 'rstream' rstream.clone(stream)
stream |
an "rstream" object. |
Methods available for all "rstream" subclasses:
rstream.mrg32k3a
,
rstream.runif
.
The label (name) of the new copy has a dot "." appended to distinguish the original object from its copy.
Josef Leydold [email protected]
## create a new rstream object (of subclass rstream.mrg32k3a) s <- new("rstream.mrg32k3a") ## make a copy (clone) clone <- rstream.clone(s)
## create a new rstream object (of subclass rstream.mrg32k3a) s <- new("rstream.mrg32k3a") ## make a copy (clone) clone <- rstream.clone(s)
Get and change the flag for increased precision of an "rstream" object in package rstream.
If the increased precision flag is on (TRUE
) the "rstream" object
returns random numbers with precision close to machine epsilon.
Most sources of pseudo random numbers generate random numbers of
precision .
When the flag is on the precision is increased to machine epsilon
(i.e.
) by combining two random
numbers of default precision.
## S4 method for signature 'rstream' rstream.incprecision(stream) rstream.incprecision(stream) <- value
## S4 method for signature 'rstream' rstream.incprecision(stream) rstream.incprecision(stream) <- value
stream |
an "rstream" object. |
value |
a boolean ( |
Methods available for the following "rstream" subclasses:
rstream.mrg32k3a
.
Josef Leydold [email protected]
## create a new rstream object (of subclass rstream.mrg32k3a) s <- new("rstream.mrg32k3a") ## set increased precision flag of rstream object rstream.incprecision(s) <- TRUE ## get increased precision flag of rstream object rstream.incprecision(s)
## create a new rstream object (of subclass rstream.mrg32k3a) s <- new("rstream.mrg32k3a") ## set increased precision flag of rstream object rstream.incprecision(s) <- TRUE ## get increased precision flag of rstream object rstream.incprecision(s)
This class is obsolete and should be replaced by class
"rstream.mrg32k3a"
!
This is the old class that implements the "rstream" interface for Pierre L'Ecuyer's RngStreams package with the MRG32K3a multiple recursive generator as its backbone generator. This package is well suited for multiple independent streams of uniform random numbers. In particular it provides antithetic variates and substreams. A very important feature is that different streams are stochastically independent (in opposition to many other random number generators where the uses has to choose seeds carefully). For that reason there only exists a package seed for all streams and indiviual seeds should be avoided (and requires expertise).
Objects can be created by calls of the form
new("rstream.lecuyer", name, seed, force.seed, antithetic,
incprecision)
.
An arbitrary string to name the stream object.
If omitted a string that consists of lecuyer
and some
number (which is increased every time when a new rstream
object is created.
An array of six numbers. The seed for the RngStreams package. If omitted a random seed is used. It should only be set at the first creation of an instance of an rstream.lecuyer object.
A boolean. If the RngStreams package should be reseeded
(which is not recommended) it must be TRUE
.
Default is FALSE
.
A boolean. Whether or not antithetic random
numbers should be produced.
Default is FALSE
.
A boolean. Whether or not random numbers with
increased precision should be produced.
Default is FALSE
.
Class "rstream"
, directly.
The class "rstream.lecuyer" provides the following methods for
handling "rstream.lecuyer" objects. Some methods that return
parameters of the stream object have a variant that uses <-
to
change the respective parameters. See the man pages for the respective
methods for details.
Methods to use the stream:
signature(object = "rstream.lecuyer")
:
Get a random sample from the stream object.
signature(object = "rstream.lecuyer")
:
Same as rstream.sample
.
signature(object = "rstream.lecuyer")
:
Reset stream into initial state.
signature(object = "rstream.lecuyer")
:
Set state of stream to next substream.
signature(object = "rstream.lecuyer")
:
Reset current substream into starting state.
Antithetic random streams and increased precision:
signature(object = "rstream.lecuyer")
:
Whether or not the stream object returns antithetic random
numbers.
signature(object = "rstream.lecuyer")
:
Change antithetic flag (TRUE
or FALSE
).
signature(object = "rstream.lecuyer")
:
Whether or not the stream object returns random numbers with
increased precision.
signature(object = "rstream.lecuyer")
:
Change flag for increased precision (TRUE
or FALSE
).
Handling "rstream.lecuyer" objects:
signature(x = "rstream.lecuyer")
:
Print state of the stream object.
signature(object = "rstream.lecuyer")
:
The name of the stream object.
signature(object = "rstream.lecuyer")
:
Change the name of the stream object.
signature(object = "rstream.lecuyer")
:
Make a copy (clone) of stream object.
signature(.Object = "rstream.lecuyer")
:
Initialize rstream object. (For Internal usage only).
When a "rstream.lecuyer" object should be used in another R session or saved for some kind of later reuse all information about the object must be packed. Notice no method other than unpacking can be applied to a packed object. It must be unpacked before.
signature(object = "rstream.lecuyer")
:
Whether or not the stream object is packed.
signature(object = "rstream.lecuyer")
:
Pack or unpack object: set packed to TRUE
or FALSE
.
The underlying RngStreams library uses a global variable to store the package seed. This variable is also stored inside R. Whenever a new instance of a "rstream.lecuyer" object is created the value of global variable is set to the value of the R object. Thus there is no problem when such "rstream.lecuyer" objects are packed for using in later R sessions. However, if such packed objects are not stored in the workspace image, then the R variable gets lost and there is a (extremely small) chance that newly created objects are not stochastically independent from restored objects.
The slots of this class must not be accessed directly. Use the above methods instead.
"rstream" objects cannot simply be copied by <-
. The new
variable does not hold a copy of an "rstream" object but just points
to the old object which might not be the wanted result (similar to R
environments). Use rstream.clone
instead.
One may miss a method for reseeding a random stream. However, there is no need for such a method as there is a method for resetting the stream to its initial state. I one needs a stream with a different stream, then a new rstream object should be created at all.
Packed objects must be unpacked before any other method can be applied.
Josef Leydold [email protected]
underlying generator: Pierre L'Ecyuer and Richard Simard
L'Ecuyer, P., Simard, R., Chen, E. J., and Kelton, W. D. (2002) An object-oriented random-number package with many long streams and substreams. Operations Research 50(6), 1073-1075.
rstream
,
rstream.mrg32k3a
,
rstream.antithetic-methods
,
rstream.clone-methods
,
rstream.incprecision-methods
,
rstream.name-methods
,
rstream.packed-methods
,
rstream.reset-methods
,
rstream.sample-methods
,
rstream.nextsubstream-methods
,
rstream.RNG
.
## create a new rstream.lecuyer object s <- new("rstream.lecuyer") ## show state of this object print(s) ## show and change name of stream object rstream.name(s) rstream.name(s) <- "mystream" ## get a random number x <- rstream.sample(s) ## get a random sample of size 100 x <- rstream.sample(s,100) ## reset random stream rstream.reset(s) ## show and set antithetic flag rstream.antithetic(s) rstream.antithetic(s) <- TRUE ## jump to next substream rstream.nextsubstream(s) ## make a clone of the rstream object sc <- rstream.clone(s) ## pack and unpack the rstream object rstream.packed(s) <- TRUE rstream.packed(s) <- FALSE
## create a new rstream.lecuyer object s <- new("rstream.lecuyer") ## show state of this object print(s) ## show and change name of stream object rstream.name(s) rstream.name(s) <- "mystream" ## get a random number x <- rstream.sample(s) ## get a random sample of size 100 x <- rstream.sample(s,100) ## reset random stream rstream.reset(s) ## show and set antithetic flag rstream.antithetic(s) rstream.antithetic(s) <- TRUE ## jump to next substream rstream.nextsubstream(s) ## make a clone of the rstream object sc <- rstream.clone(s) ## pack and unpack the rstream object rstream.packed(s) <- TRUE rstream.packed(s) <- FALSE
This class implements the "rstream" interface for Pierre L'Ecuyer's RngStreams package with the MRG32K3a multiple recursive generator as its backbone generator. This package is well suited for multiple independent streams of uniform random numbers. In particular it provides antithetic variates and substreams. A very important feature is that different streams are stochastically independent (in opposition to many other random number generators where the uses has to choose seeds carefully). For that reason there only exists a package seed for all streams and indiviual seeds should be avoided (and requires expertise).
Objects can be created by calls of the form
new("rstream.mrg32k3a", name, seed, force.seed, antithetic,
incprecision)
.
An arbitrary string to name the stream object.
If omitted a string that consists of mrg32k3a
and some
number (which is increased every time when a new rstream
object is created.
An array of six numbers. The seed for the RngStreams package. If omitted a random seed is used. It should only be set at the first creation of an instance of an rstream.mrg32k3a object.
A boolean. If the RngStreams package should be reseeded
(which is not recommended) it must be TRUE
.
Default is FALSE
.
A boolean. Whether or not antithetic random
numbers should be produced.
Default is FALSE
.
A boolean. Whether or not random numbers with
increased precision should be produced.
Default is FALSE
.
Class "rstream"
, directly.
The class "rstream.mrg32k3a" provides the following methods for
handling "rstream.mrg32k3a" objects. Some methods that return
parameters of the stream object have a variant that uses <-
to
change the respective parameters. See the man pages for the respective
methods for details.
Methods to use the stream:
signature(object = "rstream.mrg32k3a")
:
Get a random sample from the stream object.
signature(object = "rstream.mrg32k3a")
:
Same as rstream.sample
.
signature(object = "rstream.mrg32k3a")
:
Reset stream into initial state.
signature(object = "rstream.mrg32k3a")
:
Set state of stream to next substream.
signature(object = "rstream.mrg32k3a")
:
Reset current substream into starting state.
Antithetic random streams and increased precision:
signature(object = "rstream.mrg32k3a")
:
Whether or not the stream object returns antithetic random
numbers.
signature(object = "rstream.mrg32k3a")
:
Change antithetic flag (TRUE
or FALSE
).
signature(object = "rstream.mrg32k3a")
:
Whether or not the stream object returns random numbers with
increased precision.
signature(object = "rstream.mrg32k3a")
:
Change flag for increased precision (TRUE
or FALSE
).
Handling "rstream.mrg32k3a" objects:
signature(x = "rstream.mrg32k3a")
:
Print state of the stream object.
signature(object = "rstream.mrg32k3a")
:
The name of the stream object.
signature(object = "rstream.mrg32k3a")
:
Change the name of the stream object.
signature(object = "rstream.mrg32k3a")
:
Make a copy (clone) of stream object.
signature(.Object = "rstream.mrg32k3a")
:
Initialize rstream object. (For Internal usage only).
When a "rstream.mrg32k3a" object should be used in another R session or saved for some kind of later reuse all information about the object must be packed. Notice no method other than unpacking can be applied to a packed object. It must be unpacked before.
signature(object = "rstream.mrg32k3a")
:
Whether or not the stream object is packed.
signature(object = "rstream.mrg32k3a")
:
Pack or unpack object: set packed to TRUE
or FALSE
.
The underlying RngStreams library uses a global variable to store the package seed. This variable is also stored inside R. Whenever a new instance of a "rstream.mrg32k3a" object is created the value of global variable is set to the value of the R object. Thus there is no problem when such "rstream.mrg32k3a" objects are packed for using in later R sessions. However, if such packed objects are not stored in the workspace image, then the R variable gets lost and there is a (extremely small) chance that newly created objects are not stochastically independent from restored objects.
The slots of this class must not be accessed directly. Use the above methods instead.
"rstream" objects cannot simply be copied by <-
. The new
variable does not hold a copy of an "rstream" object but just points
to the old object which might not be the wanted result (similar to R
environments). Use rstream.clone
instead.
One may miss a method for reseeding a random stream. However, there is no need for such a method as there is a method for resetting the stream to its initial state. I one needs a stream with a different stream, then a new rstream object should be created at all.
Packed objects must be unpacked before any other method can be applied.
Josef Leydold [email protected]
underlying generator: Pierre L'Ecyuer and Richard Simard
L'Ecuyer, P., Simard, R., Chen, E. J., and Kelton, W. D. (2002) An object-oriented random-number package with many long streams and substreams. Operations Research 50(6), 1073-1075.
rstream
,
rstream.antithetic-methods
,
rstream.clone-methods
,
rstream.incprecision-methods
,
rstream.name-methods
,
rstream.packed-methods
,
rstream.reset-methods
,
rstream.sample-methods
,
rstream.nextsubstream-methods
,
rstream.RNG
.
## create a new rstream.mrg32k3a object s <- new("rstream.mrg32k3a") ## show state of this object print(s) ## show and change name of stream object rstream.name(s) rstream.name(s) <- "mystream" ## get a random number x <- rstream.sample(s) ## get a random sample of size 100 x <- rstream.sample(s,100) ## reset random stream rstream.reset(s) ## show and set antithetic flag rstream.antithetic(s) rstream.antithetic(s) <- TRUE ## jump to next substream rstream.nextsubstream(s) ## make a clone of the rstream object sc <- rstream.clone(s) ## pack and unpack the rstream object rstream.packed(s) <- TRUE rstream.packed(s) <- FALSE
## create a new rstream.mrg32k3a object s <- new("rstream.mrg32k3a") ## show state of this object print(s) ## show and change name of stream object rstream.name(s) rstream.name(s) <- "mystream" ## get a random number x <- rstream.sample(s) ## get a random sample of size 100 x <- rstream.sample(s,100) ## reset random stream rstream.reset(s) ## show and set antithetic flag rstream.antithetic(s) rstream.antithetic(s) <- TRUE ## jump to next substream rstream.nextsubstream(s) ## make a clone of the rstream object sc <- rstream.clone(s) ## pack and unpack the rstream object rstream.packed(s) <- TRUE rstream.packed(s) <- FALSE
Get and change the name for an "rstream" object in package rstream.
The name
is a character string that gives a user the
possibility to label an "rstream" object. Any name can be used.
## S4 method for signature 'rstream' rstream.name(stream) rstream.name(stream) <- value
## S4 method for signature 'rstream' rstream.name(stream) rstream.name(stream) <- value
stream |
an "rstream" object. |
value |
a character string that holds the label (name). |
Methods available for all "rstream" subclasses:
rstream.mrg32k3a
,
rstream.runif
.
Josef Leydold [email protected]
## create a new rstream object (of subclass rstream.mrg32k3a) s <- new("rstream.mrg32k3a") ## set name of rstream object rstream.name(s) <- "mystream" ## get name of rstream object rstream.name(s)
## create a new rstream object (of subclass rstream.mrg32k3a) s <- new("rstream.mrg32k3a") ## set name of rstream object rstream.name(s) <- "mystream" ## get name of rstream object rstream.name(s)
Get status (packed/unopacked) of an "rstream" object in package rstream.
Pack and unpack an "rstream" object in package rstream.
When a "rstream" object should be used in another R session or saved for some kind of later reuse all information about the object must be packed. Notice no method other than unpacking can be applied to a packed object. It must be unpacked before.
## S4 method for signature 'rstream' rstream.packed(stream) rstream.packed(stream) <- value
## S4 method for signature 'rstream' rstream.packed(stream) rstream.packed(stream) <- value
stream |
an "rstream" object. |
value |
a boolean ( |
Methods available for all "rstream" subclasses:
rstream.mrg32k3a
,
rstream.runif
.
Josef Leydold [email protected]
## create a new rstream object (of subclass rstream.mrg32k3a) s <- new("rstream.mrg32k3a") ## pack rstream object rstream.packed(s) <- TRUE ## status of object rstream.packed(s) ## pack rstream object rstream.packed(s) <- FALSE
## create a new rstream object (of subclass rstream.mrg32k3a) s <- new("rstream.mrg32k3a") ## pack rstream object rstream.packed(s) <- TRUE ## status of object rstream.packed(s) ## pack rstream object rstream.packed(s) <- FALSE
Reset "rstream" object in package rstream into its initial state.
By resetting the stream object the same stream of random numbers can be generated.
## S4 method for signature 'rstream' rstream.reset(stream)
## S4 method for signature 'rstream' rstream.reset(stream)
stream |
an "rstream" object. |
Methods available for all "rstream" subclasses:
rstream.mrg32k3a
,
rstream.runif
.
Josef Leydold [email protected]
## create a new rstream object (of subclass rstream.mrg32k3a) s <- new("rstream.mrg32k3a") ## generate a sample x <- rstream.sample(s,10) ## reset the stream object rstream.reset(s) ## the new sample y is identical to x y <- rstream.sample(s,10) if (identical(x,y)) print("x and y are identical samples")
## create a new rstream object (of subclass rstream.mrg32k3a) s <- new("rstream.mrg32k3a") ## generate a sample x <- rstream.sample(s,10) ## reset the stream object rstream.reset(s) ## the new sample y is identical to x y <- rstream.sample(s,10) if (identical(x,y)) print("x and y are identical samples")
Some subclasses have implmented the concept of substreams. This is especially usefull if two or more streams should be synchronized.
This interface allows to jump to the beginning of the next substream and to reset the stream object to the beginning of the current substream.
## S4 method for signature 'rstream' rstream.resetsubstream(stream) ## S4 method for signature 'rstream' rstream.nextsubstream(stream)
## S4 method for signature 'rstream' rstream.resetsubstream(stream) ## S4 method for signature 'rstream' rstream.nextsubstream(stream)
stream |
an "rstream" object. |
Methods available for the following "rstream" subclasses:
rstream.mrg32k3a
.
Josef Leydold [email protected]
## create a new rstream object (of subclass rstream.mrg32k3a) s <- new("rstream.mrg32k3a") ## jump to beginning of next substream rstream.nextsubstream(s) ## generate a sample x <- rstream.sample(s,10) ## reset substream rstream.resetsubstream(s) ## the new sample y is identical to x y <- rstream.sample(s,10) if (identical(x,y)) print("x and y are identical samples")
## create a new rstream object (of subclass rstream.mrg32k3a) s <- new("rstream.mrg32k3a") ## jump to beginning of next substream rstream.nextsubstream(s) ## generate a sample x <- rstream.sample(s,10) ## reset substream rstream.resetsubstream(s) ## the new sample y is identical to x y <- rstream.sample(s,10) if (identical(x,y)) print("x and y are identical samples")
The function rstream.RNG(stream)
is used to set a given
"rstream" object stream
as current global R uniform random
number generators.
Without an argument (or NULL
) it returns an "rstream" object
that contains the current global generator.
It is a copy (clone) of the global generator and thus it can be
handled independently from the global generator.
rstream.RNG(stream = NULL)
rstream.RNG(stream = NULL)
stream |
|
rstream.RNG
returns an "rstream" object.
Josef Leydold [email protected]
## create a new rstream.runif object s <- new("rstream.mrg32k3a") ## use this stream as global R uniform RNG rstream.RNG(s) ## get a (idenpendent) copy of the stream ## that contains the global R uniform RNG gs <- rstream.RNG() ## change the state of the global generator gs <- rstream.RNG() rstream.nextsubstream(gs) rstream.RNG(gs)
## create a new rstream.runif object s <- new("rstream.mrg32k3a") ## use this stream as global R uniform RNG rstream.RNG(s) ## get a (idenpendent) copy of the stream ## that contains the global R uniform RNG gs <- rstream.RNG() ## change the state of the global generator gs <- rstream.RNG() rstream.nextsubstream(gs) rstream.RNG(gs)
This class implements the "rstream" interface for the R internal
uniform RNGs. This class allows to access and handle these generators
in exactly the same way as external generators. In particular, one can
create copies of generators. There is no need to deal with
RNGkind
and set.seed
when different
RNGs should be used.
Objects can be created by calls of the form
new("rstream.runif", name, kind, seed, antithetic)
.
An arbitrary string to name the stream object.
If omitted a string that consists of runif
and some
number (which is increased every time when a new rstream
object is created.
A character string. The new "rstream" object uses
the RNG of type kind
. The same strings as for
RNGkind
can be used. Additionally the string
"current"
is available to use the type of generator to
which R is currently set (this is the default if no kind is
given).
User-supplied generators cannot be used.
The seed for the generator as used by the
set.seed
call. If omitted a random seed is used.
A boolean. Whether or not antithetic random
numbers should be produced.
Default is FALSE
.
Class "rstream"
, directly.
The class "rstream.runif" provides the following methods for
handling "rstream.runif" objects. Some methods that return
parameters of the stream object have a variant that uses <-
to
change the respective parameters. See the man pages for the respective
methods for details.
Methods to use the stream:
signature(object = "rstream.runif")
:
Get a random sample from the stream object.
signature(object = "rstream.runif")
:
Same as rstream.sample
.
signature(object = "rstream.runif")
:
Reset stream into initial state.
Antithetic random streams:
signature(object = "rstream.runif")
:
Whether or not the stream object returns antithetic random
numbers.
signature(object = "rstream.runif")
:
Change antithetic flag (TRUE
or FALSE
).
Handling "rstream.runif" objects:
signature(x = "rstream.runif")
:
Print state of the stream object.
signature(object = "rstream.runif")
:
The name of the stream object.
signature(object = "rstream.runif")
:
Change the name of the stream object.
signature(object = "rstream.runif")
:
Make a copy (clone) of stream object.
signature(.Object = "rstream.runif")
:
Initialize rstream object. (For Internal usage only).
When a "rstream.runif" object should be used in another R session or saved for some kind of later reuse all information about the object must be packed. Notice no method other than unpacking can be applied to a packed object. It must be unpacked before.
signature(object = "rstream.runif")
:
Whether or not the stream object is packed.
signature(object = "rstream.runif")
:
Pack or unpack object: set packed to TRUE
or FALSE
.
The slots of this class must not be accessed directly. Use the above methods instead.
"rstream" objects cannot simply be copied by <-
. The new
variable does not hold a copy of an "rstream" object but just points
to the old object which might not be the wanted result (similar to R
environments). Use rstream.clone
instead.
One may miss a method for reseeding a random stream. However, there is no need for such a method as there is a method for resetting the stream to its initial state. I one needs a stream with a different stream, then a new rstream object should be created at all.
Packed objects must be unpacked before any other method can be applied.
Josef Leydold [email protected]
rstream
,
rstream.antithetic-methods
,
rstream.clone-methods
,
rstream.name-methods
,
rstream.packed-methods
,
rstream.reset-methods
,
rstream.sample-methods
,
rstream.RNG
.
## create a new rstream.runif object s <- new("rstream.runif") ## show state of this object print(s) ## show and change name of stream object rstream.name(s) rstream.name(s) <- "mystream" ## get a random number x <- rstream.sample(s) ## get a random sample of size 100 x <- rstream.sample(s,100) ## reset random stream rstream.reset(s) ## show and set antithetic flag rstream.antithetic(s) rstream.antithetic(s) <- TRUE ## make a clone of the rstream object sc <- rstream.clone(s) ## pack and unpack the rstream object rstream.packed(s) <- TRUE rstream.packed(s) <- FALSE
## create a new rstream.runif object s <- new("rstream.runif") ## show state of this object print(s) ## show and change name of stream object rstream.name(s) rstream.name(s) <- "mystream" ## get a random number x <- rstream.sample(s) ## get a random sample of size 100 x <- rstream.sample(s,100) ## reset random stream rstream.reset(s) ## show and set antithetic flag rstream.antithetic(s) rstream.antithetic(s) <- TRUE ## make a clone of the rstream object sc <- rstream.clone(s) ## pack and unpack the rstream object rstream.packed(s) <- TRUE rstream.packed(s) <- FALSE
Get random sample from an "rstream" object in package rstream.
## S4 method for signature 'rstream,numeric' rstream.sample(stream,n=1) ## S4 method for signature 'rstream,numeric' r(stream,n=1)
## S4 method for signature 'rstream,numeric' rstream.sample(stream,n=1) ## S4 method for signature 'rstream,numeric' r(stream,n=1)
stream |
an "rstream" object. |
n |
sample size. |
Methods available for all "rstream" subclasses:
rstream.mrg32k3a-class
,
rstream.runif-class
.
r
is equivalent to rstream.sample
.
Josef Leydold [email protected]
## create a new rstream object (of subclass rstream.mrg32k3a) s <- new("rstream.mrg32k3a") ## get a random number x <- rstream.sample(s) ## get a random sample of size 100 x <- rstream.sample(s,100) ## alternatively ... x <- r(s,100)
## create a new rstream object (of subclass rstream.mrg32k3a) s <- new("rstream.mrg32k3a") ## get a random number x <- rstream.sample(s) ## get a random sample of size 100 x <- rstream.sample(s,100) ## alternatively ... x <- r(s,100)
rstream.version
can be used to switch to the behavior as it was
in an earlier version of package rstream (for reproducibility).
rstream.version(version)
rstream.version(version)
version |
a character string containing a version number of the
form |
Bug fixes may influence a generated random stream (see NEWS file,
news(package="rstream")
).
However, it is sometimes necessary to reproduce the same sequence with
a newer version of the package. Thus one can switch to this behavior
by means of rstream.version
.
version
is a character string that contains the requested
version number. Notice that only the major number and the first
minor number are relevant. The string "default"
can be used
to switch back to the new behavior.
When version
is omitted than the actual version number is
shown.
Josef Leydold [email protected]
## Switch to version 1.2 rstream.version("1.2") ## Show current version rstream.version() ## Switch back to default rstream.version("default") rstream.version()
## Switch to version 1.2 rstream.version("1.2") ## Show current version rstream.version() ## Switch back to default rstream.version("default") rstream.version()