-
Notifications
You must be signed in to change notification settings - Fork 0
/
R0_eigenvalue_from_beta.py
88 lines (67 loc) · 2.6 KB
/
R0_eigenvalue_from_beta.py
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
## Author: Anne Ewing
## Date: 07/08/15
## Function: calculate R0 from beta (want R0 to be close to 1.2)
### packages/modules ###
import csv
import sys
import datetime as date
#import networkx as nx
import numpy as np
### local modules ###
sys.path.append('/home/anne/Dropbox/Anne_Bansal_Lab')
# import functions #
import population_parameters as func
# functions #
###################################################
def calc_R_matrix (beta, gamma, alpha):
# calculate R matrix from beta value
# calc contact matrix C
C = func.calc_contact_matrix(filename_germ_contact_data, filename_germ_pop_data, alpha)
# assign components of C matrix
C_cc = C.item((0, 0))
C_ca = C.item((0, 1))
C_ac = C.item((1, 0))
C_aa = C.item((1, 1))
# multiply components of matrix C by alpha or (1-alpha)
mx_11 = (C_cc * alpha)
mx_12 = (C_ca * alpha)
mx_21 = (C_ac * (1 - alpha))
mx_22 = (C_aa * (1 - alpha))
# calc R matrix
# SB needs to check - might be (beta / (beta + gamma))
R = ((beta / gamma) * (np.matrix([[mx_11, mx_12], [mx_21, mx_22]])))
#R = ((beta / (beta + gamma)) * (np.matrix([[mx_11, mx_12], [mx_21, mx_22]])))
return C, R
###################################################
def calc_R0 (R):
#determine R0 from largest eigenvalue of R matrix
# calc eigenvalues from R matrix
# should return in descending order (largest will be first)
evals, matrix_v = np.linalg.eig(R) # matrix_v --> normalized eigenvectors
# largest eigenvalue is R0
R0 = max(evals)
return R0
###################################################
if __name__ == "__main__":
# READ POPULATION DATA FROM FILE
popdata = csv.reader(open('Dropbox/Anne_Bansal_lab/SDI_Data/totalpop_age.csv', 'r'),delimiter = ',')
dict_popdata, ages, years = func.import_popdata(popdata, 0, 1, 2)
dict_childpop, dict_adultpop = func.pop_child_adult (dict_popdata, years)
# READ GERMAN CONTACT DATA FROM FILE
filename_germ_contact_data = 'Dropbox/Anne_Bansal_lab/Contact_Data/polymod_germany_contact_matrix_Mossong_2008.csv'
filename_germ_pop_data = 'Dropbox/Anne_Bansal_lab/UNdata_Export_2008_Germany_Population.csv'
# DEFINE DISEASE PARAMETERS
# if beta/beta+gamma
# if beta/gamma
beta = 0.037
#gamma = 0.2
gamma = 0.5 #changed 090915 based on note on 080315
# CALCULATE ALPHA
year = 2010
alpha = func.calc_alpha(year, dict_childpop, dict_adultpop)
# CALCULATE R MATRIX
C, R = calc_R_matrix (beta, gamma, alpha)
# CALCULATE R0
R0 = calc_R0(R)
print R0
print C