-1

I need to implement a recursive formula in my code block which is

bayesian_prices = [] 
for i in range(10):
    q = dayqvalue(i, grid, fullData)
    w = compute_dayweights(q)
    option_data, index_prices, initial_date = get_samedated_opt(fullData, i, 1)
    callput, tmt, ex, mkt =  orderdata(option_data)
    baypr = compute_bayesian_prices(w, grid, callput, tmt, ex, index_prices)
    bayesian_prices.append(baypr)

What I need to do regards the computation of "q". Everytime I need to compute the q as sum of the old value and the new value obtained from the function "dayqvalue". In other words, when i=0 the first list of q values is computed. Then with i=1 I should have a list which is the sum of q computed at 0 and q computed at 1 and so on. The last time that the for loop runs, I should have a list of q which is the sum of all the precedeing lists plus the new one. Is there a simple way to implement it? The list q contains about a thousand elements.

EDIT

q is a list of 1000 elements. Everytime I should add entry by entry a new list of 1000 elements to the previous one. For instance, assume to have qcomputed when i=0 and when i=1. I would obtain newq = [q_0[0] + q_1[0] , q_0[1] + q_1[1]] in the first 2 entry. The list newq should be of the same lenght of q_0 and q_1. It should contain the sum of their values entry by entry.

AleB
  • 153
  • 1
  • 3
  • 10
  • `dayqvalue()` needs to take `q` as a parameter. – Barmar Dec 27 '17 at 22:55
  • Isn't there a way to do it without modifying that function which computes q value over a single day? – AleB Dec 27 '17 at 22:56
  • Maybe I don't really understand what you're saying. You talk about a sum, so is `q` a number or a list? Can you show examples of what this should do on a few iterations? – Barmar Dec 27 '17 at 22:58
  • `q` is a list of 1000 elements. Everytime I should add entry by entry a new list of 1000 elements to the previous one. For instance, assume to have `q`computed when `i=0` and when `i=1`. I would obtain `newq = [q_0[0] + q_1[0] , q_0[1] + q_1[1]]` in the first 2 entry. The list `newq` should be of the same lenght of `q_0` and `q_1`. It should contain the sum of their values entry by entry. – AleB Dec 27 '17 at 23:06
  • Put the clarification in the quesiton, not a comment. – Barmar Dec 27 '17 at 23:07

1 Answers1

0

Initialize q to a list of 1000 0's. Then each time through the loop, combine the new q returned from the function to the existing q.

bayesian_prices = [] 
q = [0]*1000
for i in range(10):
    newq = dayqvalue(i, grid, fullData)
    q = [sum(x) for x in zip(q, newq)]
    w = compute_dayweights(q)
    option_data, index_prices, initial_date = get_samedated_opt(fullData, i, 1)
    callput, tmt, ex, mkt =  orderdata(option_data)
    baypr = compute_bayesian_prices(w, grid, callput, tmt, ex, index_prices)
    bayesian_prices.append(baypr)

See Element-wise addition of 2 lists? for other ways to add two lists.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • This seems exactly what I wanted to do. In this way after each time the loop run, I have the desired list q. Thank you! – AleB Dec 27 '17 at 23:20