title: Recombining tree in python calculating asset prices
body: <p>I have a homework question :</p>
  <p><a href="https://i.stack.imgur.com/o5eeL.png" rel="nofollow noreferrer">enter image description here</a></p>
  <p>The algorithm for solving this problem is like this :
  calculating the  Y for every k . Then for the lask interaction, Vk is going to be equal to Yk .
  After that, i supposed to calculate the expected value of VK intill I reach k=0 in recursion.
  Attaches an image using a tree to calculate the value  v(0) for N=2 and s=5:</p>
  <p><a href="https://i.stack.imgur.com/ApOcW.jpg" rel="nofollow noreferrer">enter image description here</a></p>
  <p>I tried writing some code but unfortunately i think my direction is wrong for calculating V(0) as im able just to produce a matrix and not the V(0) value.</p>
  <pre><code>import numpy as np # linear algebra
  import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
  import operator as op
  from functools import reduce
  from scipy.special import comb
  def vmax1(s):
      k = 2
      Y = np.zeros(2 * k + 1)
  
      # claculate the Y for all the sigma
      for i in range(-k, k):
          Y[i + k] = np.maximum(0, 10 - s * (1 + i * 0.007))
  
      V = np.zeros((2 * k + 1, k + 1))
  
      # Vk=Yk for the last k iteration
      i = -k
      while i &lt; k:
          V[i + k, k] = Y[i + k]
          i += 2
  
      # claculate the V Matrix for all Sigma for all iterations
      for i in range(k, 0, -1):
          j = -i + 1
          while j &lt; i:
              y_minus = V[j + k - 1, i]
              p_y_minus = comb(i, (i + j - 1) / 2) / (2 ** i)
              y_plus = V[j + k + 1, i]
              p_y_plus = comb(i, (i + j + 1) / 2) / (2 ** i)
              V[j + k, i - 1] = np.maximum(Y[j + k], (y_minus * p_y_minus) + (y_plus * p_y_plus))
              j += 2
      return (V)
  </code></pre>
  <p>here is the output:
  <a href="https://i.stack.imgur.com/V0HXT.png" rel="nofollow noreferrer">enter image description here</a></p>
  <p>I get the values in the manual calculation but just in a matrix and not as a single value.</p>
  <p>is there any way to code this differntly ?</p>
  <p>thank you:)</p>