Commit ce2ac31e authored by Mark Penney's avatar Mark Penney
Browse files


Deleted CovidAlertVaccinationModel/data/canada-network-data/POLYMOD/AALPoisPriors.csv, CovidAlertVaccinationModel/data/canada-network-data/POLYMOD/ files
parent 6094d595
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
Created on 2021-03-11
Uses this POLYMOD duration data to deduce priors on the poisson
random variables controlling individual contact durations
@author: mark
import numpy as np
import pandas as pd
import math
from itertools import product
# Read the cleaned survey as a dataframe
Contact = pd.read_csv("AALContact_data.csv")
# Specify AAL categories
Ages = ['Y', 'M', 'O']
SymAge = [('Y','Y'), ('Y', 'M'), ('Y', 'O'), ('M', 'M'),('M', 'O'), ('O','O')]
Locales = ['home', 'workschool', 'rest']
# define row function for symage
def symage_row(row):
agesrc = row['age_source']
agetar = row['age_target']
for symage in SymAge:
#x, y = symage[0], symage[1]
if symage == (agesrc, agetar) or symage == (agetar, agesrc):
return symage
Contact['symage'] = Contact.apply(symage_row, axis=1)
# Dictionary separating contact data by symage-location
ContAAL = {}
columns = ['part_id', 'cont_id', 'age_source', 'age_target',
'duration_multi', 'frequency_multi']
for symage, loc in list(product(SymAge,Locales)):
x,y,z = symage[0], symage[1], loc
C = Contact[Contact['cnt_'+z] == True][Contact['symage']==symage]
ContAAL[(x,y,z)] = C[columns].copy(deep=True)
# Duration distributions stratified by SymAge-location
DurFreqAAL = {}
for symage, loc in list(product(SymAge,Locales)):
x,y,z = symage[0], symage[1], loc
C = ContAAL[(x,y,z)]
Durlist = [C[C['duration_multi']==i]["duration_multi"].count() for i in range(1,6)]
DurFreqAAL[(x,y,z)] = Durlist/np.sum(Durlist)
# Define error functions for poisson random variables
durcutoff = {'home': 6*16, 'workschool': 6*8, 'rest': 6*8}
def PoisArray(lam, loc):
arr = [math.exp(-lam + k*math.log(lam) - np.sum([math.log(n) for n in range(1,k+1)]) )
for k in range(durcutoff[loc])]
return arr/np.sum(arr)
def PoisBin(lam,loc):
Arr = PoisArray(lam,loc)
out = [Arr[0], Arr[1], np.sum(Arr[2:6]), np.sum(Arr[6:24]), np.sum(Arr[24:])]
return out/np.sum(out)
def PoisErr(lam, label): # label is SymAge-Location
agesrc, agetar, loc = label
err = np.sum((PoisBin(lam, loc) - DurFreqAAL[label])**2)
return err
PoisErr = np.vectorize(PoisErr, excluded=[1])
for symage, loc in list(product(SymAge, Locales)):
AAL = (symage[0],symage[1],loc)
arr = [1/PoisErr(i,AAL) for i in range(1,durcutoff[loc])]
zeropad = [0 for i in range(durcutoff['home'] - durcutoff[loc])]
PoisPrior[AAL] = (arr+zeropad)/np.sum(arr)
# Save them to csv
df = pd.DataFrame(list(PoisPrior.items()), columns=['col1','col2'])
dfkeys = pd.DataFrame([pd.Series(x) for x in df.col1])
dfkeys.columns = ["Age_in", "Age_out", "location"]
dfvals = pd.DataFrame([pd.Series(x) for x in df.col2])
dfout = dfkeys.join(dfvals)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment