Skip to content

Latest commit

 

History

History
156 lines (130 loc) · 4.49 KB

README.md

File metadata and controls

156 lines (130 loc) · 4.49 KB

Large-Scale OLG Modeling

I am learning how to model some Large-Scale OLG models (life cycle) in Julia.

must-read

others

Books

with: Python implementation

Other repo

Running Julia Scripts

  • Cheat Sheet
  • After testing the functionality of the code in .ipynb, the entire code must be wrapped inside a main() function. Then, the script can be called and run in the terminal.
    using Packages
    
    function main()
      the code
    end
    
    @time main()
    
    Run Julia from the terminal
    cd "path_to_script_containing_folder"
    julia script.jl

Running Dynare

in Julia

(*) does not work all the time

Documentation https://juliapackages.com/p/dynare

  • Installation
using Pkg
pkg"add Dynare"
  • Running
using Dynare

(if there is an error related to 'OpenBLAS32' )

import LinearAlgebra, OpenBLAS32_jll
LinearAlgebra.BLAS.lbt_forward(OpenBLAS32_jll.libopenblas_path)
  • Invoke .mod' file
context = @dynare "path/model.mod";

The results are stored in the context structure.

  • View results The context structure is saved in the directory <path to modfile>/<modfilenane>/output/<modfilename>.jld2. It can be loaded with
using JLD2
DD = load("<path to modfile>/<modefilename>/output/<modefilename>.jld2")``

The IRF graphs are saved in <path to modfile>/<modfilenane>/graphs.

in Matlab

Quick Start: here

addpath /Applications/Dynare/x.y/matlab
cd '/Users/USERNAME/work'
dynare example1

to edit

edit example1.mod

Snippets

Nonlinear Solver Example

# Pkg.add("NLsolve")
using NLsolve
# solving for x,y -> z, with parameters a,b,c
function system_eq!(F, z, a, b, c)
  x, y = z
  F[1] = x^2 + y^2 - a + 2b
  F[2] = (x * y)^c - a - b^2
end
#specify values of parameters
a = 1.0  
b = 2.0
c = 2.0
z_guess = [0.0, 0.0]  # Initial guess for x and y
# Solve the system of equations
result = nlsolve((F, z) -> system_eq!(F, z, a, b, c), z_guess)
# Extract the solution
x_solution = result.zero[1]
y_solution = result.zero[2]

Note: When applying this solver, the correct guess is very important. With backward iteration, remember to take the previously solved known value as the guess. For example, in Auerbach-Kotlikoff, the guess to solve k[39] should take the initial guess of k,n in k[40], k[41], and n[40].

Loops

  • while loop
max_iter = 10
i = 1
tol = 1e-6
err = 0.1
while i <= max_iter && abs(err) > tol
  do something
  if abs(err) > tol
    i += 1
  else
    break
  end
end
  • for loop forward
for s in 1:1:60
  k[s] = solve(k[s-1)
end
  • for loop backward
for s in 60:-1:1
  k[s] = solve(k[s+1])
end