import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import math # 用math.mathfunction:non vectorized function
from numpy.random import default_rng
# 8.8 randon number generator
g_rv = default_rng() # to initiate a generator object
from scipy.linalg import inv, solve, det, eig

I. Cobb-Douglas class

請完成一個CobbDouglas class定義,它必需包含至少下面提到的6個methods(q, mpl, mpk, cost, ld, mc), 程式答案寫在此大題最後的 qI code chunk:

class CobbDouglas:

which represents a class of firms equipped with Cobb-Douglas production function like: \[q_t = \phi[k_t^{\beta} l_t^{1-\beta}]\exp(\mu_t);\]

The detail of its definition is to provide the following usages:

  1. Produce a Cobb-Douglas production firm as an instance. The following call produces an instance of a Cobb-Douglous production function firm, called cd1, with \(\beta=0.5\) and \(\phi=1\). For simplicity, assume \(\phi=1\) to be a class attribute that is shared across all firms in this class.
cd1=CobbDouglas(0.5,1) 
  1. The following method produces the output of the firm under \(l=2\), \(k=3\) and \(\mu=0.3\)
cd1.q(l=2,k=3,mu=0.3)

The marginal productivity of labor (mpl) and of capital (mpk) are defined as:
\[\begin{eqnarray} mpl &=&\frac{\partial q_t}{\partial l_t}\\ mpk &=&\frac{\partial q_t}{\partial k_t} \end{eqnarray}\]

  1. The following method produces the marginal productivity of labor under \(l=2\), \(k=3\) and \(\mu=0.3\)$
cd1.mpl(l=2,k=3,mu=0.3)
  1. The following method produces the marginal productivity of capital under \(l=2\), \(k=3\) and \(\mu=0.3\)$
cd1.mpk(l=2,k=3,mu=0.3)  

Given \(r_t\) and \(w_t\) as the rental price of capital \(k_t\) and the wage of labor \(l_t\), the cost of a firm’s production will be \[w_tl_t+r_tk_t.\] Under cost minimization, for a firm to produce \(q_t\) under technology level \(\mu_t\), its inputs must satisfy the following two conditions:

  1. \(\frac{\partial q_t}{\partial k_t}/\frac{\partial q_t}{\partial l_t}=r_t/w_t,\) and
  2. \(q_t=f(l_t,k_t,\mu_t)\) where \(f(.)\) represent the production function of the firm.
  1. The following method will produce the cost function (under cost minimization) for the firm under \(q=10\), \(w=2\), \(r=5\), and \(\mu=0.3\):
cd1.cost(q=10, w=2,r=5,mu=0.3)
  1. The following method will produce the demand of labor (under cost minimization) under \(q=10\), \(w=2\), \(r=5\), and \(\mu=0.3\):
cd1.ld(q=10, w=2,r=5, mu=0.3)

The marginal cost of production is defined as \[mc_t\equiv\frac{\partial cost_t}{\partial q_t},\] where \(cost_t\) is the cost function of the firm.

  1. The following method will produce the marginal cost of production under \(q=10\), \(w=2\), \(r=5\), and \(\mu=0.3\):
cd1.mc(q=10, w=2,r=5, mu=0.3)

II. Simulation

Assume there are infinitely many Cobb-Douglas firms (aka CD firms) with different labor intensity, i.e. different \(\beta\) values, and difference technology levels, i.e. different \(\mu\) values. Assume:

  1. Randomly generate 500 CD firms as 500 CobbDoublass class instances (using the result of part I). Save them in a dictionary, called CDFirms.

The following questions should be applied to all those 500 firms.


The cost method in part I is a long-run cost function in which capital level is free to choose. In the short-run, capital \(k\) is fixed for each firm; only \(l\) is free to choose. In this case the optimal short-run labor demand should satisfy: \[mpl=w\ \mbox{and} \ q=f(l,k)\]

  1. Add a k attribute to each firm where k’s value is randomly drawn from a uniform(0.5, 1.5) distribution.

  2. Add a short-run labor demand method, srld, to each instance, so that, the following method will produce its labor demand under $q=10, w=2, $ where \(\mu\) is from question 1’s random drawn outcome (therefore you need to add mu attribute to each instance):

cd1.srld(q=10, w=2, mu)

In the short-run, the marginal cost of production is the marginal change of \(q\) to its cost \(wl+rk\). Since \(r\) and \(k\) are not up to the firm’s choice in the short-run, the short-run marginal cost of the firm is: \[ w \frac{\partial srld}{\partial q}, \] where srld is the short-run labor demand method as a function.

  1. Based on srld method, numerically compute the partial derivative of srld with respect to q with each firm’s \(q=10, w=2, \mu\). (Use an 1e-5 small change for your numerical derivation) and derive each firm’s marginal cost value.