janboone/applied-economics

square root in an optimization problem

Closed this issue · 4 comments

Hello,
I am trying to maximize this expression:
def output():
t = 0.7
R = 1.5
def Uc_1(x,t):
return \sqrt[n]{x/t}
def Uc_2(x,t,R):
return \sqrt[n]{((1-x)R)/(1-t)}
return optimize.fsolve(lambda x: t
Uc_1(x,t)+(1-t)*Uc_2(x,t,R),0.1)

When running the cell, it is giving an error that it is related with the utility function of the agent U(x)=square root of x.
How can I solve this?

Screenshot 2022-11-26 at 17 17 01

There are a couple of problems with the screenshot of the code:

  • there is no need to next function definitions; each def statement should start on the left
  • variable definitions t and R can be global definitions (e.g. defined above the function output)
  • \sqrt is latex notation; not python. I guess you want to use: https://numpy.org/doc/stable/reference/generated/numpy.sqrt.html
  • function calls in python have arguments in parentheses "(" and ")" (not [] nor {})

Does this help?

Our research paper is the Diamond and Dybvig model.
We want to derive with respect to x and = to 0 the following expression:

Screenshot 2022-11-28 at 12 41 11

Given the values of t and R.

That is why we are using optimize.fsolve(lambda x : expression) . The expression contains a squared root since the utility function is u(x) = square root of (x) , and that is where we are having problems.

By using numpy.sqrt we would have to define an array first, but we don’t understand how that is feasible since x is the value that we are trying to maximise. About x we know that 0<x<1 since it is an assumption of this model.

How can we introduce the square root in the function in order to optimise it?

By using numpy.sqrt we would have to define an array first

I am not sure what you have in mind here: np.sqrt(9) gives the desired result, without defining an array first

If you know that $x \in [0,1]$, you can use: https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.fminbound.html

We have also used this function in the lectures.