Skip to content

Commit

Permalink
minor
Browse files Browse the repository at this point in the history
  • Loading branch information
Gkreindler committed Dec 23, 2023
1 parent 5e3c807 commit de00efa
Show file tree
Hide file tree
Showing 7 changed files with 10,563 additions and 10,558 deletions.
1,000 changes: 500 additions & 500 deletions examples/temp/fits_boot.csv

Large diffs are not rendered by default.

20,000 changes: 10,000 additions & 10,000 deletions examples/temp/fits_boot_all.csv

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions examples/temp/step1/Wstep2.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
0.7160381402040145,-0.043487516296841375
-0.043487516296841375,0.0027167729488170337
0.7160381402040097,-0.04348751629684115
-0.04348751629684115,0.002716772948817025
40 changes: 20 additions & 20 deletions examples/temp/step1/results.csv
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
idx,obj_value,converged,iterations,iteration_limit_reached,time_it_took,theta_hat,theta0,is_optimum
1,6.008047737764278e-24,true,3,false,5.5143167,"[4.969793004334855, 1.1912045293451852]","[0.7333549537269617, -0.08702752778732076]",0
2,1.0557870385606695e-23,true,3,false,0.0003319,"[4.969793004361183, 1.1912045293435451]","[0.3348882897775347, -0.7803017039276787]",0
3,2.127064708754081e-24,true,3,false,0.0003428,"[4.9697930043020655, 1.1912045293472275]","[1.4969905819055915, 0.35078506512446334]",0
4,5.678514061361174e-27,true,3,false,0.0002609,"[4.9697930042562595, 1.191204529350081]","[1.9104529355027664, 1.3598422250610078]",1
5,4.821438071086582e-24,true,3,false,0.0002897,"[4.969793004326411, 1.191204529345711]","[0.9728232798844213, -0.14067410011660123]",0
6,7.813903509746564e-24,true,3,false,0.0003732,"[4.969793004346201, 1.1912045293444784]","[-0.4584614418357323, 1.0411399628260511]",0
7,1.3648352663569037e-23,true,3,false,0.0003689,"[4.969793004375888, 1.191204529342629]","[0.9174389714694687, -1.5084014988779522]",0
8,1.0390507291325953e-23,true,3,false,0.0003546,"[4.969793004360332, 1.191204529343598]","[0.12249553087948825, -0.3423467875721143]",0
9,9.437838946189772e-25,true,4,false,0.0005973,"[4.969793004285966, 1.1912045293482305]","[-0.9204024669915305, -1.2627650563754986]",0
10,2.298844392076946e-25,true,4,false,0.0007173,"[4.969793004269693, 1.1912045293492444]","[-1.425317850889396, -0.13188760801442104]",0
11,1.2036885185904109e-23,true,3,false,0.0006497,"[4.969793004368461, 1.1912045293430917]","[-0.15268667522019008, -0.17239262787204485]",0
12,1.7481638769830322e-24,true,3,false,0.0007074,"[4.969793004297571, 1.1912045293475075]","[1.3023574705316272, 0.5077729828299388]",0
13,4.069665898792638e-24,true,3,false,0.008444,"[4.969793004320511, 1.1912045293460785]","[0.15426908206241488, 1.5402712760936221]",0
14,1.8133175621256172e-23,true,3,false,0.0003636,"[4.969793004394511, 1.191204529341469]","[-1.7267252812920018, 1.0219210821259985]",0
15,8.52026540787376e-24,true,3,false,0.0003829,"[4.969793004350281, 1.1912045293442242]","[0.2747757058146626, 0.1720449857423505]",0
16,4.67837413012653e-24,true,3,false,0.0004382,"[4.969793004325323, 1.191204529345779]","[1.0110359111683276, -0.17639294266435857]",0
17,1.531655549498546e-24,true,4,false,0.0007845,"[4.969793004294775, 1.1912045293476818]","[-2.136663990166246, -0.006788132262980114]",0
18,3.1420837462174614e-26,true,4,false,0.0008888,"[4.969793004259756, 1.1912045293498632]","[-1.2334796855846475, 0.23491144426446822]",0
19,1.3659190164525006e-23,true,3,false,0.000863,"[4.969793004375919, 1.191204529342627]","[-0.2143326300938984, -0.5149354181924408]",0
20,8.313046066730828e-24,true,3,false,0.0004578,"[4.969793004349079, 1.191204529344299]","[-0.6336756499029971, 1.4154829458615907]",0
1,1.4163457027766936e-23,true,3,false,3.4384796,"[4.969793004378145, 1.1912045293424884]","[-0.8076219842829772, 0.5918509323043817]",0
2,7.342173770454245e-26,true,4,false,0.0006581,"[4.969793004262819, 1.1912045293496725]","[-1.1587471331204793, -0.34940075088763717]",0
3,1.0932881233398036e-26,true,4,false,0.0003306,"[4.969793004257213, 1.1912045293500215]","[-0.6353006462160533, -0.9195873674036902]",1
4,8.473634861085397e-24,true,3,false,0.0002803,"[4.969793004349976, 1.191204529344243]","[0.27527174259070836, 0.30030941556064095]",0
5,3.362585582304013e-26,true,4,false,0.0004551,"[4.969793004259811, 1.1912045293498599]","[-0.6427629287344698, -0.9803008108234492]",0
6,2.0917371495407535e-24,true,3,false,0.0003752,"[4.969793004301656, 1.191204529347253]","[0.6656569390663581, 1.4326322354689236]",0
7,1.420717651132178e-23,true,3,false,0.000311,"[4.969793004378357, 1.1912045293424751]","[-0.5016593235406109, 0.3019523190094938]",0
8,4.508283084756176e-24,true,3,false,0.0003411,"[4.969793004323992, 1.1912045293458617]","[0.9958389200862289, 0.1205186066375826]",0
9,1.2951672596187545e-23,true,3,false,0.0003487,"[4.96979300437268, 1.1912045293428288]","[-0.673804055074441, 0.5907971728564774]",0
10,4.1271744005337404e-24,true,3,false,0.000287,"[4.969793004321014, 1.1912045293460474]","[0.4304568038965119, 0.7198757755222319]",0
11,1.3371977031193518e-23,true,3,false,0.000281,"[4.969793004374639, 1.1912045293427067]","[-0.014987537621259102, -0.7833167315303959]",0
12,5.470820140732725e-25,true,3,false,0.000247,"[4.96979300427832, 1.1912045293487068]","[1.3092076938136725, 1.4547309414457112]",0
13,1.39776032980897e-23,true,3,false,0.0003572,"[4.9697930043773795, 1.1912045293425362]","[0.39030067536311674, -1.2455472546703017]",0
14,1.613411314026645e-23,true,3,false,0.0003058,"[4.969793004386476, 1.1912045293419693]","[-0.5750964641957442, -0.3437920439513548]",0
15,1.3954789426527514e-23,true,3,false,0.0002411,"[4.969793004377215, 1.1912045293425464]","[-0.47222742472640855, 0.3488640769180959]",0
16,8.345251990932358e-24,true,3,false,0.0002628,"[4.969793004349249, 1.1912045293442883]","[-0.04325192887904713, 0.5572627086171289]",0
17,5.8902289011426165e-24,true,3,false,0.0003143,"[4.969793004334031, 1.1912045293452365]","[0.9638744434166414, -0.6427412732087916]",0
18,5.2879433222723106e-24,true,3,false,0.0002736,"[4.969793004329834, 1.1912045293454978]","[0.9144593331397963, -0.26330903460834404]",0
19,4.3220576238842395e-24,true,3,false,0.0002354,"[4.969793004322568, 1.1912045293459506]","[0.578266773695452, 0.5731748046648842]",0
20,1.7346909545507384e-25,true,4,false,0.0005357,"[4.969793004267681, 1.1912045293493696]","[-1.1325841216738501, -0.6360254772372482]",0
40 changes: 20 additions & 20 deletions examples/temp/step2/results.csv
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
idx,obj_value,converged,iterations,iteration_limit_reached,time_it_took,theta_hat,theta0,is_optimum
1,2.6104875942959304e-30,true,3,false,5.4577173,"[4.969793004253967, 1.1912045293502238]","[0.7333549537269617, -0.08702752778732076]",0
2,4.590800393903285e-30,true,3,false,0.0003906,"[4.969793004254001, 1.1912045293502218]","[0.3348882897775347, -0.7803017039276787]",0
3,1.0024279650486932e-30,true,3,false,0.0003002,"[4.969793004253946, 1.1912045293502251]","[1.4969905819055915, 0.35078506512446334]",0
4,3.6860454164630258e-31,true,3,false,0.0002825,"[4.96979300425393, 1.1912045293502262]","[1.9104529355027664, 1.3598422250610078]",0
5,5.549664479705953e-31,true,3,false,0.0003598,"[4.969793004253943, 1.1912045293502254]","[0.9728232798844213, -0.14067410011660123]",0
6,1.4614007105013984e-30,true,3,false,0.0004908,"[4.9697930042539635, 1.191204529350224]","[-0.4584614418357323, 1.0411399628260511]",0
7,1.1980422091934478e-30,true,3,false,0.0004401,"[4.969793004253957, 1.1912045293502245]","[0.9174389714694687, -1.5084014988779522]",0
8,3.1674241591278934e-30,true,3,false,0.0004537,"[4.969793004253981, 1.191204529350223]","[0.12249553087948825, -0.3423467875721143]",0
9,1.702209400015085e-31,true,4,false,0.0006301,"[4.96979300425394, 1.1912045293502254]","[-0.9204024669915305, -1.2627650563754986]",0
10,7.968513078100302e-32,true,4,false,0.0006387,"[4.969793004253918, 1.191204529350227]","[-1.425317850889396, -0.13188760801442104]",1
11,1.1980422091934478e-30,true,3,false,0.0006252,"[4.969793004253957, 1.1912045293502245]","[-0.15268667522019008, -0.17239262787204485]",0
12,1.5522176898026683e-31,true,3,false,0.000804,"[4.969793004253927, 1.1912045293502265]","[1.3023574705316272, 0.5077729828299388]",0
13,1.2764725426287269e-30,true,3,false,0.0009039,"[4.969793004253947, 1.1912045293502251]","[0.15426908206241488, 1.5402712760936221]",0
14,5.3396474586135005e-30,true,3,false,0.0007302,"[4.969793004253985, 1.1912045293502227]","[-1.7267252812920018, 1.0219210821259985]",0
15,1.058419851027467e-31,true,3,false,0.0007494,"[4.969793004253948, 1.191204529350225]","[0.2747757058146626, 0.1720449857423505]",0
16,2.0380276004360407e-30,true,3,false,0.0005622,"[4.969793004253965, 1.191204529350224]","[1.0110359111683276, -0.17639294266435857]",0
17,5.201360867700783e-31,true,4,false,0.00058,"[4.969793004253942, 1.1912045293502254]","[-2.136663990166246, -0.006788132262980114]",0
18,1.0359523636777318e-31,true,4,false,0.0006136,"[4.969793004253933, 1.1912045293502258]","[-1.2334796855846475, 0.23491144426446822]",0
19,4.94134966795121e-30,true,3,false,0.0004195,"[4.969793004253996, 1.191204529350222]","[-0.2143326300938984, -0.5149354181924408]",0
20,1.4614007105013984e-30,true,3,false,0.0006631,"[4.9697930042539635, 1.191204529350224]","[-0.6336756499029971, 1.4154829458615907]",0
1,6.49137705291254e-30,true,3,false,3.3949744,"[4.969793004254016, 1.1912045293502211]","[-0.8076219842829772, 0.5918509323043817]",0
2,1.6354323644129672e-31,true,4,false,0.0002382,"[4.969793004253923, 1.1912045293502265]","[-1.1587471331204793, -0.34940075088763717]",0
3,8.058609872151603e-32,true,4,false,0.0002513,"[4.969793004253901, 1.191204529350228]","[-0.6353006462160533, -0.9195873674036902]",0
4,2.8589032728597546e-30,true,3,false,0.0003243,"[4.969793004253965, 1.1912045293502243]","[0.27527174259070836, 0.30030941556064095]",0
5,9.085659141630176e-31,true,4,false,0.0002693,"[4.969793004253901, 1.1912045293502278]","[-0.6427629287344698, -0.9803008108234492]",0
6,8.421345438870917e-32,true,3,false,0.0002414,"[4.969793004253897, 1.1912045293502282]","[0.6656569390663581, 1.4326322354689236]",0
7,8.817189997743046e-30,true,3,false,0.0002188,"[4.969793004254003, 1.191204529350222]","[-0.5016593235406109, 0.3019523190094938]",0
8,8.641601728373298e-31,true,3,false,0.0002225,"[4.969793004253933, 1.1912045293502262]","[0.9958389200862289, 0.1205186066375826]",0
9,8.185649978292714e-30,true,3,false,0.0002342,"[4.969793004254008, 1.1912045293502216]","[-0.673804055074441, 0.5907971728564774]",0
10,2.7977999857059986e-30,true,3,false,0.0002927,"[4.969793004253962, 1.1912045293502245]","[0.4304568038965119, 0.7198757755222319]",0
11,5.6703729896685306e-30,true,3,false,0.0003305,"[4.969793004254013, 1.1912045293502211]","[-0.014987537621259102, -0.7833167315303959]",0
12,5.391555689274147e-31,true,3,false,0.0004028,"[4.969793004253933, 1.191204529350226]","[1.3092076938136725, 1.4547309414457112]",0
13,5.181136172439019e-30,true,3,false,0.0003714,"[4.969793004253994, 1.1912045293502227]","[0.39030067536311674, -1.2455472546703017]",0
14,6.842466207120242e-30,true,3,false,0.0004347,"[4.96979300425399, 1.1912045293502227]","[-0.5750964641957442, -0.3437920439513548]",0
15,1.8576800975584e-30,true,3,false,0.0002604,"[4.969793004253951, 1.1912045293502251]","[-0.47222742472640855, 0.3488640769180959]",0
16,8.817189997743046e-30,true,3,false,0.0002645,"[4.969793004254003, 1.191204529350222]","[-0.04325192887904713, 0.5572627086171289]",0
17,4.663892005097727e-30,true,3,false,0.0004347,"[4.969793004253994, 1.1912045293502223]","[0.9638744434166414, -0.6427412732087916]",0
18,5.768947263672686e-30,true,3,false,0.0003704,"[4.969793004253992, 1.1912045293502225]","[0.9144593331397963, -0.26330903460834404]",0
19,3.6103594462833986e-31,true,3,false,0.0003485,"[4.969793004253941, 1.1912045293502256]","[0.578266773695452, 0.5731748046648842]",0
20,3.0162802754615627e-32,true,4,false,0.0004004,"[4.969793004253903, 1.1912045293502278]","[-1.1325841216738501, -0.6360254772372482]",1
35 changes: 20 additions & 15 deletions src/functions_estimation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ Base.@kwdef mutable struct GMMOptions
optim_autodiff::Symbol = :none
optim_algo = LBFGS()
optim_opts = nothing
optim_algo_bounds::Bool = false
lower_bound = nothing
upper_bound = nothing
write_iter::Bool = false # write to file each result (each initial run)
clean_iter::Bool = false #
overwrite::Bool = false
Expand Down Expand Up @@ -418,23 +421,25 @@ function fit_onerun(
# load the data
gmm_objective_loaded = theta -> gmm_objective(theta, data, mom_fn, W, weights, trace=opts.trace)

# optimize
if opts.optim_autodiff == :forward
(opts.trace > 0) && print("using AD")
time_it_took = @elapsed raw_opt_results = Optim.optimize(gmm_objective_loaded,
theta0,
opts.optim_algo, # defalut = LBFGS()
opts.optim_opts,
autodiff=:forward)

# results = optimize(f, g!, lower, upper, initial_x, Fminbox(GradientDescent()), Optim.Options(outer_iterations = 2))

# Optim.jl optimize
optim_main_args = []
push!(optim_main_args, gmm_objective_loaded)
if opts.optim_algo_bounds
push!(optim_main_args, opts.lower_bound)
push!(optim_main_args, opts.upper_bound)
end
push!(optim_main_args, theta0)

push!(optim_main_args, opts.optim_algo) # defalut = LBFGS()
push!(optim_main_args, opts.optim_opts)
if opts.optim_autodiff != :none
kwargs = (autodiff = :forward, )
else
time_it_took = @elapsed raw_opt_results = Optim.optimize(gmm_objective_loaded,
theta0,
opts.optim_algo, # defalut = LBFGS()
opts.optim_opts)
kwargs = ()
end

time_it_took = @elapsed raw_opt_results = Optim.optimize(optim_main_args...; kwargs...)

#=
summary(res)
minimizer(res)
Expand Down
2 changes: 1 addition & 1 deletion src/functions_regtable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ has_fe(m::GMMModel) = false
# RegressionTables.get_coefname(x::Tuple{Vararg{Term}}) = RegressionTables.get_coefname.(x)
# RegressionTables.replace_name(x::Tuple{Vararg{Any}}, a::Dict{String, String}, b::Dict{String, String}) = [RegressionTables.replace_name(x[i], a, b) for i=1:length(x)]

RegressionTables.formula(m::GMMModel) = term(m.responsename) ~ sum(term.(String.(m.coefnames)))
# RegressionTables.formula(m::GMMModel) = term(m.responsename) ~ sum(term.(String.(m.coefnames)))

StatsAPI.coef(m::GMMModel) = m.coef
StatsAPI.coefnames(m::GMMModel) = m.coefnames
Expand Down

0 comments on commit de00efa

Please sign in to comment.