0% found this document useful (0 votes)
6 views

Pyth

The documents model the spread of diseases using compartmental models from epidemiology. Different models like SEIR, SIS, SIR are defined using systems of differential equations and their solutions are plotted over time to show how the disease propagates through a population comprising of subgroups like susceptible, exposed, infected and recovered individuals.

Uploaded by

Othniel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Pyth

The documents model the spread of diseases using compartmental models from epidemiology. Different models like SEIR, SIS, SIR are defined using systems of differential equations and their solutions are plotted over time to show how the disease propagates through a population comprising of subgroups like susceptible, exposed, infected and recovered individuals.

Uploaded by

Othniel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

1 beta = 0.

01
2 gamma = 0 . 1
3 epsilon = 0.1
4 kappa = 0 . 4
5 S0 = 50
6 E0 = 0
7 I0 = 1
8 R0 = 0
9
10 TF = 50
11
12 # D e f i n i r e l modelo
13 d e f SEIR (X, t ) :
14 S = X[ 0 ]
15 E = X[ 1 ]
16 I = X[ 2 ]
17 R = X[ 3 ]
18 dSdt = −b e t a ∗S∗ ( I+kappa ∗E)
19 dEdt = b e t a ∗S∗ ( I+kappa ∗E)−e p s i l o n ∗E
20 d I d t = e p s i l o n ∗E −gamma∗ I
21 dRdt = gamma∗ I
22 dXdt = [ dSdt , dEdt , dIdt , dRdt ]
23 r e t u r n dXdt
24 t = np . l i n s p a c e ( 0 ,TF, 5 0 0 )
25 X0 = [ S0 , E0 , I0 , R0 ]
26 SOL = o d e i n t ( SEIR , X0 , t ) # RESUELVE EL SISTEMA DE EDO
27 # PLOTEAR
28
29 p l t . f i g u r e ( f i g s i z e =(10 ,5) )
30
31 S = SOL [ : ,0]
32 E = SOL [ : ,1]
33 I = SOL [ : ,2]
34 R = SOL [ : ,3]
35
36 plt . p l o t ( t , S , c o l o r= ’ b l u e ’ , l a b e l= ’ S u s c e p t i b l e s ’ )
37 plt . p l o t ( t , E , c o l o r= ’ o r a n g e ’ , l a b e l= ’ E x p u e s t o s ’ )
38 plt . p l o t ( t , I , c o l o r= ’ r e d ’ , l a b e l= ’ I n f e c t a d o s ’ )
39 plt . p l o t ( t , R, c o l o r= ’ g r e e n ’ , l a b e l= ’ Recuperados ’ )
40 plt . t i t l e ( ’ Modelo SEIR ’ , f o n t w e i g h t=” b o l d ” )
41 plt . x l a b e l ( ” tiempo ” , f o n t w e i g h t=” b o l d ” )
42 plt . y l a b e l ( ” I n d i v i d u o s ” , f o n t w e i g h t=” b o l d ” )
43 plt . legend ()
44 plt . grid ()

1 beta = 0.01
2 gamma = 0 . 1
3 S0 = 50
4 I0 = 1
5
6 TF = 50
7
8 # D e f i n i r e l modelo
9 d e f SIS (X, t ) :
10

1
11 S = X[ 0 ]
12 I = X[ 1 ]
13
14 dSdt = −b e t a ∗S∗ I+gamma∗ I
15 d I d t = b e t a ∗S∗ I−gamma∗ I
16
17 dXdt = [ dSdt , d I d t ]
18 r e t u r n dXdt
19
20 t = np . l i n s p a c e ( 0 ,TF, 5 0 0 )
21
22 X0 = [ S0 , I 0 ]
23
24 SOL = o d e i n t ( SIS , X0 , t ) # RESUELVE EL SISTEMA DE EDO
25
26 # PLOTEAR
27 p l t . f i g u r e ( f i g s i z e =(10 ,5) )
28
29 S = SOL [ : , 0 ]
30 I = SOL [ : , 1 ]
31
32 p l t . p l o t ( t , S , c o l o r= ’ b l u e ’ , l a b e l= ’ S u s c e p t i b l e s ’ )
33 p l t . p l o t ( t , I , c o l o r= ’ r e d ’ , l a b e l= ’ I n f e c t a d o s ’ )
34
35 plt . t i t l e ( ’ Modelo SIS ’ , f o n t w e i g h t=” b o l d ” )
36 plt . x l a b e l ( ” tiempo ” , f o n t w e i g h t=” b o l d ” )
37 plt . y l a b e l ( ” I n d i v i d u o s ” , f o n t w e i g h t=” b o l d ” )
38 plt . legend ()
39 plt . grid ()

