Closed 54ccc553-54e3-403b-918a-7eed51717160 closed 11 years ago
Author: Brian Manion
I started looking at it but hit a build issue with sage-5.9. I noticed a few little things:
the examples should be indented 4 more spaces
typo in this line: " - vol -- The volatilitry "
space after "vol," here: def black_scholes(spot_price, strike_price, time_to_maturity, risk_free_rate, vol,opt_type): "
delete blank line 11
I think the input lines should have a single backtick around variable names, not 2 single quotes.
delete blank line 43
give a reference for the formula you're using
are there generalizations you plan to implement later?
why should somebody believe the examples you give? Are they replicating some examples elsewhere (e.g., on a wikipedia page)?
The code works (assuming it is right?), and passes its tests. However, here are some other issues:
I wonder if the function could be at least 10 times faster if it were carefully coded in Cython instead (?):
sage: timeit("finance.black_scholes(100,105,0.0685,0.05,0.4,'call')")
625 loops, best of 3: 769 µs per loop
Usually a function of this complexity should only take maybe 50 microseconds. Optimizing this should not be a stopper for this ticket; it's something that could be done later in a followup ticket.
Another more important issues is that you made the standard mistake of forgetting that the input might by Python ints (instead of Sage integers), so this goes BOOM:
sage: finance.black_scholes(int(100),int(105),0.0685,0.05,0.4,'call')
Does this patch imply importing scipy at Sage start-up? That would be unfortunate.
Replying to @kcrisman:
Does this patch imply importing scipy at Sage start-up? That would be unfortunate.
Not anymore. It was too slow.
Replying to @williamstein:
- give a reference for the formula you're using
I'm using the Black-Scholes formula from a textbook called Stochastic Calculus for Finance II, by Steven Shreve, which is equivalent to the one found on wikipedia. http://en.wikipedia.org/wiki/Black%E2%80%93Scholes#Black.E2.80.93Scholes_formula
- are there generalizations you plan to implement later?
Could you elaborate just a little more on this? I do plan on implementing functionality that allows for using different underlying distributions.
- why should somebody believe the examples you give? Are they replicating some examples elsewhere (e.g., on a wikipedia page)?
The following are two examples using the Black-Sholes formula in Matlab. We'll compare them to the results of the black_scholes function presented in the patch examples. I think this will show that my examples are precise to at least 4 digits to the right of the decimal point.
The Black-Scholes formula in matlab uses the following syntax: [Call, Put] = blsprice(Price, Strike, Rate, Time, Volatility).
In this example the underlying stock has a price of $42, the strike price is $40, the time to maturity is 6 months = 0.5 years. The risk-free rate is 10%, and the volatility is 20%.
In Matlab:
>> [call,put] = blsprice(42,40,0.1,0.5,0.2)
call =
4.7594
put =
0.8086
Let's compare this with black_scholes in Sage:
sage: finance.black_scholes(42, 40, 0.5, 0.1, 0.2, 'call')
4.759422392871532
sage: finance.black_scholes(42, 40, 0.5, 0.1, 0.2, 'put')
0.8085993729000958
In this example the underlying stock has a price of $100, the strike price is $95, the time to maturity is 3 months = 0.25 years. The risk-free rate is 10%, and the volatility is 50%.
In Matlab:
>> [call,put] = blsprice(100,95,0.1,0.25,0.5)
call =
13.6953
put =
6.3497
Let's compare this with black_scholes in Sage:
sage: finance.black_scholes(100, 95, 0.25, 0.1, 0.5, 'call')
13.695272738608132
sage: finance.black_scholes(100, 95, 0.25, 0.1, 0.5, 'put')
6.349714381299734
(1) Add a blankline after the second EXAMPLES::
(2) Many doctests fail due to platform-dependent precision issues; one should use the tolerance support to fix this, which is described here: http://www.sagemath.org/doc/developer/conventions.html?highlight=conventions
~/sage-5.10.rc0$ sage -t devel/sage/sage/finance/option.pyx
Running doctests with ID 2013-06-12-11-04-15-11ddf8bd.
Doctesting 1 file.
sage -t devel/sage/sage/finance/option.pyx
**********************************************************************
File "devel/sage/sage/finance/option.pyx", line 32, in sage.finance.option.black_scholes
Failed example:
finance.black_scholes(42, 40, 0.5, 0.1, 0.2, 'call')
Expected:
4.759422392871532
Got:
4.759422392871535
**********************************************************************
File "devel/sage/sage/finance/option.pyx", line 82, in sage.finance.option._std_norm_cdf
Failed example:
x = _std_norm_cdf(1.96); x
...
Attachment: trac_14671_black_scholes.patch.gz
Updated patch
Added tolerance support and added line after second EXAMPLES::
I can't think of anything wrong with this!
I'm going to upload a patch in a second with some minor formatting things and adding to the ref manual.
Don't we already have the cdf for the normal distribution? In GSL and scipy as well? Seems odd to include this special thing.
Attachment: trac_14671-ref.patch.gz
Reviewer: William Stein, Karl-Dieter Crisman
Patchbot, apply trac_14671_black_scholes.patch and trac_14671-ref.patch
Description changed:
---
+++
@@ -1 +1,5 @@
Added Black-Scholes functionality to sage.finance. Note that #4083 seeks to create an option class with Black-Scholes functionality. However there are reasons Black-Scholes would be useful as a standalone function without the need to create an option object as described in #4083.
+
+---
+
+Apply [attachment: trac_14671_black_scholes.patch](https://github.com/sagemath/sage-prod/files/10657865/trac_14671_black_scholes.patch.gz) and [attachment: trac_14671-ref.patch](https://github.com/sagemath/sage-prod/files/10657866/trac_14671-ref.patch.gz).
Though perhaps one should review that I got everything to make it look nice. Should take just a moment with sage -docbuild reference/finance html
.
I think I understand why the custom cdf as well - just for speed, since you are doing everything with the Python math headers?
Replying to @kcrisman:
Though perhaps one should review that I got everything to make it look nice. Should take just a moment with
sage -docbuild reference/finance html
.
Looks good to me.
I think I understand why the custom cdf as well - just for speed, since you are doing everything with the Python math headers?
Yeah the custom cdf was simply for speed.
Merged: sage-5.11.beta3
Added Black-Scholes functionality to sage.finance. Note that #4083 seeks to create an option class with Black-Scholes functionality. However there are reasons Black-Scholes would be useful as a standalone function without the need to create an option object as described in #4083.
Apply attachment: trac_14671_black_scholes.patch and attachment: trac_14671-ref.patch.
Component: finance
Keywords: Black-Scholes, finance, options
Author: Brian Manion
Reviewer: William Stein, Karl-Dieter Crisman
Merged: sage-5.11.beta3
Issue created by migration from https://trac.sagemath.org/ticket/14671