{"id":4416,"date":"2012-07-23T16:11:03","date_gmt":"2012-07-23T15:11:03","guid":{"rendered":"http:\/\/www.walkingrandomly.com\/?p=4416"},"modified":"2012-07-23T18:47:53","modified_gmt":"2012-07-23T17:47:53","slug":"black-scholes-option-pricing-in-matlab-using-the-nag-toolbox","status":"publish","type":"post","link":"https:\/\/walkingrandomly.com\/?p=4416","title":{"rendered":"Black-Scholes Option Pricing in MATLAB using the NAG Toolbox"},"content":{"rendered":"<p>A MATLAB user at <a href=\"http:\/\/www.manchester.ac.uk\/\">Manchester University<\/a> contacted me recently asking about <a href=\"http:\/\/en.wikipedia.org\/wiki\/Black%E2%80%93Scholes\">Black-Scholes<\/a> option pricing.\u00a0 The <a href=\"http:\/\/www.mathworks.co.uk\/products\/finance\/\">MATLAB Financial Toolbox<\/a> has a range of functions that can calculate Black-Scholes put and call option prices along with several of the sensitivities (<a href=\"http:\/\/en.wikipedia.org\/wiki\/Greeks_%28finance%29\">or &#8216;greeks<\/a>&#8216;) such as <a href=\"http:\/\/www.mathworks.co.uk\/help\/toolbox\/finance\/blsprice.html\">blsprice<\/a>, <a href=\"http:\/\/www.mathworks.co.uk\/help\/toolbox\/finance\/blsdelta.html\">blsdelta<\/a> and so on.<\/p>\n<p>The user&#8217;s problem is that we don&#8217;t have any site-wide licenses for the Financial Toolbox.\u00a0 We do, however, have a full site license for the <a href=\"http:\/\/www.nag.co.uk\/numeric\/MB\/start.asp\">NAG Toolbox for MATLAB<\/a> which has a nice set of option pricing routines.\u00a0 Even though they calculate the same things, NAG Toolbox option pricing functions look very different to the Financial Toolbox ones and so I felt that a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Rosetta_Stone\">Rosetta Stone type<\/a> article might be useful.<\/p>\n<p>For Black-Scholes option pricing, there are three main differences between the two systems:<\/p>\n<ol>\n<li>The Financial Toolbox has separate functions for calculating the option price and each greek (e.g. <a href=\"http:\/\/www.mathworks.co.uk\/help\/toolbox\/finance\/blsprice.html\">blsprice<\/a>, <a href=\"http:\/\/www.mathworks.co.uk\/help\/toolbox\/finance\/blsgamma.html\">blsgamma<\/a>, <a href=\"http:\/\/www.mathworks.co.uk\/help\/toolbox\/finance\/blsdelta.html\">blsdelta<\/a> etc) whereas NAG calculates the price and all greeks simultaneously with a single function call.<\/li>\n<li>Where appropriate, The MATLAB functions calculate Put and Call values with one function call whereas with NAG you need to explicitly specify Call or Put.<\/li>\n<li>NAG calculates more greeks than MATLAB.<\/li>\n<\/ol>\n<p>The following code example pretty much says it all.\u00a0  Any variable calculated with the NAG Toolbox is prefixed NAG_ whereas anything calculated with the financial toolbox is prefixed MW_.\u00a0 When I developed this, I was using MATLAB 2012a with NAG Toolbox Mark 22.<\/p>\n<pre>%Input parameters for both NAG and MATLAB.\r\nPrice=50;\r\nStrike=40;\r\nRate=0.1;\r\nTime=0.25;\r\nVolatility=0.3;\r\nYield=0;\r\n\r\n%calculate all greeks for a put using NAG\r\n[NAG_Put, NAG_PutDelta, NAG_Gamma, NAG_Vega, NAG_PutTheta, NAG_PutRho, NAG_PutCrho, NAG_PutVanna,...\r\nNAG_PutCharm, NAG_PutSpeed, NAG_PutColour, NAG_PutZomma,NAG_PutVomma, ifail] =...\r\n s30ab('p', Strike, Price, Time, Volatility, Rate, Yield);\r\n\r\n%calculate all greeks for a Call using NAG\r\n[NAG_Call, NAG_CallDelta, NAG_Gamma, NAG_Vega, NAG_CallTheta, NAG_CallRho, NAG_CallCrho, NAG_CallVanna,...\r\n NAG_CallCharm, NAG_CallSpeed, NAG_CallColour,NAG_CallZomma, NAG_CallVomma, ifail] = ...\r\ns30ab('c', Strike, Price, Time, Volatility, Rate, Yield);\r\n\r\n%Calculate the Elasticity (Lambda)\r\nNAG_CallLambda = Price\/NAG_Call*NAG_CallDelta;\r\nNAG_PutLambda = Price\/NAG_Put*NAG_PutDelta;\r\n\r\n%Calculate the same set of prices and greeks using the MATLAB Finance Toolbox\r\n[MW_Call, MW_Put] = blsprice(Price, Strike, Rate, Time, Volatility, Yield);\r\n[MW_CallDelta, MW_PutDelta] = blsdelta(Price, Strike, Rate, Time, Volatility, Yield);\r\nMW_Gamma = blsgamma(Price, Strike, Rate, Time, Volatility, Yield);\r\nMW_Vega = blsvega(Price, Strike, Rate, Time, Volatility, Yield);\r\n[MW_CallTheta, MW_PutTheta] = blstheta(Price, Strike, Rate, Time,Volatility, Yield);\r\n[MW_CallRho, MW_PutRho]= blsrho(Price, Strike, Rate, Time, Volatility,Yield);\r\n[MW_CallLambda,MW_PutLambda]=blslambda(Price, Strike, Rate, Time, Volatility,Yield);<\/pre>\n<p>Note that NAG doesn&#8217;t output the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Greeks_%28finance%29#Lambda\">elasticity (Lambda)<\/a> directly but it is trivial to obtain it using values that it does output. Also note that as far as I can tell, NAG outputs more Greeks than the Financial Toolbox does.<\/p>\n<p>I&#8217;m not going to show the entire output of the above program because there are a lot of numbers.\u00a0 However, here are the Put values as calculated by NAG shown to 4 decimal places.  I have checked and they agree with the Financial Toolbox to within numerical noise.<\/p>\n<pre>NAG_Put =0.1350\r\nNAG_PutDelta =-0.0419\r\nNAG_PutLambda =-15.5066\r\nNAG_Gamma =0.0119\r\nNAG_Vega =2.2361\r\nNAG_PutTheta =-1.1187\r\nNAG_PutRho =-0.5572\r\nNAG_PutCrho = -0.5235\r\nNAG_PutVanna =-0.4709\r\nNAG_PutCharm =0.2229\r\nNAG_PutSpeed =-0.0030\r\nNAG_PutColour =-0.0275\r\nNAG_PutZomma =0.0688\r\nNAG_PutVomma =20.3560<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A MATLAB user at Manchester University contacted me recently asking about Black-Scholes option pricing.\u00a0 The MATLAB Financial Toolbox has a range of functions that can calculate Black-Scholes put and call option prices along with several of the sensitivities (or &#8216;greeks&#8216;) such as blsprice, blsdelta and so on. The user&#8217;s problem is that we don&#8217;t have [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[58,11,28,7],"tags":[],"class_list":["post-4416","post","type-post","status-publish","format-standard","hentry","category-financial-math","category-matlab","category-nag-library","category-programming"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3swhs-19e","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/4416","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4416"}],"version-history":[{"count":12,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/4416\/revisions"}],"predecessor-version":[{"id":4428,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/4416\/revisions\/4428"}],"wp:attachment":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}