1 S0 = 100 # S u s c e p t i b l e
2 I0 = 1 # Infectada
3 R0 = 0 # R e t i r a d a / Recuperada
4
5 # Modelo SIR con d i n a m i c a s v i t a l e s
6 Lambda = 0 # [ i n d / d i a ] e n t r a d a de i n d i v i d u o s a l s i s t e m a
7 b e t a = 0 . 8 # Parametro de c o n t a g i o
8 gamma = 0 . 2 # t a s a de r e c u p e r a c i o n
9 d e l t a = 0 # t a s a de muerte d e b i d o a l a enfermedad
10 mu = 0 # t a s a de m o r t a l i d a d n a t u r a l
11
12 N = 50 # Numero de d i a s ( 2 meses )
13 t = r a n g e (N+1)
14 # t=np . l i n s p a c e ( 1 , 1 0 , 1 0 )
15 S = np . z e r o s ( l e n ( t ) )
16 I = np . z e r o s ( l e n ( t ) )
17 R = np . z e r o s ( l e n ( t ) )
18 # # Model
19 S [ 0 ] = S0
20 I [ 0 ] = I0
21 R [ 0 ] = R0
22 f o r n i n t [ 1 : ] : # n e s hoy e n t o n c e s n−1 e s a y e r
23 S [ n ] = S [ n−1] + Lambda − b e t a ∗S [ n−1] ∗ I [ n −1]/( S [ n−1]+ I [ n−1]+
R[ n −1]) − mu∗S [ n−1]
24 I [ n ] = I [ n−1] + b e t a ∗S [ n−1] ∗ I [ n −1]/( S [ n−1]+ I [ n−1]+R[ n −1]) −

2
(mu+d e l t a+gamma) ∗ I [ n−1]
25 R[ n ] = R[ n−1] + gamma∗ I [ n−1] − mu∗R[ n−1]
26
27 plt . f i g u r e ( f i g s i z e =(10 ,5) )
28 plt . s c a t t e r ( t , S , c o l o r= ’ b l u e ’ , l a b e l= ’ S u s c e p t i b l e s ’ )
29 plt . s c a t t e r ( t , I , c o l o r= ’ r e d ’ , l a b e l= ’ I n f e c t a d o s ’ )
30 plt . s c a t t e r ( t , R, c o l o r= ’ g r e e n ’ , l a b e l= ’ Recuperados ’ )
31 plt . t i t l e ( ’ Modelo SIR d i s c r e t o ’ , f o n t w e i g h t=” b o l d ” )
32 plt . x l a b e l ( ” tiempo ” , f o n t w e i g h t=” b o l d ” )
33 plt . y l a b e l ( ” I n d i v i d u o s ” , f o n t w e i g h t=” b o l d ” )
34 plt . legend ()
35 plt . grid ()

1 beta1 = 0.01
2 gamma1 = 0 . 1
3 b1 = 0
4 d1 = 0
5 S01 = 50
6 I01 = 1
7
8 R01 = 0
9 beta2 = 0.02
10 gamma2 = 0 . 1
11 b2 = 0
12 d2 = 0
13 S02 = 50
14 I02 = 1
15 R02 = 0
16 p11 = 24/24
17 p12 = 0/24
18 p21 = 1/24
19 p22 = 23/24
20 TF = 50
21 # MATRIZ DE TIEMPOS DE RESIDENCIA (PROPORCION)
22 P = [ [ p11 , p12 ] , [ p21 , p22 ] ]
23 # D e f i n i r e l modelo
24 d e f SIR12 (X, t ) :
25 S1 = X [ 0 ]
26 S2 = X [ 1 ]
27 I1 = X[ 2 ]
28 I2 = X[ 3 ]
29 R1 = X [ 4 ]
30 R2 = X [ 5 ]
31 dS1dt = b1−d1 ∗S1−b e t a 1 ∗ S1 ∗ p11 ∗ ( p11 ∗ I 1+p21 ∗ I 2 )−b e t a 2 ∗ S1 ∗ p12 ∗
( p12 ∗ I 1+p22 ∗ I 2 )
32 d I 1 d t = −d1 ∗ I 1+b e t a 1 ∗ S1 ∗ p11 ∗ ( p11 ∗ I 1+p21 ∗ I 2 )+b e t a 2 ∗ S1 ∗ p12 ∗ (
p12 ∗ I 1+p22 ∗ I 2 )−gamma1∗ I 1
33 dR1dt = −d1 ∗R1+gamma1∗ I 1
34 dS2dt = b2−d2 ∗S2−b e t a 1 ∗ S2 ∗ p21 ∗ ( p11 ∗ I 1+p21 ∗ I 2 )−b e t a 2 ∗ S2 ∗ p22 ∗
( p12 ∗ I 1+p22 ∗ I 2 )
35 d I 2 d t = −d2 ∗ I 2+b e t a 1 ∗ S2 ∗ p21 ∗ ( p11 ∗ I 1+p21 ∗ I 2 )+b e t a 2 ∗ S2 ∗ p22 ∗ (
p12 ∗ I 1+p22 ∗ I 2 )−gamma2∗ I 2
36 dR2dt = −d2 ∗R2+gamma2∗ I 2
37 dXdt = [ dS1dt , dS2dt , dI1dt , dI2dt , dR1dt , dR2dt ]
38 r e t u r n dXdt

3
39 t = np . l i n s p a c e ( 0 ,TF, 5 0 0 )
40 X0 = [ S01 , S02 , I01 , I02 , R01 , R02 ]
41 SOL12 = o d e i n t ( SIR12 , X0 , t ) # RESUELVE EL SISTEMA DE EDO
42
43 # PLOTEAR
44
45 #f i g , ax = p l t . s u b p l o t s ( 1 , 2 )
46 p l t . f i g u r e ( f i g s i z e =(10 ,3) )
47 S1 = SOL12 [ : , 0 ]
48 S2 = SOL12 [ : , 1 ]
49 I 1 = SOL12 [ : , 2 ]
50 I 2 = SOL12 [ : , 3 ]
51 R1 = SOL12 [ : , 4 ]
52 R2 = SOL12 [ : , 5 ]
53 p l t . subplot (1 , 2 , 1)
54 p l t . p l o t ( t , S1 , c o l o r= ’ b l u e ’ , l a b e l= ’ S u s c e p t i b l e s ’)
55 p l t . p l o t ( t , I1 , c o l o r= ’ r e d ’ , l a b e l= ’ I n f e c t a d o s ’ )
56 p l t . p l o t ( t , R1 , c o l o r= ’ g r e e n ’ , l a b e l= ’ Recuperados ’)
57 p l t . t i t l e ( ’ Modelo SIR m e t a p o b l a c i o n a l ( Zona 1 ) ’ , f o n t w e i g h t=”
bold ” )
58 p l t . x l a b e l ( ” tiempo ” , f o n t w e i g h t=” b o l d ” )
59 p l t . y l a b e l ( ” I n d i v i d u o s ” , f o n t w e i g h t=” b o l d ” )
60 plt . grid ()
61 p l t . subplot (1 , 2 , 2)
62 p l t . p l o t ( t , S2 , c o l o r= ’ b l u e ’ , l a b e l= ’ S u s c e p t i b l e s ’)
63 p l t . p l o t ( t , I2 , c o l o r= ’ r e d ’ , l a b e l= ’ I n f e c t a d o s ’ )
64 p l t . p l o t ( t , R2 , c o l o r= ’ g r e e n ’ , l a b e l= ’ Recuperados ’)
65 p l t . t i t l e ( ’ Modelo SIR m e t a p o b l a c i o n a l ( Zona 2 ) ’ , f o n t w e i g h t=”
bold ” )
66 p l t . x l a b e l ( ” tiempo ” , f o n t w e i g h t=” b o l d ” )
67 p l t . y l a b e l ( ” I n d i v i d u o s ” , f o n t w e i g h t=” b o l d ” )
68 plt . grid ()

1 im po rt m a t p l o t l i b . p y p l o t a s p l t
2 from s c i p y . i n t e g r a t e i mpo rt o d e i n t
3 im po rt numpy a s np
4
5 # MODELO SEAIR CON 2 METAPOBLACIONES
6
7 # Condiciones i n i c i a l e s
8
9 N01 = 50
10
11 J01 = 1
12 A01 = 0
13 E01 = 0 #PREGUNTA
14 R01 = 0
15 F01 = 0
16 S01 = N01−E01−J01−A01−R01−F01
17 N02 = 50
18 J02 = 1
19 A02 = 0
20 E02 = 0
21 R02 = 0
22 F02 = 0

4
23 S02 = N02−E02−J02−A02−R02−F02
24 # Parametros
25 b e t a = 0 . 5 #Tasa de c o n t a g i o ( p r o p o r c i o n )
26 d e l t a = 0 #F a c t o r de d i s m i n u c i o n de c o n t a g i o s con i n d i v i d u o s
asintomaticos
27 e p s i l o n = 1/5 #Tasa de i n c u b a c i o n ( 6 +− 1 en promedio , maximo
14)
28 sigma = 0 #P r o p o r c i o n de i n d i v i d u o s a s i n t o m a t i c o s
29 gamma = 1/14 #Tasa de r e c u p e r a c i o n
30 mu = 0 #Tasa de l e t a l i d a d (2 ,5% +− 0 ,5%)
31
32 TF = 100
33
34 #Parametros de c o n t r o l fantasma
35 a l p h a 1 f = 0 #C o n t r o l zona n o r t e
36 a l p h a 2 f = 0 #C o n t r o l zona s u r
37 p 1 1 f = 20/24
38 p 2 2 f = 20/24
39 p 1 2 f = 1−p 1 1 f
40 p 2 1 f = 1−p 2 2 f
41
42 d e f SEAIR(X, t , alpha1 , alpha2 , p11 , p22 ) :
43
44 S1 = X [ 0 ]
45 S2 = X [ 1 ]
46 E1 = X [ 2 ]
47 E2 = X [ 3 ]
48 A1 = X [ 4 ]
49 A2 = X [ 5 ]
50 J1 = X [ 6 ]
51 J2 = X [ 7 ]
52 R1 = X [ 8 ]
53 R2 = X [ 9 ]
54 F1 = X [ 1 0 ]
55 F2 = X [ 1 1 ]
56 p12 = 1−p11
57 p21 = 1−p22
58 V1 = d e l t a ∗A1+J1
59 V2 = d e l t a ∗A2+J2
60 N1 = S1 + E1 + A1 + J1 + R1 + F1
61 N2 = S2 + E2 + A2 + J2 + R2 + F2
62 dS1dt = −b e t a ∗(1− a l p h a 1 ) ∗ S1 ∗ p11 ∗ ( ( V1∗ p11+V2∗ p21 ) / (N1∗ p11+N2
∗ p21 ) )−b e t a ∗(1− a l p h a 2 ) ∗ S1 ∗ p12 ∗ ( ( V1∗ p12+V2∗ p22 ) / (N1∗ p12+N2∗ p22 ) )
63 dE1dt = b e t a ∗(1− a l p h a 1 ) ∗ S1 ∗ p11 ∗ ( ( V1∗ p11+V2∗ p21 ) / (N1∗ p11+N2∗
p21 ) )+b e t a ∗(1− a l p h a 2 ) ∗ S1 ∗ p12 ∗ ( ( V1∗ p12+V2∗ p22 ) / (N1∗ p12+N2∗ p22 ) )−
e p s i l o n ∗E1
64 dA1dt = sigma ∗ e p s i l o n ∗E1−gamma∗A1
65 dJ1dt = (1− sigma ) ∗ e p s i l o n ∗E1−gamma∗ J1−mu∗ J1
66 dR1dt = gamma∗A1+gamma∗ J1
67 dF1dt = mu∗ J1
68 dS2dt = −b e t a ∗(1− a l p h a 1 ) ∗ S2 ∗ p21 ∗ ( ( V1∗ p11+V2∗ p21 ) / (N1∗ p11+N2
∗ p21 ) )−b e t a ∗(1− a l p h a 2 ) ∗ S2 ∗ p22 ∗ ( ( V1∗ p12+V2∗ p22 ) / (N1∗ p12+N2∗ p22 ) )
69 dE2dt = b e t a ∗(1− a l p h a 1 ) ∗ S2 ∗ p21 ∗ ( ( V1∗ p11+V2∗ p21 ) / (N1∗ p11+N2∗
p21 ) )+b e t a ∗(1− a l p h a 2 ) ∗ S2 ∗ p22 ∗ ( ( V1∗ p12+V2∗ p22 ) / (N1∗ p12+N2∗ p22 ) )−
e p s i l o n ∗E2
70 dA2dt = sigma ∗ e p s i l o n ∗E2−gamma∗A2
71 dJ2dt = (1− sigma ) ∗ e p s i l o n ∗E2−gamma∗ J2−mu∗ J2

5
72 dR2dt = gamma∗A2+gamma∗ J2
73 dF2dt = mu∗ J2
74 dXdt = [ dS1dt , dS2dt , dE1dt , dE2dt , dA1dt , dA2dt , dJ1dt ,
dJ2dt , dR1dt , dR2dt , dF1dt , dF2dt ]
75 r e t u r n dXdt
76
77 t = np . l i n s p a c e ( 0 ,TF, TF)
78
79 X0 = [ S01 , S02 , E01 , E02 , A01 , A02 , J01 , J02 , R01 , R02 , F01 ,
F02 ]
80
81 d e f SOL(X, alpha1 , alpha2 , p11 , p22 ) :
82
83 s o l = o d e i n t ( SEAIR , X, t , a r g s =( alpha1 , alpha2 , p11 , p22 ) ) #
RESUELVE EL SISTEMA DE EDO
84 return sol
85 d e f I 1 ( alpha1 , alpha2 , p11 , p22 ) :
86 I 1 = SOL(X0 , alpha1 , alpha2 , p11 , p22 ) [ : , 4 ] + SOL(X0 , alpha1 ,
alpha2 , p11 , p22 ) [ : , 6 ]
87 return I1
88
89 d e f I 2 ( alpha1 , alpha2 , p11 , p22 ) :
90 I 2 = SOL(X0 , alpha1 , alpha2 , p11 , p22 ) [ : , 5 ] + SOL(X0 , alpha1 ,
alpha2 , p11 , p22 ) [ : , 7 ]
91 return I2
92
93 #GRaFICA 1
94
95 f i g , ax = p l t . s u b p l o t s ( f i g s i z e = ( 1 0 , 1 0 ) )
96
97 #Zona 1
98 p l t . subplot (2 , 2 , 1)
99 p l t . a x i s ( [ 0 , TF, 0 , 3 0 ] )
100
101 p l t . p l o t ( t , I 1 ( a l p h a 1 f , a l p h a 2 f , p11f , p 2 2 f ) , c o l o r= ’ r e d ’ , l i n e s t y l e=
’−− ’ , l a b e l=r ’ $ \ a l p h a 1=\a l p h a 2=0\%$ ’ )
102
103 #p l t . p l o t ( t , I 1 ( 0 . 1 , 0 . 1 , p11f , p 2 2 f ) , c o l o r =’ b l a c k ’ , l a b e l=r ’ $ \
a l p h a 1=\a l p h a 2 =10\%$ ’ )
104 p l t . p l o t ( t , I 1 ( 0 . 7 , 0 . 7 , p11f , p 2 2 f ) , c o l o r= ’ g r e e n ’ , l a b e l=r ’ $ \
a l p h a 1=\a l p h a 2 =70\%$ ’ )
105 #p l t . p l o t ( t , I 1 ( 0 . 5 , 0 . 5 , p11f , p 2 2 f ) , c o l o r =’ b l u e ’ , l a b e l=r ’ $ \
a l p h a 1=\a l p h a 2 =40\%$ ’ )
106 p l t . p l o t ( t , I 1 ( 0 . 7 , 0 . 2 , p11f , p 2 2 f ) , c o l o r= ’ brown ’ , l a b e l=r ’ $ \
a l p h a 1 =70\%;\ a l p h a 2 =20\%$ ’ )
107 p l t . p l o t ( t , I 1 ( 0 . 2 , 0 . 7 , p11f , p 2 2 f ) , c o l o r= ’ o r a n g e ’ , l a b e l=r ’ $ \
a l p h a 1 =20\%;\ a l p h a 2 =70\%$ ’ )
108
109 plt . t i t l e ( ’ Zona 1 ’ )
110 plt . x l a b e l ( ’ $t$ ( d i a s ) ’ )
111 plt . ylabel ( ’ Infectados ’ )
112 plt . legend ()
113 plt . grid ()
114
115 # Zona 2
116 p l t . subplot (2 , 2 , 2)
117 p l t . a x i s ( [ 0 , TF, 0 , 3 0 ] )

6
118
119 p l t . p l o t ( t , I 2 ( a l p h a 1 f , a l p h a 2 f , p11f , p 2 2 f ) , c o l o r= ’ r e d ’ , l i n e s t y l e=
’−− ’ , l a b e l=r ’ $ \ a l p h a 1=\a l p h a 2=0\%$ ’ )
120 #p l t . p l o t ( t , I 2 ( 0 . 1 , 0 . 1 , p11f , p 2 2 f ) , c o l o r =’ b l a c k ’ , l a b e l=r ’ $ \
a l p h a 1=\a l p h a 2 =10\%$ ’ )
121 p l t . p l o t ( t , I 2 ( 0 . 7 , 0 . 7 , p11f , p 2 2 f ) , c o l o r= ’ g r e e n ’ , l a b e l=r ’ $ \
a l p h a 1=\a l p h a 2 =70\%$ ’ )
122 #p l t . p l o t ( t , I 2 ( 0 . 4 , 0 . 4 , p11f , p 2 2 f ) , c o l o r =’ b l u e ’ , l a b e l=r ’ $ \
a l p h a 1=\a l p h a 2 =40\%$ ’ )
123 p l t . p l o t ( t , I 2 ( 0 . 7 , 0 . 2 , p11f , p 2 2 f ) , c o l o r= ’ brown ’ , l a b e l=r ’ $ \
a l p h a 1 =70\%;\ a l p h a 2 =20\%$ ’ )
124 p l t . p l o t ( t , I 2 ( 0 . 2 , 0 . 7 , p11f , p 2 2 f ) , c o l o r= ’ o r a n g e ’ , l a b e l=r ’ $ \
a l p h a 1 =20\%;\ a l p h a 2 =70\%$ ’ )
125
126 plt . t i t l e ( ’ Zona 2 ’ )
127 plt . x l a b e l ( ’ $t$ ( d i a s ) ’ )
128 plt . ylabel ( ’ Infectados ’ )
129 plt . legend ()
130 plt . grid ()
131
132 plt . tight layout ()
133 p l t . show ( )

1 im po rt numpy a s np
2 im po rt m a t p l o t l i b . p y p l o t a s p l t
3 im po rt x l s x w r i t e r
4 from s c i p y i mpo rt s p a r s e
5 from s c i p y . s p a r s e i mp ort c s r m a t r i x
6 from s c i p y . s p a r s e i mp ort l i l m a t r i x
7 from s c i p y . s p a r s e . l i n a l g im por t s p s o l v e
8 from s c i p y . s p a r s e i mp ort v s t a c k
9 im po rt time
10 im po rt math
11 N=99
12 U=20
13 # Modelo SIR con d i n a m i c a s v i t a l e s
14 b e t a = 0 . 9 /N # 0 . 0 1 Parametro de c o n t a g i o
15 gamma = 0 . 2 # t a s a de r e c u p e r a c i o n
16 d e f SIR ( i , r , a ) :
17 a l p h a=a /10
18 s=N−i −r
19 Sn=0
20 I n=0
21 Rn=0
22 Sn = s − b e t a ∗ s ∗(1− a l p h a ) ∗ i
23 I n = i + b e t a ∗ s ∗(1− a l p h a ) ∗ i − gamma∗ i
24 Rn = r + gamma∗ i
25 I n=math . c e i l ( I n )
26 Rn=i n t (Rn)
27 r e t u r n ( i n t ( Sn ) , i n t ( I n ) , i n t (Rn) )
28
29 def rec ( i , r , a) :
30 S = i n t ( SIR ( i , r , a ) [ 0 ] )
31 I = i n t ( SIR ( i , r , a ) [ 1 ] )
32 R = i n t ( SIR ( i , r , a ) [ 2 ] )

7
33 i f I >=0 and R>=0 and I+R+S!=N:
34 i f I<U:
35 r e t u r n ( 2 ∗ I ) ∗ ∗ 1.2+(N∗ a / 1 0 ) ∗ ∗ 1 . 2
36 else :
37 r e t u r n +1000
38 else :
39 r e t u r n +1000
40 d e f Rd( dn ) :
41 f = np . z e r o s ( 1 0 0 0 0 )
42 f o r i in range (100) :
43 f o r r in range (100) :
44 a = i n t ( dn [ i ∗100+ r ] )
45 f [ i ∗100+ r ] = r e c ( i , r , a )
46 return f
47 d e f f i l a ( a , i , r , L) :
48 f = np . z e r o s ( 1 0 0 0 0 )
49 I = i n t ( SIR ( i , r , a ) [ 1 ] )
50 R = i n t ( SIR ( i , r , a ) [ 2 ] )
51 f [ I ∗100+R]=L
52 return f
53 Id = s p a r s e . i d e n t i t y (10000)
54
55 d e f I LPd ( d , L) :
56 M = l i l m a t r i x ( Id )
57 f o r i in range (100) :
58 f o r r in range (100) :
59 a=i n t ( d [ i ∗100+ r ] )
60 I = i n t ( SIR ( i , r , a ) [ 1 ] )
61 R = i n t ( SIR ( i , r , a ) [ 2 ] )
62 i f i==I and r==R:
63 M[ i ∗100+ r , I ∗100+R] = 1−L
64 else :
65 M[ i ∗100+ r , I ∗100+R]=−L
66 return M
67
68 d e f i t e r P o l i t i c a (L ) :
69 d0=np . z e r o s ( 1 0 0 0 0 )
70 c o n v e r g e n c i a 0 =10000
71 dn=np . z e r o s ( 1 0 0 0 0 )
72 p a r a r=0
73 n=0
74
75 w h i l e ( p a r a r ==0) :
76 t 0 = time . time ( )
77 dn = np . z e r o s ( 1 0 0 0 0 )
78 I LPd0 = c s r m a t r i x ( I LPd ( d0 , L) )
79 R d0 = Rd( d0 )
80
81 V = s p s o l v e ( I LPd0 , R d0 )
82 f o r i in range (100) :
83 f o r r in range (100) :
84 v a l = np . z e r o s ( 1 0 )
85 f o r a in range (10) :
86 v a l [ a ]= r e c ( i , r , a ) + np . dot ( f i l a ( a , i , r , L
) , V)
87 vMin = np . min ( v a l )
88 i f v a l [ i n t ( d0 [ i ∗100+ r ] ) ]==vMin :

8
89 dn [ i ∗100+ r ]= i n t ( d0 [ i ∗100+ r ] )
90 else :
91 dn [ i ∗100+ r ]= i n t ( np . argmin ( v a l ) )
92 c o n v e r g e n c i a = l e n ( np . n o n z e r o ( np . a r r a y ( d0 )−np . a r r a y
( dn ) ) [ 0 ] )
93 t f=time . time ( )
94 #p r i n t ( c o n v e r g e n c i a , t f −t 0 )
95
96 i f ( d0==dn ) . a l l ( ) o r c o n v e r g e n c i a >=c o n v e r g e n c i a 0 :
97 parar = 1
98
99 d0=dn
100 c o n v e r g e n c i a 0=c o n v e r g e n c i a
101 n+=1
102 p r i n t ( ’ numero de i t e r a c i o n e s ’ , n )
103 r e t u r n ( d0 )
104
105 d e f s i m u l a c i o n ( I0 , R0 , d ) :
106
107 S = np . z e r o s ( d i a s + 1)
108 I = np . z e r o s ( d i a s + 1)
109 R = np . z e r o s ( dias + 1)
110 A = np . zeros ( dias )
111
112 # # Model
113 S [ 0 ] = N−I0−R0
114 I [ 0 ] = I0
115 R [ 0 ] = R0
116
117 f o r n in range (1 , d i a s + 1) :
118 i=i n t ( I [ n −1])
119 r=i n t (R[ n −1])
120 a=i n t ( d [ i ∗100+ r ] )
121 A[ n−1]=a
122 ( Sn , In , Rn)=SIR ( i , r , a )
123 S [ n]= i n t ( Sn )
124 I [ n]= i n t ( I n )
125 R[ n]= i n t (Rn)
126
127 r e t u r n ( S , I , R,A)
128
129 d e f f i g S I S e s t p o l ( I0 , R0 ,D) :
130
131 ( S , I , R,A)=s i m u l a c i o n ( I0 , R0 ,D)
132 p l t . f i g u r e ( f i g s i z e =(15 ,5) )
133
134 p l t . subplot (1 , 2 , 1)
135 p l t . s c a t t e r ( t , I , c o l o r= ’ r e d ’ , l a b e l= ’ I n f e c t a d o s ’ )
136 p l t . s c a t t e r ( t , [U f o r i i n t ] , c o l o r= ’ p u r p l e ’ ,
l i n e s t y l e= ’−− ’ , l a b e l= ’ L i m i t e UCI ’ )
137 p l t . t i t l e ( ’ Modelo SIR e s t o c a s t i c o o p t i m i z a d o con
a l g o r i t m o IP ’ , f o n t w e i g h t=” b o l d ” )
138 p l t . x l a b e l ( ” tiempo ” , f o n t w e i g h t=” b o l d ” )
139 p l t . y l a b e l ( ” I n d i v i d u o s I n f e c t a d o s ” , f o n t w e i g h t=” b o l d ” )
140 plt . legend ()
141 plt . grid ()
142

