diff --git a/examples/bayesian.jl b/examples/bayesian.jl index 25c2013..e72b9ae 100644 --- a/examples/bayesian.jl +++ b/examples/bayesian.jl @@ -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 @@ -72,4 +73,8 @@ df[!, :period] = 1:nrow(df) y=:value, color=:variable, Geom.line - ) \ No newline at end of file + ) + +function loglikelihood(results, model, exos, params_dict, unobserved, particles=10000) + # return log(sum(...)) +end \ No newline at end of file diff --git a/src/ConstructResiduals.jl b/src/ConstructResiduals.jl index b85e83d..d40bb8a 100644 --- a/src/ConstructResiduals.jl +++ b/src/ConstructResiduals.jl @@ -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 @@ -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! diff --git a/src/Macros.jl b/src/Macros.jl index a99c3f3..e6c2b51 100644 --- a/src/Macros.jl +++ b/src/Macros.jl @@ -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 @@ -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 """ @@ -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