Uploaded by Yao Jun Chia

Chapter 10 Simulation An Introduction (1)

advertisement
Chapter 10
Simulation:
An Introduction
ST2137 Computer Aided Data Analysis
CYM
10.1
1. Introduction
1.1 Definition
• A simulation is an imitation of some real things, state of
affairs, or processes.
• The act of simulating something generally entails certain
key characteristics or behaviours of a selected physical or
abstract system.
ST2137 Computer Aided Data Analysis
CYM
10.2
1.2 Use of Simulation
• Simulation is used in many contexts, including the
modeling of natural systems or human systems in order to
gain insight into their functioning.
• Simulation can be used to show the eventual real effects of
alternative conditions and courses of action.
ST2137 Computer Aided Data Analysis
CYM
10.3
Example 1: Checking distribution theory
Theory:
• A sample of size 4 is taken from a normal distribution with
mean 0.
• Consider the statistic
๐‘‹เดค
๐‘‡=
๐‘†/ 4
where ๐‘‹เดค and S are the sample mean and s.d. respectively
• The statistic T follows a t-distribution with 3 degrees of
freedom.
ST2137 Computer Aided Data Analysis
CYM
10.4
Example 1
(Continued)
Simulation:
• Generate 1000 random samples each of size 4 from a
normal distribution.
• For each sample, compute the value of the statistic T.
• Construct a histogram for these 1000 realizations of the
statistic T.
ST2137 Computer Aided Data Analysis
CYM
10.5
Example 1
(Continued)
Result:
• The histogram matches
well with the t(3)
distribution.
ST2137 Computer Aided Data Analysis
CYM
10.6
Example 2: Comparing estimators
We have learnt several robust estimators of location
Question: Which estimator is the best, the trimmed mean or
the Winsorized mean?
Secondary questions:
• Under what conditions ( in terms of the underlying
distributions ) is the estimator better?
We may consider various underlying distributions
ST2137 Computer Aided Data Analysis
CYM
10.7
Example 2: Comparing estimators
• How to measure the performance?
We may use the Mean Squared Error (MSE)
๐‘€๐‘†๐ธ = ๐ต๐‘–๐‘Ž๐‘  2 + ๐‘‰๐‘Ž๐‘Ÿ๐‘–๐‘Ž๐‘›๐‘๐‘’
• It may be intractable to compute the MSE for each of the
estimators under different underlying distributions.
• Simulation is an alternative solution
• How to design such a simulation study?
ST2137 Computer Aided Data Analysis
CYM
10.8
Example 3: Buffon’s needle experiment
A needle of length ๐‘™ is thrown randomly onto a grid of
parallel lines with distance ๐‘‘ ( > ๐‘™ ).
ST2137 Computer Aided Data Analysis
CYM
10.9
Example 3
(Continued)
•
What is the probability that the needle intersects a line?
Answer:
2/๐‘™
๐œ‹๐‘‘
•
•
Can we get the answer through simulations?
A website gives the visualization of the experiment
http://www.metablake.com/pi.swf
ST2137 Computer Aided Data Analysis
CYM
10.10
Example 3
(Continued)
The experiment
• Simulate the throwing of a needle into a grid of parallel
lines, say N times
• Count the number of times the needle intersects a line, say
n times
• Then n/N gives estimate of the probability that the needle
intersects a line.
ST2137 Computer Aided Data Analysis
CYM
10.11
2. How to do the simulation
• Step 1: Generate the position and the inclination of the
needle.
– Generate a random number, ๐‘ฅ, from Uniform (0, ๐‘‘/2). This
number represents the location of the centre of the needle.
– Generate a random number, ๐œƒ, from Uniform (0, ๐œ‹). This
number represents the angle of between the needle and the
parallel lines.
ST2137 Computer Aided Data Analysis
CYM
10.12
How to do the simulation
(Continued)
• Step 2: Check if the needle cuts a line.
– The needle cuts a line if ๐‘ฅ < (๐‘™/2) sin(๐œƒ).
– Create a variable ๐‘ค = 1 if ๐‘ฅ < (๐‘™/2) sin(๐œƒ) and 0 otherwise.
• Step 3: Repeat Steps 1 and 2 N times.
– Count the number of times that ๐‘ค = 1, let say n times.
– Then an estimate of the probability of the needle intersects a
line is given by ๐‘›/๐‘.
ST2137 Computer Aided Data Analysis
CYM
10.13
Example 3: R Code
> # Buffon's needle
> # X ~ U(0,d/2), t ~ U(0,pi) where d is the distance
between 2 parallel lines
> # A needle of length L cut one of the lines if x <
L/2*sin(t)
> # Theoretical Probability = 2*L/(pi*d)
> ns <- 50000
> d <- 2
> L <- 1
> d2 <- d/2
ST2137 Computer Aided Data Analysis
CYM
10.14
Example 3: R Code
> # Buffon's needle
> # Theoretical answer
> 2*L/(pi*d)
[1] 0.3183099
> x <- runif(ns,0,d2) # Generate a sample of size ns
from uniform(0, d2)
> t <- runif(ns,0,pi) # Generate a sample of size ns
from uniform (0, pi)
> length(x[x < L/2*sin(t)])/ns
[1] 0.3186
ST2137 Computer Aided Data Analysis
CYM
10.15
3. Random number generator
3.1 Definition
• A sequence of pseudo-random numbers {Ui} is a
deterministic sequence of number in [0, 1] having the
same relevant statistical properties as a sequence of
random numbers
ST2137 Computer Aided Data Analysis
CYM
10.16
3.2 Congruential generators
• Congruential generators are defined by
๐‘‹๐‘– = ๐‘Ž๐‘‹๐‘–−1 + ๐‘ ๐‘š๐‘œ๐‘‘ ๐‘€
for a multiplier a, shift c, and modulus M.
• ๐‘Ž, ๐‘ and ๐‘€ are all integers
• For example: Let ๐‘€ = 10, ๐‘Ž = 2, ๐‘ = 0, ๐‘ฅ0 = 1
• Then the sequence generated is 1, 2, 4, 8, 6, 2, 4, 8, 6, 2, 4, 8, โ‹ฏ
• Are these numbers random? There is a pattern, 2, 4, 8 ,6.
• Uniform random numbers are obtained by
๐‘ˆ๐‘– = ๐‘‹๐‘– /๐‘€
ST2137 Computer Aided Data Analysis
CYM
10.17
3.2 Congruential generators
• To initialize, we have to provide a seed, ๐‘‹0 .
• If ๐‘ = 0, generators having the form
๐‘‹๐‘– = ๐‘Ž๐‘‹๐‘–−1 ๐‘š๐‘œ๐‘‘ ๐‘€
are called multiplicative congruential generator.
• If ๐‘ > 0, they are called linear congruential generator.
ST2137 Computer Aided Data Analysis
CYM
10.18
Remarks
• M + 1 values {๐‘‹0 , ๐‘‹1 , โ‹ฏ , ๐‘‹๐‘€ } cannot be distinct and at least one
value must occur twice, as ๐‘‹๐‘– and ๐‘‹๐‘–+๐‘˜ , say.
• ๐‘‹๐‘– , ๐‘‹๐‘–+1 , โ‹ฏ , ๐‘‹๐‘–+๐‘˜−1 is repeated as ๐‘‹๐‘–+๐‘˜ , ๐‘‹๐‘–+๐‘˜+1 , โ‹ฏ , ๐‘‹๐‘–+2๐‘˜−1
• The sequence {๐‘‹๐‘– } is periodic with period ๐‘˜ ≤ ๐‘€
• For multiplicative generators, the maximal period is ๐‘€ − 1
If 0 ever occurs, it is repeated indefinitely.
• One of our primary objectives is to use a generator with as
large period as possible.
• However, a large period does not guarantee a good generator.
ST2137 Computer Aided Data Analysis
CYM
10.19
3.3 R function for congruential generators
> # Linear congruential generators
> lcg <- function(n,a,m,c,x0){
+
ran <- NULL
+
for (i in 1:n) {
+
x1 <- (a*x0+c)%%m
+
x0 <- x1
+
ran <- c(ran,x1/m) }
+ return(ran) }
> lcg(10,397204094,2^31-1,0,1234)
[1] 0.24381116
0.08947511
0.38319371
0.72792771
0.17503827
[7] 0.40680994
0.90923700
0.34271140
ST2137 Computer Aided Data Analysis
CYM
0.72800325
0.55960111
10.20
4. Generate uniform random numbers
4.1 SAS code:
*Generate Uniform Random Numbers;
data try1;
seed = 1234;
do i = 1 to 10;
x = ranuni(seed);
output;
end;
keep x;
run;
proc print data=try1;
var x;
run;
ST2137 Computer Aided Data Analysis
CYM
10.21
4. Generate uniform random numbers
4.1 SAS code (Continued):
* Alternative way to generate Uniform Random Numbers;
data try1;
seed = 1234; call streaminit(seed);
do i = 1 to 10;
x = rand(‘uniform’); output;
end; keep x;
run;
• Without call streaminit(seed), the sets of random numbers
generated will be different.
• If seed value is not specified, then the time at which the
command is executed is used for the seed.
ST2137 Computer Aided Data Analysis
CYM
10.22
Generate uniform random numbers
(Continued)
4.2 R code
> # Generate 1000 random numbers from U(0, 1) distribution
> set.seed(1234)
> x <- runif(1000)
In general, “runif(n, a, b)” generates a vector of n random
numbers from a uniform distribution between a and b.
ST2137 Computer Aided Data Analysis
CYM
10.23
5 Generate non-uniform random numbers
5.1 Inversion method
• If X has a continuous distribution function ๐น(๐‘ฅ) (Recall
๐น ๐‘ฅ = Pr(๐‘‹ ≤ ๐‘ฅ)), then
๐น ๐‘ฅ ~ ๐‘ˆ๐‘›๐‘–๐‘“๐‘œ๐‘Ÿ๐‘š(0, 1)
Algorithm:
• Generate U from Uniform(0, 1)
• Set ๐‘‹ = ๐น −1 (๐‘ˆ) provided that the inverse function exists.
ST2137 Computer Aided Data Analysis
CYM
10.24
5.2 Exponential distribution
• If X follows an exponential distribution with parameter λ
(i.e. ๐ธ ๐‘‹ = ๐œ†), then
๐‘ฅ
1 −๐‘ก
๐น ๐‘ฅ = Pr ๐‘‹ ≤ ๐‘ฅ = เถฑ ๐‘’ ๐œ† ๐‘‘๐‘ก
0 ๐œ†
• Note : The p.d.f. of ๐‘‹ is given by ๐‘“ ๐‘ก =
1 −๐‘ก
๐‘’ ๐œ†
๐œ†
for 0 ≤ ๐‘ก ≤ ∞
• Let ๐‘ฆ = ๐‘ก/๐œ†. Then ๐‘‘๐‘ฆ = ๐‘‘๐‘ก/๐œ†. ๐‘ก = 0 ⇒ ๐‘ฆ = 0 and ๐‘ก = ๐‘ฅ ⇒
๐‘ฆ = ๐‘ฅ/๐œ†. Hence
๐‘ฅ/๐œ†
๐น(๐‘ฅ) = เถฑ ๐‘’ −๐‘ฆ ๐‘‘๐‘ฆ = 1 − ๐‘’ −๐‘ฅ/๐œ†
ST2137 Computer Aided Data Analysis
0
CYM
10.25
5.2 Exponential distribution
• Let ๐‘ˆ = ๐น ๐‘‹ = 1 − ๐‘’ −๐‘‹/๐œ† .
• Then ๐‘‹ = ๐น −1 ๐‘ˆ = −๐œ† log(1 − ๐‘ˆ)
Suppose ๐œ† = 2.
Step 1: Generate U from Uniform(0, 1)
Step 2: Set ๐‘‹ = −2 log(1 − ๐‘ˆ).
ST2137 Computer Aided Data Analysis
CYM
10.26
5.3 Weibull distribution
If X follows a Weibull distribution with parameter ๐›ฝ, then it
can be shown that
๐น ๐‘ฅ = 1 − exp −๐‘ฅ ๐›ฝ , on (0, ∞)
• Note: The p.d.f. for ๐‘‹ is given by ๐‘“ ๐‘ฅ = ๐›ฝ๐‘ฅ ๐›ฝ−1 exp −๐‘ฅ ๐›ฝ ,
for ๐‘ฅ in 0, ∞
1. Generate U from Uniform(0, 1)
2. Set ๐‘‹ = − log 1 − ๐‘ˆ 1/๐›ฝ .
(It is obtained by solving for ๐‘‹ in the equation ๐‘ˆ = 1 − exp(−๐‘ฅ ๐›ฝ ))
ST2137 Computer Aided Data Analysis
CYM
10.27
5.4 Cauchy distribution
• If ๐‘‹ follows a Cauchy distribution with parameter ๐œ‡ and ๐œŽ,
then it can be shown that
1 1
๐‘ฅ−๐œ‡
−1
๐น ๐‘ฅ = + tan
,
for ๐‘ฅ in (−∞, ∞)
2 ๐œ‹
๐œŽ
• Note:
1
๐‘“ ๐‘ฅ =
๐‘ฅ−๐œ‡ 2
๐œ‹๐œŽ 1 +
๐œŽ
1. Generate U from Uniform(0, 1).
2. Set ๐‘‹ = ๐œŽ tan ๐œ‹ ๐‘ˆ − 0.5 + ๐œ‡
ST2137 Computer Aided Data Analysis
CYM
10.28
6. Algorithm to generate a normal random variable
6.1 Box-Muller Algorithm
• Generate ๐‘ˆ1 and ๐‘ˆ2 from Uniform(0, 1).
• Set ๐œƒ = 2๐œ‹๐‘ˆ1 .
• Set ๐‘… = −2 log ๐‘ˆ 1/2
2
• Set ๐‘‹ = ๐‘… cos(๐œƒ) and ๐‘Œ = ๐‘… sin ๐œƒ .
• X and Y are independent standard normal variables.
• For simplicity, only X or Y is used.
ST2137 Computer Aided Data Analysis
CYM
10.29
7. Generate a random variable from other
random variables
7.1 Cauchy distribution
• If Y and Z are independent and follow ๐‘(0, 1), then
๐‘‹ = ๐‘Œ/๐‘
follows a Cauchy(0, 1) distribution.
• If ๐‘Œ~๐‘(๐œ‡, ๐œŽ 2 ) and ๐‘~๐‘(0, 1), and are independent, then
๐‘‹ = ๐‘Œ/๐‘
follows a Cauchy(๐, ๐ˆ๐Ÿ) distribution
ST2137 Computer Aided Data Analysis
CYM
10.30
Generate a random variable from other random
variables (Continued)
7.2 Chi-square distribution
• If ๐‘Œ~๐‘(0, 1) and ๐‘‹ = ๐‘Œ 2 , then
๐‘‹ ~๐œ’ 2 (1)
• If ๐‘Œ1 , ๐‘Œ2 , โ‹ฏ , ๐‘Œ๐‘› are independent and identically distributed
standard normal variables and ๐‘‹ = ๐‘Œ12 + ๐‘Œ22 + โ‹ฏ + ๐‘Œ๐‘›2 ,
then
๐‘‹~๐œ’ 2 (๐‘›)
ST2137 Computer Aided Data Analysis
CYM
10.31
Generate a random variable from other random
variables (Continued)
7.3 Student’s t-distribution
• If ๐‘Œ ~ ๐‘(0, 1) and ๐‘ ~ ๐œ’ 2 (๐‘), then
๐‘Œ
๐‘‹=
๐‘/๐‘
follows a Student’s t distribution with p degrees of
freedom
ST2137 Computer Aided Data Analysis
CYM
10.32
Generate a random variable from other random
variables (Continued)
7.4 F distribution
• If ๐‘Œ ~ ๐œ’ 2 ๐‘š , and ๐‘ ~ ๐œ’ 2 ๐‘› , then
๐‘Œ/๐‘š
๐‘‹=
๐‘/๐‘›
follows an F distribution with degrees of freedom m and n.
ST2137 Computer Aided Data Analysis
CYM
10.33
8. Functions to generate random numbers from
specific distributions
References
SAS
http://support.sas.com/documentation/cdl/en/lrdict/6431
6/HTML/default/viewer.htm#a001466748.htm
R
https://stat.ethz.ch/R-manual/Rdevel/library/stats/html/Distributions.html
ST2137 Computer Aided Data Analysis
CYM
10.34
8.1 Function to generate random numbers from
Uniform (a, b)
To generate random numbers from Uniform (a, b)
1
๐‘“ ๐‘ฅ =
,
for ๐‘Ž < ๐‘ฅ < ๐‘
๐‘−๐‘Ž
R program
>
>
>
>
>
>
>
# Generate uniform random variables
n <- 100
a <- 0
b <- 100
set.seed(1234) # set the seed number
x <- runif(n,a,b)
x
ST2137 Computer Aided Data Analysis
CYM
10.35
8.1 Function to generate uniform distribution
random variables (Continued)
SAS program
/* Generate uniform random variables */
data unif;
call streaminit(1234); /* 1234 is used as the seed */
n = 100; a = 0; b = 10;
do i = 1 to n;
x = a + (b - a)*rand('uniform');
output;
end;
keep x;
proc print data=unif;
run;
ST2137 Computer Aided Data Analysis
CYM
10.36
8.2 Function to generate Normal distribution
random variables
To generate random numbers from Normal(๐œ‡, ๐œŽ2)
1
๐‘ฅ−๐œ‡ 2
๐‘“ ๐‘ฅ =
exp −
,
for − ∞ < ๐‘ฅ < ∞
2
2๐œŽ
2πσ
R program
>
>
>
>
>
>
# Generate normal random variables
n <- 100
mu <- 0
sigma <- 1
x <- rnorm(n, mean=mu, sd=sigma)
x
ST2137 Computer Aided Data Analysis
CYM
10.37
8.2 Function to generate Normal distribution
random variables (Continued)
SAS program
/* Generate normal random variables */
data norm;
seed = 1234;
call streaminit(seed);
n = 100; mu = 0; sigma = 1;
do i = 1 to n;
x = rand('normal',mu,sigma);
output;
end;
keep x;
run;
ST2137 Computer Aided Data Analysis
CYM
10.38
8.3 Function to generate Exponential distribution
random variables
To generate random numbers from Exponential(λ)
1
๐‘ฅ
๐‘“ ๐‘ฅ = exp − ,
๐œ†
๐œ†
for ๐‘ฅ > 0
R program
>
>
>
>
>
# Generate exponential random variables
n <- 100
lambda <- 5
x <- rexp(n, mean=lambda)
x
ST2137 Computer Aided Data Analysis
CYM
10.39
8.3 Function to generate Exponential distribution
random variables (Continued)
SAS program
/* Generate exponential random variables */
data expno;
seed = 1234;
call streaminit(seed);
n = 100; lambda = 5;
do i = 1 to n;
x = lambda*rand('exponential');
output;
end;
keep x;
run;
ST2137 Computer Aided Data Analysis
CYM
10.40
8.4 Function to generate Gamma distribution
random variables
To generate random numbers from Gamma(๐›ผ, ๐›ฝ)
1
๐‘ฅ
๐›ผ−1
๐‘“ ๐‘ฅ = ๐›ผ
๐‘ฅ
exp −
,
๐›ฝ Γ ๐›ผ
๐›ฝ
for ๐‘ฅ > 0
R program
>
>
>
>
>
>
# Generate Gamma random varaibles
n <- 100
alpha <- 1
beta <- 2
x <- rgamma(n, shape = alpha, scale = beta)
x
ST2137 Computer Aided Data Analysis
CYM
10.41
8.4 Function to generate Gamma distribution
random variables (Continued)
SAS program
/* Generate Gamma random variables */
data gammano;
seed = 1234;
call streaminit(seed);
n = 100; alpha = 1; beta = 2;
do i = 1 to n;
x = beta*rand('gamma',alpha);
output;
end;
keep x;
run;
ST2137 Computer Aided Data Analysis
CYM
10.42
8.5 Function to generate Chi-square distribution
random variables
To generate random numbers from Chi-square(p)
๐‘“ ๐‘ฅ =
1
๐‘
22 Γ
๐‘
2
๐‘
๐‘ฅ 2−1 exp
๐‘ฅ
− ,
2
for ๐‘ฅ > 0
R program
>
>
>
>
>
#
n
p
x
x
Generate Chisquare random variables
<- 100
<- 10
<- rchisq(n, df = p)
ST2137 Computer Aided Data Analysis
CYM
10.43
8.5 Function to generate Chi-square distribution
random variables (Continued)
SAS program
/* Generate chisquare random variables */
data chisqno;
seed = 1234;
call streaminit(seed);
n = 100; df = 10;
do i = 1 to n;
x = rand('chisquare',df);
output;
end;
keep x;
run;
ST2137 Computer Aided Data Analysis
CYM
10.44
8.6 Function to generate Beta distribution
random variables
To generate random numbers from Beta(α, β)
Γ ๐›ผ + ๐›ฝ ๐›ผ−1
๐‘“ ๐‘ฅ =
๐‘ฅ
1−๐‘ฅ
Γ ๐›ผ Γ ๐›ฝ
๐›ฝ−1 ,
for 0 < ๐‘ฅ < 1
R program
>
>
>
>
>
>
#
n
a
b
x
x
Generate Beta random variables
<- 100
<- 2
<- 3
<- rbeta(n, shape1 = a, shape2 = b)
ST2137 Computer Aided Data Analysis
CYM
10.45
8.6 Function to generate Beta distribution
random variables (Continued)
SAS program
/* Generate Beta random variables */
data betano;
seed = 1234;
call streaminit(seed);
n = 100; alpha = 2; beta = 3;
do i = 1 to n;
x = rand('beta',alpha,beta);
output;
end;
keep x;
run;
ST2137 Computer Aided Data Analysis
CYM
10.46
8.7 Function to generate t-distribution random
variables
To generate random numbers from t(k)
๐‘˜+1
Γ
2
๐‘“ ๐‘ฅ =
๐‘˜
Γ
2
1
1
๐‘˜๐œ‹
1+
๐‘˜+1 ,
๐‘ฅ2 2
for − ∞ < ๐‘ฅ < ∞
๐‘˜
R program
>
>
>
>
>
#
n
k
x
x
Generate t random variables
<- 100
<- 5
<- rt(n, df = k)
ST2137 Computer Aided Data Analysis
CYM
10.47
8.7 Function to generate t-distribution random
variables (Continued)
SAS program
/* Generate t random variables */
data tno;
seed = 1234;
call streaminit(seed);
n = 100; df = 5;
do i = 1 to n;
x = rand('t',df);
output;
end;
keep x;
run;
ST2137 Computer Aided Data Analysis
CYM
10.48
8.8 Function to generate F-distribution random
variables
To generate random numbers from F(m, n)
๐‘›1 + ๐‘›2
Γ
๐‘“ ๐‘ฅ = ๐‘› 2 ๐‘›
Γ 1 Γ 2
2
2
๐‘›1
๐‘›2
๐‘›1
2
๐‘›1
๐‘ฅ 2 −1
๐‘›1
1+ ๐‘ฅ
๐‘›2
๐‘›1 +๐‘›2
2
,
for 0 < ๐‘ฅ < ∞
R program
>
>
>
>
>
>
# Generate F random variables
n <- 100
n1 <- 5
n2 <- 10
x <- rf(n, df1 = n1, df2 = n2)
x
ST2137 Computer Aided Data Analysis
CYM
10.49
8.8 Function to generate F-distribution random
variables (Continued)
SAS program
/* Generate F random variables */
data fno;
seed = 1234;
call streaminit(seed);
n = 100; df1 = 5; df2 = 10;
do i = 1 to n;
x = rand('f',df1,df2);
output;
end;
keep x;
run;
ST2137 Computer Aided Data Analysis
CYM
10.50
8.9 Function to generate Binomial distribution
random variables
To generate random numbers from Binomial(n, p)
๐‘› ๐‘ฅ
๐‘“ ๐‘ฅ =
๐‘ 1−๐‘
๐‘ฅ
๐‘›−๐‘ฅ ,
for ๐‘ฅ = 0, 1, โ‹ฏ , ๐‘›
R program
>
>
>
>
>
>
# Generate Binomial random variables
nn <- 100
n <- 10
p <- 0.3
x <- rbinom(100, size = n, prob = p)
x
ST2137 Computer Aided Data Analysis
CYM
10.51
8.9 Function to generate Binomial distribution
random variables (Continued)
SAS program
/* Generate Binomial random variables */
data binomno;
seed = 1234;
call streaminit(seed);
ns = 100; n = 10; p = 0.3;
do i = 1 to ns;
x = rand('binomial',p,n);
output;
end;
keep x;
run;
ST2137 Computer Aided Data Analysis
CYM
10.52
8.10 Function to generate Poisson distribution
random variables
To generate random numbers from Poisson(λ)
๐‘’ −๐œ† ๐œ†๐‘ฅ
๐‘“ ๐‘ฅ =
,
๐‘ฅ!
for ๐‘ฅ = 0, 1, 2, โ‹ฏ
R porgram
>
>
>
>
>
# Generate Poisson random variables
n <- 100
lambda <- 3
x <- rpois(100, lambda)
x
ST2137 Computer Aided Data Analysis
CYM
10.53
8.10 Function to generate Poisson distribution
random variables (Continued)
SAS program
/* Generate Poisson random variables */
data poisno;
seed = 1234;
call streaminit(seed);
n = 100; lambda = 3;
do i = 1 to n;
x = rand('poisson',lambda);
output;
end;
keep x;
run;
ST2137 Computer Aided Data Analysis
CYM
10.54
8.11 Function to generate Hypergeometric
distribution random variables
To generate random numbers from Hypergeo(n, N, S)
๐‘†
๐‘“ ๐‘ฅ = ๐‘ฅ
R program
>
>
>
>
>
>
>
๐‘−๐‘†
๐‘›−๐‘ฅ ,
๐‘
๐‘›
for ๐‘ฅ = 0, 1, 2, โ‹ฏ , min(๐‘›, ๐‘†)
# Generate Hypergeometric random variables
ns <- 100
n <- 10
S <- 20
N <- 50
x <- rhyper(ns,S,N,n)
x
ST2137 Computer Aided Data Analysis
CYM
10.55
8.11 Function to generate Hypergeometric
distribution random variables (Continued)
SAS porgram
/* Generate Hypergeometric random variables */
data hypergeono;
seed = 1234;
call streaminit(seed);
ns = 100; popnsize = 10; numbsucc = 5; samplsize =3;
do i = 1 to ns;
x=rand('hyper',popnsize,numbsucc,samplsize);
output;
end;
keep x;
run;
ST2137 Computer Aided Data Analysis
CYM
10.56
8.12 Function to generate Negative Binomial
distribution random variables
To generate random numbers from NBinom(r, p)
๐‘Ÿ+๐‘ฅ−1 ๐‘Ÿ
๐‘“ ๐‘ฅ =
๐‘ 1 − ๐‘ ๐‘ฅ,
๐‘ฅ
for ๐‘ฅ = 0, 1, 2, โ‹ฏ
R program
>
>
>
>
>
>
#
n
r
p
x
x
Generate Negative Binomial random var
<- 100
<- 10
<- 0.3
<- rnbinom(n, size=r, prob=p)
ST2137 Computer Aided Data Analysis
CYM
10.57
8.12 Function to generate Negative Binomial
random variables (Continued)
SAS program
/* Generate Negative Binomial random variables */
data negbinomno;
seed = 1234;
call streaminit(seed);
n = 100; p=0.3; k=5;
do i = 1 to n;
x = rand('negbinomial',p,k);
output;
end;
keep x;
run;
ST2137 Computer Aided Data Analysis
CYM
10.58
Related documents
Download