In this lecture we will cover how to get up and running with Julia
Topics:
Like Python, and unlike Matlab and Stata, there is a looser connection between Julia as a programming language and Julia as a development environment
Because of this, you will have different options for editing code on your local computer or in the cloud
Several of the options rely on Jupyter which provides a browser-based interface to access computational kernels for different languages (e.g. Julia, Python, R, etc.)
While you will eventually use other editors, there are some advantages of starting with Jupyter while learning the language
Whether you end up using Jupyter as your primary work environment or not, you’ll find learning about it an excellent investment
In order of priority, for those just getting started, we suggest
Eventually, you will both want to do a local installation and move from just using jupyter to using other tools and editors such as Atom/Juno, but don’t let the environment get in the way of learning the language
If you have access to a cloud based solution for Jupyter, then that is typically the easiest solution
www.syzygy.ca <www.syzygy.ca>
_ and juliabox.com <www.juliabox.com>
_ )- Students: ask your department if these resources are availableIf you have are given an online Jupyter installation for a class, you may not need to do anything to begin using these notebooks
Otherwise, if there are errors when you attempt to use an online Jupyterhub, you will need to go open a Jupyter notebook and type
none
] add InstantiateFromURL
Docker is a technology that you use to host a “virtual ” version of a minimal, self-contained operating system on another computer
While it is largely used for running code in the cloud and in distributed computing, it is also convenient for using on local computers
In essence, it allows people to share a consistent and reproducible environment with no setup or tinkering required
QuantEcon has constructed a pre-built docker image
Download and install
Open a terminal on OS/X and Linux, or a “Windows PowerShell” terminal on Windows
To verify that there are no obvious errors in the installation, runRun docker version
in the terminal to check there are no obvious errorsDownload the QuantEcon Docker image by running the following in your terminal (this may take some time depending on your internet connection)
none
docker pull quantecon/base
After this is finished, first clear any existing volumes and then create a persistent storage volume (ignoring any warnings from the rm step)
none
docker volume rm quantecon
docker volume create quantecon
The Docker image can exchange files locally (and recursively below in the tree) to where it is run
Open a terminal and cd to the directory you are interested in storing local files
To run an image local to those files, type the following in the terminal (on Linux and OSX)
none
docker run --rm -p 8888:8888 -v quantecon:/home/jovyan/.julia -v "$(pwd)":/home/jovyan/local quantecon/base
And on Powershell on Window
none
docker run --rm -p 8888:8888 -v quantecon:/home/jovyan/.julia -v ${PWD}:/home/jovyan/local quantecon/base
Remark: When you call this command, Docker may require you to give it permissions to access the drive and the network. If you do not see the output within 20 or so seconds, then look for confirmation windows which may be hidden behind the terminal/etc.
In the output, you should see some text near that bottom that looks like
none
127.0.0.1):8888/?token=7c8f37bf32b1d7f0b633596204ee7361c1213926a6f0a44b
Copy the text after ?token= (e.g. 7c8f37bf32b1d7f0b633596204ee7361c1213926a6f0a44b)
In a browser, go to a URL like the following
none
http://127.0.0.1:8888/lab
Note: Ctrl+C is also the keyboard shortcut you use to kill the container. So be sure to copy using the mouse.
Paste the text into Password or token: and choose Log in
Ensure you save any important files to the local notebooks in the local directory, which stores in the local filesystem where you ran the docker command
A few useful maintenance features
While using the Docker instance is convenient and error-proof, you may eventually want to install things locally
The easiest way to do that is to install Anaconda for a full Python and Jupyter environment,
Install Anaconda by: downloading the binary (3.7 version) and install
This could be in a separate section* Note that the packages in Anaconda update regularly --- you can keep up to date by typing conda update anaconda
in a terminalDownload and install Julia, from download page , accepting all default options.
Assuming there were no problems, you should now be able to start Julia either by
Either way you should now be looking at something like this (modulo your operating system — this is a Linux machine)
In that julia terminal, type the following
] add IJulia InstantiateFromURL Revise
Updating registry at `~/.julia/registries/General` Updating git-repo `https://github.com/JuliaRegistries/General.git` Resolving package versions... Installed Tables ───────── v0.1.11 Installed NLsolve ──────── v3.0.1 Installed DiffEqJump ───── v5.6.0 Installed Optim ────────── v0.17.2 Installed OrdinaryDiffEq ─ v4.17.2 Updating `~/.julia/environments/v1.0/Project.toml` [2774e3e8] ↑ NLsolve v2.1.0 ⇒ v3.0.1 [429524aa] ↑ Optim v0.17.1 ⇒ v0.17.2 Updating `~/.julia/environments/v1.0/Manifest.toml` [c894b116] ↑ DiffEqJump v5.5.0 ⇒ v5.6.0 [d3d80556] ↑ LineSearches v7.0.0 ⇒ v7.0.1 [2774e3e8] ↑ NLsolve v2.1.0 ⇒ v3.0.1 [429524aa] ↑ Optim v0.17.1 ⇒ v0.17.2 [1dea7af3] ↑ OrdinaryDiffEq v4.17.0 ⇒ v4.17.2 [bd369af6] ↑ Tables v0.1.10 ⇒ v0.1.11
Then, install and precompile all of the key packages for these lecture notes (which may take 10-20 minutes),
using InstantiateFromURL
activate_github("QuantEcon/QuantEconLecturePackages", tag = "v0.9.0") # activate the QuantEcon environment
using LinearAlgebra, Statistics, Compat # load common packages
To run Jupyter, in a terminal cd to the location you wish to modify local files in and type
none
jupyter lab
For convenience, you may find it useful on your operating system to change the directory where the REPL starts
Whenever the Julia compiler or REPL starts, it will look for a file called startup.jl (see Julia Manual)
The location for the file is relative to your default Julia environment (e.g. ~/.julia/config/startup.jl or C:\Users\USERNAME.julia\config\startup.jl on Windows)
To add one, first create the ~/.julia/config/ directory if necessary in the terminal or file explorer (to find the ~/.julia location in the REPL, you can look at the output of ] st)
Next, either download the file startup.jl into that directory, or create a file and paste in the following text
import REPL
Base.atreplinit() do repl
# make the ; shell mode sticky
!isdefined(repl, :interface) && (repl.interface = REPL.setup_interface(repl))
repl.interface.modes[2].sticky=true
end
try
@eval using Revise
# Turn on Revise's automatic-evaluation behavior
Revise.async_steal_repl_backend()
catch err
@warn "Could not load Revise."
end
After you have started Jupyter (either on the cloud, the docker, or locally installed on your computer)
You should see something (not exactly) like this
The page you are looking at is called the “dashboard”
The address localhost:8888/tree you see in the image indicates that the browser is communicating with a Julia session via port 8888 of the local machine
If you click on “New” you should have the option to start a Julia notebook
Here’s what your Julia notebook should look like
The notebook displays an active cell, into which you can type Julia commands
Notice that in the previous figure the cell is surrounded by a green border
This means that the cell is in edit mode
As a result, you can type in Julia code and it will appear in the cell
When you’re ready to execute these commands, hit Shift-Enter instead of the usual Enter
The next thing to understand about the Jupyter notebook is that it uses a modal editing system
This means that the effect of typing at the keyboard depends on which mode you are in
The two modes are
Edit mode
Indicated by a green border around one cell, as in the pictures above
Whatever you type appears as is in that cell-
Command mode
The green border is replaced by a blue border
Key strokes are interpreted as commands — for example, typing b adds a new cell below the current one-
(To learn about other commands available in command mode, go to “Keyboard Shortcuts” in the “Help” menu)
The modal behavior of the Jupyter notebook is a little tricky at first but very efficient when you get used to it
To run an existing Julia file using the notebook you can copy and paste the contents into a cell in the notebook
If it’s a long file, however, you have the alternative of
The present working directory can be found by executing the command pwd()
using InstantiateFromURL
activate_github("QuantEcon/QuantEconLecturePackages", tag = "v0.9.0") # activate the QuantEcon environment
using LinearAlgebra, Statistics, Compat # load common packages
Now try copying the following into a notebook cell and hit Shift-Enter
using Plots
gr(fmt=:png)
plot(sin, -2pi, pi, label="sine function")
You’ll see something like this (although the style of plot depends on your installation — more on this later)
Let’s go over some more Jupyter notebook features — enough so that we can press ahead with programming
A simple but useful feature of IJulia is tab completion
For example if you type rep and hit the tab key you’ll get a list of all commands that start with rep
IJulia offers up the possible completions
This helps remind you of what’s available and saves a bit of typing
To get help on the Julia function such as repmat, enter ?repmat
Documentation should now appear in the browser
In addition to executing code, the Jupyter notebook allows you to embed text, equations, figures and even videos in the page
For example, here we enter a mixture of plain text and LaTeX instead of code
Next we Esc to enter command mode and then type m to indicate that we are writing Markdown, a mark-up language similar to (but simpler than) LaTeX
(You can also use your mouse to select Markdown from the Code drop-down box just below the list of menu items)
Now we Shift + Enter to produce this
Julia supports the use of unicode characters such as α and β in your code
Unicode characters can be typed quickly in Jupyter using the tab key
Try creating a new code cell and typing \alpha, then hitting the tab key on your keyboard
You can execute shell commands (system commands) in IJulia by prepending a semicolon
For example, ;ls will execute the UNIX style shell command ls, which — at least for UNIX style operating systems — lists the contents of the current working directory
These shell commands are handled by your default system shell and hence are platform specific
You can enter the package manager by prepending a ]
For example, ] st will give the the current status of installed pacakges in the current environment
Notebook files are just text files structured in JSON and typically end with .ipynb
A notebook can easily be saved and shared between users — you just need to pass around the ipynb file
To open an existing ipynb file, import it from the dashboard (the first browser page that opens when you start Jupyter notebook) and run the cells or edit as discussed above
The Jupyter organization has a site for sharing notebooks called nbviewer which provides a static HTML representations of notebooks
However, each notebook can be downloaded as an ipynb file by clicking on the download icon at the top right of its page
While we have not emphasized it, on any JupyterHub or locally installed Jupyter installation you will also have access to the Julia REPL
This is a Julia specific terminal disconnected from the graphical interface of Jupyter, and becomes increasingly important as you learn Julia
To start the REPL on a typical jupyterlab environment
Otherwise, if you have a local installation, then
The REPL is one of the best places to add and remove packages, so a good test is to see the current status of the package manager
] st
Project QuantEconLecturePackages v0.1.0 Status `~/Research/lecture-source-jl/notebooks/.projects/QuantEconLecturePackages-v0.9.0/Project.toml` [aae01518] BandedMatrices v0.7.1 [6e4b80f9] BenchmarkTools v0.4.1 [3da002f7] ColorTypes v0.7.5 [34da2185] Compat v1.3.0 [a93c6f00] DataFrames v0.14.1 [1313f7d8] DataFramesMeta v0.4.0 [31c24e10] Distributions v0.16.4 [2fe49d83] Expectations v1.0.2 [1a297f60] FillArrays v0.3.0 [f6369f11] ForwardDiff v0.10.1 [28b8d3ca] GR v0.35.0 [7073ff75] IJulia v1.13.0 [a98d9a8b] Interpolations v0.10.5 [5ab0869b] KernelDensity v0.5.1 [b964fa9f] LaTeXStrings v1.0.3 [5078a376] LazyArrays v0.3.2 [76087f3c] NLopt v0.5.1 [2774e3e8] NLsolve v3.0.0 [429524aa] Optim v0.17.1 [d96e819e] Parameters v0.10.1 [91a5bcdd] Plots v0.21.0 [f27b6e38] Polynomials v0.5.1 [1fd47b50] QuadGK v2.0.2 [fcd29c91] QuantEcon v0.15.0 [295af30f] Revise v0.7.12 [f2b01f46] Roots v0.7.3 [60ddc479] StatPlots v0.8.1 [90137ffa] StaticArrays v0.9.2 [37e2e46d] LinearAlgebra [9a3f8284] Random [2f01184e] SparseArrays [10745b16] Statistics [8dfed614] Test