Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #C7 b
- infectt=function(infected,total=25){
- newinf=infected
- while(infected > 0 & newinf < total){
- infected=infected-1
- count=total-newinf
- while(count > 0){
- count=count-1
- x=runif(1)
- if(x < 0.3) newinf=newinf+1
- }
- }
- return (newinf)
- }
- uninfectt=function(infected,total=25,uninfect=4){
- x=uninfect
- if(x < infected) return(infected-x)
- return (0)
- }
- infected_dayss=function(days,total=25,infected=1)
- {
- c=0
- while(days > 0){
- days=days-1
- infected=infectt(infected)
- infected=uninfectt(infected)
- }
- return (infected)
- }
- probb=function(days,k,nrmin=21,total=25,infected=1){
- c=0
- for(i in 1:k){
- x=infected_dayss(days,total,infected)
- if(x >= nrmin) c=c+1
- }
- return (c/k)
- }
- x=infectt(1)
- x
- uninfectt(x)
- probb(4,1000,nrmin=21)
- probb(1,1000,nrmin=1)
- ```
- ```{r}
- #C7 a,c
- infect=function(comp){
- newcomp=comp
- for(i in 1:length(comp)){
- if(comp[i] == 1){
- for(j in 1:length(comp)){
- if(newcomp[j] == 0){
- x=runif(1)
- if(x < 0.3) newcomp[j] = 1
- }
- }
- }
- }
- return (newcomp)
- }
- uninfect=function(comp){
- infected=vector()
- c=0
- for(i in 1:length(comp)){
- if(comp[i] == 1) {c=c+1; infected[c]=i;}
- }
- if(c > 4) c=4
- while(c > 0){
- index=runif(1,1,length(infected)+1)
- if(infected[index] > 0){
- comp[infected[index]]=0
- infected[index]=0
- c=c-1
- }
- }
- return (comp)
- }
- all_infected=function(comp)
- {
- wasinf=comp
- ok=2
- while(ok == 2){
- comp=infect(comp)
- for(i in 1:length(comp)){
- if(wasinf[i] == 0 & comp[i] == 1) wasinf[i] = 1
- }
- if(sum(wasinf) == length(wasinf)) ok=1
- comp=uninfect(comp)
- if(sum(comp) == 0) ok=0
- }
- return(ok)
- }
- prob_all_inf=function(nrcomp=25,nr=1000){
- cmp=vector()
- cmp[1]=1
- for(i in 2:nrcomp) cmp[i]=0
- c=0
- for(i in 1:nr){
- c=c+all_infected(cmp)
- }
- return (c/nr)
- }
- p=prob_all_inf()
- p
- N=p*(1-p)*(qnorm(0.005)/0.02)^2
- N
- prob_all_inf(nr=N)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement