generate random sample - Maple Help

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : Statistics : Statistics Package : Simulation : Statistics/Sample

Statistics[Sample] - generate random sample

Calling Sequence

Sample(X, n, opts)

Sample(X, m, opts)

Sample(X, rng, opts)

Sample(X, out, opts)

Sample(X, opts)

Parameters

X

-

algebraic; random variable or distribution

n

-

nonnegative integer; sample size

m

-

list of two nonnegative integers; Matrix dimensions

rng

-

integer range or list of integer ranges; Array dimensions

out

-

float rtable; to be filled with data

options

-

(optional) equations of the form option = value, where option is method, possibly indexed by a name; specify options for sample generation

Efficiency

• 

When implementing an algorithm that uses a large number of random samples, it can be worthwhile to think about efficiency of the random sample generation. In most cases, the best efficiency is achieved when all samples are generated at once in a preprocessing phase and stored in a Vector (using the first calling sequence, above), and the values are then used one by one in the algorithm.

  

In some cases, however, this is not possible. For example, this might take too much memory (if a very large number of samples is needed), it might be difficult or impossible to predict the number of samples needed, or the parameters of the random variable might change during the algorithm. In the first two cases, the recommended strategy is to use the fourth calling sequence to create a procedure p, then use p to create a Vector that can hold a large number of samples (using, say, v:=p105), using the elements of v one by one, and calling pv to refill v when the samples run out. If the parameters of the random variable keep changing, then one can define the random variable with parameters that are unassigned initially, use the fourth calling sequence to create a procedure p, then assign values to the parameters afterwards. An example is provided below.

• 

For some of the discrete distributions, the method selected by default is not method = custom but method = discrete. For these distributions, this method is faster when generating more than about 1000 random numbers. If you need to generate fewer random numbers, you can select method = custom by including that option explicitly.

Description

• 

The Sample command generates a random sample drawn from the distribution given by X.

• 

The first parameter, X, can be a distribution (see Statistics[Distribution]), a random variable, or an algebraic expression involving random variables (see Statistics[RandomVariable]).

• 

In the first calling sequence, the second parameter, n, is the sample size. This calling sequence will return a newly created Vector of length n, filled with the sample values. This calling sequence, or one of the next two, is recommended for all cases where there are no great performance concerns.

• 

In the second calling sequence, the second parameter, m, is a list of two nonnegative integers. This calling sequence will return a newly created Matrix with the specified dimensions, filled with the sample values.

• 

In the third calling sequence, the second parameter, rng, is a range or a list of ranges determining the dimensions of an Array. This Array will be created, filled with the sample values, and returned.

• 

In the fourth calling sequence, the second parameter, out, is an rtable (such as a Vector) that was created beforehand. Upon successful return of the Sample command, out will have been filled with the sample values.

  

out needs to have rectangular storage and the float data type that is consistent with the current settings of Digits and UseHardwareFloats. That is, if either UseHardwareFloats = true, or UseHardwareFloats = deduced and Digits <= evalhf(Digits) (which is the default), then out needs to have datatype = float[8]; in the other case, that is, if either UseHardwareFloats = false, or UseHardwareFloats = deduced and Digits > evalhf(Digits), then out needs to have datatype = sfloat. This can easily be achieved by supplying the option datatype = float to the rtable creation function; this will automatically select the correct data type for the current settings.

• 

In the fourth calling sequence, Sample returns a procedure p, which can subsequently be called to generate samples of X repeatedly. The procedure p accepts a single argument, which can be n, m, rng, or out, and then behaves as if one of the first three calling sequences were called. p does not accept options; any options should be given in the call to Sample itself.

Examples

withStatistics&colon;

Straightforward sampling of a distribution.

X:=RandomVariableNormal0&comma;1

X:=_R

(1)

A:=SampleX&comma;106&colon;

P:=DensityPlotX&comma;range&equals;2..2&comma;thickness&equals;3&comma;color&equals;red&colon;

Q:=HistogramA&comma;range&equals;2..2&colon;

plots&lsqb;display&rsqb;P&comma;Q

We can also sample an expression involving two random variables.

Y:=RandomVariableNormal0&comma;1

Y:=_R0

(2)

B:=SampleXY&comma;106&colon;

P:=DensityPlotCauchy0&comma;1&comma;range&equals;2..2&comma;thickness&equals;3&comma;color&equals;red&colon;

Q:=HistogramB&comma;range&equals;2..2&colon;

plots&lsqb;display&rsqb;P&comma;Q

Sampling of a custom-defined distribution.

dist:=DistributionPDF&equals;t&rarr;piecewiset<0&comma;0&comma;t<1&comma;36t243t7&comma;0

dist:=moduleoptionDistribution&comma;Continuous&semi;exportConditions&comma;PDF&semi;end module

(3)

B:=SampleRandomVariabledist&comma;106&colon;

P:=DensityPlotdist&comma;range&equals;0..1&comma;thickness&equals;3&comma;color&equals;red&colon;

Q:=HistogramB&colon;

plots&lsqb;display&rsqb;P&comma;Q

If we supply a list of ranges instead of a number, we get an Array. With a list of two numbers, we get a Matrix.

