Teake Nutma t.a.nutma@rug.nl
14-01-2019
I will be using a couple of keyboard shortcuts that are available in Jupyter:
# Command mode
Enter: Go to edit mode
a: Insert cell above
b: Insert cell below
x: Cut cell
m: Change cell to markdown (text)
y: Change cell to code
# Edit mode:
Esc: Go to command mode
Shift+Enter: Run cell
Tab: Code completion
You can press H
to show them all.
# This is code.
# You can run it with Shift+Enter
2 + 2
This is text.
This is text with an inline formula: $e^{i\pi} + 1 = 0$.
This is text with a centered formula:
$$ R_{\mu\nu} - \tfrac 1 2 g_{\mu\nu} + \Lambda_{\mu\nu} = \frac{8\pi G}{c^4} T_{\mu\nu} $$
We can also define a function:
def fibonacci(n):
if n == 0:
return 0
if n == 1:
return 1
return fibonacci(n-1) + fibonacci(n-2)
Let's see if it works:
fibonacci(10)
fibs = [fibonacci(n) for n in range(1,11)]
fibs
We can also plot it:
import matplotlib.pyplot as plt
plt.plot(fibs)
Let's define another function:
import math
def binomial(n,p):
return math.factorial(n) // (math.factorial(n-p) * math.factorial(p))
bins = [binomial(10, p) for p in range(0, 11)]
bins
We can computer the sum of this, which turns out to be $2^{10}$.
sum(bins)
Plot it:
plt.plot(bins)
Let's define a convenience function that will give us a list of binomials for given $n$:
def binomial_list(n):
return [binomial(n, p) for p in range(0, n+1)]
We can now easily plot this:
plt.plot(binomial_list(30))
Let's try it with on a log scale:
plt.semilogy(binomial_list(10))
from ipywidgets import interact
def plotbinomials(n):
plt.semilogy(binomial_list(n))
interact(plotbinomials, n=(1,20))
This nicely demonstrates the limiting behaviour of the binomial distribution.
Lastly, let's double-check that $\sum_{p=0}^n \binom{n}{p} = 2^n$:
[sum(binomial_list(n)) for n in range(1,11)]