Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
coding a function
#1
Hello everyone

I have an assignment for university which I am having some problems with. We have to define a function which will return the utility depending on consumption u_1, u_2 and u_3 and parameters which are already given. U(sol,parameters) = c1**0.5 + c2**0.5 + c3**0.5. The problem comes in when we have a look how u_2 is defined: u_2 <= q * n / z. In this case n and z are known but q and u_2 arent.

This is the code I used to define the function but obviously u_3 will be 0. how can I code this an other way?

def U(sol,params):
"""
Returns the lifetime utility of a young person as a function of his consumption and money holdings (sol)

as well as the parameters of the model (params)

"""

y, z, n, beta = params

q = sol

u_1 = (y - q) ** (1/2)

u_2 = ((n / z) * q) ** (1/2)

u_3 = ((n/z)**2 * q - (n/z)**2 * q)** (1/2)

return u_1 + beta * u_2 + beta**2 * u_3
Reply
#2
Please use proper code tags while coding
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#3
Not enough information. You say that U1-U3 are given, but then you calculate them. From your formulas you have to be given q. This does not quite make sense. Csn you rephrase, and repost your code using tags?
Reply
#4
Hello everyone

I have an assignment for university which I am having some problems with. We have to define a function which will return the total utility depending on consumption single utilities u_1, u_2 and u_3.
U(sol,parameters) = u_1 + beta * u_2 + beta**2 * u_3. The problem comes in when we have a look how u_2 is defined: u_2 <= (q * n / z). In this case n and z are known(as are y and beta later on) but q isnt.

This is the code I used to define the function but obviously u_3 will be 0. how can I code this an other way?

I have to create a function where I can plug in q and the parameters and get the total Utility U
The first part is the description of the function that my teacher has given me.
 def U(sol,params):
"""
Returns the lifetime utility of a young person as a function of his consumption and money holdings (sol)

as well as the parameters of the model (params)

"""

y, z, n, beta = params

q = sol

u_1 = (y - q) ** (1/2)

u_2 = ((n / z) * q) ** (1/2)

u_3 = ((n/z)**2 * q - (n/z)**2 * q)** (1/2)

return u_1 + beta * u_2 + beta**2 * u_3 
Reply
#5
I stand by my prior post. Repeating the original (though with tags) is not really helpful
Reply
#6
u_1, u_2 and u_3 are not given. only the parameters are given. What other info do you need in order to help me?
Reply
#7
You have created a function that calculates U1-3, what specifically do you need?
Reply
#8
What is missing, I think, is a problem statement. Is the problem that you don't know how to solve the equation? That is not a question for this forum. Is the question that you don't know how to write the equation(s) in Python? For that we need to know the equation(s), and all you've provided is a python implementation that supposedly doesn't work
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Need help with NumPy Histogram function coding Triikey 1 880 May-15-2023, 01:45 PM
Last Post: deanhystad
  New to Python - tiny coding assistance on user input function and assign to variable Mountain_Duck 1 2,464 Mar-23-2019, 06:54 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020