sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.37k stars 466 forks source link

Adding Black-Scholes to Sage finance #14671

Closed 54ccc553-54e3-403b-918a-7eed51717160 closed 11 years ago

54ccc553-54e3-403b-918a-7eed51717160 commented 11 years ago

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

54ccc553-54e3-403b-918a-7eed51717160 commented 11 years ago

Author: Brian Manion

williamstein commented 11 years ago
comment:3

I started looking at it but hit a build issue with sage-5.9. I noticed a few little things:

williamstein commented 11 years ago
comment:5

The code works (assuming it is right?), and passes its tests. However, here are some other issues:

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.

kcrisman commented 11 years ago
comment:6

Does this patch imply importing scipy at Sage start-up? That would be unfortunate.

54ccc553-54e3-403b-918a-7eed51717160 commented 11 years ago
comment:7

Replying to @kcrisman:

Does this patch imply importing scipy at Sage start-up? That would be unfortunate.

Not anymore. It was too slow.

54ccc553-54e3-403b-918a-7eed51717160 commented 11 years ago
comment:8

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

Example 1

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 

Example 2

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
williamstein commented 11 years ago
comment:10

(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                                                                                              
... 
54ccc553-54e3-403b-918a-7eed51717160 commented 11 years ago

Attachment: trac_14671_black_scholes.patch.gz

Updated patch

54ccc553-54e3-403b-918a-7eed51717160 commented 11 years ago
comment:11

Added tolerance support and added line after second EXAMPLES::

williamstein commented 11 years ago
comment:12

I can't think of anything wrong with this!

kcrisman commented 11 years ago
comment:13

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.

kcrisman commented 11 years ago

Attachment: trac_14671-ref.patch.gz

kcrisman commented 11 years ago

Reviewer: William Stein, Karl-Dieter Crisman

kcrisman commented 11 years ago
comment:14

Patchbot, apply trac_14671_black_scholes.patch and trac_14671-ref.patch

kcrisman commented 11 years ago

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).
kcrisman commented 11 years ago
comment:15

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?

54ccc553-54e3-403b-918a-7eed51717160 commented 11 years ago
comment:16

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.

jdemeyer commented 11 years ago

Merged: sage-5.11.beta3