-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExp9-4 Edit Solution.R
More file actions
92 lines (75 loc) · 1.7 KB
/
Exp9-4 Edit Solution.R
File metadata and controls
92 lines (75 loc) · 1.7 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#re-edited Mar 2021
#This R repository is for demonstration of algorithms involved in the book
#Mathematical Modeling (4th Edition) written by Prof. Mark. M. Meerschaert
#
#Edited from the original repository on the website of the book before 2018
#https://www.stt.msu.edu/users/mcubed/modeling.html
# Example 9.4: Bombing Run Problem
#
p=0.9*0.5
q=0.6
m=10
N=15
P=seq(1,m+1,1)
PrX=seq(1,m+1,1)
S=0
for (i1 in 1:m+1){
i=i1-1
P[i1]=1-(1-p)^(N-i)
PrX[i1]=dbinom(i, size=m, prob=q)
S=S+P[i1]*PrX[i1]}
S
#-----------------------------------------
rm(list=ls())#clear the variables#NOT ENCOURAGED WAY OF CODING
#Calculate minimum N from a given S
library(Ryacas)
p=0.9*0.5
q=0.6
m=10
Pi<-expression(1-(1-p)^(N-i))
Pi<-as.expression(yacas(Pi))
Pi
N<-0
S<-0
while(S<0.99){
S<-0
N<-N+1
for (i in seq(from=0,to=m,by=1)){
P<-eval({Pi})
B<-dbinom(i, size=m, prob=q)
S<-S+P*B
}
}
remove(i)
N#The minimum N required for S>0.99
#---------------------wrap up a function to calculate S from N
#Calculation of N from S will be time consuming
NfromS2<-function(S.targ){
p=0.9*0.5
q=0.6
m=10
Pi<-function(N,i){
p=0.9*0.5
(1-(1-p)^(N-i))
}
for (k in seq_along(N)){
S<-0
while(S<S.targ[k]){
S<-0
N[k]<-N[k]+1
for (i in seq(from=0,to=m,by=1)){
P<-Pi(N[k],i)
B<-dbinom(i, size=m, prob=q)
S<-S+P*B
}
remove(i)
}
}
N
}
#
#---------------------Sensitivity(S)-------
S<-seq(from = 0.86, to = 1 , by = 0.001)#
N<-sapply(S,NfromS2)
plot(N~S,type = 'l')
#A more efficient way to this Sensitivity Analysis may be loop N for S and plot N~S