Notes:
- \(\LaTeX\): It renders correctly from the markdown file. The \(\LaTeX\) within the notebook markdown cells renders the equations, but not the inline math. TODO: fix this
- When you reference a single cell, you can't say cells[4], you have to write cells[4:5].
- The numbers in cells[m:n] don't refer to the "input numbers" you see in the notebook. The markdown cells are counted too. This makes it a bit of a pain to locate particular cells within a larger notebook.
Here I am selecting a cell from the notebook.
a = 0
b = 1
print(fib(a, b, 10))
a = 5
b = 6
print(fib(a, b, 10))
Now I am displaying the whole notebook.
This is an example jupyter notebook.¶
Demo some things for reference:
- make sure imports work
- test plotting: %inline vs %notebook
- import the whole notebook OR just selected pieces
- does the notebook Markdown look OK when called into the webpage?
- do the Markdown calls depend on the order of execution? or just the order in the notebook?
As a test project, we'll look at the Fibonacci numbers and make some plots of their ratios.
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
Which versions of these packages will be found?
This notebook won't be running live on the website, so this will just depend on how I execute the cells locally. One question is whether or not this will be impacted by the virtual environment (that I am typically in when working on the site).
You can see below that it is independent of the virtual environment.
print(np.__version__)
print(np.__file__)
# This is something I know my site venv imports as an old version.
import nbconvert
print(nbconvert.__version__)
print(nbconvert.__file__)
Now let's do some example coding and plotting.¶
def fib(a, b, n):
"""Using `a` and `b` as seeds, return the n'th Fibonacci number.
Example:
a = 2, b = 5, n = 6
Compute like this:
(2, 5, 2 + 5 = 7, 5 + 7 = 12, 7 + 12 = 19, 12 + 19 = 31)
"""
seq = [a, b]
for i in range(n-2):
seq.append(seq[-1] + seq[-2])
return seq
Let's test this function.¶
a = 0
b = 1
print(fib(a, b, 10))
a = 5
b = 6
print(fib(a, b, 10))
Let's look at the ratio of successive Fibonacci numbers for various choices of the seeds.¶
def ratio_plot(seq, ax=None):
seq = np.array(seq)
if ax is None:
fig = plt.figure()
ax = fig.gca()
plt.plot(seq[1:] / seq[:-1], 'o-')
Plot one sequence of ratios.¶
a = 0
b = 1
seq = fib(a, b, 10)
ratio_plot(seq)
fig = plt.figure()
ax = fig.gca()
n = 10
for a in range(4):
for b in range(4):
seq = fib(a, b, n)
ratio_plot(seq, ax=ax)
Observations:¶
- Ratios of successive Fibonacci numbers approach some constant value.
- The constant value appears to be roughly 1.6 independent of the seeds.
Can we derive this value?¶
Let's see what the $ \LaTeX $ rendering looks like..
Assume there is some value $a$ that is the limit of this sequence of ratios.
Consider three adjacent values in the sequence $(x, y, x + y)$ near to the limit (making use of the physicist lemma).
Then the two ratios will be the same.
$$ \frac{y}{x} = \frac{x + y}{y} $$We can scale the entire sequence so $x = 1$.
$$ \frac{y}{1} = \frac{1 + y}{y} $$And rearrange.
$$ y^2 - y - 1 = 0 $$Solving for the roots we find:
$$ y_+ = \frac{1 + \sqrt{5}}{2} , y_- = \frac{1 - \sqrt{5}}{2} $$$$ y_+ \simeq 1.618 , y_- \simeq -0.618 $$Congrats! We uncovered the "golden ratio" $\phi = 1.618...$.¶
Armed with this information we are ready to build famous buildings, write magnificent symphonies, and film epic Hollywood movies.
Fewer have explored the lesser-known "ochre ratio" $ \theta = -0.618... $.¶
a = 1
ochre = (1 - 5**0.5)/2
seq = fib(a, ochre, 10)
ratio_plot(seq)
plt.ylim(-2, 2)
This ratio has been important in things such as ultra-flat tables, horizon-design, and the limiting behavior of EEGs.¶