using InstantiateFromURL # optionally add arguments to force installation: instantiate = true, precompile = true github_project("QuantEcon/quantecon-notebooks-julia", version = "0.8.0") using LinearAlgebra, Statistics, QuantEcon using QuantEcon, LinearAlgebra # parameters a0 = 10.0 a1 = 2.0 β = 0.96 γ = 12.0 # in LQ form A = I + zeros(3, 3) B1 = [0.0, 1.0, 0.0] B2 = [0.0, 0.0, 1.0] R1 = [ 0.0 -a0 / 2.0 0.0; -a0 / 2.0 a1 a1 / 2.0; 0.0 a1 / 2.0 0.0] R2 = [ 0.0 0.0 -a0 / 2.0; 0.0 0.0 a1 / 2.0; -a0 / 2.0 a1 / 2.0 a1] Q1 = Q2 = γ S1 = S2 = W1 = W2 = M1 = M2 = 0.0 # solve using QE's nnash function F1, F2, P1, P2 = nnash(A, B1, B2, R1, R2, Q1, Q2, S1, S2, W1, W2, M1, M2, beta=β) # display policies println("Computed policies for firm 1 and firm 2:") println("F1 = $F1") println("F2 = $F2") Λ1 = A - (B2 * F2) lq1 = QuantEcon.LQ(Q1, R1, Λ1, B1, bet=β) P1_ih, F1_ih, d = stationary_values(lq1) F1_ih isapprox(F1, F1_ih, atol=1e-7) using Plots gr(fmt=:png); AF = A - B1 * F1 - B2 * F2 n = 20 x = zeros(3, n) x[:, 1] = [1 1 1] for t in 1:n-1 x[:, t+1] = AF * x[:, t] end q1 = x[2, :] q2 = x[3, :] q = q1 + q2 # total output, MPE p = a0 .- a1 * q # price, MPE plt = plot(q, color=:blue, lw=2, alpha=0.75, label="total output") plot!(plt, p, color=:green, lw=2, alpha=0.75, label="price") plot!(plt, title="Output and prices, duopoly MPE") δ = 0.02 D = [ -1 0.5; 0.5 -1] b = [25, 25] c1 = c2 = [1, -2, 1] e1 = e2 = [10, 10, 3] # parameters a0 = 10.0 a1 = 2.0 β = 0.96 γ = 12.0 # in LQ form A = I + zeros(3, 3) B1 = [0.0, 1.0, 0.0] B2 = [0.0, 0.0, 1.0] R1 = [ 0.0 -a0 / 2.0 0.0; -a0 / 2.0 a1 a1 / 2.0; 0.0 a1 / 2.0 0.0] R2 = [ 0.0 0.0 -a0 / 2.0; 0.0 0.0 a1 / 2.0; -a0 / 2.0 a1 / 2.0 a1] Q1 = Q2 = γ S1 = S2 = W1 = W2 = M1 = M2 = 0.0 # solve using QE's nnash function F1, F2, P1, P2 = nnash(A, B1, B2, R1, R2, Q1, Q2, S1, S2, W1, W2, M1, M2, beta=β) AF = A - B1 * F1 - B2 * F2 n = 20 x = zeros(3, n) x[:, 1] = [1 1 1] for t in 1:(n-1) x[:, t+1] = AF * x[:, t] end q1 = x[2, :] q2 = x[3, :] q = q1 + q2 # Total output, MPE p = a0 .- a1 * q # Price, MPE R = a1 Q = γ A = B = 1 lq_alt = QuantEcon.LQ(Q, R, A, B, bet=β) P, F, d = stationary_values(lq_alt) q̄ = a0 / (2.0 * a1) qm = zeros(n) qm[1] = 2 x0 = qm[1]-q̄ x = x0 for i in 2:n x = A * x - B * F[1] * x qm[i] = float(x) + q̄ end pm = a0 .- a1 * qm plt_q = plot(qm, color=:blue, lw=2, alpha=0.75, label="monopolist output") plot!(plt_q, q, color=:green, lw=2, alpha=0.75, label="MPE total output") plot!(plt_q, xlabel="time", ylabel="output", ylim=(2,4),legend=:topright) plt_p = plot(pm, color=:blue, lw=2, alpha=0.75, label="monopolist price") plot!(plt_p, p, color=:green, lw=2, alpha=0.75, label="MPE price") plot!(plt_p, xlabel="time", ylabel="price",legend=:topright) plot(plt_q, plt_p, layout=(2,1), size=(700,600)) δ = 0.02 D = [-1 0.5; 0.5 -1] b = [25, 25] c1 = c2 = [1, -2, 1] e1 = e2 = [10, 10, 3] δ_1 = 1-δ # create matrices needed to compute the Nash feedback equilibrium A = [δ_1 0 -δ_1 * b[1]; 0 δ_1 -δ_1 * b[2]; 0 0 1] B1 = δ_1 * [1 -D[1, 1]; 0 -D[2, 1]; 0 0] B2 = δ_1 * [0 -D[1, 2]; 1 -D[2, 2]; 0 0] R1 = -[0.5 * c1[3] 0 0.5 * c1[2]; 0 0 0; 0.5 * c1[2] 0 c1[1]] R2 = -[0 0 0; 0 0.5 * c2[3] 0.5*c2[2]; 0 0.5 * c2[2] c2[1]] Q1 = [-0.5*e1[3] 0; 0 D[1, 1]] Q2 = [-0.5*e2[3] 0; 0 D[2, 2]] S1 = zeros(2, 2) S2 = copy(S1) W1 = [ 0.0 0.0; 0.0 0.0; -0.5 * e1[2] b[1] / 2.0] W2 = [ 0.0 0.0; 0.0 0.0; -0.5 * e2[2] b[2] / 2.0] M1 = [0.0 0.0; 0.0 D[1, 2] / 2.0] M2 = copy(M1) F1, F2, P1, P2 = nnash(A, B1, B2, R1, R2, Q1, Q2, S1, S2, W1, W2, M1, M2) println("\nFirm 1's feedback rule:\n") println(F1) println("\nFirm 2's feedback rule:\n") println(F2) AF = A - B1 * F1 - B2 * F2 n = 25 x = zeros(3, n) x[:, 1] = [2 0 1] for t in 1:(n-1) x[:, t+1] = AF * x[:, t] end I1 = x[1, :] I2 = x[2, :] plot(I1, color=:blue, lw=2, alpha=0.75, label="inventories, firm 1") plot!(I2, color=:green, lw=2, alpha=0.75, label="inventories, firm 2") plot!(title="delta = 0.02")