simulation = function(N, p, rounds){
for (x in 1:rounds){
num_active = rbinom(N, 1, p)
}
return(mean(num_active)/N)
}
N = c(4, 8)
p = c(0.25, 0.5, 0.75)
rounds = 1000
table = matrix(nrow = length(N), ncol = length(p))
for (x in 1:length(N)){
for (y in 1:length(p)){
table[x, y] = simulation(N[x], p[y], rounds)
}
}
print("Avg Fraction of active users in a time period")
## [1] "Avg Fraction of active users in a time period"
colnames(table) = c("p=0.25", "p=0.5", "p=0.75")
rownames(table) = c("N=4", "N=8")
print(table)
## p=0.25 p=0.5 p=0.75
## N=4 0.062500 0.12500 0.25000
## N=8 0.046875 0.09375 0.09375
print("Avg Number of active users in a time period")
## [1] "Avg Number of active users in a time period"
simulation = function(N, p, rounds){
sum_active <- numeric(rounds)
for (x in 1:rounds){
active = rbinom(N, 1, p)
sum_active[x] <- sum(active)
}
return(sum(sum_active)/N)
}
N = c(4, 8)
p = c(0.25, 0.5, 0.75)
rounds = 1000
table = matrix(nrow = length(N), ncol = length(p))
for (x in 1:length(N)){
for (y in 1:length(p)){
table[x, y] = simulation(N[x], p[y], rounds)
}
}
colnames(table) = c("p=0.25", "p=0.5", "p=0.75")
rownames(table) = c("N=4", "N=8")
print(table)
## p=0.25 p=0.5 p=0.75
## N=4 250.25 488.5 740.25
## N=8 248.50 502.5 738.00
simulation = function(N, p, rounds){
num_blocked = numeric(rounds)
for (x in 1:rounds){
num_active = rbinom(N, 1, p)
if (sum(num_active)>2){
rand_num = sample(1:10, N, replace = TRUE)
sorted_rand_num = sort(rand_num)
lowest = sorted_rand_num[1:2]
}
else{
lowest = numeric(0)
}
num_blocked[x] = sum(num_active[-lowest])
}
return(mean(num_blocked)/N)
}
N = c(4, 8)
p = c(0.25, 0.5, 0.75)
rounds = 1000
table = matrix(nrow = length(N), ncol = length(p))
for (x in 1:length(N)){
for (y in 1:length(p)){
table[x, y] = simulation(N[x], p[y], rounds)
}
}
print("Avg Fraction of Blocked Users")
## [1] "Avg Fraction of Blocked Users"
colnames(table) = c("p=0.25", "p=0.5", "p=0.75")
rownames(table) = c("N=4", "N=8")
print(table)
## p=0.25 p=0.5 p=0.75
## N=4 0.02825 0.168250 0.425
## N=8 0.10900 0.372125 0.604
#######################
simulation = function(N, p, rounds){
num_blocked = numeric(rounds)
for (x in 1:rounds){
num_active = rbinom(N, 1, p)
if (sum(num_active)>2){
rand_num = sample(1:10, N, replace = TRUE)
sorted_rand_num = sort(rand_num)
lowest = sorted_rand_num[1:2]
}
else{
lowest = numeric(0)
}
num_blocked[x] = sum(num_active[-lowest])
}
return(sum(num_blocked)/N)
}
table = matrix(nrow = length(N), ncol = length(p))
for (x in 1:length(N)){
for (y in 1:length(p)){
table[x, y] = simulation(N[x], p[y], rounds)
}
}
print("Avg Number of Blocked Users")
## [1] "Avg Number of Blocked Users"
colnames(table) = c("p=0.25", "p=0.5", "p=0.75")
rownames(table) = c("N=4", "N=8")
print(table)
## p=0.25 p=0.5 p=0.75
## N=4 30.5 176.0 427.500
## N=8 106.0 376.5 594.875
The numbers I got from part 2 are generally smaller than the numbers from part 1.