-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw1-2p3-4.R
66 lines (57 loc) · 2.27 KB
/
hw1-2p3-4.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
NUM_OF_EXPS = 100000
find_prob_dead_detected <- function(num_of_experiments) {
total_num_of_dead_detected <- 0
for (count in 1:num_of_experiments) {
sensA_detects_dead <- sample(c(TRUE,FALSE), 1, prob=c(0.8,0.2))
sensB_detects_dead <- sample(c(TRUE,FALSE), 1, prob=c(0.9,0.1))
if (sensA_detects_dead | sensB_detects_dead) {
total_num_of_dead_detected <- total_num_of_dead_detected + 1
}
}
prob_dead_detected <- total_num_of_dead_detected /
num_of_experiments
return(prob_dead_detected)
}
find_prob_dead_detected_by_1_only <- function(num_of_experiments) {
total_num_detected_by_1_only <- 0
for (count in 1:num_of_experiments) {
sensA_detects_dead <- sample(c(TRUE,FALSE), 1, prob=c(0.8,0.2))
sensB_detects_dead <- sample(c(TRUE,FALSE), 1, prob=c(0.9,0.1))
if (xor(sensA_detects_dead, sensB_detects_dead)) {
total_num_detected_by_1_only <- total_num_detected_by_1_only + 1
}
}
prob_dead_detected_by_1_only <- total_num_detected_by_1_only /
num_of_experiments
return(prob_dead_detected_by_1_only)
}
find_prob_locating_dead <- function(num_of_experiments) {
total_num_of_dead_located <- 0
for (count in 1:num_of_experiments) {
sensA_detects_dead <- sample(c(TRUE,FALSE), 1, prob=c(0.8,0.2))
sensB_detects_dead <- sample(c(TRUE,FALSE), 1, prob=c(0.9,0.1))
if (sensA_detects_dead & sensB_detects_dead) {
total_num_of_dead_located <- total_num_of_dead_located + 1
} else {
if (sensA_detects_dead) {
sensA_locates_dead <- sample(c(TRUE,FALSE), 1, prob=c(0.7,0.3))
} else {
sensA_locates_dead <- FALSE
}
if (sensA_detects_dead) {
sensB_locates_dead <- sample(c(TRUE,FALSE), 1, prob=c(0.4,0.6))
} else {
sensB_locates_dead <- FALSE
}
if (sensA_locates_dead | sensB_locates_dead) {
total_num_of_dead_located <- total_num_of_dead_located + 1
}
}
}
prob_locating_dead <- total_num_of_dead_located /
num_of_experiments
return(prob_locating_dead)
}
print(find_prob_dead_detected(num_of_experiments=NUM_OF_EXPS))
print(find_prob_dead_detected_by_1_only(num_of_experiments=NUM_OF_EXPS))
print(find_prob_locating_dead(num_of_experiments=NUM_OF_EXPS))