janboone / applied-economics

course material for the course applied economics
17 stars 21 forks source link

Question on q_choice #28

Closed florisvanlaarhoven closed 3 years ago

florisvanlaarhoven commented 4 years ago

Hi again,

I'm not completely sure whether my coding for the question on defining q_choice is correct, since the function returns a negative value of q. This is what I did:

def q_choice(w_a, w_g, ability): w_b = 0 optimal_q = optimize.fmin(lambda q: w_gq_g(q,ability) + w_aq + w_b*q_b(q,ability),0.2) return optimal_q

What is wrong with this code? I also feel like the screencasts do not really explain what we should do. Is there any way we can check our coding to see whether we did it correctly, by providing all answers somewhere? Because I'm actually never sure about my answers, even if the coding is correct.

janboone commented 4 years ago

The function is fine and great that you observe that q < 0 and q > 1 is possible. So we need small adjustment.

The function that I use is:

def q_choice(w,ability): # w = [w_a,w_g]
    choice = optimize.fminbound(lambda x: -(q_g(x,ability)*w[1] + x*w[0]),0,1,disp=0)
    return choice

Hence, there are two differences:

  1. to avoid q <0, you can use the function fminbound. The syntax is similar to fmin except that we do not provide an initial guess, but the bounds for the optimization problem. Here we specify that x (q in your case) has to be between 0 and 1. You can experiment with disp=0 to see what this does (or look at the documentation).

  2. the relevant wages --w_a,w_g-- are provided in a vector w. This makes it easier below to optimize over the choice of w.

It is correct that the screencasts do not fully explain what you need to do. This is by design (apologies for this, I do see that this can be annoying). The point is that it is more important that you try to code than that you get the correct code. If we all know that the correct code will be provided at some point, a number of students will not try. You only learn to program by trying.

If you want to check whether your solutions are correct, use github issues and we will discuss the code in class (as you are doing at the moment; well done!).

florisvanlaarhoven commented 4 years ago

Thank you for your response!