Created
February 26, 2020 15:43
-
-
Save acarril/fc30b7ed5c4df8669fcd921ec6f16a2b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#%% Load libraries | |
using DataFrames, CSV | |
using Statistics | |
using Plots, LaTeXStrings | |
#%% Read and prepare data | |
cd("/home/alvaro/Dropbox/Princeton/2020-Spring/542-IO/PS1") | |
df = CSV.read("data/Pset1_2020Data.csv"); | |
# Add dummy columns for number of active firms in each market | |
for j in levels(df.firms_in_market) | |
var = Symbol(:Nfirms, Int(j)) | |
df[var] = Int.(df.firms_in_market .== j) | |
end | |
# Compute d_{-i} (total ATMs in market sans own) | |
d_total = by(df, :market_index, :d_i => sum) | |
df = join(d_total, df, on = :market_index) | |
df[:d_others] = (df.d_i_sum - df.d_i)./(df.firms_in_market .- 1) | |
#%% Define functions | |
# Define Moments() function | |
function Moments(β1, β2, df, instruments) | |
ineq_minus = (df.r - df.r_minus) .- β1 .+ β2 .* (1 .- 2 .* df.d_i) | |
ineq_plus = (df.r - df.r_plus) .+ β1 .+ β2 .* (1 .+ 2 .* df.d_i) | |
Z = [DataFrame(Constant = ones(size(df)[1])) select(df, instruments)] | |
M = hcat(ineq_minus .* Z, ineq_plus .* Z, makeunique = true) | |
[mean(col) for col in eachcol(M)] | |
end | |
# Define searchB() function to search for parameters whose expected moment condition is non-negative | |
function searchB(β1_grid, β2_grid, data, instruments) | |
B = zeros(size(β1_grid)[1]*size(β2_grid)[1], 2) | |
k = 1 | |
for x = 1:size(β1_grid)[1] | |
β1 = β1_grid[x] | |
for y = 1:size(β2_grid)[1] | |
β2 = β2_grid[y] | |
M = Moments(β1, β2, df, instruments) | |
if minimum(M) >= 0 | |
B[k, :] = [β1 β2] | |
k = k + 1 | |
end | |
end | |
end | |
# Truncate zeros from B matrix | |
last_nonzero = findall(x->x==0, B)[1][1]-1 | |
B[1:last_nonzero, :] | |
end | |
#%% Define grids | |
# Note: values found after fine-tuning | |
β1_grid = collect(25000:100:45000) | |
β2_grid = collect(1500:10:4000) | |
#%% Question 6: Number-of-firms-in-market dummies as instruments | |
Z = [:Nfirms2, :Nfirms3, :Nfirms4] | |
B = searchB(β1_grid, β2_grid, df, Z) | |
# Plot B using [:Nfirms2, :Nfirms3, :Nfirms4] as instruments | |
plot(B[:,1], B[:,2], seriestype=:scatter, | |
xlabel = L"\beta_1", ylabel = L"\beta_2", | |
guidefontsize = 16, | |
legend = false, | |
marker = (:dot, 1, 0.2, :blue, stroke(1, 0.2, :blue)) | |
) | |
savefig("writeup/figs/params_Z_marketsize.pdf") | |
#%% Question 8 | |
# Search for parameter values adding d_others as additional instrument | |
Z2 = [:d_others] | |
B2 = searchB(β1_grid, β2_grid, df, Z2) | |
B = Int.(B) | |
plot(B2[:,1], B2[:,2], seriestype=:scatter, | |
formatter = Int, | |
xlabel = L"\beta_1", ylabel = L"\beta_2", | |
label = "Average number of ATMs of competitors", | |
legendfont = 9, | |
guidefontsize = 16, | |
marker = (:dot, 1, 0.2, :red, stroke(1, 0.2, :red)) | |
) | |
plot!(B[:,1], B[:,2], seriestype=:scatter, | |
marker = (:dot, 1, 0.2, :blue, stroke(1, 0.2, :blue)), | |
legendfont = 9, | |
label = "Number of firms in market (dummies)", | |
legendtitle = "Instrument\n", | |
legendtitlefont = 12, | |
) | |
savefig("writeup/figs/params_Z_marketsize_plus.pdf") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment