1

So I'm building a SIQRA epidemic model simulation on Python and can't really figure out why my code only works when I add .T after calling my function in the last line:

# A grid of time pofloats
t = np.linspace(0, 10)

# The SIQRA model differential equations
def SIQRA(y, t, N, beta, delta, alfa, sigma, omega, 
          alfaSA, alfaIA, alfaQA):
    S, I, Q, R, A = y
    dSdt = -alfaSA * S * A - beta * S * I + sigma * R + omega * Q
    dIdt = beta * S * I - alfaIA * A * I - delta * I
    dQdt = delta * I - omega * Q - alfaQA * Q - alfa * Q
    dRdt = alfa * Q - sigma * R
    dAdt = alfaSA * S * A + alfaIA * A * I + alfaQA * Q
    return dSdt, dIdt, dQdt, dRdt, dAdt

# Initial conditions vector
y0 = S0, I0, Q0, R0, A0
# Integrate the SIQRA equations over the time grid, t
ret = odeint(SIQRA, y0, t, args=(N, beta,  delta, alfa,
                                  sigma, omega, alfaSA, alfaIA, alfaQA))
S, I, Q, R, A = ret.T

I know it has something to do with the matrix it creates but I can't really figure it out completely.

jared
  • 4,165
  • 1
  • 8
  • 31
  • `S, I, Q, R, A = ret` would only be valid if `ret` was a sequence of exactly five items (this would include a matrix with a first dimension of 5). Apparently it isn't - did you even look at `ret` to see what it actually is? I guess it's something like a 1x5 matrix, so that its transpose is something that can validly be unpacked into five variables. – jasonharper Aug 23 '23 at 01:45
  • What's the error if you don't use the transpose? – hpaulj Aug 23 '23 at 02:45
  • odeint docs says it returns a `shape (len(t), len(y0))`. Your `y0` has 5 terms, which is also what you are trying to unpack. `unpack` works on the 1st dimension, hence the need to transpose. – hpaulj Aug 23 '23 at 02:47
  • @jasonharper I tried looking into ret but it wouldn't even print without the .T – amdkeepsmehot Aug 23 '23 at 03:00
  • @hpaulj too many values to unpack. So basically I'm getting a 5x1 matrix but I need a 1x5? – amdkeepsmehot Aug 23 '23 at 03:01
  • "and can't really figure out why my code only works when I add .T" - well, do you understand *what the `.T` means*? When you write `S, I, Q, R, A =`, what do you suppose are the implications, for the thing that has to be on the right-hand side? How many values should it contain if you loop over it? How many values does `ret` contain if you loop over it? How many values does `ret.T` contain if you loop over it? – Karl Knechtel Aug 23 '23 at 03:22
  • `t` has 10 values (linspace?), So `ret` is (10,5). `ret.T` is (5,10). After unpacking each one of the 5 variables is (10,). Verify for yourself. – hpaulj Aug 23 '23 at 04:23

0 Answers0