This is lecture is a slightly modified version of https://lectures.quantecon.org/jl/getting_started.html Thank you to the amazing Quantecon.org team!
In this lecture we will cover how to get up and running with Julia
Topics:
While you will eventually use other editors, there are some advantages to starting with Jupyter while learning Julia:
Whether you end up using Jupyter as your primary work environment or not, you’ll find learning about it an excellent investment
Download and install Julia, from download page , accepting all default options.
Open Julia, by navigating to Julia through your menus or desktop icons.
You should now be looking at something like this (modulo your operating system)
This is called the JULIA REPL (Read-Evaluate-Print-Loop), which we discuss more below
] add IJulia InstantiateFromURL; precompile
This installs enough Julia packages to begin using the lecture notes Note: On OS/X you will need to type the ] separately and cannot copy/paste the whole string
using IJulia
notebook(dir=".",detached=true)
this will open a notebook browser in directory dir
.
Another Approach is to install the latest version Anaconda and then Julia
Install Anaconda by downloading the binary https://www.anaconda.com/download/
To run Jupyter, open a terminal or windows console, cd
to the location you wish to write files and type
jupyter lab
After you started the notebook browser from the REPL, you will see something like:
To open an empty julia notebook, click on new and select julia 1.0
.
To use the curated set of packages in the QuantEcon lecture notes,
put the following text in a notebook cell, and hit Shift-Enter
to run the cell
using InstantiateFromURL
# activate the QuantEcon environment
activate_github("QuantEcon/QuantEconLecturePackages", tag = "v0.9.5");
This downloads, installs, and compiles the correct version of all of packages used in the QuantEcon lectures
Depending on your computer, this may take 10-15 minutes to run the first-time, but be virtually instantaneous thereafter
This code can be put at the top of any notebook in order to get a tested set of packages compatible with the code in the QuantEcon notes
More details on packages are explained in a later lecture
Notice that in the previous figure the cell is surrounded by a blue border
This means that the cell is selected, and double-clicking will place it 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
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
Command mode
(To learn about other commands available in command mode, go to “Keyboard Shortcuts” in the “Help” menu)
Esc
key Enter
or click in a cell 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
include("filename")
in a cell The present working directory can be found by executing the command pwd()
Let’s generate some plots
First, ensure that you have activated a set of packages within the current Jupyter notebook
using InstantiateFromURL
# activate the QuantEcon environment
activate_github("QuantEcon/QuantEconLecturePackages", tag = "v0.9.5");
Now try copying the following into a notebook cell and hit Shift-Enter
using Plots
gr(fmt=:png);
plot(sin, -2π, 2π, label="sin(x)")
┌ Info: Recompiling stale cache file /Users/florian.oswald/.julia/compiled/v1.1/Plots/ld3vC.ji for Plots [91a5bcdd-55d7-5caf-9e0b-520d859cae80] └ @ Base loading.jl:1184
You’ll see something like this (although the style of plot depends on your installation — more on this later)
Note: The “time-to-first-plot” in Julia takes a while, since it needs to precompile everything
Let’s go over some more Jupyter notebook features — enough so that we can press ahead with programming
Tab completion in Jupyter makes it easy to find Julia commands and functions available
For example if you type rep
and hit the tab key you’ll get a list of all
commands that start with rep
To get help on the Julia function such as repeat
, enter ? repeat
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 Jupyter 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 status of installed packages 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
QuantEcon also hosts the QuantEcon Notes website, where you can upload and share your notebooks with other economists and the QuantEcon community
While we have not emphasized it, on any JupyterHub or local 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
For example, the REPL is what we used in the beginning of this lecture to install
InstantiateFromURL.jl
and IJulia
To start the REPL from the Jupyter notebook browser, click on "New", then "Terminal" in the dropdown menu:
Otherwise, if you have a local installation, then
julia
(Linux) 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.9.4 Status `~/Dropbox/teaching/ScPo/ScPo-CompEcon/CoursePack/Notebooks/.projects/QuantEconLecturePackages-v0.9.5/Project.toml` [6e4b80f9] BenchmarkTools v0.4.1 [3da002f7] ColorTypes v0.7.5 [34da2185] Compat v1.4.0 [a93c6f00] DataFrames v0.15.2 [1313f7d8] DataFramesMeta v0.4.0 [39dd38d3] Dierckx v0.4.1 [31c24e10] Distributions v0.16.4 [2fe49d83] Expectations v1.0.2 [f6369f11] ForwardDiff v0.10.1 [28b8d3ca] GR v0.36.0 [a98d9a8b] Interpolations v0.11.0 [5ab0869b] KernelDensity v0.5.1 [b964fa9f] LaTeXStrings v1.0.3 [76087f3c] NLopt v0.5.1 [2774e3e8] NLsolve v3.0.1 [429524aa] Optim v0.17.2 [d96e819e] Parameters v0.10.2 [91a5bcdd] Plots v0.22.0 [f27b6e38] Polynomials v0.5.1 [1fd47b50] QuadGK v2.0.3 [fcd29c91] QuantEcon v0.15.0 [295af30f] Revise v0.7.14 [f2b01f46] Roots v0.7.4 [60ddc479] StatPlots v0.8.2 [90137ffa] StaticArrays v0.10.0 [37e2e46d] LinearAlgebra [9a3f8284] Random [2f01184e] SparseArrays [10745b16] Statistics [8dfed614] Test
There are alternative workflows, such as
Eventually, you will want to 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 an easy solution
If you are using 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
Docker is a technology that you use to host a “virtual” version of a software setup on another computer
While it is largely used for running code in the cloud, it is also convenient for using on local computers
QuantEcon has constructed a pre-built docker image
For instructions on how to set this up, see the tools and editors lecture
Note: The Docker installation is easy and complete, but it has limitations on operating systems (in particular, Windows 10 is only supported for the Professional and Education editions, and not the Home edition)