{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Series\n",
"\n",
"This is an exposition on series, loosely following § 1.4 of *Applied Scientific Computing With Python* by Peter R. Turner, Thomas Arildsen, and Kathleen Kavanagh.\n",
"\n",
"The goal here is to stress that we can do rigorous computations in Python even with infinite quantities. However, to do so, we need to combine mathematical arguments, with careful coding.\n",
"\n",
"**Mathematics reviewed:**\n",
"* Series\n",
"* Partial sums\n",
"* Remainders\n",
"\n",
"**Programming topics used:**\n",
"* Loops\n",
"* Functions\n",
"* Interval Arithmetic"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Basic definitions and ideas\n",
"Recall that a *series* is the sum of infitely many terms in a sequence,\n",
"$$X = \\sum_{k=0}^{+\\infty} a_k.$$\n",
"Formally, the value a series takes is a limit of the *partial sums*. By definition the $N$-th partial sum is the sum $S_N$ of the first $N$ terms in the series. Assuming the series starts at zero (which will not always be true), we have \n",
"$$S_N = \\sum_{k=0}^{N-1} a_k.$$\n",
"Thus $X = \\lim_{N \\to +\\infty} S_N.$\n",
"\n",
"The $N$-th *remainder* is \n",
"$$R_N = X-S_N = \\sum_{k=N}^{\\infty} a_k.$$\n",
"We call $|R_N|$ the *truncation error*. A series converges if and only if $\\lim_{N \\to \\infty} |R_N|=0$. \n",
"\n",
"Usually to evaluate a series, we are happy with an estimate for an answer using a partial sum, but we'd like to know how accurate our estimate is. There are two sources of errors:\n",
"* Truncation error, mentioned above. We should be able to compute a bound on this in terms of $N$.\n",
"* Rounding errors, from using floating point arithmetic.\n",
"\n",
"First consider only truncation error. If you know that the $N$-th truncation error satisfies $|R_N| \\leq \\epsilon$, then you can conclude that the value $X$ taken by a convergent series satisfies\n",
"$$S_N-\\epsilon \\leq X=S_N+R_N \\leq S_N +\\epsilon.$$\n",
"\n",
"Saying $|R_N| \\leq \\epsilon$ is equivalent to $-\\epsilon \\leq R_N \\leq \\epsilon$. As we will see below, sometimes you are able to confine the $N$-th remainder to a interval $a \\leq R_N \\leq b$. \n",
"In this case, we can conclude that \n",
"$$S_N+a \\leq X = S_N+R_N \\leq S_N + b.$$\n",
"\n",
"Usually, we can get away with ignoring rounding errors. (But, for example, subtracting numbers that agree to $n$-significant digits results in a loss of precision of approximately $n$ digits.) If you want to be careful about rounding error, you can use interval arithmetic. Interval arithmetic can be used to confine a partial sum $S_N$ to an interval, say $c \\leq S_n \\leq d$. Then depending on which case we are in above, we have\n",
"$$c-\\epsilon \\leq X \\leq d+\\epsilon \\quad \\text{or} \\quad a+c \\leq X \\leq b+d$$\n",
"(with interval arithmetic used to round these bounds outward)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Naive evaluation (An example)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define the sequence $a_k=\\frac{1}{k(k+1)}$ for $k \\geq 1$. Then the sequence has the form\n",
"$$a_1=\\frac{1}{2},~ a_2 = \\frac{1}{6},~ a_3 = \\frac{1}{12},~ a_4 = \\frac{1}{20},~ \\ldots.$$\n",
"The following function returns the $k$-th term as a float."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"def a(k):\n",
" return 1/(k*(k+1))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We now print the first $10$ terms."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a_1=0.50000, a_2=0.16667, a_3=0.08333, a_4=0.05000, a_5=0.03333, a_6=0.02381, a_7=0.01786, a_8=0.01389, a_9=0.01111, a_10=0.00909, "
]
}
],
"source": [
"for k in range(1, 11):\n",
" print(\"a_{}={:.5f}\".format(k, a(k)), end=\", \")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The following function computes the partial sum $S_N = \\sum_{k=1}^{N} a_k.$"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def partial_sum(N):\n",
" total = 0.0\n",
" for k in range(1, N+1): # iterates through {1, 2, ..., N}\n",
" total += a(k) # Same as total = total + a(k)\n",
" return total"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we will print some values:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"S_1 = 0.50000\n",
"S_2 = 0.66667\n",
"S_3 = 0.75000\n",
"S_4 = 0.80000\n",
"S_5 = 0.83333\n",
"S_10 = 0.90909\n",
"S_100 = 0.99010\n",
"S_1000 = 0.99900\n",
"S_100000 = 0.99999\n"
]
}
],
"source": [
"for N in [1, 2, 3, 4, 5, 10, 100, 1000, 100000]:\n",
" ps = partial_sum(N)\n",
" print(\"S_{} = {:.5f}\".format(N, ps))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We might guess that this is getting closer and closer to $1$. In fact, it is true that $1 = \\sum_{k=0}^\\infty a_k$.\n",
"\n",
"Furthermore, you might notice a pattern in the sums computed above. It looks like \n",
"$$S_1 = \\frac{1}{2}, \\quad S_2 = \\frac{2}{3}, \\quad S_3 = \\frac{3}{4}, \\quad S_4=\\frac{4}{5}, \\ldots.$$\n",
"So, we conjecture that generally $S_N = \\frac{N}{N+1}$. This statement can be proved by induction."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Proposition 1.** We have $S_N=\\frac{N}{N+1}$ for all $N \\geq 1$. \n",
"\n",
"**Proof:** It is clearly true that $S_1 = a_1 = \\frac{1}{2}$. Therefore the statement holds for $k=1$. \n",
"\n",
"Now suppose the statement holds for $N=j$. We will prove that it holds when $N=j+1$. Since the statement holds for $N=j$, we know that $S_j = \\frac{j}{j+1}$. Observe that by definition \n",
"$$S_{j+1} = S_j + a_{j+1}.$$\n",
"Since $a_{j+1}=\\frac{1}{(j+1)(j+2)}$ we have\n",
"$$S_{j+1} = \\frac{j}{j+1} + \\frac{1}{(j+1)(j+2)} = \\frac{j(j+2)}{(j+1)(j+2)}+\\frac{1}{(j+1)(j+2)},$$\n",
"where in the second step we have produced a common denominator. Expanding and simplifying, we see\n",
"$$S_{j+1} = \\frac{j^2+2j+1}{(j+1)(j+2)} = \\frac{(j+1)^2}{(j+1)(j+2)}=\\frac{j+1}{j+2}.$$\n",
"This proves that $S_{j+1}=\\frac{j+1}{j+2}$, completing the inductive step. By the principal of mathematical induction, $S_N=\\frac{N}{N+1}$ for all $N \\geq 1$, as desired. **QED**\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"From the proposition above, we can rigorously prove that the series converges to $1$."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Theorem 2.** We have $1=\\sum_{k=0}^\\infty \\frac{1}{k(k+1)}.$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Proof:** By definition, the series is the limit of the partial sums. From Proposition 1, we know that the partial sums have the form \n",
"$$S_N=\\frac{N}{N+1} = 1-\\frac{1}{N+1}.$$\n",
"Observe that $\\lim_{N \\to \\infty} \\frac{1}{N+1}=0$ since the denominator goes to infinity. Thus, \n",
"$$\\lim_{N \\to \\infty} S_N = \\lim_{N \\to \\infty} 1-\\frac{1}{N+1} = 1 - 0 = 1.$$\n",
"**QED**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Also as a consequence we have that the $N$th remainder satisfies\n",
"$$R_N=\\sum_{j=N+1}^\\infty a_n = 1-S_N = \\frac{1}{N+1}.$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## A famous example\n",
"\n",
"We will consider the following series\n",
"$$\\sum_{k=1}^\\infty \\frac{1}{k^2}.$$\n",
"\n",
"The problem of computing the limit of this series is called the *Basel problem*. Wikipedia has a good article on the subject.\n",
"\n",
"The following function uses Python floats to compute the partial sums $S'_N$ consisting of the sum of the first $N$ terms. Concretely,\n",
"$$S'_N = \\sum_{k=1}^{N} \\frac{1}{k^2}.$$\n",
"(We will use primes (${}'$) to denote partial sums and remainders for this new series.)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def naive_basel_partial_sum(N):\n",
" total = 0.0\n",
" for k in range(1, N+1): # iterate through [1, 2, ..., N].\n",
" total += 1/k**2\n",
" return total"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"S_1 = 1.00000\n",
"S_2 = 1.25000\n",
"S_3 = 1.36111\n",
"S_4 = 1.42361\n",
"S_5 = 1.46361\n",
"S_10 = 1.54977\n",
"S_100 = 1.63498\n",
"S_1000 = 1.64393\n",
"S_100000 = 1.64492\n"
]
}
],
"source": [
"for N in [1, 2, 3, 4, 5, 10, 100, 1000, 100000]:\n",
" ps = naive_basel_partial_sum(N)\n",
" print(\"S_{} = {:.5f}\".format(N, ps))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The first few digits of the correct answer are $1.64493$ so we are getting very close."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Controlling error from the remainder\n",
"\n",
"The $N$-th remainder is the limit of the series minus $S'_N$, or equivalently\n",
"$$R'_N = \\sum_{k=N+1}^{\\infty} \\frac{1}{k^2}.$$\n",
"We would like to control $R'_N$ by finding lower and upper bounds. This will allow us to control how well a partial sum approximates the limit.\n",
"\n",
"**Upper bound.** \n",
"Let $a_k=\\frac{1}{(k+1)(k+2)}$, which is the terms in the series studied in the previous example. Observe that if $k \\geq 2$, we have\n",
"$$\\frac{1}{k^2} < \\frac{1}{k (k-1)}=a_{k-1}.$$\n",
"(This holds because $k^2>k (k-1)$.) Since this holds for any $k \\geq 2$, we have that for $N \\geq 1$, \n",
"$$R'_N = \\sum_{k=N+1}^\\infty \\frac{1}{k^2} < \\sum_{k=N}^\\infty a_k =R_{N-1} = \\frac{1}{N}.$$\n",
"Here, the remainder $R_{N-1}$ comes from the previous example, and from our work there we get that $R_{N-1}=\\frac{1}{N}$.\n",
"\n",
"**Lower bound.**\n",
"We also need a bound in the other direction.\n",
"An obvious lower bound is $0 \\frac{1}{N+1}$.\n",
"\n",
"**Combined bounds.**\n",
"We have shown that the remainder satisfies\n",
"$$\\frac{1}{N+1} < R'_N = \\sum_{k=N+1}^{\\infty} \\frac{1}{k^2} < \\frac{1}{N}.$$\n",
"\n",
"**Remark.** It is a common trick to control truncation error by comparing a series to a series we understand well."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Computation of series to within an interval"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Suppose we want to compute our series to within an interval of length less than some $\\epsilon>0$. \n",
"From the above, we know that \n",
"$$\\frac{1}{N+1} < R'_N < \\frac{1}{N}.$$\n",
"The actual value of the series is $S'_N + R'_N$ so, we get that\n",
"$$S'_N + \\frac{1}{N+1} < S'_N + R'_N < S'_N + \\frac{1}{N}.$$\n",
"This confines the value taken by the series to an interval of length\n",
"$$\\frac{1}{N} - \\frac{1}{N+1} = \\frac{1}{N(N+1)}.$$\n",
"So, we see that given $\\epsilon$, we should choose $N$ such that\n",
"$$\\frac{1}{N(N+1)} < \\epsilon.$$\n",
"\n",
"It is somewhat painful to solve this expression for $N$, but observe that \n",
"$\\frac{1}{N(N+1)}<\\frac{1}{N^2}.$\n",
"So it suffices to arrange that \n",
"$$\\frac{1}{N^2} \\leq \\epsilon.$$\n",
"Solving for $N$ we see that we should have\n",
"$$N \\geq \\frac{1}{\\sqrt{\\epsilon}}.$$\n",
"So, the smallest integer we can take is\n",
"$$N=\\left\\lceil \\frac{1}{\\sqrt{\\epsilon}} \\right\\rceil,$$ \n",
"where $\\lceil \\cdot \\rceil$ denotes the ceiling function. The *ceiling* of an real number is the smallest integer greater than or equal to that number. This is implemented in Python as `ceil` in the `math` package.\n",
"\n",
"Revisiting the discussion above, with the choice of $N==\\left\\lceil \\frac{1}{\\sqrt{\\epsilon}} \\right\\rceil$, we know that the value taken by the series lies in an interval of length less than $\\epsilon$ with endpoints $S'_N+\\frac{1}{N+1}$ and $S'_N+\\frac{1}{N}$. A sensible thing to use to approximate the limit is the average\n",
"$$S'_N + \\frac{1}{2}\\left(\\frac{1}{N+1}+\\frac{1}{N}\\right)=\n",
"S'_N + \\frac{2N+1}{N(N+1)},$$\n",
"which we know is within $\\frac{\\epsilon}{2}$ of the actual limit."
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
"from math import ceil, sqrt\n",
"\n",
"def basel_sum(epsilon):\n",
" \"\"\"\n",
" Return the basel sum to within epsilon/2.\n",
" \"\"\"\n",
" N = ceil(1/sqrt(epsilon))\n",
" total = 0.0\n",
" for k in range(1, N+1): # iterate through [1, 2, ..., N].\n",
" total += 1/k**2\n",
" # Now total stores the partial sum\n",
" return total + (2*N+1)/(2*N*(N+1))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To get the value of the series correct to the thousandths place, we can call:"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.6449437779794394"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"basel_sum(1/1000)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To get the value of the series correct $7$th significant digit (the millionths place), we call:"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.644934067181062"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"basel_sum(1/10**6)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Rigorous bounds"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the previous part, we controlled truncation error but not round off error. We can combine the above with interval arithmetic to get a rigorous interval containing our answer."
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"from mpmath import iv"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The comment in the first few lines of the function explains what the function does."
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
"def rigorous_basel_sum(prec, epsilon):\n",
" \"\"\"\n",
" Return the basel sum computed using interval arithmetic using\n",
" interval arithmetic with prec bits of precision and a truncation\n",
" error of less than epsilon>0.\n",
" \"\"\"\n",
" iv.prec = prec # set precision\n",
"\n",
" # The following should be safe even without interval\n",
" # arithmetic.\n",
" N = ceil(1/sqrt(epsilon))\n",
" \n",
" remainder_upper = 1 / iv.mpf(N)\n",
" remainder_lower = 1 / iv.mpf(N+1)\n",
" remainder = iv.mpf([remainder_lower, remainder_upper])\n",
" # Now remainder is an interval containing [1/N+1, 1/N]\n",
" \n",
" total = iv.mpf(0)\n",
" for k in range(1, N+1): # iterate through [1, 2, ..., N].\n",
" total += 1/iv.mpf(k**2)\n",
" # Now total contains the Nth interval aritmetic partial sum\n",
" \n",
" # To give the rigorous bound we add the partial sum and\n",
" # the remainder interval.\n",
" return total + remainder"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"mpi('1.643494', '1.646423')"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x1 = rigorous_basel_sum(15, 0.5/10**3)\n",
"x1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So we know that the sum lies in the interval above. You an get this intervals width (as a degenerate interval) as follows:"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"mpi('0.002929688', '0.002929688')"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x1.delta"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So the interval is correct to approximately $\\frac{3}{1000}$. As noted above, the calculation should narrow t an interval of size $\\frac{0.5}{1000}$. The reason this is not true is round off error. About $87\\%$ of the error is due to round off error. For this reason, we should be able to get a more accurate evaluation by increasing the precision. This will lower the round off error, while keeping the truncation error the same."
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"mpi('1.6446960215', '1.6451791879')"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x2 = rigorous_basel_sum(30, 0.5/10**3)\n",
"x2"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"mpi('0.00048316642642', '0.00048316642642')"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x2.delta"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The contribution of the truncation error is going to be almost exactly $0.0005$, so the round off error is very small now.\n",
"\n",
"Here is a bit better estimate:"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"mpi('1.64493406273573', '1.64493407094756')"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x3 = rigorous_basel_sum(43, 0.5/10**8)\n",
"x3"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"mpi('8.21182766230777e-9', '8.21182766230777e-9')"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x3.delta"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here again the $5 \\times 10^{-9}$ is truncation error, while the remaining roughly $3 \\times 10^{-9}$ is due to round off error. We can rigorously see from the above that the series evaluates to a real number whose decimal expansion begins either\n",
"$$1.64493406 \\quad \\text{or} \\quad 1.64493407.$$\n",
"\n",
"In fact, the series sums to $\\frac{\\pi^2}{6}$. Using interval arithmetic, we see can compute the this rigorously as well."
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"mpi('1.644934066819', '1.644934066891')"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"iv.dps = 10\n",
"iv.pi**2 / 6"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Thus the first few digits of $\\frac{\\pi^2}{6}$ are $1.6449340668$, which falls in the interval found above."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}