Confusion about numeric calculation typing

asked 2022-12-22 20:21:14 +0200

jesuslop gravatar image

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



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?



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(, 100, 0.0283, 0.05, 0.34, 'call')) 
print(call(98.5, 100, 0.0283, 0.05, 0.34).numerical_approx())
edit retag flag offensive close merge delete