I need to calculate two interdependent values at the same time. The problem is that a
and b
depend on the previous value of a
and b
. So we need to calculate them simultaneously while referring to the last calculated values of the loop. What I have so far is this:
x = df.x # just a list containing randomly 1 and 0
df['a']=100 # 100 is just the starting value for a and b and shall be then overwritten by the while loop
df['b']=100
i=1
while i<len(df.index):
df.a[i] = x*((df.a.shift(1)*0.45)+(df.b.shift(1)*0.5))+abs(1-x)*df.a.shift(1)
df.b[i] = x*((df.b.shift(1)*0.5)+(df.a.shift(1)*0.59))+abs(1-x)*df.b.shift(1)
i+1
df is a DataFrame.
Currently I get the error: ValueError: setting an array element with a sequence.
I also know why I am getting this this problem, see this question. The question is, how can I solve this? There is probably a more efficient solution than a while
loop...