0% found this document useful (0 votes)
41 views5 pages

Comptools March 4 2020: Function Max Runif Experiment

The document discusses using R to simulate experiments and estimate probabilities. It shows how to define a single experiment as a function, use replicate() to run the experiment multiple times, and calculate probabilities based on the results. As an example, it simulates the birthday problem to estimate the probability of a shared birthday in a group of a given size.

Uploaded by

NikuBotnari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views5 pages

Comptools March 4 2020: Function Max Runif Experiment

The document discusses using R to simulate experiments and estimate probabilities. It shows how to define a single experiment as a function, use replicate() to run the experiment multiple times, and calculate probabilities based on the results. As an example, it simulates the birthday problem to estimate the probability of a shared birthday in a group of a given size.

Uploaded by

NikuBotnari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CompTools March 4 2020

we deal with simulation: - yesterday case; - replicate an experiment using “replicate”! -


section 5.4 - birthday problem
The quality of a new item is assessed using 2 numeric measures. Assume they are uniform
random numbers. Estimate how likely it is that the maximum of the measures exceeds 0.5.
> a) About 0.22; b) About 0.75; c) About 0.9; d) About 0.39.
you can define a function with a single experiment
experiment <- function() max(runif(2))>0.5 #<0.7
experiment()

## [1] TRUE
use the command replicate to replicate many experiments: in R you can type replicate(number,
experiment())
res <- replicate(1000,experiment())
# res
sum(res)/1000

## [1] 0.742
lessons to be learned: i can use an explicit loop, ad we did it yesterday, but i can define a
single experiment and, then, use replicate to “replicate” many experiments.
res[1:10]

## [1] FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
question by Aurora: how to count the FALSE?
sum(res==FALSE)

## [1] 258
going back the the personnel: i have workers with different skills and want to form committees
with 4 members. how likely is that at last three skills are present (included) in the committee?
personnel <- c(rep("sales",4),rep("marketing",3),rep("production",5),rep("HR",2))
personnel

## [1] "sales" "sales" "sales" "sales" "marketing"


## [6] "marketing" "marketing" "production" "production" "production"
## [11] "production" "production" "HR" "HR"
table(personnel)

## personnel

1
## HR marketing production sales
## 2 3 5 4
i’m aiming at defining a single experiment
committee <- sample(personnel,4)
committee

## [1] "sales" "production" "marketing" "sales"


unique(committee)

## [1] "sales" "production" "marketing"


length(unique(committee))

## [1] 3
now, i’m ready to define a single experiment
experiment <- function() {
committee <- sample(personnel,4)
length(unique(committee))
}
# a single experiment can be run
experiment()

## [1] 3
using replicate
replicate(10,experiment())

## [1] 2 3 3 2 2 3 3 1 3 2
res <- replicate(1000,experiment())
sum(res>=3)/1000

## [1] 0.747
increasing accuracy
res <- replicate(10000,experiment())
sum(res>=3)/10000

## [1] 0.7243
what if i ask what’s the mean number of skills in a committee of 4?
# res is storing the number of skills
mean(res)

## [1] 2.8335

2
moving to section 5.4 n. 1 you pay a cost of 100 to get a random revenue of (1000,100,1)
with probalities 1/6,2/6,3/6. what’s the average gain?
revenues <- sample(c(1000,100,1),1000,rep=T)
# gain is revenues-cost
mean(revenues)-100

## [1] 250.551
this was wrong! because we forgot to specify the probabilities and R was, by default, using
uniform probabilities
revenues <- sample(c(1000,100,1),10000,rep=T,prob=c(1/6,2/6,3/6))
mean(revenues)-100

## [1] 99.3159
birthday example: how likely that there is a duplicated bithday in a group? it should be
low. . .
56/366

## [1] 0.1530055
simulation: i’ll define one single experiment
days <- 1:366
# assuming 10 people
birthdays <- sample(days,10,rep=T)
birthdays

## [1] 93 74 144 105 195 256 93 355 252 259


duplicated(birthdays)

## [1] FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE
sum(duplicated(birthdays))

## [1] 1
now, i can define a function with a single experiment
experiment <- function(n) {#n is the number of people in the group
birthdays <- sample(days,n,rep=T)
sum(duplicated(birthdays))
}
experiment(10)

## [1] 0
# now, let's try with n=56
experiment(56)

3
## [1] 1
using replicate
res <- replicate(1000,experiment(56))
sum(res>=1)/1000

## [1] 0.991
99% of times there will be a common birthday in a group of 56!
what if you want to know the previus probability for all n? (say n=10 or n=30. . . ) i’m
defining a function of the size n providing the probability of a common birthday
comple <- function(n){
days <- 1:366
res <- replicate(1000,experiment(n))
sum(res>=1)/1000
}
comple(56)

## [1] 0.992
comple(10) # under the hood there are lots of computations

## [1] 0.118
comple(30)

## [1] 0.697
yet another advanced R function sapply stands for simply apply: sapply(x,function) applies
the function to all the items of of x and outputs a vector with the results. Below we apply
“comple” to a vector with the sizes (of the groups) and then we prot the probability.
x <- 5:100
y <-sapply(x,comple)
y

## [1] 0.018 0.039 0.046 0.074 0.095 0.118 0.133 0.183 0.171 0.228 0.256
## [12] 0.274 0.309 0.334 0.378 0.381 0.434 0.461 0.480 0.536 0.548 0.581
## [23] 0.622 0.661 0.685 0.710 0.713 0.751 0.777 0.799 0.849 0.832 0.826
## [34] 0.878 0.876 0.901 0.899 0.920 0.927 0.932 0.947 0.945 0.954 0.962
## [45] 0.960 0.972 0.980 0.978 0.981 0.987 0.986 0.988 0.993 0.996 0.989
## [56] 0.995 0.996 0.998 0.998 1.000 0.998 0.998 1.000 0.999 0.999 1.000
## [67] 1.000 1.000 1.000 0.998 1.000 0.999 1.000 1.000 1.000 1.000 1.000
## [78] 1.000 0.999 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000
## [89] 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000

4
plot(x,y,xlab="x -Size of group",ylab="Prob of a common birthday")

1.0
Prob of a common birthday

0.8
0.6
0.4
0.2
0.0

20 40 60 80 100

x −Size of group

You might also like