12

I have defined the following function:

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)

My question pertains to the variable delta bound inside of the function. Outside of the function I will set the initial value of delta0. Now, ultimately I will minimize this function. What I would like to have happen is that each time the function GMM evaluates, delta from the previous evaluation is used as the new delta0. I tried defining delta0 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?

3 Answers 3

25

There are multiple ways to achieve what you want. delta is saved across function calls in the following examples.

1- Class

class Example:
  def __init__(self, value):
    self.delta = value
  def gmm(self):
    self.delta += 1
    return self.delta

e = Example(0)
print e.gmm()

2- Generator

def gmm():
  delta = 0
  while True:
    delta += 1
    yield delta

for v in gmm():
  print v

3- Function attribute

def gmm():
  gmm.delta += 1
  return delta
gmm.delta = 0

4- Global variable (discouraged as you said):

delta = 0
def gmm():
  global delta
  delta += 1
  return delta

etc...

Sign up to request clarification or add additional context in comments.

Comments

13
globalVariable = 0

def test():
    global globalVariable
    globalVariable = 10

test()
print globalVariable

You can edit a global variable in this way.

1 Comment

+1. It would have been better had you formulated the example code in terms of GMM, delta, and delta0
4

When faced with this, a common kludge I use is stuff an object into a module, which then puts it into a namespace accessible by everything in the program. It is a big kludge, but I find it removes any ambiguity about what's global. For standalone stuff I put it into os, if it's an entire project I'll generally create an empty python file called my_globals and import it, ie

import my_globals
my_globals.thing = "rawp"
def func():
    my_globals.thing = "test"
func()
print my_globals.thing # "test"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.