9
143 p l t . subplot (1 , 2 , 2)
144 p l t . p l o t ( r a n g e ( 1 , d i a s +1) ,A, c o l o r= ’ b l u e ’ )
145 p l t . t i t l e ( ’ D e c i s i o n e s a p a r t i r de l a p o l i t i c a a r r o j a d a
por e l a l g o r i t m o IP ’ , f o n t w e i g h t=” b o l d ” )
146 p l t . x l a b e l ( ” tiempo ” , f o n t w e i g h t=” b o l d ” )
147 p l t . y l a b e l ( ” P o l i t i c a a p l i c a d a ” , f o n t w e i g h t=” b o l d ” )
148 plt . grid ()
149 return

1 im po rt m a t p l o t l i b . p y p l o t a s p l t
2 from s c i p y . i n t e g r a t e i mpo rt o d e i n t
3 im po rt numpy a s np
4 im po rt pandas a s pd
5 im po rt time
6
7 #Parametros
8 b e t a =0.3/N
9 gamma=1/7
10
11 N=100
12 TF=100
13 X0=[S0 , I 0 ]
14
15 t = np . l i n s p a c e ( 0 ,TF, TF)
16
17 d e f S I S d i s ( Ik , c o n t r o l ) :
18
19 Sk=N−I k
20 S=Sk−b e t a ∗Sk∗(1− c o n t r o l ) ∗ I k+gamma∗ I k
21 I=I k+b e t a ∗Sk∗(1− c o n t r o l ) ∗ Ik−gamma∗ I k
22 return S , I
23
24 def SIS est (S , control ) :
25
26 I=N−S
27 lambd = b e t a ∗(1− c o n t r o l ) ∗ I
28 i f r a c = 1 . 0 − np . exp(−lambd )
29 r f r a c = 1 . 0 − np . exp(−gamma)
30 i n f e c t i o n = np . random . b i n o m i a l ( S , i f r a c ) # Moving from S t o
I
31 r e c o v e r y = np . random . b i n o m i a l ( I , r f r a c ) # Moving from I t o S
32 r e t u r n [ i n t ( S−i n f e c t i o n+r e c o v e r y ) , i n t ( I+i n f e c t i o n −r e c o v e r y )
]
33 U=30
34
35 E s t a d o s = [ a f o r a i n r a n g e (N) ]
36
37 A = [0 ,0.2 ,0.4 ,0.6 ,0.8]
38
39 def trad ( a ) :
40
41 return i n t ( a∗ 10/2)
42
43 def recEsp (S , a ) :
44

10
45 I=N−S
46 lambd = b e t a ∗(1−a ) ∗ I /N
47 i f r a c = 1 . 0 − np . exp(−lambd )
48 r f r a c = 1 . 0 − np . exp(−gamma)
49 i n f e c t i o n = S∗ i f r a c
50 recovery = I ∗ rfrac
51 S=S−i n f e c t i o n+r e c o v e r y
52 I=I+i n f e c t i o n −r e c o v e r y
53 i f S>=0 and I >=0:
54 i f I<U:
55 r e t u r n ( 2 ∗ I ) ∗ ∗ 1.2+(N∗ a ) ∗ ∗ 1 . 2
56 else :
57 r e t u r n 1000
58 else :
59 r e t u r n 5000
60 R = [ [ r e c E s p ( S , a ) f o r a i n A] f o r S i n E s t a d o s ]
61
62 d e f pcond ( j , S , a ) :
63
64 M = [ 0 f o r a in range (1000) ]
65 I=N−S
66 lambd = b e t a ∗(1−a ) ∗ I /N
67 i f r a c = 1 . 0 − np . exp(−lambd )
68 r f r a c = 1 . 0 − np . exp(−gamma)
69 f o r a in range (1000) :
70 i n f e c t i o n = np . random . b i n o m i a l ( S , i f r a c ) # Moving from S
to I
71 r e c o v e r y = np . random . b i n o m i a l ( I , r f r a c )
72 M[ a ]= i n t ( S−i n f e c t i o n+r e c o v e r y )
73 f r e c = M. count ( j )
74 r e t u r n f r e c /1000
75 PC = [ [ [ pcond ( j , S , a ) f o r a i n A] f o r S i n E s t a d o s ] f o r j i n
Estados ]
76
77 d e f Rd( dn ) :
78 f = np . z e r o s ( 1 0 0 )
79 f o r s in Estados :
80 a = i n t ( dn [ s ] )
81 f [ s ] = recEsp ( s , a )
82 return f
83
84 d e f I l P d ( l , dn ) :
85 M=np . z e r o s ( ( 1 0 0 , 1 0 0 ) )
86 f o r s in Estados :
87 a=dn [ s ]
88 f o r j in Estados :
89 M[ s ] [ j ]= l ∗PC [ j ] [ s ] [ t r a d ( a ) ]
90 I d=np . i d e n t i t y ( 1 0 0 )
91 r e t u r n Id−M
92 def f i l a (a , s , l ) :
93 f = np . z e r o s ( 1 0 0 )
94 f o r j in range (100) :
95 f [ j ]= l ∗PC [ j ] [ s ] [ t r a d ( a ) ]
96 return f
97
98 def i t e r P o l i t i c a ( l ) :
99 d0=np . z e r o s ( 1 0 0 )

