Problem set 3 model solutions

ECON-L1300
Helena Rantakaulio, 14.3.2024
clear; close all; clc;
 
% Define some constant variables to use
S = 5000; %simulations
T = 1000; %markets
J = 4; %products
 
% 1. Set seed to 2
 
%legacy MATLAB generates the exact same data as given in PS1
randn('seed',2);
rand('seed',2);
unifrnd('seed',2);
 
%but nowadays you should use
%rng(2);
 
% 2. Specify the parameter values
beta1=2;
beta2=1/2;
beta3=normrnd(4,4,S,1); %1x5000, use the same draws of random coefs for all markets
alpha=-2;
gamma1=1;
gamma2=1/5;
gamma3=1;
gamma4=1/5;
 
% 3. Simulate the exogenous variables, these are all 4x1000 matrices
x = abs(normrnd(0,1,J,T));
w = abs(normrnd(0,1,J,T));
l = repmat(unifrnd(5,10,J,1),1,T);
xi = normrnd(0,2,J,T);
roof = repmat([1;1;0;0],1,T);
 
% 4. Calculate marginal cost for each firm in each market
mc = gamma1.*w + gamma2.*l + gamma3.*roof + gamma4.*xi;
 
% For problems 5 and 6 see the end of this file. Function definitions in a script must appear at end of file in MATLAB.
 
% Problems 7-9 use the functions written in 5 and 6.
% Let's iterate over markets
 
% Before the iteration, initialize a 4x1000 matrix for the equilibrium prices and for the
% equilibrium market shares.
p_equilibrium = zeros(4,1000);
s_equilibrium = zeros(4,1000);
 
% Initial guess for the prices (4x1) at 1
%p0 = zeros(4,1)-1;
%p0 = ones(4,1);
p0 = 10*rand(J,1); %original data generation uses these starting values
 
for t = 1:T
%To pass functions mshare and d_mshare to the function handle written
%in problem 7, we'll need function handles (@).
%Note that now everything except for p is known, so the function handles only need p
%as an argument.
%Only give the t'th column (market) of each variable as input.
s = @(p) (mshare(x(:,t), l(:,t), p, xi(:,t), roof(:,t), beta1, beta2, alpha, beta3));
d_s = @(p) (d_mshare(x(:,t), l(:,t), p, xi(:,t), roof(:,t), beta1, beta2, alpha, beta3));
 
% 7. Write down the first order condition
% As mentioned in the exercise session, the original data was created
% raising FOC to the power of two for reasons unknown. This generates
% the exact same data as boat_data.xlsx, but not raising FOC to the
% power is correct.
%foc = @(p) (p - mc(:,t) + s(p)./d_s(p));
foc = @(p) (p - mc(:,t) + s(p)./d_s(p)).^2;
 
% 8. Solve the system of equations using fsolve
p_equilibrium(:,t) = fsolve(foc, p0);
 
% 9. Calculate observed shares for your fake data set using your parameters, your draws of
% exogenous variables and your equilibrium prices.
s_equilibrium(:,t) = mshare(x(:,t), l(:,t), p_equilibrium(:,t), xi(:,t), roof(:,t), beta1, beta2, alpha, beta3);
 
end
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the <a href = "matlab: helpvie...
% 10. Collect all the variables and save them as csv file.
 
%gather matrices into vectors
prices = reshape(p_equilibrium, 4000, 1);
shares = reshape(s_equilibrium, 4000, 1);
length = reshape(l, 4000, 1);
quality = reshape(x, 4000, 1);
cost_shifter = reshape(w, 4000, 1);
roof2 = reshape(roof, 4000, 1);
 
%generate vectors for firm IDs and market IDs
firm_id = repmat([1;2;3;4], 1000, 1);
market_id = 1:1000;
market_id = market_id';
market_id = repelem(market_id, 4);
 
%Combine all data into one matrix
full_data = [prices shares length quality cost_shifter roof2 firm_id market_id];
 
%Add a header row
header = ["prices" "shares" "length" "quality" "cost_shifter" "roof" "firm_ids" "market_ids"];
 
%Write to a csv file
%writematrix([header; full_data], 'boat_data_2024.csv');
writematrix([header; full_data], 'boat_data.csv');
 
Note that the starting values for price may affect the results slightly. The original data was generated using starting values drawn from uniform distribution p0 = 10*rand(J,1);. When I change the starting values to ones, the resulting prices and market shares are almost exactly the same but slightly different. Also note that the results are different if you generate the data using the correct function to set the seed (rng() instead of the legacy functions) and don't raise the FOC to the power of two.
I have also uploaded boat_data_2024.csv which was generated using rng() to set the seed, using ones as starting values and using the correct FOC (not raising it to the power of two). All the simulated values are different, and so are the prices and shares. The starting values and FOC don't change the results that much but using rng() instead of the legacy functions makes a big difference.
5. Write a procedure to approximate the derivatives of market shares with respect to prices (taking prices, shares, x, ξ and demand parameters as inputs). The formula for the derivative is given at least in Lecture 3 in connection with price elasticities
where
Note that we have already taken 5000 draws of the random coefficients above, so let's use those draws to approximate the integral.
function d_mshare = d_mshare(x, l, p, xi, roof, beta1, beta2, alpha, beta3)
%Let's assume this is iterated for each market at a time, then the
%input vectors are 4x1.
%beta3 is 5000x1.
delta = beta1*x + beta2*l + alpha*p + xi; %4x1 mean utility (excluding the mean of beta_3)
delta = repmat(delta,1,5000);%4x5000 replicated for each random coefficient
random_part = repmat(beta3',4,1).*repmat(roof, 1, 5000); %4x5000
numerator = exp(delta+random_part);%4x5000
denominator = 1 + sum(numerator); %1x5000, sum of each column
denominator = repmat(denominator, 4, 1); %4x5000
s = numerator./denominator;
 
%take mean for each product to approximate the integral
%alpha*s.*(-s+1) is 4x5000 and we want to take row-wise mean (dim=2)
d_mshare = mean(alpha*s.*(-s+1), 2); %returns 4x1 matrix
 
end
6. Write a Matlab function that produces the market shares of firms given the parameters and variables (including price, which eventually will be endogenously determined in the equilibrium) of the model. From lecture 3 we know that the market shares can be approximated as
function mshare = mshare(x, l, p, xi, roof, beta1, beta2, alpha, beta3)
 
%Again assume this is iterated for each market at a time
%the denominator and numerator are the same as in d_mshare. Let's just
%copy and paste.
delta = beta1*x + beta2*l + alpha*p + xi; %4x1 mean utility (excluding the mean of beta_3)
delta = repmat(delta,1,5000);%4x5000 replicated for each random coefficient
random_part = repmat(beta3',4,1).*repmat(roof, 1, 5000); %4x5000
numerator = exp(delta+random_part);%4x5000
denominator = 1 + sum(numerator); %1x5000, sum of each column
denominator = repmat(denominator, 4, 1); %4x5000
 
%take mean for each product to approximate the integral
mshare = mean(numerator./denominator, 2); %returns 4x1 matrix
 
end