Advertisement
Guest User

Untitled

a guest
May 21st, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.00 KB | None | 0 0
  1. #C7 b
  2. infectt=function(infected,total=25){
  3.   newinf=infected
  4.   while(infected > 0 & newinf < total){
  5.     infected=infected-1
  6.     count=total-newinf
  7.     while(count > 0){
  8.       count=count-1
  9.       x=runif(1)
  10.       if(x < 0.3) newinf=newinf+1
  11.     }
  12.   }
  13.   return (newinf)
  14. }
  15. uninfectt=function(infected,total=25,uninfect=4){
  16.   x=uninfect
  17.   if(x < infected) return(infected-x)
  18.   return (0)
  19. }
  20. infected_dayss=function(days,total=25,infected=1)
  21. {
  22.   c=0
  23.   while(days > 0){
  24.     days=days-1
  25.     infected=infectt(infected)
  26.     infected=uninfectt(infected)
  27.   }
  28.   return (infected)
  29. }
  30. probb=function(days,k,nrmin=21,total=25,infected=1){
  31.   c=0
  32.   for(i in 1:k){
  33.   x=infected_dayss(days,total,infected)
  34.   if(x >= nrmin) c=c+1
  35.   }
  36.   return (c/k)
  37. }
  38. x=infectt(1)
  39. x
  40. uninfectt(x)
  41. probb(4,1000,nrmin=21)
  42. probb(1,1000,nrmin=1)
  43.  
  44. ```
  45. ```{r}
  46. #C7 a,c
  47. infect=function(comp){
  48.   newcomp=comp
  49.   for(i in 1:length(comp)){
  50.     if(comp[i] == 1){
  51.       for(j in 1:length(comp)){
  52.         if(newcomp[j] == 0){
  53.           x=runif(1)
  54.           if(x < 0.3) newcomp[j] = 1
  55.         }
  56.       }
  57.     }
  58.   }
  59.   return (newcomp)
  60. }
  61. uninfect=function(comp){
  62.   infected=vector()
  63.   c=0
  64.   for(i in 1:length(comp)){
  65.     if(comp[i] == 1) {c=c+1; infected[c]=i;}
  66.   }
  67.   if(c > 4) c=4
  68.   while(c > 0){
  69.     index=runif(1,1,length(infected)+1)
  70.     if(infected[index] > 0){
  71.       comp[infected[index]]=0
  72.       infected[index]=0
  73.       c=c-1
  74.     }
  75.   }
  76.   return (comp)
  77. }
  78. all_infected=function(comp)
  79. {
  80.   wasinf=comp
  81.   ok=2
  82.   while(ok == 2){
  83.     comp=infect(comp)
  84.     for(i in 1:length(comp)){
  85.       if(wasinf[i] == 0 & comp[i] == 1) wasinf[i] = 1
  86.     }
  87.     if(sum(wasinf) == length(wasinf)) ok=1
  88.     comp=uninfect(comp)
  89.     if(sum(comp) == 0) ok=0
  90.   }
  91.   return(ok)
  92. }
  93. prob_all_inf=function(nrcomp=25,nr=1000){
  94.   cmp=vector()
  95.   cmp[1]=1
  96.   for(i in 2:nrcomp) cmp[i]=0
  97.   c=0
  98.   for(i in 1:nr){
  99.     c=c+all_infected(cmp)
  100.   }
  101.   return (c/nr)
  102. }
  103. p=prob_all_inf()
  104. p
  105. N=p*(1-p)*(qnorm(0.005)/0.02)^2
  106. N
  107. prob_all_inf(nr=N)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement