Ask Your Question

Revision history [back]

Confusion about numeric calculation typing

Hi, still in the learning curve.

I'm experimenting with Options Pricing theory, trying to derive the formula of call-option pricing on my own. I'm comparing my results with built-in (but deprecated) sage.finance.option package, so I think it's python to be transpiled to C, and it is using "double" type internally. The comparison (Sage's vs own implementation) results

1.6480585695086134

1.64805856950892

So I think my thing is ok, but I would like to fix typing in my code to get a beautiful result with both numbers exactly matching naturally (so digit truncation would be 'cheating'). Can I force the typing of the formal computations in my code to match with the cython module so the results are the same?

import sage.finance.option

var('x')

var('X') # strike
var('T') # time from purchase to expiration
var('S') # underlying spot price (at purchase time)
var('V') # underlying standard deviation per unit time
var('r') # risk-free rate

assume(X > 0)
assume(T > 0)
assume(S > 0)
assume(V > 0)

gaussian_density(x, mu, sigma) = (1/(sigma * sqrt(2*pi)))*exp((-1/2) * ((x-mu)/sigma)^2)

call(S, X, T, r, V) = exp(-r*T)*integrate((exp(x)-X) * gaussian_density(x, r*T + log(S) - ((V * sqrt(T))^2)/2, V*sqrt(T)),(x,log(X),oo))

# check against built-in
print(sage.finance.option.black_scholes(98.5, 100, 0.0283, 0.05, 0.34, 'call')) 
print(call(98.5, 100, 0.0283, 0.05, 0.34).numerical_approx())