Пример 1. Расчет по формуле Блэка-Шоулза.

 
Напишем расчет премии опционов по формуле Блэка-Шоулза. Код представлен ниже
function cnd(x)

-- taylor series coefficients
   local a1 = 0.31938153
   local a2 = -0.356563782
   local a3 = 1.781477937
   local a4 = -1.821255978
   local a5 = 1.330274429
   local l = math.abs(x)
   local k = 1.0 / (1.0 + 0.2316419 * l)
   local w = 1.0 - 1.0 / math.sqrt(2 * math.pi) * math.exp(-l * l / 2) *
   (a1 * k + a2 * k * k + a3 * math.pow(k, 3) + a4 * math.pow(k, 4) + a5 * math.pow(k, 5))
   if x < 0 then w = 1.0 - w end
   return w
end

-- The Black-Scholes option valuation function
function black_scholes(
	is_call, -- true for call, false for put
	s,       -- current price
	x,       -- strike price
	t,       -- time
	r,       -- interest rate
	v        -- volatility
)
   local d1 = (math.log(s / x) + (r + v * v / 2.0) * t) / (v * math.sqrt(t))
   local d2 = d1 - v * math.sqrt(t)
   if is_call then
      return s * cnd(d1) - x * math.exp(-r * t) * cnd(d2)
   else
      return x * math.exp(-r * t) * cnd(-d2) - s * cnd(-d1)
   end
end

print(black_scholes(true, 79790, 80000, (1/252)*30, 0.05, 0.4))   -- 4516.0784287596