August 2, 2023

Goals for Today

  1. Understand how your computer works and interacts with programming languages.
  2. Learn some advanced Julia concepts, especially to improve how fast your code runs.

Machine Language

  • Computers are very good at performing calculations and executing algorithms.
  • Unfortunately, a computer’s hardware only understands things written in machine language.
  • A machine language consists of instructions written using only of binary 0’s and 1’s.
  • This machine language cannot easily be understood by humans.

Progamming Languages

  • Programming languages act as the translator between you and your computer
  1. You write a set of instructions in a programming language, such as Julia.
  2. When you execute your code, these instructions get translated to machine language.
  3. Your computer executes the task.
  • How fast your code runs will be impacted by:
  1. How you write your code.
  2. How your instructions get translated to machine language.
  3. What computer you are using.

Compiled vs. Interpretted Languages

  • Programming languages generally fall into two groups:
  1. Compiled languages (C, Fortran)
  • All of the code gets translated before execution.
  • Generates very efficient machine code.
  • Cannot code interactively or make changes on the fly.
  1. Interpretted languages (Stata, R, Python, Matlab)
  • Translates each line of code just before its execution.
  • This allows for dynamic and interactive coding.
  • Will not generate efficient machine code.

Just-in-time compilation

  • Julia tries to have the best of both worlds with just-in-time compilation.
  • When executing a piece of code, such as a function, for the first time Julia compiles it to machine language.
  • When executing the next time, Julia machine language is called directly instead of re-compiling.
  • This allows for the benefits of interactive coding of an interpretted language.
  • Efficient machine code can also be generated if you follow best practices.

https://docs.julialang.org/en/v1/manual/performance-tips/

Optimal Investment Problem

  • Given a capital \(k\), we have resources: \[ y = k^\alpha + (1-\delta)k. \]
  • We live forever and we discount future at rate \(\beta\). Utility from consumption is \(\log(c)\).
  • We need to decide how much to consume \(c\) and how much capital to save \(k'\).

\[ V(k) = \max_{c,k'} \:\:\: \log(c) + \beta V(k'). \] \[ c + k' = k^\alpha + (1-\delta)k. \]

Solving for V

  • We know that this is a contraction mapping, so we can iterate over \(V\) until convergence.
  • Guess \(V_1\). Solve

\[ V_2(k) = \max_{c,k'} \:\:\: \log(c) + \beta V_1(k'). \] - Then: \[ V_3(k) = \max_{c,k'} \:\:\: \log(c) + \beta V_2(k'). \] - We know that \(V_n \xrightarrow{} V\) as \(n \xrightarrow{} \infty\).