Using Markdown and Python in Jupyter notebooks

CS-E5885 Modeling Biological Networks
Spring 2021 Juho Timonen


A notebook consists of markdown cells and code cells. You can

If you submit your solution as a Jupyter notebook, always remember to Kernel > Restart & Run all and check that the output looks as desired before submitting.


1. Markdown

The Jupyter notebook has extensive markup capabilities. Here we show some examples. You can use markdown cells to write a complete answer with text and equations. They support Latex syntax for equations inside dollar signs, as in $a = \sqrt{2}$.

\begin{equation} a = \sqrt{2} \end{equation}

You can go ahead and double click cells below to see their Markdown code.

1.2 Matrices

Matrices can be written like here:

$$ \begin{align*} M &= \begin{bmatrix} a & b \\ c & d \end{bmatrix} & M^T &= \begin{bmatrix} a & c \\ b & d \end{bmatrix} \end{align*} $$

1.3 Tables

Example table:

Species Number of Molecules
A 50
B 100

1.4 Showing intermediate steps

In some exercises you may need to do longer calculations, and should show intermediate steps in your solution. You use \begin{align*} like in this example.

Problem: Let $X,Y$ be two independent random variables so that $X \sim \text{Exponential}(\lambda_1)$ and $Y \sim \text{Exponential}(\lambda_2)$. Derive the distribution of $Z = \min\{X,Y\}$.

Solution: We know the cumulative density functions of $X$ and $Y$, which are

$$ F_X(x) = 1 - e^{-\lambda_1 x} \hspace{1cm} \text{and} \hspace{1cm} F_Y(y) = 1 - e^{-\lambda_2 y} $$

and can compute the cumulative density function of $Z$ as

$$ \begin{align*} F_Z(z) &= P(Z \leq z)\\ &= 1 - P(Z \geq z) \\ &= 1 - P(\min\{X,Y\} \geq z) \\ &= 1 - P(X \geq z \text{ and } Y \geq z)\\ &= 1 - P(X \geq z) \cdot P(Y \geq z)\\ &= 1 - (1 - F_X(z)) \cdot (1 - F_Y(z))\\ &= 1 - e^{-\lambda_1 z} \cdot e^{-\lambda_2 z} \\ &= 1 - e^{-(\lambda_1 + \lambda_2) z} \end{align*} $$

which means that $Z \sim \text{Exponential}(\lambda_1 + \lambda_2)$.

2. Python programming

We will first import some libraries that we commonly need.

2.1 Variables

You can declare variables and check their type like so:

2.2 Numpy arrays

2.2.1 Creating numpy arrays

2.2.2 Reshaping

2.2.3 Accessing array elements

2.2.4 Editing array content

Note that in Python, as opposed to for example Matlab, arrays are not copied by default when using =. It only sets a reference to the original array.

2.3 Matrix and vector arithmetic

In some exercise problems it may need to do matrix computations on a computer. You can use numpy arrays to represent mathematical matrices and vectors.

2.4 List comprehension

2.5 Loops

The below code shows how to simulate 3 realizations of a stochastic process and visualize it.

2.6 More material