11
100 c o n v e r g e n c i a 0 =10000
101 dn=np . z e r o s ( 1 0 0 )
102 p a r a r=0
103 n=0
104 w h i l e ( p a r a r ==0) :
105
106 t 0 = time . time ( )
107 dn = np . z e r o s ( 1 0 0 )
108 I l P d 0 = I l P d ( l , d0 )
109 R d0 = Rd( d0 )
110
111 V = np . l i n a l g . s o l v e ( I l P d 0 , R d0 )
112
113 f o r s in range (100) :
114 v a l = np . z e r o s ( 5 )
115 f o r a i n A:
116
117 v a l [ t r a d ( a ) ]= r e c E s p ( s , a ) + np . dot ( f i l a ( a , s , l ) ,
V)
118 vMin = np . min ( v a l )
119 i f v a l [ t r a d ( d0 [ s ] ) ]==vMin :
120 dn [ s ]= d0 [ s ]
121 else :
122 dn [ s ]=np . argmin ( v a l ) /5
123 c o n v e r g e n c i a = l e n ( np . n o n z e r o ( np . a r r a y ( d0 )−np . a r r a y ( dn )
) [0])
124 t f=time . time ( )
125 #p r i n t ( c o n v e r g e n c i a , t f −t 0 )
126
127 i f ( d0==dn ) . a l l ( ) o r c o n v e r g e n c i a <5:
128 parar = 1
129
130 d0=dn
131 c o n v e r g e n c i a 0=c o n v e r g e n c i a
132 n+=1
133 #p r i n t ( ’ numero de i t e r a c i o n e s ’ , n )
134 r e t u r n ( d0 )
135 def f i g S I S e s t p o l 4 ( I0 ,D) :
136 S0=N−I 0
137 S=[0 f o r i i n r a n g e ( 1 0 0 ) ]
138 S [ 0 ] = S0
139 I =[0 f o r i i n r a n g e ( 1 0 0 ) ]
140 I [0]= I0
141 f o r d i n r a n g e ( l e n (D) −1) :
142 c o n t r o l=D[ d ]
143 s , i=S I S e s t ( S [ d ] , c o n t r o l )
144 S [ d+1]= s
145 I [ d+1]= i
146 plt . f i g u r e ( f i g s i z e =(15 ,5) )
147 plt . subplot (1 , 2 , 1)
148 plt . s c a t t e r ( [ i f o r i i n r a n g e (TF) ] , I , c o l o r= ’ r e d ’ , l a b e l= ’
Infectados ’ )
149 plt . s c a t t e r ( t , [U f o r i i n t ] , c o l o r= ’ p u r p l e ’ , l i n e s t y l e= ’−− ’ ,
l a b e l= ’ L i m i t e UCI ’ )
150 plt . t i t l e ( ’ Modelo SIR e s t o c a s t i c o o p t i m i z a d o con a l g o r i t m o IP ’ ,
f o n t w e i g h t=” b o l d ” )
151 plt . x l a b e l ( ” tiempo ” , f o n t w e i g h t=” b o l d ” )

12
152 p l t . y l a b e l ( ” I n d i v i d u o s I n f e c t a d o s ” , f o n t w e i g h t=” b o l d ” )
153 plt . legend ()
154 plt . grid ()
155
156 p l t . subplot (1 , 2 , 2)
157 p l t . p l o t ( t , D, c o l o r= ’ b l u e ’ )
158 p l t . t i t l e ( ’ P o l i t i c a optima con metodo de I t e r a c i o n por P o l i t i c a ’ ,
f o n t w e i g h t=” b o l d ” )
159 p l t . x l a b e l ( ” I n d i v i d u o s I n f e c t a d o s ” , f o n t w e i g h t=” b o l d ” )
160 p l t . y l a b e l ( ” Parametro de c o n t r o l ” , f o n t w e i g h t=” b o l d ” )
161 plt . grid ()
162
163 #p l t . s a v e f i g ( ’FIGURES/ o p t i / 1 0 . png ’ , b b o x i n c h e s =’ t i g h t ’ )
164 return

13

You might also like