def resid(c): F.c = c # update basis coefficients f = F(x) # interpolate at basis nodes x return f ** -2 + f ** -5 - 2 * x # ### Compute function inverse # In[4]: c0 = np.zeros(n) # set initial guess for coeffs c0[0] = 0.2 problem = NLP(resid) F.c = problem.broyden(c0) # compute coeff by Broyden's method # ### Plot setup # In[5]: n = 1000 x = np.linspace(a, b, n) r = resid(F.c) # ### Plot function inverse # In[6]:
tnodes = BasisChebyshev(n - 1, 0, T).nodes F = BasisChebyshev(n, 0, T, y=np.ones((2, n))) def resid(c, tnodes, T, n, F, r, k, eta, s0): F.c = np.reshape(c[:], (2, n)) (p, s), d = F(tnodes, [[0, 1]]) d[0] -= (r * p + k) d[1] += p ** -eta (p_0, p_T), (s_0, s_T) = F([0, T]) return np.r_[d.flatten(), s_0 - s0, s_T] storage = NLP(resid, F.c.flatten(), tnodes, T, n, F, r, k, eta, s0) c = storage.broyden(print=True) F.c = np.reshape(c, (2, n)) nplot = 501 t = np.linspace(0, T, nplot) (p, s), (dp, ds) = F(t, [[0, 1]]) res_p = dp - r * p - k res_s = ds + p ** -eta plt.figure() plt.subplot(2, 1, 1) plt.plot(t, res_p) plt.title('Residuals') plt.ylabel('d(price) residual') plt.subplot(2, 1, 2) plt.plot(t, res_s) plt.xlabel('time')
# ### Approximation structure # In[3]: n, a, b = 21, 0.5, 2.5 Q = BasisChebyshev(n, a, b) c0 = np.zeros(n) c0[0] = 2 p = Q.nodes # ### Solve for effective supply function # In[4]: monopoly = NLP(resid) Q.c = monopoly.broyden(c0) # ### Setup plot # In[5]: nplot = 1000 p = np.linspace(a, b, nplot) rplot = resid(Q.c) # ### Plot effective supply # In[6]: demo.figure("Monopolist's Effective Supply Curve", 'Quantity', 'Price') plt.plot(Q(p), p)
S.c = c # update interpolation coefficients q = S(p) # compute quantity supplied at price nodes return p - q * (p ** (eta+1) / eta) - alpha * np.sqrt(q) - q ** 2 # Notice that `resid` only takes one argument. The other parameters (`Q`, `p`, `eta`, `alpha`) should be declared as such in the main script, were Python's scoping rules will find them. # ### Solve for effective supply function # # Class `NLP` defines nonlinear problems. It can be used to solve `resid` by Broyden's method. # In[7]: cournot = NLP(resid) S.c = cournot.broyden(S.c, tol=1e-12) # ### Plot demand and effective supply for m=5 firms # In[8]: prices = np.linspace(a, b, 501) fig1 = demo.figure('Cournot Effective Firm Supply Function', 'Quantity', 'Price', [0, 4], [0.5, 2]) plt.plot(5 * S(prices), prices, D(prices), prices) plt.legend(('Supply','Demand')) # ### Plot residual
tnodes = BasisChebyshev(n - 1, 0, T).nodes F = BasisChebyshev(n, 0, T, y=np.ones((2, n))) def resid(c, tnodes, T, n, F, r, k, eta, s0): F.c = np.reshape(c[:], (2, n)) (p, s), d = F(tnodes, [[0, 1]]) d[0] -= (r * p + k) d[1] += p**-eta (p_0, p_T), (s_0, s_T) = F([0, T]) return np.r_[d.flatten(), s_0 - s0, s_T] storage = NLP(resid, F.c.flatten(), tnodes, T, n, F, r, k, eta, s0) c = storage.broyden(print=True) F.c = np.reshape(c, (2, n)) nplot = 501 t = np.linspace(0, T, nplot) (p, s), (dp, ds) = F(t, [[0, 1]]) res_p = dp - r * p - k res_s = ds + p**-eta plt.figure() plt.subplot(2, 1, 1) plt.plot(t, res_p) plt.title('Residuals') plt.ylabel('d(price) residual') plt.subplot(2, 1, 2) plt.plot(t, res_s) plt.xlabel('time')
n, a, b = 21, 0.5, 2.5 Q = BasisChebyshev(n, a, b) c0 = np.zeros(n) c0[0] = 2 p = Q.nodes # ### Solve for effective supply function # In[4]: monopoly = NLP(resid) Q.c = monopoly.broyden(c0) # ### Setup plot # In[5]: nplot = 1000 p = np.linspace(a, b, nplot) rplot = resid(Q.c) # ### Plot effective supply # In[6]: