Quantitative Analysis, Risk Management, Modelling, Algo Trading, and Big Data Analysis

# Deriving Limits in Python

Lesson 6>>

Some quant problems require an intensive work with mathematical (time-)series given initial conditions. In this short lesson on Python, let’s consider the following problem and solve it analytically and with aid of Python.

Problem

Given a series $\{a_n\}$, where $n\in N^{+}$, and $a_1=\sqrt{2}$ and $a_{n+1} = \sqrt{2}^{\log_2 {a_n}}$, solve:
$$\lim_{n \to \infty} b_n \ \ \ \ \mbox{where} \ \ b_n = a_1\cdot a_2\cdot … \cdot a_n \ .$$

Solution

By employing
$$a^{\log_a b} = b \ \ \ \ \mbox{where} \ \ a\in \Re^{+} – \{1\} \ \ \ \mbox{and} \ \ \ b\in \Re^{+}$$and taking into account the initial conditions for our problem, we derive:
$$a_1 = \sqrt{2} = 2^{\frac{1}{2}} \\ a_2 = \sqrt{2}^{\log_2 a_1} = 2^{\frac{1}{2}\log_2 2^{\frac{1}{2}}} = 2^{\log_2 2^\frac{1}{4}} = 2^\frac{1}{2^2} \\ a_3 = \sqrt{2}^{\log_2 a_2} = 2^{\frac{1}{2}\log_2 2^{\frac{1}{2^2}}} = 2^\frac{1}{2^3} \\ … \\ a_n = \sqrt{2}^{\log_2 a_{n-1}} = 2^\frac{1}{2^n}$$ therefore
$$a_n = 2^\frac{1}{2^n} \ .$$
We find
$$b_n = a_1\cdot a_2\cdot … \cdot a_n = 2^{\frac{1}{2}} \cdot 2^\frac{1}{2^2} \cdot … \cdot 2^\frac{1}{2^n} =$$
$$= 2^{\frac{1}{2}+\frac{1}{4}+…+\frac{1}{2^n}} = 2^{1-\left( \frac{1}{2} \right)^n} \ .$$
Having that, we might code in Python the limit for $\{b_n\}$-series in an iterative way:

1 2 3 4 5 6 7 # Deriving Limits in Python, Accelerated Python for Quants Tutorial, Lesson 5 # (c) 2014 QuantAtRisk   from sympy import *   for m in xrange(1,40): print 2**(1-0.5**m)

what results in the output:

1.41421356237 1.68179283051 1.83400808641 1.91520656140 1.95714412418 1.97845602639 1.98919884697 1.99459211217 1.99729422578 1.99864665501 1.99932321299 1.99966157786 1.99983078177 1.99991538910 1.99995769410 1.99997884694 1.99998942344 1.99999471171 1.99999735586 1.99999867793 1.99999933896 1.99999966948 1.99999983474 1.99999991737 1.99999995869 1.99999997934 1.99999998967 1.99999999484 1.99999999742 1.99999999871 1.99999999935 1.99999999968 1.99999999984 1.99999999992 1.99999999996 1.99999999998 1.99999999999 1.99999999999 2.0

while more elegant solution for $\{b_n\}$ we may obtain as follows:

9 10 11 12 n=Symbol('n') expr=2**(1-0.5**n) a=limit(expr, n, oo) print a

revealing

2

confirmed by an analytical solution:
$$\lim_{n \to \infty} b_n = \lim_{n \to \infty} 2^{1-\left( \frac{1}{2} \right)^n} = 2 \ \ .$$
Please note that Python’s command limit requires explicit definition of the variable $n$ to be symbolically assigned before calculation, and sympy package needs to be uploaded.