def log_test_rhs(Y, t, beta1_rho, beta2, gammab, v, b, d, dv, dEEE, bc_coeff_mat, bm_coeff_mat, mospop, mosprime, mosin, fun, mos_coeff, p): eps = .001 Y = Y.clip(-20, numpy.inf) s = Y[0:p] i = Y[p:2 * p] sv = Y[-2] iv = Y[-1] alpha_val = alpha_calc(bloodmeal.bloodmeal_function(bm_coeff_mat, t), BirdCount.birdcounts_function(bc_coeff_mat, t)) theta = BirdCount.birdcounts_function( bc_coeff_mat, t) # Theta is the vector of the MCMC sample coeffieicents theta_v = bloodmeal.vector_pop(mos_coeff, t) denom = numpy.dot(theta, alpha_val) lambdab = beta1_rho * v * numpy.exp(iv) * numpy.array( alpha_val) * theta_v / denom lambdav = v * (numpy.dot(beta2 * numpy.exp(i) * theta, alpha_val)) / denom # Bird Mortality #ds = BirdCount.Xi(bc_coeff_mat,t)*((1-eps)*numpy.exp(-s)-1)-lambdab ds = BirdCount.Xi(bc_coeff_mat, t) * ( (1 - eps) * numpy.exp(-s) - 1) - lambdab * numpy.where(s <= -20, 0, 1) di = BirdCount.Xi(bc_coeff_mat, t) * ( eps * numpy.exp(-i) - 1) + lambdab * numpy.exp(s - i) - gammab #dr = gammab*numpy.exp(i-r) - BirdCount.Xi(bc_coeff_mat,t) dsv = bloodmeal.Xi(mos_coeff, t) * numpy.exp(iv - sv) - lambdav div = lambdav * numpy.exp(sv - iv) - bloodmeal.Xi(mos_coeff, t) dY = numpy.hstack((ds, di, dsv, div)) return dY
def test_rhs(Y, t, beta1_rho, beta2, gammab, v, b, d, dv, dEEE, bc_coeff_mat, bm_coeff_mat, mospop, mosprime, mosin, fun, mos_coeff, p): # Here beta1 = beta1/rho_v, where Nv = theta_v/rho_v eps = .001 s = Y[0:p] i = Y[p:2 * p] r = Y[2 * p:3 * p] sv = Y[-2] iv = Y[-1] alpha_val = alpha_calc(bloodmeal.bloodmeal_function(bm_coeff_mat, t), BirdCount.birdcounts_function(bc_coeff_mat, t)) theta = BirdCount.birdcounts_function( bc_coeff_mat, t) # Theta is the vector of the MCMC sample coeffieicents theta_v = bloodmeal.vector_pop(mos_coeff, t) denom = numpy.dot(theta, alpha_val) lambdab = beta1_rho * v * iv * numpy.array(alpha_val) * theta_v / denom lambdav = v * (numpy.dot(beta2 * i * theta, alpha_val)) / denom # Bird Mortality ds = BirdCount.Xi(bc_coeff_mat, t) * ((1 - eps) - s) - lambdab * s di = BirdCount.Xi(bc_coeff_mat, t) * (eps - i) + lambdab * s - gammab * i dr = gammab * i - r * BirdCount.Xi(bc_coeff_mat, t) dsv = bloodmeal.Xi(mos_coeff, t) * iv - lambdav * sv div = lambdav * sv - bloodmeal.Xi(mos_coeff, t) * iv dY = 365. / 2 * numpy.hstack( (ds, di, dr, dsv, div)) # the 365/2 is the rate of change of the time transform return dY
def rhs(Y, t, beta1, beta2, gammab, v, b, d, dv, dEEE, bc_coeff_mat, bm_coeff_mat, mospop, mosprime, mosin, fun, mos_coeff, p): # Consider adding epsilon term , for proportion infected entering population eps = .001 eps = .001 s = Y[0:p] i = Y[p:2 * p] r = Y[2 * p:3 * p] sv = Y[-2] iv = Y[-1] alpha_val = alpha_calc(bloodmeal.bloodmeal_function(bm_coeff_mat, t), BirdCount.birdcounts_function(bc_coeff_mat, t)) N = BirdCount.birdcounts_function(bc_coeff_mat, t) denom = numpy.dot(N, alpha_val) lambdab = beta1 * v * iv * numpy.array(alpha_val) / denom lambdav = v * (numpy.dot(beta2 * i, alpha_val)) / denom # Bird Mortality ds = BirdCount.xi(bc_coeff_mat, t) * (1 - eps) - lambdab * s - BirdCount.F( bc_coeff_mat, t) * s / N di = BirdCount.xi(bc_coeff_mat, t) * eps + lambdab * s - gammab * i - BirdCount.F( bc_coeff_mat, t) * i / N dr = gammab * i - BirdCount.F(bc_coeff_mat, t) * r / N dsv = bloodmeal.vector_in(mos_coeff, t) - lambdav * sv - bloodmeal.fun( mos_coeff, t) * sv #Need to be updated div = lambdav * sv - bloodmeal.fun(mos_coeff, t) * iv # Need to be updated #dSv = mospop(t) - dIv dY = numpy.hstack((ds, di, dr, dsv, div)) return dY
def get_ODE_vals(Y, T, beta1_rho, beta2, gammab, v, b, d, dv, dEEE, bc_coeff_mat, bm_coeff_mat, mospop, mosprime, mosin, fun, mos_coeff, p): ds = [] di = [] dsv = [] div = [] lbdv = [] alpha = [] eps = .001 s = Y[0:p] i = Y[p:2 * p] sv = Y[-2] iv = Y[-1] dt = T[1] - T[0] for t in T: alpha_val = alpha_calc(bloodmeal.bloodmeal_function(bm_coeff_mat, t), BirdCount.birdcounts_function(bc_coeff_mat, t)) theta = BirdCount.birdcounts_function( bc_coeff_mat, t) # Theta is the vector of the MCMC sample coeffieicents theta_v = bloodmeal.vector_pop(mos_coeff, t) denom = numpy.dot(theta, alpha_val) lambdab = beta1_rho * v * iv * numpy.array(alpha_val) * theta_v / denom lambdav = v * (numpy.dot(beta2 * i * theta, alpha_val)) / denom # Bird Mortality ds.append( BirdCount.Xi(bc_coeff_mat, t) * ((1 - eps) - s) - lambdab * s) di.append( BirdCount.Xi(bc_coeff_mat, t) * (eps - i) + lambdab * s - gammab * i) #dr = gammab*numpy.exp(i-r) - BirdCount.Xi(bc_coeff_mat,t) dsv.append(bloodmeal.Xi(mos_coeff, t) * iv - lambdav * sv) div.append(lambdav * sv - bloodmeal.Xi(mos_coeff, t) * iv) alpha.append(alpha_val) s += ds[-1] * dt i += di[-1] * dt sv += dsv[-1] * dt iv += div[-1] * dt lbdv.append(lambdav) return (ds, di, dsv, div, lbdv, alpha)
def run_ode(beta1, rhs_func, bm_coeff_mat, bc_coeff_mat, mos_coeff, tstart, tend, flag): p = len(bm_coeff_mat[:, 0]) + 1 beta2 = 1 gammab = .1 * numpy.ones(p) v = .14 # Biting Rate of Vectors on Hosts b = 0 # Bird "Recruitment" Rate d = 0 # Bird "Death" Rate dv = .10 # Mosquito Mortality Rate dEEE = 0 # Run for ~ 6 Months T = scipy.linspace(tstart, tend, 1001) if flag == 0: Sv = 1 * bloodmeal.vector_pop(mos_coeff, tstart) Iv = 0 * bloodmeal.vector_pop(mos_coeff, tstart) S0 = 1 * BirdCount.birdcounts_function(bc_coeff_mat, tstart) I0 = 0 * BirdCount.birdcounts_function(bc_coeff_mat, tstart) R0 = 0 * BirdCount.birdcounts_function(bc_coeff_mat, tstart) Y0 = numpy.hstack((S0, I0, R0, Sv, Iv)) elif flag == 1: Sv = .99 Iv = .01 S0 = .99 * numpy.ones(p) I0 = .01 * numpy.ones(p) R0 = 0 * numpy.ones(p) Y0 = numpy.hstack((S0, I0, R0, Sv, Iv)) elif flag == 2: print("hi") Sv = numpy.log(.99) Iv = numpy.log(.01) S0 = numpy.log(.99) * numpy.ones(p) I0 = numpy.log(.01) * numpy.ones(p) Y0 = numpy.hstack((S0, I0, Sv, Iv)) Y = scipy.integrate.odeint( rhs_func, Y0, T, args=(beta1, beta2, gammab, v, b, d, dv, dEEE, bc_coeff_mat, bm_coeff_mat, bloodmeal.vector_pop, bloodmeal.vector_derivative, bloodmeal.vector_in, bloodmeal.fun, mos_coeff, p)) return (Y)
def eval_ode_results(Y, bm_coeff_mat, bc_coeff_mat, mos_coeff, tstart, tend, bird_data_file, flag, alpha=1): import pylab birdnames = pd.read_csv(bird_data_file, index_col=0).index name_list = list(birdnames) name_list.append('Vector') T = scipy.linspace(tstart, tend, 1001) p = len(bm_coeff_mat[:, 0]) + 1 s, i, r, sv, iv = get_SIR_vals(Y, p) bc = numpy.zeros((p, len(T))) bm = numpy.zeros((p, len(T))) alpha_val = numpy.zeros((p, len(T))) mos_pop = numpy.zeros(len(T)) for j in range(len(T)): bc[:, j] = BirdCount.birdcounts_function(bc_coeff_mat, T[j]) bm[:, j] = bloodmeal.bloodmeal_function(bm_coeff_mat, T[j]) alpha_val[:, j] = alpha_calc(bm[:, j], bc[:, j]) mos_pop[j] = bloodmeal.vector_pop(mos_coeff, T[j]) sym = ['b', 'g', 'r', 'c', 'm', 'y', 'k', '--', 'g--'] pylab.figure(1) for k in range(p): #if k==p: # pylab.plot(T,mos_pop,sym[k]) #else: pylab.plot(T, bc[k], sym[k], alpha=alpha) pylab.title("Populations") pylab.legend(name_list) N = s + i + r N = numpy.clip(N, 0, numpy.inf) pylab.figure(2) for k in range(p): if flag == 0: temp = numpy.divide(i[:, k], N[:, k]) elif flag == 1: temp = i[:, k] elif flag == 2: temp = numpy.exp(i[:, k]) pylab.plot(T, temp, sym[k], alpha=alpha) pylab.legend(birdnames) pylab.title("Infected Birds") pylab.figure(3) for k in range(p): pylab.plot(T, alpha_val[k], alpha=alpha) pylab.legend(birdnames) pylab.title("Feeding Index Values") return ()
def alpha_test(bm, bc_coeff_mat, tstart, tend): bc_coeff_mat, results = BirdCount.get_birdcounts_sample( "Days_BirdCounts.csv", lin_poiss_log_lik) bm_coeff_mat, results = get_bloodmeal_sample("Days_BloodMeal.csv", loglikelihood) bm = pd.read_csv("Days_BloodMeal.csv", index_col=0) bm = bm.drop("Total", axis=1) bc = pd.read_csv("Days_BirdCounts.csv", index_col=0) bc = bc.drop("Total", axis=1) bm_time = numpy.array([int(x) for x in bm.columns]) bc_time = numpy.array([int(x) for x in bc.columns]) # Setting Start Time to April 1st T = scipy.linspace(tstart, tend, 1001) alpha_val = numpy.zeros(())
def run_ode_solver(beta1, rhs_func, bm_coeff_mat, bc_coeff_mat, mos_coeff, tstart, tend, flag): # Set up to run the ODE using the ode_solver function, which manually integrates the ode for each iteration p = len(bm_coeff_mat[:, 0]) + 1 beta2 = 1 gammab = .1 * numpy.ones(p) v = .14 # Biting Rate of Vectors on Hosts b = 0 # Bird "Recruitment" Rate d = 0 # Bird "Death" Rate dv = .10 # Mosquito Mortality Rate dEEE = 0 # Run for ~ 6 Months T = scipy.linspace(tstart, tend, 1001) if flag == 0: Sv = 1 * bloodmeal.vector_pop(mos_coeff, tstart) Iv = 0 * bloodmeal.vector_pop(mos_coeff, tstart) S0 = 1 * BirdCount.birdcounts_function(bc_coeff_mat, tstart) I0 = 0 * BirdCount.birdcounts_function(bc_coeff_mat, tstart) R0 = 0 * BirdCount.birdcounts_function(bc_coeff_mat, tstart) elif flag == 1: Sv = .99 Iv = .01 S0 = .99 * numpy.ones(p) I0 = .01 * numpy.ones(p) R0 = 0 * numpy.ones(p) Y0 = numpy.hstack((S0, I0, R0, Sv, Iv)) #Y = scipy.integrate.odeint(rhs_func,Y0,T,args = (beta1, beta2, gammab, v, b, d, dv, dEEE, bc_coeff_mat,bm_amean,bm_bmean,bloodmeal.vector_pop,bloodmeal.vector_derivative,bloodmeal.vector_in,bloodmeal.fun,mos_coeff,p)) Y = ode_solver(Y0, T, args=(beta1, beta2, gammab, v, b, d, dv, dEEE, bc_coeff_mat, bm_coeff_mat, bloodmeal.vector_pop, bloodmeal.vector_derivative, bloodmeal.vector_in, bloodmeal.fun, mos_coeff, p)) return (Y)
def findbeta(beta1, rhs_func, bm_coeff_mat, bc_coeff_mat, mos_coeff, tstart, tend, flag, ODE_flag): print beta1 p = len(bm_coeff_mat[:, 0]) + 1 Y = run_ode(beta1, rhs_func, bm_coeff_mat, bc_coeff_mat, mos_coeff, tstart, tend, flag) s, i, r, sv, iv = get_SIR_vals(Y, p) N = s + i + r if ODE_flag == 0: finalrec = numpy.ma.divide(r[-1].sum() + i[-1].sum(), N[-1].sum()) final = finalrec - .13 else: N = BirdCount.birdcounts_function(bc_coeff_mat, tend) r = r[-1] * N i = i[-1] * N finalrec = numpy.ma.divide(r.sum() + i.sum(), N.sum()) final = finalrec - .13 print(numpy.abs(final)) return numpy.abs(final)
if flag == 1: bm_ci = get_ci(bm_results, poly_deg) mos_ci = get_ci(mos_results[0], poly_deg) bc_ci = numpy.zeros((2, 7, poly_deg + 1)) for j in range(7): val = get_ci(bc_results[j], poly_deg) bc_ci[0, j, :] = val[0] bc_ci[1, j, :] = val[1] #plotting the values with the data, and ci values bloodmeal.BloodmealTest(bm_file, bm_coeff_mat, poly_deg) bloodmeal.BloodmealTest(bm_file, bm_ci[0], poly_deg) bloodmeal.BloodmealTest(bm_file, bm_ci[1], poly_deg) BirdCount.BirdcountTest(bc_file, bc_coeff_mat, poly_deg) BirdCount.BirdcountTest(bc_file, bc_ci[0], poly_deg) BirdCount.BirdcountTest(bc_file, bc_ci[1], poly_deg) bloodmeal.MosquitoTest(msq_file, mos_coeff, poly_deg) bloodmeal.MosquitoTest(msq_file, mos_ci[0], poly_deg) bloodmeal.MosquitoTest(msq_file, mos_ci[1], poly_deg) #Calculate beta1 for the data, and the CI'sms def get_beta1(bm_coeff_mat, bc_coeff_mat, mos_coeff): beta1 = scipy.optimize.minimize(Seasonal_ODE.findbeta, .5, args=(rhs_func, bm_coeff_mat, bc_coeff_mat, mos_coeff, tstart, tend, 1,
bm_data = pd.read_csv(bm_file, index_col=0) bm_time = numpy.array([int(x) for x in bm_data.columns]) bm_data = bm_data.as_matrix() tstart = Seasonal_ODE.time_transform(90) # Setting Start Time to April 1st tend = Seasonal_ODE.time_transform(270) flag = 2 write_flag = 0 # If set to 0 will write the results, if set to 1 will not if flag == 0: # This section runs the MCMC for the various areas of interest, and stores the results mos_coeff, mos_results = bloodmeal.vector_coeff( msq_file, MCMC.poly_poiss_log_lik, poly_deg, maxruns=maxruns) bm_coeff_mat, bm_results = bloodmeal.get_bloodmeal_sample( bm_file, MCMC.bm_polynomial_loglikelihood, poly_deg, maxruns=maxruns) bc_coeff_mat, bc_results = BirdCount.get_birdcounts_sample( bc_file, MCMC.poly_poiss_log_lik, poly_deg, maxruns=maxruns) #BirdCount.BirdcountTest(bc_file,bc_coeff_mat,poly_deg) bc_DIC = MCMC.get_DIC(bc_results, MCMC.poly_poiss_log_lik, bc_file) bm_DIC = MCMC.DIC(bm_results, MCMC.bm_polynomial_loglikelihood, bm_data, Seasonal_ODE.time_transform(bm_time)) mos_DIC = MCMC.get_DIC(mos_results, MCMC.poly_poiss_log_lik, msq_file) if write_flag == 0: with open('Mos_coeff_poly_deg(%d).pkl' % poly_deg, 'wb') as output: pickle.dump(mos_coeff, output) with open('bloodmeal_coeff_poly_deg(%d).pkl' % poly_deg, 'wb') as output: pickle.dump(bm_coeff_mat, output) with open('host_coeff_poly_deg(%d).pkl' % poly_deg,