Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. # Heckman's sample selection estimated using maximum likelihood
  2.  
  3. using Distributions
  4. using LinearAlgebra
  5. using GLM
  6. using StatFiles
  7. using DataFrames
  8. using Optim
  9. using NLSolversBase
  10. using ForwardDiff
  11. using BenchmarkTools
  12. using Distributions
  13.  
  14. # read the data
  15. women = DataFrame(load("/Users/maciej/git/dydaktyka/cda-2019/data-raw/womenwk.dta"))
  16. size(women)
  17.  
  18. # build models
  19. women.selection = ismissing.(women.wage) .== false
  20. women_sel = women[women.selection,:]
  21. women_sel.wage_new = Float64.(women_sel.wage)
  22. m1 = glm(@formula(selection ~ married + children + education +age),women, Bernoulli(), ProbitLink());
  23. m2 = lm(@formula(wage_new ~ education + age), women_sel);
  24.  
  25. θ = vcat(coef(m1), coef(m2)[:], 0.5, std(m2.mf.df.wage_new))'
  26. xs = m1.mm.m
  27. xo = m2.mm.m
  28. sel = women.selection
  29. y = m2.mf.df.wage_new
  30.  
  31.  
  32. function heckman(θ, xs, xo, sel, y)
  33. ncol_xs = size(xs,2)
  34. ncol_xo = size(xo, 2)
  35.  
  36. βs = θ[1:ncol_xs]
  37. βo = θ[(ncol_xs+1):(ncol_xs+ncol_xo)]
  38. ρ = θ[end-1]
  39. σ = θ[end]
  40.  
  41. xsβs_nosel = xs[ .!sel,:]*βs
  42. xsβs_sel = xs[sel,:]*βs
  43. xoβo = xo * βo
  44.  
  45.  
  46. global ll_s = 0
  47. for i in 1:length(xsβs_nosel)
  48. global ll_s += log(cdf(Normal(), -xsβs_nosel[i]))
  49. end
  50.  
  51. global ll_o = 0
  52. for i in 1:length(xoβo)
  53. global ll_o += log(cdf(Normal(), (xsβs_sel[i]+ρ/σ*(y[i]-xoβo[i])) / √(1-ρ^2))) - 0.5( (y[i] - xoβo[i])/σ )^2 - log(√(2π)σ)
  54. end
  55. l = ll_s+ll_o
  56. -l
  57. end
  58.  
  59. heckman(θ, xs, xo, sel, y)
  60.  
  61.  
  62. res = optimize(θ -> heckman(θ, xs, xo, sel, y), θ; autodiff = :forward);
  63.  
  64. function heckit(θ, xs, xo, sel, y)
  65. res = optimize(θ -> heckman(θ, xs, xo, sel, y), θ; autodiff = :forward)
  66. θ̂ = Optim.minimizer(res)
  67. ∇h(θ) = ForwardDiff.gradient(θ -> heckman(θ, xs, xo, sel, y), θ̂);
  68. ∇h²(θ) = ForwardDiff.hessian(θ -> heckman(θ, xs, xo, sel, y), θ̂);
  69. se = sqrt.(diag(inv(∇h²(θ))))
  70. return (θ̂[:], se)
  71. end
  72.  
  73.  
  74. par, se = heckit(θ, xs, xo, sel, y);
  75. tab_est = DataFrame(est = par, se_est = se)
  76. tab_est.t = tab_est.est./tab_est.se_est
  77.  
  78. ## fastforward
  79. od = OnceDifferentiable(x -> heckman(θ, xs, xo, sel, y), θ; autodiff = :forward)
  80.  
  81. ### comparison
  82. @btime optimize(od, θ, NelderMead())
  83. @btime optimize(od, θ, BFGS());
  84. @btime optimize(θ -> heckman(θ, xs, xo, sel, y), θ; autodiff = :forward);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement