Skip to content

Commit

Permalink
Merge branch 'main' into compathelper/new_version/2023-11-24-01-06-31…
Browse files Browse the repository at this point in the history
…-213-03104436089
  • Loading branch information
JohannesNaegele authored May 6, 2024
2 parents f598d83 + 65bccd0 commit 9c1625f
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 12 deletions.
6 changes: 5 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ Distributions = "0.25"
ForwardDiff = "0.10"
MacroTools = "0.5"
NLsolve = "4"
NonlinearSolve = "2"
NonlinearSolve = "3"
Optimization = "3"
OptimizationOptimJL = "0.1"
OrderedCollections = "1"
Pipe = "1"
Statistics = "1"
Zygote = "0.6"
julia = "1"

[extras]
Expand Down
7 changes: 6 additions & 1 deletion examples/bayesian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ priors_dict = @parameters begin
α_1 = Uniform()
α_2 = Uniform()
end
unobserved = @variables Y, T, YD, C

# let's say we know that some kind of variables is prone to measurement error
# if it is just a constant, we can introduce some bias variable
Expand Down Expand Up @@ -72,4 +73,8 @@ df[!, :period] = 1:nrow(df)
y=:value,
color=:variable,
Geom.line
)
)

function loglikelihood(results, model, exos, params_dict, unobserved, particles=10000)
# return log(sum(...))
end
19 changes: 18 additions & 1 deletion src/ConstructResiduals.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function construct_residuals(name, function_body, args)
function construct_residuals_for_print(name, function_body)
f! = quote
function f!($(name[1]), $(name[2]), $(name[3]), $(name[4]), $(name[5]))
nothing
Expand All @@ -10,6 +10,23 @@ function construct_residuals(name, function_body, args)
function_body[i] = :($(name[1])[$i] = $(function_body[i]))
end

# add function body to function
f!.args[2].args[end] = Expr(:block, function_body...)
return f!
end

function construct_residuals(name, function_body)
f! = quote
($(name[1]), $(name[2]), $(name[3]), $(name[4]), $(name[5])) -> begin
nothing
end
end

# construct function body
for i in eachindex(function_body)
function_body[i] = :($(name[1])[$i] = $(function_body[i]))
end

# add function body to function
f!.args[2].args[end] = Expr(:block, function_body...)
return f!
Expand Down
13 changes: 8 additions & 5 deletions src/Macros.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function build_f!(endos, exos, params, args)
function build_f!(endos, exos, params, args, verbose=false)
endos = endos.variables
exos = exos.variables
params = params.variables
Expand Down Expand Up @@ -26,7 +26,11 @@ function build_f!(endos, exos, params, args)
end

# construct function for residuals of model variables
return MacroTools.striplines(:(Consistent.f! = $(construct_residuals(name, function_body, args))))
if verbose
return construct_residuals_for_print(name, function_body)
else
return construct_residuals(name, function_body)
end
end

"""
Expand Down Expand Up @@ -80,16 +84,15 @@ function model(;
end

if verbose
println(build_f!(endos, exos, parameters, eqs.exprs))
println(MacroTools.striplines(build_f!(endos, exos, parameters, eqs.exprs, true).args[2]))
end

eval(build_f!(endos, exos, parameters, eqs.exprs))
return Model(
endos,
exos,
parameters,
eqs,
deepcopy(Consistent.f!)
eval(build_f!(endos, exos, parameters, eqs.exprs))
)
end

Expand Down
11 changes: 7 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ using Test
z = y * (y[-1] + 0.5 * z) * θ + x[-1]
y = z[-2] * x * b
end
@test Consistent.replace_vars(test_eqs.args[[2, 4]], [:z, :y], Symbol[:x], []) == [
:(endos[1] = endos[2] * (lags[2, end - 0] + 0.5 * endos[1]) * params[1] + exos[1, end + -1]),
:(endos[2] = lags[1, end - -1] * exos[1, end + 0] * b)
]
replace_worked = @test_warn "Symbols [:b] are not in variables or parameters" Consistent.replace_vars(
test_eqs.args[[2, 4]], [:z, :y], Symbol[:x], []
) == [
:(endos[1] = endos[2] * (lags[2, end - 0] + 0.5 * endos[1]) * params[1] + exos[1, end + -1]),
:(endos[2] = lags[1, end - -1] * exos[1, end + 0] * b)
]
@test replace_worked

# test non-equation
let eqs = @equations begin
Expand Down

0 comments on commit 9c1625f

Please sign in to comment.