Discussion:
[Tutor] Cumulative distribution function
Michel Guirguis
2015-10-01 07:48:57 UTC
Permalink
Good morning,

I have a problem with the cumulative distribution function in calculating derivatives. I am getting a call option figure of 2.5961 while it should be 2.9081. Could you please help.
S=50
K=50
r=0.03
q=0.02
sig=0.20
T=0.5
from math import*
d1=(log(S/K)+(r-q+(sig*sig)*0.5)*T)/(sig*sqrt(T))
d1
0.10606601717798211
d2 =d1-sig*sqrt(T)
d2
-0.03535533905932742
call = 50*exp(-0.02*0.5)*cumdist*(d1)-50*exp(-0.03*0.5)*cumdist*(d2)
call
2.596102990952506

Thanks,

Michel
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Laura Creighton
2015-10-01 08:36:17 UTC
Permalink
Post by Michel Guirguis
Good morning,
I have a problem with the cumulative distribution function in calculating derivatives. I am getting a call option figure of 2.5961 while it should be 2.9081. Could you please help.
S=50
K=50
r=0.03
q=0.02
sig=0.20
T=0.5
from math import*
d1=(log(S/K)+(r-q+(sig*sig)*0.5)*T)/(sig*sqrt(T))
d1
0.10606601717798211
d2 =d1-sig*sqrt(T)
d2
-0.03535533905932742
call = 50*exp(-0.02*0.5)*cumdist*(d1)-50*exp(-0.03*0.5)*cumdist*(d2)
call
2.596102990952506
Thanks,
Michel
I do not know if this is your problem, but you can get significant errors
if you try to use binary floating point to represent money. You
should use decimal floating point instead.
see: https://docs.python.org/3.4/library/decimal.html

Laura
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Peter Otten
2015-10-01 08:57:18 UTC
Permalink
Michel Guirguis wrote:

[Please don't start a new thread for an old problem]
Post by Michel Guirguis
I have a problem with the cumulative distribution function in calculating
derivatives. I am getting a call option figure of 2.5961 while it should
be 2.9081. Could you please help.
S=50
K=50
r=0.03
q=0.02
sig=0.20
T=0.5
from math import*
d1=(log(S/K)+(r-q+(sig*sig)*0.5)*T)/(sig*sqrt(T))
d1
0.10606601717798211
d2 =d1-sig*sqrt(T)
d2
-0.03535533905932742
Something is missing here...
Post by Michel Guirguis
call = 50*exp(-0.02*0.5)*cumdist*(d1)-50*exp(-0.03*0.5)*cumdist*(d2)
... and here seem to be erroneous * operators.
Post by Michel Guirguis
call
2.596102990952506
Did you see Laura's answer to your previous question? Using
scipy.stats.norm.cdf and turning your interactive session into a little
script I get

$ cat cumulative.py
import scipy.stats
from math import log, exp, sqrt

cumdist = scipy.stats.norm.cdf

S = 50
K = 50
r = 0.03
q = 0.02
sig = 0.20
T = 0.5

d1 = (log(S/K)+(r-q+(sig*sig)*0.5)*T)/(sig*sqrt(T))
print("d1 = {}".format(d1))

d2 = d1-sig*sqrt(T)
print("d2 = {}".format(d2))

call = 50*exp(-0.02*0.5)*cumdist(d1)-50*exp(-0.03*0.5)*cumdist(d2)
print("call = {}".format(call))

$ python cumulative.py
d1 = 0.106066017178
d2 = -0.0353553390593
call = 2.9087784079

Not exactly 2.9081, but closer :)

Assuming 2.9088 is the correct value, but for some reason you cannot use
scipy the problem is with your cumdist() function.

You don't provide that function and you don't provide the source where you
adapted it from. How do you expect us to help you fix code without spec --
and without seeing said code?


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Laura Creighton
2015-10-01 10:26:26 UTC
Permalink
Post by Peter Otten
$ python cumulative.py
d1 = 0.106066017178
d2 = -0.0353553390593
call = 2.9087784079
Not exactly 2.9081, but closer :)
And sort of in the 'this is a float not a decimal' range I consider
usual for such errors, not the huge difference you reported.
Post by Peter Otten
Assuming 2.9088 is the correct value, but for some reason you cannot use
scipy the problem is with your cumdist() function.
I think we should work on getting you scipy. I think you are going to
need plenty of things from there ...

Laura
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Loading...