title: Modifying a global variable inside a function
body: <p>I have defined the following function:</p>
  
  <pre><code>def GMM(s1, s2, s3, s4, s5, a):
      """The GMM objective function.
  
      Arguments
      ---------
          si: float
              standard deviations of preference distribution
  
          a: float
              marginal utility of residutal income
  
      Paramters
      ---------
          Px: array (1,ns) 
              projector onto nonprice characteristic space
  
          xk, z: arrays (J, 5) and (J, 12)
              nonprice char. and instruments
  
          invW: array (12, 12) 
              GMM weight matrix
  
      Returns
      -------
          float."""
      delta = invert(s1, s2, s3, s4, s5, a, delta0) # Invert market shares to get mean utility
      bmean = np.dot(Px, delta)                     # Project delta onto charancteristic space
      xihat = delta - np.dot(xk, bmean)             # Compute implied unobservable prod. quality
      temp1 = np.dot(xihat.T, z)
      if np.any(np.isnan(delta)) == True:
          value = 1e+10
      else:
          value = np.dot(np.dot(temp1, invW), temp1.T)
      return np.sqrt(value)
  </code></pre>
  
  <p>My question pertains to the variable <code>delta</code> bound inside of the function.  Outside of the function I will set the initial value of <code>delta0</code>.  Now, ultimately I will minimize this function.  What I would like to have happen is that each time the function <code>GMM</code> evaluates, <code>delta</code> from the previous evaluation is used as the new <code>delta0</code>.  I tried defining <code>delta0</code> as a global variable, but it did not seem to work... likely this was my error though.  Although, I have read here that generally this is a bad approach.  Any suggestions?</p>