Julia
Julia/SLURM
Perfomance tips
The foremost perfomance tip for Julia is to avoid globals. That is, wrap code in functions when benchmarking.
Type-stability is also important for performance. A function is type stable if it always returns a value of the same type. When Julia has good information on types it can optimize the underlying code.
- Performance Tips (Docs)
- Huda Nassar’s JuliaCon2020 talk on performance tips; Notebook. HN’s goes through all of the main performance tips + some others on reducing memory allocation, with useful examples.
- Performance Tips for Julia (Purdue)
- Writing Type-Stable Code in Julia
- Why Does Julia Work So Well?
- Ronan’s post on pre-allocation
Plotting
Named colours
http://juliagraphics.github.io/Colors.jl/stable/namedcolors/.
Annotating
annotate()!
is very useful. Note that using TeX and non-TeX in the same annotation is fiddly. See PhiExperiment.jl
for examples. Not sure if that file has the cleanest implementation, but it seems to work fairly well.
Axis label text rotation
The attribute guidefontrotation
and (yguidefontrotation
/xguidefontrotation
) is supposed to control the rotation of the axes labels. However, when I tried using it, it actually control the axis label font SIZE, which obviously seems like a bug. I should make a MWE and report this as a bug on the Plots.jl github page.
Manual legend positioning
After much googling, it looks like you cannot set the legend position numerically using Plots
(only allows for keywords like bottomright, topleft, etc.). PyPlot
allows numeric positioning, but I think you have to create an actual PyPlot plot, rather than using Plots with the PyPlot backend. But, you can annotate plots using annotate!()
Other useful plotting resources
- A blog post about making “publication quality” in Julia.
- Plotting for publications. #897: has some nice tips on setting font defaults, etc.
Krusell-Smith in Julia
This topic also deserves its own post.
- QuantEcon notebook on solving KS in Julia
- Joao Rodrigues’s comp econ repo: contains replication of KS with and without endogenous labor supply.
Parallelization
This topic desrves it’s own post. But for now, the posts below about parallelizing value function iterations could be useful:
- GitHub repo for Fernandez-Villaverde and Zarruk’s (FVZ) parallel computing survey: code for solving a basic lifecycle savings model in Julia using
Threads
,distributed
andpmap
. - Using the GPU to do Value Function Iteration: Florian Oswald replicates FVZ’s Julia code, and also shows how to parallelize using the GPU (graphics card).
- Examples of parallel value function iteration in Julia: solves a simple consumption-savings model using
Threads
andpmap
; findsThreads
is 2x faster in this very simple application, but suggestspmap
improves with the complexity of the problem.
In my own limited testing, Threads
, distributed
and pmap
all seem roughly the same speed for my third-year-paper application (I will write a notebook about this). This result seems consistent with Oswald’s post above. Note that FVZ document that pmap
performs poorly in their application, but they suggest that this is because their model is very simple (so overhead outweights the benefits of pmap
); but since my model is a little more complicated, it makes sense that pmap
performs just about the same as Threads
and distributued
.
Update: during my third-year-paper I found that distributed
worked the best on econstat6. For some reasons threads
seemed to perform quite poorly on econstat6; could be related to the hardware, somehow. I’ve also found that my distributed
code uses around only 40-60% of CPU for my third-year-paper code. This low utilization is worrying, but could be related to some aspects of the program not being parallelizable (e.g. computing stationary distribution).
Miscellanious things
Repo for Perla, Tonetti, Waugh (2020)
https://github.com/jlperla/PerlaTonettiWaugh.jl
The GitHub repo for an AER paper seems like a good way to organise code, etc. Probably has some well written Julia code too.
Florian Oswald’s (Sciences Po) comp. econ course
https://scpo-compecon.github.io/CoursePack/
This guy’s computational econ course (in Julia) looks potentially useful!
I should check out how he implements VFI, etc.
CartesianIndicies
Tim Holy’s post about how to use CartesianIndicies
for multidimensional iterative algorithms is very cool!
Converting Jupyter notebook to .jl
Converting a Jupyter notebook to executable Julia script is easy with nbconvert. Just use the following command in Terminal:
jupyter nbconvert --to script my_julia_notebook.ipynb
Checking what packages are installed
Use ]
to enter pkg
mode, then type st
to check what packages are installed. This might be useful for Great Lakes, since I’ve installed most packages on the econstat servers.
Upgrading Julia + packages to a new release
For some reason there is little documentation of how to do this properly! Luckily, this great Discourse post does the job. Some macOS-specific notes:
- The julia folder which must be added to
PATH
via ~/.bashrc is located at:/Applications/Julia-<version>.app/Contents/Resources/julia/bin
- To carry packages from the old version of julia (e.g. v1.3) to the new one (e.g. 1.4) run the following commands in Terminal:
cp ~/.julia/environments/v1.3/Manifest.toml ~/.julia/environments/v1.4/Manifest.toml
cp ~/.julia/environments/v1.3/Project.toml ~/.julia/environments/v1.4/Project.toml
Only after you run these commands can you do Pkg.build("IJulia")
in a Julia session, which allows you to create Jupyter notebooks with the new version of julia.
Useful links
- Julia forum: a good place to look for answers
- Julia cheat sheet
- Kerby Shedden’s (CSCAR/UM Stats) Julia workshop GitHub repo contains some potentially useful code for basic data analysis (using DataFrames, CSV, etc.)
- Noteworthy differences from Matlab
- JuMP style guide: has some good tips about writing readable and maintainable code
- John Stachurski’s 2016 NYU Topics in Computational Economics summer course
- NY Fed’s DSGE model in Julia