2

I am a python beginner and I have the following problem:

I need to calculate two interdependent values at the same time. What I have so far ist this:

   asset_a = CLOSE_SPX
   asset_b = Close_iBoxx
   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.5)*(df.b.shift(1)*0.5))+abs(1-x)*(df.a.shift(1)*asset_a)
       df.b[i] = x*((df.b.shift(1)*0.5)*(df.a.shift(1)*0.5))+abs(1-x)*(df.b.shift(1)*asset_b)
       i+1

Currently I get the error: ValueError: setting an array element with a sequence. I also know why I am getting this this problem, see: Numpy ValueError: setting an array element with a sequence. This message may appear without the existing of a sequence?

The question is how can I solve this?

The problem is that a and b depend on the previous value of a and b. So we need to calculate them simultaniously while referring to the last calculated values of the loop. It's kind of a hen and egg problem... and there is probably a more efficient solution than a while function...

This is a short example of the dataframe = df used:

              CLOSE_SPX    Close_iBoxx  A_Returns  B_Returns  A_Vola    B_Vola
2014-05-15    1870.85      234.3017    -0.009362   0.003412   0.170535  0.075468   
2014-05-16    1877.86      234.0216     0.003747  -0.001195   0.170153  0.075378
2014-05-19    1885.08      233.7717     0.003845  -0.001068   0.170059  0.075384   
2014-05-20    1872.83      234.2596    -0.006498   0.002087   0.170135  0.075410   
2014-05-21    1888.03      233.9101     0.008116  -0.001492   0.169560  0.075326   
2014-05-22    1892.49      233.5429     0.002362  -0.001570   0.169370  0.075341   
2014-05-23    1900.53      233.8605     0.004248   0.001360   0.168716  0.075333   
2014-05-27    1911.91      234.0368     0.005988   0.000754   0.168797  0.075294   
2014-05-28    1909.78      235.4454    -0.001114   0.006019   0.168805  0.075474   
2014-05-29    1920.03      235.1813     0.005367  -0.001122   0.168866  0.075451   
2014-05-30    1923.57      235.2161     0.001844   0.000148   0.168844  0.075430   
2014-06-02    1924.97      233.8868     0.000728  -0.005651   0.168528  0.075641   
2014-06-03    1924.24      232.9049    -0.000379  -0.004198   0.167852  0.075267
Community
  • 1
  • 1
hb.klein
  • 381
  • 1
  • 4
  • 16
  • 2
    1. please choose your variables name following [Python naming conventions](https://www.python.org/dev/peps/pep-0008/) (i.e. don't start with capital letters, use snake-case not camel-case etc). 2. `i+1` at the end does nothing - it also doesn't make sense to use it inside the loop unless you meant to do: `i += 1`. And 3. give meaningful names to your variables. to me (or anyone other than yourself) all of this doesn't make any sense - your code should be readable and its meaning should be clear to anyone who reads it. Good luck! – Nir Alfasi Jun 03 '15 at 03:49
  • @Paul Rooney: Sorry, we were editing at the same time, I don't know how to return to your version. – hb.klein Jun 03 '15 at 03:56
  • Its ok. The changes were minor. I see you caught the python misspelling. – Paul Rooney Jun 03 '15 at 03:58
  • @ alfasin: hope it is clearer now? – hb.klein Jun 03 '15 at 04:23
  • 1
    You might consider saving a and b in temporary variable. – fsacer Jun 03 '15 at 07:09
  • I get what you mean, but you have to be able to write and access this variable. How would you implement this? – hb.klein Jun 03 '15 at 08:09

0 Answers0