Help : IndexError - probably pretty simple

Steve Holden sholden at holdenweb.com
Thu Oct 18 18:02:26 CEST 2001


[posted & mailed]

"JT" <x1xx1x at hotmail.com> wrote ...
> 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.
>
You're pretty close. You can assign to a existent element of a list, but you
are starting with an empty list, so any subscripted assignment is going to
give you problems.

> 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
> >>>
>
You'll notice the interpreter is specifically complaining about the index
value, so you are correctly accessing the list, but with an illegal index.

>
> 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))
>
if you change the line above to read:

    ov.append(max(0, z * (stock * pow(u, i) * pow(d, n-1) - strike)))

the rest of your code should be good, as this loop will add n elements to
the list, with the values you require.

> 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]

regards
 Steve
--
www.holdenweb.com






More information about the Python-list mailing list