jupytext | kernelspec | orphan | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
true |
See "Check Your Understanding" from {doc}Control Flow <../python_fundamentals/control_flow>
, {doc}Functions <../python_fundamentals/functions>
, and {doc}Introduction to Numpy <../scientific/numpy_arrays>
Write a for loop that uses the lists of cities and states to print a message saying “{city} is in {state}”
, where {city}
and {state}
are replaced by different values on each iteration. You are not allowed to use zip
.
cities = ["Phoenix", "Austin", "San Diego", "New York"]
states = ["Arizona", "Texas", "California", "New York"]
# Your code here
Now, do the same thing with a for
loop using zip
.
cities = ["Phoenix", "Austin", "San Diego", "New York"]
states = ["Arizona", "Texas", "California", "New York"]
# Your code here
Write a function that takes in a tuple as (city, state)
and returns a string “{city} is in {state}”
with the values substituted.
# Your function here
Now, use your function and a comprehension
to print out “{city} is in {state}”
as we did above.
cities = ["Phoenix", "Austin", "San Diego", "New York"]
states = ["Arizona", "Texas", "California", "New York"]
# Your code here
This exercise explores the concept of higher order functions, or functions that can be an input or output of another function.
Below is code that implements a version of the generalized Cobb-Douglas production function which takes the form
It takes as an argument alpha_1
, alpha_2
, and z
and then
returns a function that implements that parameterization of the
Cobb-Douglas production function.
def cobb_douglas_factory(alpha_1, alpha_2, z=1.0):
"""
Return a function F(K, L) that implements the generalized Cobb-Douglas
production function with parameters alpha_1, alpha_2, and z
The returned function takes the form F(K, L) = z K^{\alpha_1} L^{\alpha_2}
"""
# I'm defining a function inside a function
def return_func(K, L):
return z * K**alpha_1 * L**alpha_2
# Notice I'm returning a function! :mind_blown:
return return_func
We can use this function in two steps:
- Call it with
alpha_1
,alpha_2
, andz
and get a function in return. - Call the returned function with values of
K
andL
.
Here's how we would repeat the first Cobb-Douglas example from above:
# step 1
F2 = cobb_douglas_factory(0.33, 1-0.33)
# step 2
F2(1.0, 0.5)
Now, it is your turn...
Re-write the returns_to_scale
function above as we had in {doc}Functions <../python_fundamentals/functions>
to accept an additional argument
F
that represents a production function. The function should take in K
and L
and return output.
We've written some code below to get you started.
def returns_to_scale2(F, K, L, gamma):
# call F with K and L
# scale K and L by gamma and repeat
# compute returns to scale
# Delete ``pass`` below after you have code
pass
Test out your new function using the original F2
that we defined above and
using the cobb_douglas
function defined earlier in the lecture.
Do you get the same answer?
# your code here
Let's use our cobb_douglas_factory
and returns_to_scale2
functions
to study returns to scale.
What are the returns to scale when you set alpha_1 = 0.3
and alpha_2 = 0.6
?
# test with alpha_1 = 0.3 and alpha_2 = 0.6
What about when you use alpha_1 = 0.4
and alpha_2 = 0.65
?
# test with alpha_1 = 0.4 and alpha_2 = 0.65
What do returns to scale have to do with the quantity
# your code here
Take a production function of only labor, L
, with the following form
Write a function to calculate the marginal product of labor (MPL) numerically by using a method similar to what we did in class.
# your code here
Plot the MPL for np.linspace
).
# your code here
Consider the scenario where you increase the scale of production by a factor of 10 percent more labor. Plot the returns to scale for a grid on returns_to_scale
function specific to this production function or carefully use the one above. Either way of implementation is fine.
# your code here
Compare these returns to the scale of the Cobb-Douglas functions we have worked with.
Take the following definition for X
. How would you do to extract the array [[5, 6], [7, 8]]
?
import numpy as np
X = np.array([[[1, 2, 3], [3, 4, 5]], [[5, 6, 7], [7, 8, 9]]])
# your code here
Let's revisit a bond pricing example we saw in {doc}Control flow <../python_fundamentals/control_flow>
.
We price the bond today (period 0). Starting from period 1, this bond pays back a coupon
Recall that the equation for pricing a bond with coupon payment
In the code cell below, we have defined variables for i
, M
and C
.
You have two tasks:
- Define a numpy array
N
that contains all maturities between 1 and 10. (Hint: look at thenp.arange
function) - Using the equation above, determine the price of bonds with all maturity levels in your array.
i = 0.03
M = 100
C = 5
# Define array here
# price bonds here