Getting Started with Julia

Julia is a young programming language that is gaining speed and usage in economics. There’s a growing number of sites exploring and teaching Julia in the context of economics. One great site is quantecon.org, and I hope to outline a few more examples in the future. To get started in Julia, here are a few tips, ordered by my subjective idea of how important they are to getting started:

  • Code Editor: Use Microsoft Virtual Studio Code (VS Code). It is now the default editor for the Julia community; it’s free; other editors are depreciated and everyone’s moved to VS Code. This also allows for “CodeTogether” sessions where collaborators can edit the same code, and you can use VS Code to do cloud computing in JuliaHub, too.
  • Julia in VS Code: Install the Julia VS Code extension. This allows you to run line-by-line chunks of Julia code in a REPL (read–eval–print loop, like in R studio, stata, PyCharm, etc), and includes a debugger which can be super useful for problems in dynamic programming because we’re often running functions of functions.
  • Debugging: If you haven’t used a debugger before, definitely look up some short tutorials. Utilizing debugging tools can cut out a lot of copy/paste time and re-running code.
  • Dataframes: If you end up loading data into dataframes, definitely use the DataFrames.jl and DataFramesMeta.jl packages. They make the syntax of manipulating dataframes and running functions on dataframes very nice.
  • Notebooks: If you want to use an interactive notebook that can mix markdown and code, Jupyter notebooks work (the “Ju” in Jupyter stands for Julia). This is useful for homework sets, short reports, and exploration where you need a lot of documentation. You can run the Jupyter notebook in the browser, but I suggest opening it in VS Code (this works with the Julia extension). One common issue I see is people getting different results when they run the notebook chunks in a different order. For this reason, also consider Pluto notebooks, which allow all chunks to be interactively updated when a dependent chunk is changed. This means the order of chunks will not effect the output. Pluto also has a great suite of interactive tools which are great for both exploration of a problem and classroom education (variable sliders, drop-down options, etc). There is also the less-reactive Neptune notebooks (read that link for the code-instances you might use Neptune over Pluto).
  • For loops: Don’t be afraid of using for loops. In R and python, functions often need to be vectorized and the syntax can be confusing. One of the benefits of Julia is that it’s fast and for loops are just as fast as vectorized functions. It’s generally agreed in the Julia community that for loops are more readable in many cases, especially educational settings, so just use for loops.
  • Optimizing functions: To optimize functions (maximize or minimize), definitely use the optim.jl package. It’s standard use, basically the same syntax as standard python and matlab optimizers, and has a growing number of advanced algorithms (the most basic ones we’ll need are there).
  • Other useful packages: If doing anything with expectations or probability, you’ll need the Random and Distributions packages, perhaps the Expectations package. StatsPlots is nice for making cool plots (it has some convenience functions built of the more basic Plots package). You might need FiniteDifferences if you need a completely numerical derivative, but there’s also Symbolics for symbolic differentiation and Zygote for auto-differentiation. Optim.jl will try to run auto-differentiation on your functions if possible, but if it’s not a nice (smooth) function, you might need to explicitly give optim a finite differences derivative of your functions. For more on differentiation options in Julia, see juliadiff.org.
  • Using greek letters and subscripts: In VS Code, you can use latex-style greek letters and subscripts by typing \alpha <tab> and it will autocomplete into the greek letter. This is built into the Julia extension. Don’t go overboard — you can add subscripts and lots of symbols but that also means you need to type \alpha <tab>  every time you want to use it. I think a nice balance is to use greek letters in the top-level equations to make it obvious how the typed equations relate to the theoretical equations. But when applying them to dataframes many times, it’s probably just easier to apply f(b) instead of f(β) if f is already defined.
  • Reading the stacktrace: When you get an error, be sure to start at the top of the stacktrace if it’s an error with a built-in package or with the package installation system. The stacktrace is the list of errors, in the order that they occurred. In Julia, you want to start at the top/first error; the first error often has the most useful hint (e.g. ERROR: SystemError: close: No space left on device means that you have no space left on the disc you are trying to write information to). If it’s an error with your own function, you will often have to start at the first error and work your way down each error until you find where you made a coding mistake. You often can skip all the detailed information about each error, and just move from one ERROR statement to the next until you find the one that you can fix.

Published by acwatt

PhD student at Berkeley Agricultural and Resource Economics. Research interests: energy, low-carbon transitions, climate change, exhaustible resource economics

Leave a comment