Help : IndexError - probably pretty simple

JT x1xx1x at hotmail.com
Thu Oct 18 11:43:32 EDT 2001


I've been trying to learn Python and I was working on a function which
would price options.  But I've run in to an Indexing Error.  This is
probably pretty simple, but I still haven't figured out what I need to
change.  It gets hung up at the first "for" loop.  I thought you could
assign a value to each element of the list individually by placement,
but maybe I need to use append.  Any help would definitely be
appreciated.  I'll check back to the group for any responses, or you
can respond directly to my email: x1xx1x at hotmail.com   Thanks.

Here is the error message:

Python 2.1.1 (#20, Jul 20 2001, 01:19:29) [MSC 32 bit (Intel)] on
win32
Type "copyright", "credits" or "license" for more information.
IDLE 0.8 -- press F1 for help
>>> import crrbinomial
>>> crrbinomial.CRRBi()
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in ?
    crrbinomial.CRRBi()
  File "C:\PROGRA~1\Python21\crrbinomial.py", line 23, in CRRBi
    ov[i] = max(0, z * (stock * pow(u, i) * pow(d, n-1) - strike))
IndexError: list assignment index out of range
>>>


And here is the code:

"""The Cox-Ross-Rubinstein Binomial Tree Function CRRBi(): can be used
to price European and American options on stocks (cc=interest),
stocks and stock indexes paying a continuous dividend yield q
(cc=interest-q), futures (cc=0), and currency options with foreign
interest rate Rf (cc=interest-Rf)."""

from math import exp, sqrt

def CRRBi(ae="a", cp="c", stock=100, strike=100, time=(1.00/12),
interest=0.6, cc=0.10, vol=1.00, n=10):
	"""CRRBi: the Cox-Ross-Rubinstein Binomial Option Pricing Model"""
	ov = []

	if cp == "c":
		z = 1
	elif cp =="p":
		z = -1

	dt = time / n 
	u = exp(vol * sqrt(dt)) 
	d = 1 / u 
	a = exp(cc * dt) 
	p = (a - d) / (u - d) 
	Df = exp(-interest * dt) 

	for i in range(n):
		ov[i] = max(0, z * (stock * pow(u, i) * pow(d, n-1) - strike))

	for j in range(n-1, 0, -1):
		for i in range(j):
			if ae == "e":
				ov[i] = (p * ov[i+1] + (1 - p) * ov[i]) * Df
			elif ae == "a":
				ov[i] = max((z * (stock * pow(u, i) * pow(d, abs(i - j)) -
strike)), -(p * ov[i+1] + (1 - p) * ov[i]) * Df)

	return ov[0]



More information about the Python-list mailing list