This notebook demonstrates the logistics of completing one of these assignments.
The first things to say is that you cannot complete one of these tutorials if you are viewing it as HTML in a web browser! You will need to be able to make changes and run the notebook in Jupyter, either on your own computer or some remote system (see Getting Started).
The first executable block of one of these notebooks always looks like this:
TutorialName = 'demo'
exec(open('tbc.py').read()) # define TBC and TBC_above
# normally followed by lots of import statements
The file tbc.py
is located in the same directory as the tutorial notebooks, so you will need to either modify the cell or copy/link the file if you are working elsewhere for some reason. It provides the TBC
(to be completed) functions described below, which you will find everywhere you are expected to provide a missing solution. (There is a reason we use this inelegant construction rather than import
, but not an interesting one.)
A notebook typically has a great deal of narrative text, and a great deal of functional code provided. This is because we are not concerned with having you learn obscure python shortcuts or the minutia of making plots with matplotlib.pyplot
, for example. Code of that nature will usually be given to you, and you can learn from it if it's useful (or not, if you know better ways of doing something than we do). However, when it comes to implementing the statistical methods we study in this course, you will find lines and blocks of code that need to be completed.
Here's a simple example of such a cell:
# For no good reason, let's define x to be 3.14159.
TBC() # x = ...
Try to run the cell as it stands, and you'll see an Exception
reminding you that there is a TBC line that needs to be completed. Between the textual and psuedo-code comments, it should be clear that you're expected to assign the variable x
a numeric value. Note that, while you're encouraged to experiment with anything you find interesting, arbitrarily changing names of variables and such can easily result in broken code farther along.
You might edit the code block as follows:
# For no good reason, let's define x to be 3.14159.
x = 3.14159
Feel free to delete/replace the TBC
line in the original cell. We only added a second cell here to show the before and after.
You'll also see to-be-completed function and class definitions, for example:
# Define f to be a very silly function.
def f(a, b):
"""
Add a and b.
Arguments
a: something to add to b
b: something to add to a
Return value: a+b
"""
TBC()
# In real life, there might be some suggested code or pseudo-code here.
TBC_above()
Notice that there is a TBC
in the function where you are expected to provide a solution, and a TBC_above
call at the end of the cell. The latter is just to ensure that you don't accidentally run through the cell without realizing that there's work to be done, since the TBC
within the function definition isn't triggered at this point - it would lie in wait until the function is called. If you comment the TBC_above
and run the cell, you'll see what happens when trying to use f
below; this might be a useful trick if you want to put off completing the definition of f
until absolutely necessary for some reason.
f(5,-3)
Naturally, a completed cell would look something like:
# Define f to be a very stupid function.
def f(a, b):
"""
Add a and b.
Arguments
a: something to add to b
b: something to add to a
Return value: a+b
"""
return a+b
Finally, each notebook will typically include at least one point where your work can be instantly compared to a known solution. Sometimes, this involves reading in previously generated results of some kind from the solutions
directory. In the case of the particularly simple tasks above, the correct answers might be directly included in the notebook, as below:
# Check value of x - should evaluate to zero
x - 3.14159
# Check f - third entry on each line should be zero
print(1, 1, f(1,1)-2)
print(9, 4, f(9,4)-13)
print(5, -2, f(5,-2)-3)
Sometimes these checks will, admittedly, be worded more ambigously. For example, "really small" in this context means consistent with zero within round-off error. "Basically constant" means equal to at least several significant digits. Use your best judgement, and ask if in doubt.
These checkpoints are there to help you, and it goes without saying that later results are unlikely to be correct if they are not passed. To discourage you from charging onward with code that doesn't crash yet is still incorrect, they will often be followed with:
TBC() # Did your notebook pass the self-check?
You will also be asked to do things other than writing code. Text responses can be filled in using Markdown-mode cells like this one. When executed, simple text in these cells will be displayed nicely as... simple text. You probably will never need to use markdown formatting, but there is a guide here if you want.
You will need to write some equations, however. These can be included in markdown cells using LaTeX syntax, between $ characters. Here is an example (double-click this cell in Jupyter to see the underlying code):
$f(a,b) = a+b = e^{\ln(a+b)} = \frac{1}{e^{-\ln(a+b)}}$
If you're not yet a LaTeX magician, don't worry; you'll probably see everything you need for the moment as we go. When in doubt, the appendices of this guide list many helpful math commands.
Finally, you will be asked make simple sketches of what are called Probabilistic Graphical Models (the free body diagrams of statistics!) and possibly some other cartoons of a physical model. You can produce an image file however you like (I usually resort to exporting from Google Drawings or Slides when the result doesn't need to be pretty). The simplest and preferred way to include these in your notebook is as demonstrated in this cell (double click to show the Markdown code). An easy way to accomplish this is to use the "Insert Image" option in the "Edit" menu, which appears when you have a Markdown cell selected.
When you include an image as above, the image data is actually embedded in the notebook, so there is no need to remember to submit additional files.
Important: At this writing, the "Insert Image" option is only available in classic Jupyter Notebook, not JupyterLab. Given that many tutorials will require imagery to answer some question, we strongly suggest using this older interface, which does not require installing anything different. Just launch jupyter-nbclassic
instead of jupyter-lab
, or launch the "Classic Notebook" server from the "Help" menu in JupyerLab.
Note that required non-code responses will still have a TBC()
call associated with them, necessarily in a code-mode notebook cell. You will need to convert the cell to Markdown mode (ESC-M is the usual shortcut, or via a menu) before filling in your answer. As in:
Did this demo help you understand the mechanics of tutorials?
TBC() # convert to Markdown and answer the question above
In a completed notebook, the above would have its mode changed to Markdown, and read something like:
Yes, I understand everything!