Julia

Anirudh Yadav 2020-02-04 5 minute read

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.

Plotting

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

Krusell-Smith in Julia

This topic also deserves its own post.

Parallelization

This topic desrves it’s own post. But for now, the posts below about parallelizing value function iterations could be useful:

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:

  1. The julia folder which must be added to PATH via ~/.bashrc is located at: /Applications/Julia-<version>.app/Contents/Resources/julia/bin
  2. 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.