a:=SampleXY&comma;1..1000&comma;1..1000

a:= 1..1000 x 1..1000 ArrayData Type: float8Storage: rectangularOrder: Fortran_order

(4)

m:=SampleXY&comma;3&comma;3

m:=1.243686648096802.428115110570600.65732035396944848.57197486907761.799250302100580.65396099861875118.48167505905530.5450421571829950.549903521004792

(5)

We can use envelope rejection sampling to restrict X and Y to a certain range.

s1:=SampleXY&comma;106&comma;method&equals;envelope&comma;range&equals;0..1

s1:= 1 .. 1000000 VectorrowData Type: float8Storage: rectangularOrder: Fortran_order

(6)

Histograms1&comma;range&equals;0..10

Or to restrict only X to a certain range.

s2:=SampleXY&comma;106&comma;methodX&equals;envelope&comma;range&equals;0..1

s2:= 1 .. 1000000 VectorrowData Type: float8Storage: rectangularOrder: Fortran_order

(7)

Histograms2&comma;range&equals;10..10

We can refill s2 with different samples as follows.

SampleCauchy0&comma;1&comma;s2

1 .. 1000000 VectorrowData Type: float8Storage: rectangularOrder: Fortran_order

(8)

Histograms2&comma;range&equals;10..10

Another option is to use a procedure.

p:=SampleX

p:=procn::`Sample:-sizeType`...end proc

(9)

ps2

1 .. 1000000 VectorrowData Type: float8Storage: rectangularOrder: Fortran_order

(10)

Histograms2

Sampling of a custom-defined discrete distribution with non-integer values. This distribution attains the value 32n with probability 2n for positive n.

dist2:=DistributionType&equals;discrete&comma;ProbabilityFunction&equals;t&rarr;2log32t&comma;DiscreteValueMap&equals;n&rarr;32n&comma;Support&equals;1..&infin;

dist2:=moduleoptionDistribution&comma;Discrete&semi;exportProbabilityFunction&comma;Conditions&comma;DiscreteValueMap&comma;Support&semi;end module

(11)

s:=Sampledist2&comma;105

s:= 1 .. 100000 VectorrowData Type: float8Storage: rectangularOrder: Fortran_order

(12)

sortTallys

1.50000000000000&equals;49843&comma;2.25000000000000&equals;24962&comma;3.37500000000000&equals;12661&comma;5.06250000000000&equals;6234&comma;7.59375000000000&equals;3129&comma;11.3906250000000&equals;1592&comma;17.0859375000000&equals;766&comma;25.6289062500000&equals;395&comma;38.4433593750000&equals;226&comma;57.6650390625000&equals;94&comma;86.4975585937500&equals;43&comma;129.746337890625&equals;30&comma;194.619506835938&equals;13&comma;291.929260253906&equals;7&comma;437.893890380859&equals;4&comma;7481.82764267921&equals;1

(13)

Finally, here is a somewhat longer example, where we want to generate exponentially distributed numbers; the rate parameter λ starts as being 1, but for each subsequent value it is the square root of the previous sample value. In order to be able to use a procedure (important for efficiency), we need to make sure that λ is not defined when we create the procedure, otherwise it will only generate samples for the value that λ had at the time of definition. (If λ has a value, it can be undefined by executing lambda := 'lambda';, but since we have not used λ yet, that should not be necessary in this case.)

X:=RandomVariableExponential&lambda;

X:=_R6

(14)

p:=SampleX

p:=procn::`Sample:-sizeType`...end proc

(15)

If we now compute a sample of X, then Maple will complain, because λ is unassigned:

p1

Error, (in p) unable to evaluate lambda to floating-point

Instead, we assign 1 to λ and start an iteration.

&lambda;:=1

&lambda;:=1

(16)

N:=103

N:=1000

(17)

v:=VectorN&comma;&apos;datatype&apos;&equals;&apos;float&apos;

v:= 1 .. 1000 VectorcolumnData Type: float8Storage: rectangularOrder: Fortran_order

(18)

foritoNdovi:=p11&semi;&lambda;:=viend do&colon;

We now create a point plot where pairs of subsequent samples are the horizontal and vertical coordinates.

plots&lsqb;pointplot&rsqb;v..2&verbar;v2..&comma;axis&equals;mode&equals;log&comma;symbolsize&equals;1&comma;color&equals;red

See Also

Statistics, Statistics[Computation], Statistics[Distributions], Statistics[RandomVariables]

References

  

Stuart, Alan, and Ord, Keith. Kendall's Advanced Theory of Statistics. 6th ed. London: Edward Arnold, 1998. Vol. 1: Distribution Theory.

  

Walker, Alastair J. New Fast Method for Generating Discrete Random Numbers with Arbitrary Frequency Distributions, Electronic Letters, 10, 127-128.

  

Walker, Alastair J. An Efficient Method for Generating Discrete Random Variables with General Distributions, ACM Trans. Math. Software, 3, 253-256.


Download Help Document

Was this information helpful?



Please add your Comment (Optional)
E-mail Address (Optional)
What is ? This question helps us to combat spam