errors while running a code

import numpy as np
from scipy.integrate import odeint
from scipy.optimize import minimize
import matplotlib.pyplot as plt

Define the ODE system based on your model

def lassa_model(y, t, params):
q, PI, betah, muh, phi0, phie, gammah, alphah, rhoh, di, etae, mue, K, pr, kc, betarh, betaeh = params

S_H, S_L, I_h, R_h, E_n, S_R, I_R = y  # Unpack the state variables

# Define the system of ODEs
dS_H_dt = (1-q)*PI - (betah*I_R + betaeh*(E_n/(kc + E_n)) + betarh*I_R)*S_H - (muh + phi0*phie)*S_H + gammah*R_h
dS_L_dt = q*PI - (1-phi0*phie)*(betah*I_R + betaeh*(E_n/(kc + E_n)) + betarh*I_R)*S_L - muh*S_L + (1-gammah)*alphah*R_h + phi0*phie*S_H
dI_h_dt = (betah*I_R + betaeh*(E_n/(kc + E_n)))*S_H + (1-phi0*phie)*(betah*I_R + betaeh*(E_n/(kc + E_n)))*S_L - (rhoh + muh + di)*I_h
dR_h_dt = rhoh*I_h - (muh + alphah)*R_h
dE_n_dt = etae*I_R - mue*E_n
dS_R_dt = pr*(1 - (S_R/K)) - betarh*I_R*S_R - mue*S_R
dI_R_dt = betarh*I_R*S_R - mue*I_R

return [dS_H_dt, dS_L_dt, dI_h_dt, dR_h_dt, dE_n_dt, dS_R_dt, dI_R_dt]

Function to simulate the model given parameters and time

def simulate_model(params, time):
# Initial conditions
S_H0 = 0.9
S_L0 = 0.05
I_h0 = 0.05
R_h0 = 0
E_n0 = 0.4
S_R0 = 0.3
I_R0 = 0.01
y0 = [S_H0, S_L0, I_h0, R_h0, E_n0, S_R0, I_R0]

# Solve the ODE system
sol = odeint(lassa_model, y0, time, args=(params,))

# Return the infected human population (I_h) for fitting
return sol[:, 2]  # I_h is the third variable

Define the objective function (sum of squared differences)

def objective_function(params, time, data_population):
simulated_population = simulate_model(params, time)
return np.sum((simulated_population - data_population) ** 2)

Time and observed data (replace with your actual data)

data_time = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])
data_population = np.array([5, 4, 4, 9, 5, 9, 9, 3, 8, 12, 17, 11, 16, 21, 53, 81, 83, 66, 96, 109])

Initial guess for parameters (adjust based on your specific model)

initial_params = np.array([0.6, 68000, 0.22, 0.00005, 0.4, 0.025, 0.6, 0.5, 0.0236, 0.2, 0.02, 0.002, 4000, 41, 0.02, 0.3045, 0.22])

Perform optimization to fit the model to the data

result = minimize(objective_function, initial_params, args=(data_time, data_population), method=‘Nelder-Mead’)

Optimized parameters

optimized_params = result.x
print("Optimized parameters: ", optimized_params)

Simulate the model with optimized parameters

fitted_population = simulate_model(optimized_params, data_time)

Plot the fitted model against the observed data

plt.plot(data_time, data_population, ‘o’, label=‘Observed Data’)
plt.plot(data_time, fitted_population, ‘-’, label=‘Fitted Model’)
plt.legend()
plt.xlabel(‘Time’)
plt.ylabel(‘Population’)
plt.title(‘Lassa Fever Model Fitting’)
plt.show()

The vague title is not addressed in this long post.
Please read Getting good answers to your questions. (I would also suggest reading through How do I ask a good question? at the bottom there. ) Please, look at other questions that have good engagement and compare them to yours.

Also consider if this is pertinent to this forum about Jupyter? If you ran this Python code in a more traditional way, such as in the console or as the content a script you called from the command line for Python run, would it give the same outcome? If so, it isn’t a Jupyter issue and so not pertinent to this forum.

1 Like