Jupyter Notebook¶

by Professor Throckmorton
for Time Series Econometrics
W&M ECON 408/PUBP 616
Slides

This cell contains Markdown with the title (i.e., a section title preceded by #) and other important information.

Summary¶

This Jupyter Notebook contains cells with examples of Markdown, $\LaTeX$, and Python. These are versatile tools and the output is easy to create, read, and share.

Markdown¶

For Markdown basic syntax, please see https://www.markdownguide.org/basic-syntax/.

If you open this notebook in Jupyter Notebook or JupyterLab, which are both available in Anaconda Navigator, there is a dropdown menu in the toolbar above to select whether a cell contains Markdown or Code (and highlighted in the red box in the following screenshot).
toolbar screenshot
Note: I embeded this screenshot by simply copying and pasting the image into this Markdown cell. You may also save images in a/the sub/directory where the notebook is located and embed them, e.g., ![toolbar screenshot](jupyter-notebook-toolbar.png).

$\LaTeX$¶

You can typeset $\LaTeX$ in a Markdown cell. Here is a $\LaTeX$ math cheat sheet.

$\LaTeX$ surrounded by a single dollar sign, $...$, is typeset inline, e.g., $y = x^2$ becomes $y = x^2$. However, $\LaTeX$ surrounded by double dollar signs, $$...$$, is typeset on a separate line. For example, the Gaussian function, or the normal distribution's probability density function (PDF), is

$$ f(x) = \frac{1}{\sqrt{2\pi\sigma^2}}\exp\left\{\frac{(x-\mu)^2}{2\sigma^2}\right\}. $$

is the result of

$$
  f(x) = \frac{1}{\sqrt{2\pi\sigma^2}}\exp\left\{\frac{(x-\mu)^2}{2\sigma^2}\right\}.
$$

If you want to list a system of $N$ equations, then I recommend this syntax:

\begin{gather*}
    u(c,\ell) = \log(c) + \chi \log(\ell), \\
    \ell = 1 - n, \\
    c + i + g = f(k,n), \\
    f(k,n) = k^\alpha n^{1-\alpha}.
\end{gather*}

which looks like

\begin{gather*} u(c,\ell) = \log(c) + \chi \log(\ell), \\ \ell = 1 - n, \\ c + i + g = f(k,n), \\ f(k,n) = k^\alpha n^{1-\alpha}. \end{gather*}

Python¶

Libraries¶

The following code cell contains Python that imports libraries for plotting (matpotlib) and scientific computing (numpy). Then it sets a random number generator seed and assigns it to rng. A Python program will always begin with a header like this.

In [3]:
# Import libraries
#   Plotting
import matplotlib.pyplot as plt
#   Scientific computing
import numpy as np

# Set rng seed
rng = np.random.default_rng(seed=0)

Variables¶

The following code cell assigns the sample size to T and randomly generated data from a standard normal distribution to eps, i.e., $\varepsilon_t \overset{i.i.d.}{\sim} N(0,1)$ ($\varepsilon_t$ is a random variable with a standard normal distribution that is independently and identically distributed across time). eps is also known as white noise data.

In [4]:
# Sample size
T = 201
# White noise data
eps = rng.standard_normal(T)

Histogram¶

The following cell will create a histogram of eps. If we weren't thinking about eps as a time series variable, then the histogram could represent the distribution of the variable in the cross section dimension.

In [5]:
#   Plot histogram
plt.figure(figsize=(6.5,3))
plt.hist(eps,edgecolor='black',density='true')
plt.xlabel('White-noise data')
plt.ylabel('probability')
plt.grid()
No description has been provided for this image

Time series¶

But since we want to think of eps as a time series variable, we should plot it as a function of time.

In [6]:
#   Plot time series
plt.figure(figsize=(6.5,3))
plt.plot(eps)
plt.title('White-noise data')
plt.xlabel('Time')
plt.xlim([0,T-1])
plt.grid()
No description has been provided for this image

Slides¶

Slides are easily created from a Jupyter Notebook (via reveal.js). For example, see Jupyter Notebook Slides.

  1. Open the property inspector by clicking the double gears in the upper-right-hand corner, and assign the "Slide Type" field.
  • "Slide" starts a new slide
  • "Fragment" does not create a new slide and instead hides the content until the presenter increments forward jupyter-notebook-slides
  1. Press ctrl/command+shift+c (to open the command palette) and type "slides". Press enter or click "Save and Export Notebook: Reveal.js Slides". jupyter-notebook-command-palette
  2. Save the html file, which you can then open in any browser.

Conclusion¶

  • Jupyter Notebooks are versatile tools to display, run, and view the output of programs in Python (or other languages), to typeset math expressions in $\LaTeX$, and to format text and analysis in Markdown.
  • Furthermore, Jupyter Notebooks are easily converted to presentation slides with the built-in support for reveal.js.