Skip to content

Commit

Permalink
snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Creel committed May 7, 2024
1 parent e673529 commit 1a4901d
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 365 deletions.
45 changes: 23 additions & 22 deletions Examples/Data/klein.data
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
1920 39.8 12.7 28.8 2.7 180.1 44.9 2.2 4.6 3.4
1921 41.9 12.4 25.5 -0.2 182.8 45.6 2.7 6.6 7.7
1922 45.0 16.9 29.3 1.9 182.6 50.1 2.9 6.1 3.9
1923 49.2 18.4 34.1 5.2 184.5 57.2 2.9 5.7 4.7
1924 50.6 19.4 33.9 3.0 189.7 57.1 3.1 6.6 3.8
1925 52.6 20.1 35.4 5.1 192.7 61.0 3.2 6.5 5.5
1926 55.1 19.6 37.4 5.6 197.8 64.0 3.3 6.6 7.0
1927 56.2 19.8 37.9 4.2 203.4 64.4 3.6 7.6 6.7
1928 57.3 21.1 39.2 3.0 207.6 64.5 3.7 7.9 4.2
1929 57.8 21.7 41.3 5.1 210.6 67.0 4.0 8.1 4.0
1930 55.0 15.6 37.9 1.0 215.7 61.2 4.2 9.4 7.7
1931 50.9 11.4 34.5 -3.4 216.7 53.4 4.8 10.7 7.5
1932 45.6 7.0 29.0 -6.2 213.3 44.3 5.3 10.2 8.3
1933 46.5 11.2 28.5 -5.1 207.1 45.1 5.6 9.3 5.4
1934 48.7 12.3 30.6 -3.0 202.0 49.7 6.0 10.0 6.8
1935 51.3 14.0 33.2 -1.3 199.0 54.4 6.1 10.5 7.2
1936 57.7 17.6 36.8 2.1 197.7 62.7 7.4 10.3 8.3
1937 58.7 17.3 41.0 2.0 199.8 65.0 6.7 11.0 6.7
1938 57.5 15.3 38.2 -1.9 201.8 60.9 7.7 13.0 7.4
1939 61.6 19.0 41.6 1.3 199.9 69.5 7.8 14.4 8.9
1940 65.0 21.1 45.0 3.3 201.2 75.7 8.0 15.4 9.6
1941 69.7 23.5 53.3 4.9 204.5 88.4 8.5 22.3 11.6
YEAR,C,P,WP,I,KLAG,X,WG,G,T
1920,39.8,12.7,28.8,2.7,180.1,44.9,2.2,4.6,3.4
1921,41.9,12.4,25.5,-0.2,182.8,45.6,2.7,6.6,7.7
1922,45.0,16.9,29.3,1.9,182.6,50.1,2.9,6.1,3.9
1923,49.2,18.4,34.1,5.2,184.5,57.2,2.9,5.7,4.7
1924,50.6,19.4,33.9,3.0,189.7,57.1,3.1,6.6,3.8
1925,52.6,20.1,35.4,5.1,192.7,61.0,3.2,6.5,5.5
1926,55.1,19.6,37.4,5.6,197.8,64.0,3.3,6.6,7.0
1927,56.2,19.8,37.9,4.2,203.4,64.4,3.6,7.6,6.7
1928,57.3,21.1,39.2,3.0,207.6,64.5,3.7,7.9,4.2
1929,57.8,21.7,41.3,5.1,210.6,67.0,4.0,8.1,4.0
1930,55.0,15.6,37.9,1.0,215.7,61.2,4.2,9.4,7.7
1931,50.9,11.4,34.5,-3.4,216.7,53.4,4.8,10.7,7.5
1932,45.6,7.0,29.0,-6.2,213.3,44.3,5.3,10.2,8.3
1933,46.5,11.2,28.5,-5.1,207.1,45.1,5.6,9.3,5.4
1934,48.7,12.3,30.6,-3.0,202.0,49.7,6.0,10.0,6.8
1935,51.3,14.0,33.2,-1.3,199.0,54.4,6.1,10.5,7.2
1936,57.7,17.6,36.8,2.1,197.7,62.7,7.4,10.3,8.3
1937,58.7,17.3,41.0,2.0,199.8,65.0,6.7,11.0,6.7
1938,57.5,15.3,38.2,-1.9,201.8,60.9,7.7,13.0,7.4
1939,61.6,19.0,41.6,1.3,199.9,69.5,7.8,14.4,8.9
1940,65.0,21.1,45.0,3.3,201.2,75.7,8.0,15.4,9.6
1941,69.7,23.5,53.3,4.9,204.5,88.4,8.5,22.3,11.6
8 changes: 4 additions & 4 deletions Examples/GMM/portfolio.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ d = data[:,3]
r = (p + d) ./ lag(p,1) .- 1.0
c = c ./ lag(c,1); # ensure stationarity

## choose the instruments
inst = [lag(c,1) r d p]
maxlag = 2
## choose the instruments: note - the code is off by one w.r.t. the notes
inst = [lag(c,1) lag(r,1)]
maxlag = 5
inst = [inst lags(inst, maxlag)]
inst = inst[maxlag+2:end,:] # drop first two obs, due to two lags of c
inst, junk, junk = stnorm(inst) # for numeric stability
Expand All @@ -64,7 +64,7 @@ moments = θ -> portfolio_moments(θ, data)
weight = 1.0
names = ["beta","gamma"]
# initial consistent estimate
θstart = [0.95, 0.5]
θstart = [0.95, 2.]
θhat, obj_value, D, ms, convergence = gmm(moments, θstart , weight)
# second step with efficient weight matrix
Ω = cov(ms)
Expand Down
26 changes: 0 additions & 26 deletions Examples/GMM/portfolio1.out

This file was deleted.

27 changes: 0 additions & 27 deletions Examples/GMM/portfolio2.out

This file was deleted.

38 changes: 14 additions & 24 deletions Examples/Simeq/Klein2SLS.jl
Original file line number Diff line number Diff line change
@@ -1,32 +1,25 @@
# Estimates the Klein consumption equation by 2SLS
using Econometrics, DelimitedFiles, Statistics
## Estimates the Klein consumption equation by 2SLS


function main()
## read in the data and create needed variables
using Econometrics, CSV, DataFrames, DataFramesMeta, Statistics, Term
cd(@__DIR__)
data = readdlm("klein.data")
# construct missing lags, and drop first row that has missing data
profits = data[:,3]
output = data[:,7]
data = [data lag(profits,1) lag(output,1)]
data = data[2:end,:]
n = size(data,1)
klein = CSV.read("klein.data", DataFrame; header=true)
## construct missing lags, and drop first row that has missing data
klein.lagP = vcat(missing, [klein.P[i-1] for i=2:size(klein,1)])
klein.lagX = vcat(missing,[klein.X[i-1] for i=2:size(klein,1)])
klein.WAGES = klein.WP + klein.WG
klein = dropmissing(klein)

# define instruments
exogs = [1, 6, 8, 9, 10, 11, 12]
exogs = data[:,exogs]
n = size(klein,1)
exogs = Matrix(@select(klein, :YEAR, :WG, :G, :T, :lagP,:lagX))
exogs = [ones(n,1) exogs]

# CONSUMPTION
println("CONSUMPTION EQUATION")
# define variables in consumption equation
y = data[:,2]
profits = data[:,3]
lagprofits = data[:,11]
wp = data[:,4]
wg = data[:,8]
wages = wp + wg
# regressors in consumption equation
x = [profits lagprofits wages]
y = Matrix(@select(klein, :C))
x = Matrix(@select(klein, :P, :lagP, :WAGES))
x = [ones(n,1) x]
# 2SLS estimation
names = ["Constant", "Profits", "Profits-1", "Wages"]
Expand All @@ -35,6 +28,3 @@ printstyled("\nNote that 2SLS does not give the same results as does GMM.\n", co
printstyled("This is because the equation is overidentified. GMM is\n", color=:green)
printstyled("using an estimate of the efficient weight matrix. 2SLS does not.\n", color=:green)

return
end
main()
40 changes: 18 additions & 22 deletions Examples/Simeq/KleinCUE.jl
Original file line number Diff line number Diff line change
@@ -1,35 +1,31 @@
# Estimates the Klein consumption equation by GMM
using Econometrics, DelimitedFiles, Statistics
# Estimates the Klein consumption equation by CUE GMM
using Econometrics, CSV, DataFrames, DataFramesMeta, Statistics, Term

function KleinMoments(θ, y, x, z)
e = y - x*θ
m = e.*z
end

function main()
## read in the data and create needed variables
cd(@__DIR__)
data = readdlm("klein.data")
# construct missing lags, and drop first row that has missing data
profits = data[:,3]
output = data[:,7]
data = [data lag(profits,1) lag(output,1)]
data = data[2:end,:]
n = size(data,1)
# define instruments
exogs = [1, 6, 8, 9, 10, 11, 12]
exogs = data[:,exogs]
exogs = [ones(n,1) exogs]
klein = CSV.read("klein.data", DataFrame; header=true)
## construct missing lags, and drop first row that has missing data
klein.lagP = vcat(missing, [klein.P[i-1] for i=2:size(klein,1)])
klein.lagX = vcat(missing,[klein.X[i-1] for i=2:size(klein,1)])
klein.WAGES = klein.WP + klein.WG
klein = dropmissing(klein)

# CONSUMPTION
println("CONSUMPTION EQUATION")
# define instruments
n = size(klein,1)
exogs = Matrix(@select(klein, :YEAR, :WG, :G, :T, :lagP,:lagX))
exogs = [ones(n) exogs]
# define variables in consumption equation
y = data[:,2]
profits = data[:,3]
lagprofits = data[:,11]
wp = data[:,4]
wg = data[:,8]
wages = wp + wg
# regressors in consumption equation
x = [profits lagprofits wages]
x = [ones(n,1) x]
y = Matrix(@select(klein, :C))
x = Matrix(@select(klein, :P, :lagP, :WAGES))
x = [ones(n) x]
# GMM estimation using CUE
θstart = x\y # ols start values
names = ["Constant", "Profits", "Profits-1", "Wages"]
Expand Down
43 changes: 20 additions & 23 deletions Examples/Simeq/KleinGMM.jl
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
# Estimates the Klein consumption equation by GMM
using Econometrics, DelimitedFiles, Statistics
function KleinMoments(theta, y, x, z)
e = y - x*theta
using Econometrics, CSV, DataFrames, DataFramesMeta, Statistics, Term

function KleinMoments(θ, y, x, z)
e = y - x*θ
m = e.*z
end

function main()
## read in the data and create needed variables
cd(@__DIR__)
data = readdlm("klein.data")
# construct missing lags, and drop first row that has missing data
profits = data[:,3]
output = data[:,7]
data = [data lag(profits,1) lag(output,1)]
data = data[2:end,:]
n = size(data,1)
# define instruments
exogs = [1, 6, 8, 9, 10, 11, 12]
exogs = data[:,exogs]
exogs = [ones(n,1) exogs]
klein = CSV.read("klein.data", DataFrame; header=true)
## construct missing lags, and drop first row that has missing data
klein.lagP = vcat(missing, [klein.P[i-1] for i=2:size(klein,1)])
klein.lagX = vcat(missing,[klein.X[i-1] for i=2:size(klein,1)])
klein.WAGES = klein.WP + klein.WG
klein = dropmissing(klein)

# CONSUMPTION
println("CONSUMPTION EQUATION")
# define instruments
n = size(klein,1)
exogs = Matrix(@select(klein, :YEAR, :WG, :G, :T, :lagP,:lagX))
exogs = [ones(n) exogs]
# define variables in consumption equation
y = data[:,2]
profits = data[:,3]
lagprofits = data[:,11]
wp = data[:,4]
wg = data[:,8]
wages = wp + wg
# regressors in consumption equation
x = [profits lagprofits wages]
x = [ones(n,1) x]
y = Matrix(@select(klein, :C))
x = Matrix(@select(klein, :P, :lagP, :WAGES))
x = [ones(n) x]

# GMM estimation
theta = x\y # ols start values
weight = 1.0
Expand Down
Loading

0 comments on commit 1a4901d

Please sign in to comment.