Having written about pricing American-style options on a binomial tree in q, I thought it would be instructive to do the same in Python and NumPy. Here is the code:

import functools as ft import numpy as np def BPTree(n, S, u, d): r = [np.array([S])] for i in range(n): r.append(np.concatenate((r[-1][:1]*u, r[-1]*d))) return r def GBM(R, P, S, T, r, b, v, n): t = float(T)/n u = np.exp(v * np.sqrt(t)) d = 1./u p = (np.exp(b * t) - d)/(u - d) ptree = BPTree(n, S, u, d)[::-1] R_ = ft.partial(R, np.exp(-r*t), p) return reduce(R_, map(P, ptree))[0] def American(D, p, a, b): return np.maximum(b, D*(a[:-1]*p + a[1:]*(1-p))) def VP(S, K): return np.maximum(K - S, 0) ABM = ft.partial(GBM, American)

There is a minor deviation from the q code: we are allowing d to be specified in BPTree. But otherwise, they are doing the same thing. Performance (as measured in ipython) isn’t too far-off either:

In [1]: from binomial import * In [2]: %timeit ABM(ft.partial(VP,K=102.0), 100.0, 1.0, 0.08, 0.08, 0.2, 1000) 10 loops, best of 3: 38.4 ms per loop In [3]: ABM(ft.partial(VP,K=102.0), 100.0, 1.0, 0.08, 0.08, 0.2, 1000) Out[3]: 6.2215001602514555

Note the similarity between the q and Python code. The similarity is a result of using NumPy and functools which enabled Python to perform array-oriented computation and partial function application. We did use a loop in BPTree as Python/NumPy does not seem to have the same “scan” operation as q. I suppose we could have created a numpy.ufunc to use accumulate()… but the loop felt cleaner and more Pythonic.