I am looking at the code here for Asian option pricing:
Here is the code:
import scipy as sp
s0=40. #today stock price
x=40. #excercise price
T=0.5 #maturity in years
r=0.05 #risk-free rate
sigma=0.2 # volatility
n_simulation =100 # number of simulations
n_steps=100
dt=T/n_steps
call=sp.zeros([n_simulation],dtype=float)
for j in range(0, n_simulation):
sT*=s0
total=0
for i in range(0,int(n_steps)):
e=sp.random.normal()
sT*=sp.exp((r-0.5*sigma*sigma)*dt+sigma*e*sp.sqrt(dt))
total+=sT
price_average=total/n_steps
call[j]=max(price_average-x,0)
call_price=mean(call)*exp(-r*T)
print 'call price = ', round(call_price,3)
What is the sT variable defined as here? I don't see a definition. What is the for loop for j in range(0, n_simulation) doing exactly? From context it seems like it should be the stock price at time T. If so how should sT be defined?
Thanks.
EDIT: I understand the code is illegal/won't compile, I'm just asking if anyone can help fill in the blanks
Here's an attempt at figuring out what was intended. Obviously some guesswork and assumptions are involved in this.
The changes involved are:
Fixing the line you questioned from
sT*=s0tosT = s0(i.e. at the start of each simulation, reset the current stock price back to today's price)Fixing some indentation so the loops make sense. (Do
jsimulations; in each simulation evolve the price foritimesteps and calculate the option payoff; finally average the option payoff over all simulations.)Fixing the line that draws values from the normal distribution.
Fixing some imports (to be fair, these might have worked fine in earlier versions of scipy).
Fixing the
printto be Python 3 compatible.This now runs in Python 3 and prices the call at about $1.50 - $1.60. (Running more simulations would get you tighter bounds on it.)
In general, I'd gently suggest that a resource isn't the best learning vehicle if the code needs this much massaging in order to run and produce sensible results :)
As an alternative, using numpy array operations the same algorithm could be done faster and without explicit
forloops: