## 欧式期权定价回顾

```while i < num_iterations:
S_T = generate_asset_price()
payoffs += payoff_function(S_T)
i += 1
option_price = exp(-r*T) * (payoffs / num_iterations)```

## 二元期权

```def binary_call_payoff(K, S_T):
if S_T >= K:
return 1.0
else:
return 0.0```

## 模拟计算过程

```def gbm(S, v, r, T):
return S * exp((r - 0.5 * v**2) * T + v * sqrt(T) * random.gauss(0,1.0))```

```import random
from math import exp, sqrt
def gbm(S, v, r, T):
return S * exp((r - 0.5 * v**2) * T + v * sqrt(T) * random.gauss(0,1.0))
def binary_call_payoff(K, S_T):
if S_T >= K:
return 1.0
else:
return 0.0
# parameters
S = 40.0 # asset price
v = 0.2 # vol of 20%
r = 0.01 # rate of 1%
maturity = 0.5
K = 40.0 # ATM strike
simulations = 50000
payoffs = 0.0
# run simultaion
for i in xrange(simulations):
S_T = gbm(S, v, r, maturity)
payoffs += binary_call_payoff(K, S_T)
# find prices
option_price = exp(-r * maturity) * (payoffs / float(simulations))
print 'Price: %.8f' % option_price```

## 检验结果

```>>> from scipy.stats import norm
>>> from math import exp, log, sqrt
>>> S, K, v, r, T = 100.0, 100.0, 0.2, 0.01, 0.5
>>> d2 = (log(S/K) + (r - 0.5 * v**2) * T) / v*sqrt(T)
>>> print exp(-r * T) * norm.cdf(d2)
0.490489409105```