<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>stotastic</title>
	<atom:link href="http://stotastic.com/wordpress/feed/" rel="self" type="application/rss+xml" />
	<link>http://stotastic.com/wordpress</link>
	<description>A very random place on the web</description>
	<lastBuildDate>Fri, 20 Aug 2010 18:32:40 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Integrating PHP and R</title>
		<link>http://stotastic.com/wordpress/2010/08/integrating-php-and-r/</link>
		<comments>http://stotastic.com/wordpress/2010/08/integrating-php-and-r/#comments</comments>
		<pubDate>Thu, 19 Aug 2010 02:39:30 +0000</pubDate>
		<dc:creator>Lee</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[web app]]></category>

		<guid isPermaLink="false">http://stotastic.com/wordpress/?p=1135</guid>
		<description><![CDATA[&#8220;How can I integrate PHP and R?&#8221;
I know I&#8217;m not the only one who&#8217;s asked this question.  After all, with great content management systems like Drupal, it would very cool to be able to drop an R module into some PHP code and instantly have a web app popping out some snazzy looking ggplot [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>&#8220;How can I integrate PHP and R?&#8221;</p></blockquote>
<p>I know I&#8217;m not the only one who&#8217;s asked this question.  After all, with great content management systems like Drupal, it would very cool to be able to drop an R module into some PHP code and instantly have a web app popping out some snazzy looking ggplot graphics.  After spending some time Google searching for an easy implementation and finding very little in the PHP + R space, I was able to piece together a method for integrating the two.  It uses no JavaScript&#8230; no AJAX&#8230; just plain old PHP.</p>
<p><span id="more-1135"></span></p>
<h2>The poor man&#8217;s R web app</h2>
<p><strong>Disclaimer</strong>:  There are many reasons why you shouldn&#8217;t actually stick the following code on the web for all to see.  It is a very stripped down example, which is great for experimenting on a localhost, but has some serious security flaws.</p>
<p>This implementation of PHP and R consists of only two files.  One written in PHP, and the other an R script.  The PHP returns a form which uses the GET method to send a variable N to the server.  When the form is submitted, the PHP will then execute an R script from the shell using a combination of the PHP command <code>exec()</code> and the <code>Rscript</code> shell command.  This command will pass the variable N to the R script.  The R script will then execute and save a histogram plot of N normally distributed values to the filesystem.  Finally, when the R script is complete, the PHP will return the HTML tag containing the saved images path. First, the PHP file&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// poorman.php</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;form action='poorman.php' method='get'&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Number values to generate: &lt;input type='text' name='N' /&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;input type='submit' /&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;/form&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'N'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$N</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'N'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// execute R script from shell</span>
  <span style="color: #666666; font-style: italic;">// this will save a plot at temp.png to the filesystem</span>
  <span style="color: #990000;">exec</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Rscript my_rscript.R <span style="color: #006699; font-weight: bold;">$N</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// return image tag</span>
  <span style="color: #000088;">$nocache</span> <span style="color: #339933;">=</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">echo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;img src='temp.png?<span style="color: #006699; font-weight: bold;">$nocache</span>' /&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>and the R script&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;"># my_rscript.R</span>
&nbsp;
<span style="color: #0000FF; font-weight: bold;">args</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">commandArgs</span><span style="color: #080;">&#40;</span>TRUE<span style="color: #080;">&#41;</span>
&nbsp;
N <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">args</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span>
x <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">rnorm</span><span style="color: #080;">&#40;</span>N,<span style="color: #ff0000;">0</span>,<span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #0000FF; font-weight: bold;">png</span><span style="color: #080;">&#40;</span>filename<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;temp.png&quot;</span>, width<span style="color: #080;">=</span><span style="color: #ff0000;">500</span>, height<span style="color: #080;">=</span><span style="color: #ff0000;">500</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">hist</span><span style="color: #080;">&#40;</span>x, <span style="color: #0000FF; font-weight: bold;">col</span><span style="color: #080;">=</span><span style="color: #ff0000;">&quot;lightblue&quot;</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">dev.<span style="">off</span></span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span></pre></div></div>

<p>Finally, to help you visualize the whole process a bit better, below is a screenshot of the results&#8230;<br />
<a href="http://stotastic.com/wordpress/wp-content/uploads/2010/08/poorman_after.png"><img src="http://stotastic.com/wordpress/wp-content/uploads/2010/08/poorman_after-300x253.png" alt="" title="poorman_after" width="300" height="253" class="alignnone size-medium wp-image-1145" /></a></p>
<h2>Some Issues</h2>
<p>This is a very simple example that works fine on a localhost, but there are some BIG issues that need to be resolved before this code is opened up to the web.</p>
<ol>
<li>The user input isn&#8217;t sanitized</li>
<li>Only one user can be using this web app at a time</li>
<li>More care needs to be taken with read/write access</li>
<li>More care needs to be taken with the relative file paths</li>
<li>Speed?</li>
<li>&#8230; and I&#8217;m sure I&#8217;m missing many more</li>
</ol>
<p>The bottom line is that with less than 20 lines of code we are able to generate R graphics in a browser&#8230; everything else is just details.</p>
]]></content:encoded>
			<wfw:commentRss>http://stotastic.com/wordpress/2010/08/integrating-php-and-r/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Introducing visualVaR.com</title>
		<link>http://stotastic.com/wordpress/2010/08/introducing-visualvar-com/</link>
		<comments>http://stotastic.com/wordpress/2010/08/introducing-visualvar-com/#comments</comments>
		<pubDate>Fri, 13 Aug 2010 22:13:22 +0000</pubDate>
		<dc:creator>Lee</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[data visualization]]></category>
		<category><![CDATA[Drupal]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[visualVaR]]></category>

		<guid isPermaLink="false">http://stotastic.com/wordpress/?p=1116</guid>
		<description><![CDATA[
After a month of on-again, off-again coding, I&#8217;ve finally completed a web site geared towards calculating the Value at Risk of the average investor&#8217;s portfolio.  The site is visualvar.com.  The big idea was to combine the statistical and visualization tools of R (especially ggplot2) with the web interface of Drupal.  While I&#8217;m [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://stotastic.com/wordpress/wp-content/uploads/2010/08/visualvar_home.png"><img src="http://stotastic.com/wordpress/wp-content/uploads/2010/08/visualvar_home-300x239.png" alt="" title="visualvar_home" width="300" height="239" class="alignnone size-medium wp-image-1123" /></a></p>
<p>After a month of on-again, off-again coding, I&#8217;ve finally completed a web site geared towards calculating the Value at Risk of the average investor&#8217;s portfolio.  The site is <a href="http://visualvar.com">visualvar.com</a>.  The big idea was to combine the statistical and visualization tools of R (especially ggplot2) with the web interface of Drupal.  While I&#8217;m happy with the results, I think this may only be the tip of the iceberg in mashing up these technologies.  As a side note, I took a bit of a shortcut and I don&#8217;t actually have R running directly on the web server, which means I had to settle for &#8216;overnight&#8217; calculations rather than &#8216;On-Demand&#8217;.  But I still think it is a good proof of concept for combing Drupal with R.  </p>
<p><span id="more-1116"></span></p>
<h2>Some Examples</h2>
<p>For all the data lovers out there, below are some of my favorite visualizations that visualVaR produces using the users inputted portfolio.  This one showing the rank correlation between the daily returns of the stocks in a portfolio (obviously the higher the dependency between two stocks, the greater the VaR).</p>
<p><a href="http://stotastic.com/wordpress/wp-content/uploads/2010/08/demo_stock_correlation.png"><img src="http://stotastic.com/wordpress/wp-content/uploads/2010/08/demo_stock_correlation.png" alt="" title="demo_stock_correlation" width="500" height="500" class="alignnone size-full wp-image-1117" /></a></p>
<p>And this one showing the 4 years of daily returns (the amount of data used in the VaR model) for a single stock in a portfolio, BP in the case. </p>
<p><a href="http://stotastic.com/wordpress/wp-content/uploads/2010/08/demo_stock_data.png"><img src="http://stotastic.com/wordpress/wp-content/uploads/2010/08/demo_stock_data.png" alt="" title="demo_stock_data" width="500" height="500" class="alignnone size-full wp-image-1118" /></a></p>
<p>I was a little hesitant to turn the site loose on the web, but I figured it would be best to just &#8217;see what happens&#8217;.  Please feel free to try it out, just cross your fingers that the server holds!</p>
]]></content:encoded>
			<wfw:commentRss>http://stotastic.com/wordpress/2010/08/introducing-visualvar-com/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Save R plot as a BLOB</title>
		<link>http://stotastic.com/wordpress/2010/07/save-r-plot-as-a-blob/</link>
		<comments>http://stotastic.com/wordpress/2010/07/save-r-plot-as-a-blob/#comments</comments>
		<pubDate>Sat, 31 Jul 2010 02:48:19 +0000</pubDate>
		<dc:creator>Lee</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[R]]></category>

		<guid isPermaLink="false">http://stotastic.com/wordpress/?p=1096</guid>
		<description><![CDATA[I recently posed a question on stackoverflow on whether anyone knew an efficient way to save an R plot to a MySQL database as a BLOB.  My plan was to use my personal desktop to perform R routines and save them to a web server, where they could then be accessed and displayed on [...]]]></description>
			<content:encoded><![CDATA[<p>I recently posed a question on <a href="http://stackoverflow.com/questions/3285307/save-r-plot-to-web-server">stackoverflow</a> on whether anyone knew an efficient way to save an R plot to a MySQL database as a BLOB.  My plan was to use my personal desktop to perform R routines and save them to a web server, where they could then be accessed and displayed on a web page using a little PHP magic.  After getting numerous responses on what a terrible idea this was, I was able to piece my own solution together.  The steps are fairly simple.  First, save the plot as a temp file, Second, read it back into R as a binary string.  Third, insert the binary text into the database using the RODBC library.  The code snippet is below.</p>
<p><span id="more-1096"></span></p>

<div class="wp_syntax"><div class="code"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">## open connection</span>
<span style="color: #0000FF; font-weight: bold;">library</span><span style="color: #080;">&#40;</span>RODBC<span style="color: #080;">&#41;</span>
channel <span style="color: #080;">&lt;-</span> odbcConnect<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">## create a plot</span>
x <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">rnorm</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">100</span>,<span style="color: #ff0000;">0</span>,<span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">## save plot as temp file</span>
<span style="color: #0000FF; font-weight: bold;">png</span><span style="color: #080;">&#40;</span>filename<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;temp.png&quot;</span>, width<span style="color: #080;">=</span><span style="color: #ff0000;">500</span>, height<span style="color: #080;">=</span><span style="color: #ff0000;">500</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">hist</span><span style="color: #080;">&#40;</span>x, <span style="color: #0000FF; font-weight: bold;">col</span><span style="color: #080;">=</span><span style="color: #ff0000;">&quot;light blue&quot;</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">dev.<span style="">off</span></span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">## read temp file as a binary string</span>
plot_binary <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">paste</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">readBin</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;temp.png&quot;</span>, what<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;raw&quot;</span>, n<span style="color: #080;">=</span>1e6<span style="color: #080;">&#41;</span>, collapse<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">## insert binary sting into a table</span>
sqlQuery<span style="color: #080;">&#40;</span>channel, <span style="color: #0000FF; font-weight: bold;">paste</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;INSERT INTO test VALUES (1, x'&quot;</span>,plot_binary,<span style="color: #ff0000;">&quot;')&quot;</span>, sep<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">## close connection</span>
odbcClose<span style="color: #080;">&#40;</span>channel<span style="color: #080;">&#41;</span></pre></div></div>

<p>I understand the &#8216;other way&#8217; to do this (and most would argue better) would be to ftp the file to the server and save only the file path to the database.  I guess I&#8217;m just being a bit lazy by letting the database do all the storage details, but so far so good.</p>
]]></content:encoded>
			<wfw:commentRss>http://stotastic.com/wordpress/2010/07/save-r-plot-as-a-blob/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Yahoo Finance and Google Charts Mashup using PHP</title>
		<link>http://stotastic.com/wordpress/2010/06/yahoo-finance-and-google-charts-mashup-using-php/</link>
		<comments>http://stotastic.com/wordpress/2010/06/yahoo-finance-and-google-charts-mashup-using-php/#comments</comments>
		<pubDate>Tue, 22 Jun 2010 03:40:24 +0000</pubDate>
		<dc:creator>Lee</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Google Charts]]></category>
		<category><![CDATA[mashup]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Yahoo Finance]]></category>

		<guid isPermaLink="false">http://stotastic.com/wordpress/?p=1045</guid>
		<description><![CDATA[Just for fun, lets use a little web voodoo to combine Yahoo Finance data with Google Charts.  Why?&#8230; because we can.  All we need is a little PHP, a web server, and a URL.  The goal is to download Yahoo Finance daily price data for two stocks, convert it to an array [...]]]></description>
			<content:encoded><![CDATA[<p>Just for fun, lets use a little web voodoo to combine <a href="http://finance.yahoo.com/">Yahoo Finance</a> data with <a href="http://code.google.com/apis/chart/image_charts.html">Google Charts</a>.  Why?&#8230; because we can.  All we need is a little PHP, a web server, and a URL.  The goal is to download Yahoo Finance daily price data for two stocks, convert it to an array of daily returns, and plot the results using Google Charts simply by calling a URL.  For instance, to produce this:</p>
<p><a href="http://stotastic.com/wordpress/wp-content/uploads/2010/06/scatterPlot_BP_SPY.png"><img src="http://stotastic.com/wordpress/wp-content/uploads/2010/06/scatterPlot_BP_SPY.png" alt="" title="scatterPlot_BP_SPY" width="400" height="400" class="alignnone size-full wp-image-1046" /></a></p>
<p>We just need to type this into our browser: <a href="http://stotastic.com/php/scatterPlot.php?tick1=BP&#038;tick2=SPY&#038;mm=01&#038;dd=01&#038;yyyy=2008">http://stotastic.com/php/scatterPlot.php?tick1=BP&#038;tick2=SPY&#038;mm=01&#038;dd=01&#038;yyyy=2008</a><br />
Where tick1 and tick2 are the stock tickers; and mm, dd, yyyy represent the starting date from which data is grabbed. So lets dig in and find out how PHP is used to make all this web magic work. </p>
<p><span id="more-1045"></span></p>
<h1>PHP Code</h1>
<p> First, we need a function that downloads the data from Yahoo Finance.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// read in Yahoo finance, parse, and return array</span>
<span style="color: #000000; font-weight: bold;">function</span> yahoo_get_adjclose<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ticker</span><span style="color: #339933;">,</span> <span style="color: #000088;">$mm</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dd</span><span style="color: #339933;">,</span> <span style="color: #000088;">$yyyy</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// subtract 1 from mm since January is 0</span>
  <span style="color: #000088;">$mm</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mm</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://ichart.finance.yahoo.com/table.csv?s=<span style="color: #006699; font-weight: bold;">$ticker</span>&amp;a=<span style="color: #006699; font-weight: bold;">$mm</span>&amp;b=<span style="color: #006699; font-weight: bold;">$dd</span>&amp;c=<span style="color: #006699; font-weight: bold;">$yyyy</span>&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$ticker_data</span> <span style="color: #339933;">=</span> CSV_to_array<span style="color: #009900;">&#40;</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// extract adjusted close and reverse data</span>
  <span style="color: #666666; font-style: italic;">// last row excluded since blank after parse</span>
  <span style="color: #000088;">$rowcount</span> <span style="color: #339933;">=</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ticker_data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rowcount</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$ticker_adjclose</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>float<span style="color: #009900;">&#41;</span><span style="color: #000088;">$ticker_data</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> 
&nbsp;
  <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ticker_adjclose</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Notice that we called a utility function to convert the data from a CSV to an array. We need this function because the data is returned from Yahoo Finance in a CSV and needs to be parsed into an array to be useful to us.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// function to parse CSV and return array</span>
<span style="color: #000000; font-weight: bold;">function</span> CSV_to_array<span style="color: #009900;">&#40;</span><span style="color: #000088;">$strData</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000088;">$arrRows</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$strData</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arrRows</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$strRow</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$arrReturn</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$strRow</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$arrReturn</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Also, in order to convert an array of daily close prices to returns, we&#8217;ll use the following utility function.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// convert value to log return</span>
<span style="color: #000000; font-weight: bold;">function</span> log_return<span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">&lt;</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$r</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">log</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">/</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$r</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Finally, we put the above code to work by taking in a GET request, using the above functions to download Yahoo Finance data and convert it to an array of returns, send the data to Google in a POST request, and pass on the resulting image back to the user.  The reason we are sending a POST request to send the data to Google rather than a GET is because we will be exceeding the data size of a GET request.  More info on how to send a POST request to Google can be found <a href="http://code.google.com/apis/chart/docs/post_requests.html">here</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'content-type: image/png'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://chart.apis.google.com/chart'</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #666666; font-style: italic;">// get variables</span>
<span style="color: #000088;">$ticker1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;tick1&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$ticker2</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;tick2&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mm</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;mm&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dd</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;dd&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$yyyy</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;yyyy&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$title</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Daily Return (%)&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// limit date to keep data size transfer down</span>
<span style="color: #000088;">$year_limit</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span><span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Y&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$yyyy</span><span style="color: #339933;">&lt;</span><span style="color: #000088;">$year_limit</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$mm</span> <span style="color: #339933;">=</span> <span style="color: #208080;">01</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$dd</span> <span style="color: #339933;">=</span> <span style="color: #208080;">01</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$yyyy</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2008</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// read in Yahoo Finance data</span>
<span style="color: #000088;">$ticker1_data</span> <span style="color: #339933;">=</span> yahoo_get_adjclose<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ticker1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$mm</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dd</span><span style="color: #339933;">,</span> <span style="color: #000088;">$yyyy</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$ticker2_data</span> <span style="color: #339933;">=</span> yahoo_get_adjclose<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ticker2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$mm</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dd</span><span style="color: #339933;">,</span> <span style="color: #000088;">$yyyy</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// calculate log returns</span>
<span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> log_return<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ticker1_data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$y</span> <span style="color: #339933;">=</span> log_return<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ticker2_data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// scale data to 1 to 100 range</span>
<span style="color: #000088;">$xmax</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ceil</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">max</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$xmin</span> <span style="color: #339933;">=</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">min</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$ymax</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ceil</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">max</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$ymin</span> <span style="color: #339933;">=</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">min</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$xscaled</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #339933;">-</span><span style="color: #000088;">$xmin</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xmax</span><span style="color: #339933;">-</span><span style="color: #000088;">$xmin</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$y</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$yscaled</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #339933;">-</span><span style="color: #000088;">$ymin</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ymax</span><span style="color: #339933;">-</span><span style="color: #000088;">$ymin</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// calculate chart range params</span>
<span style="color: #000088;">$xrange_max</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$xmax</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$xrange_min</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$xmin</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$yrange_max</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$xmax</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$yrange_min</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$xmin</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$xdash</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">/</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xrange_max</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$xrange_min</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$ydash</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">/</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$yrange_max</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$yrange_min</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Add chart array with data and params </span>
<span style="color: #000088;">$chart</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> 
  <span style="color: #0000ff;">'cht'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'s'</span><span style="color: #339933;">,</span> 
  <span style="color: #0000ff;">'chs'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'400x400'</span><span style="color: #339933;">,</span> 
  <span style="color: #0000ff;">'chd'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;t:&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$xscaled</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;|&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$yscaled</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">'chxt'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;x,x,y,y&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">'chxr'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;0,<span style="color: #006699; font-weight: bold;">$xrange_min</span>,<span style="color: #006699; font-weight: bold;">$xrange_max</span>,5|2,<span style="color: #006699; font-weight: bold;">$yrange_min</span>,<span style="color: #006699; font-weight: bold;">$yrange_max</span>,5&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">'chm'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;o,3399FF,0,-1,5&quot;</span><span style="color: #339933;">,</span> 
  <span style="color: #0000ff;">'chg'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$xdash</span>,<span style="color: #006699; font-weight: bold;">$ydash</span>&quot;</span><span style="color: #339933;">,</span> 
  <span style="color: #0000ff;">'chtt'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$title</span>&quot;</span><span style="color: #339933;">,</span> 
  <span style="color: #0000ff;">'chxl'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;1:|<span style="color: #006699; font-weight: bold;">$ticker1</span>|3:|<span style="color: #006699; font-weight: bold;">$ticker2</span>|&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">'chxp'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;1,50|3,50&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Send the request, and print out the returned bytes. </span>
<span style="color: #000088;">$context</span> <span style="color: #339933;">=</span> <span style="color: #990000;">stream_context_create</span><span style="color: #009900;">&#40;</span> 
  <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> 
    <span style="color: #0000ff;">'method'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'POST'</span><span style="color: #339933;">,</span> 
    <span style="color: #0000ff;">'content'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">http_build_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$chart</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #990000;">fpassthru</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'r'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #000088;">$context</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>And there you have it! Google performs very little data processing, probably in order to keep server capacity down; therefore, a big chunk of the code above is needed to scale and format the data <em>before</em> sending it to Google. Take my server for a spin by typing something like this into your browser: <a href="http://stotastic.com/php/scatterPlot.php?tick1=JPM&#038;tick2=GS&#038;mm=01&#038;dd=01&#038;yyyy=2009">http://stotastic.com/php/scatterPlot.php?tick1=JPM&#038;tick2=GS&#038;mm=01&#038;dd=01&#038;yyyy=2009</a>, but please be kind to my server! (there is a GET sanitizing step I&#8217;m not showing in the code above).</p>
]]></content:encoded>
			<wfw:commentRss>http://stotastic.com/wordpress/2010/06/yahoo-finance-and-google-charts-mashup-using-php/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Multivariate normal generator in JavaScript</title>
		<link>http://stotastic.com/wordpress/2010/06/multivariate-normal-generato-in-javascript/</link>
		<comments>http://stotastic.com/wordpress/2010/06/multivariate-normal-generato-in-javascript/#comments</comments>
		<pubDate>Wed, 02 Jun 2010 02:11:11 +0000</pubDate>
		<dc:creator>Lee</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[multivariate normal]]></category>
		<category><![CDATA[RNG]]></category>

		<guid isPermaLink="false">http://stotastic.com/wordpress/?p=1018</guid>
		<description><![CDATA[I recently needed to generate multivariate normal values in a web browser.  Since I was unable to find a simple implementation, I wrote the following JavaScript code.  This code is not the fastest, and it also makes use of the Math.random() method which I can&#8217;t vouch for, but it seems to do a [...]]]></description>
			<content:encoded><![CDATA[<p>I recently needed to generate multivariate normal values in a web browser.  Since I was unable to find a simple implementation, I wrote the following JavaScript code.  This code is not the fastest, and it also makes use of the Math.random() method which I can&#8217;t vouch for, but it seems to do a decent job.  The first two functions are needed to generate normal values via the <a href="http://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform">Box-Muller</a> method.  This implementation uses the basic trig functions of the <a href="http://www.w3schools.com/js/js_obj_math.asp">JavaScript Math object</a>.  The third function performs a <a href="http://en.wikipedia.org/wiki/Cholesky_decomposition">Cholesky decomposition</a>, which is need to decompose a correlation matrix.  Finally, the last function combines these functions to generate iid normals and then transform them to correlated normals.</p>
<p><span id="more-1018"></span></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// generates a pair of iid standard normals using Box-Muller</span>
<span style="color: #003366; font-weight: bold;">function</span> boxMuller<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> u1 <span style="color: #339933;">=</span> Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> u2 <span style="color: #339933;">=</span> Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #003366; font-weight: bold;">var</span> R <span style="color: #339933;">=</span> Math.<span style="color: #660066;">sqrt</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">2</span><span style="color: #339933;">*</span>Math.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>u1<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> theta <span style="color: #339933;">=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">*</span>Math.<span style="color: #660066;">PI</span><span style="color: #339933;">*</span>u2<span style="color: #339933;">;</span>
&nbsp;
  x<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> R<span style="color: #339933;">*</span>Math.<span style="color: #660066;">cos</span><span style="color: #009900;">&#40;</span>theta<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  x<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> R<span style="color: #339933;">*</span>Math.<span style="color: #660066;">sin</span><span style="color: #009900;">&#40;</span>theta<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">return</span><span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #006600; font-style: italic;">// generates a vector of iid standard normals</span>
<span style="color: #006600; font-style: italic;">// n is the number of normals to generate</span>
<span style="color: #003366; font-weight: bold;">function</span> rnorm<span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> y <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> k <span style="color: #339933;">=</span> n <span style="color: #339933;">%</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// if n is even</span>
  <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>k<span style="color: #339933;">==</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span><span style="color: #009900;">&#40;</span>n<span style="color: #339933;">/</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      y <span style="color: #339933;">=</span> boxMuller<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      x<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">*</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span>     <span style="color: #339933;">=</span> y<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      x<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">*</span><span style="color: #CC0000;">2</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> y<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// if n is odd</span>
  <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>k<span style="color: #339933;">==</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>Math.<span style="color: #660066;">round</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>n<span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      y <span style="color: #339933;">=</span> boxMuller<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      x<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">*</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span>     <span style="color: #339933;">=</span> y<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      x<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">*</span><span style="color: #CC0000;">2</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> y<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    y <span style="color: #339933;">=</span> boxMuller<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    x<span style="color: #009900;">&#91;</span>n<span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> y<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>  
  <span style="color: #000066; font-weight: bold;">return</span><span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// returns a cholesky decomposed matrix</span>
<span style="color: #006600; font-style: italic;">// A is the square matrix to decompose</span>
<span style="color: #003366; font-weight: bold;">function</span> cholesky<span style="color: #009900;">&#40;</span>A<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> n <span style="color: #339933;">=</span> A.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
  <span style="color: #006600; font-style: italic;">// create square matrix L</span>
  <span style="color: #003366; font-weight: bold;">var</span> L <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span>
  <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>n<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    L<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span>
    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> j<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> j<span style="color: #339933;">&lt;</span>n<span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      L<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>n<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> j<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> j<span style="color: #339933;">&lt;=</span>i<span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #003366; font-weight: bold;">var</span> ss <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> k<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> k<span style="color: #339933;">&lt;</span>j<span style="color: #339933;">;</span> k<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        ss <span style="color: #339933;">=</span> ss <span style="color: #339933;">+</span> L<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span> <span style="color: #339933;">*</span> L<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">==</span>j<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        L<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> Math.<span style="color: #660066;">sqrt</span><span style="color: #009900;">&#40;</span>A<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">-</span> ss<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
        L<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>A<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">-</span> ss<span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> L<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">return</span><span style="color: #009900;">&#40;</span>L<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// generates multivariate normals</span>
<span style="color: #006600; font-style: italic;">// n is the number of multivariate normals to generate</span>
<span style="color: #006600; font-style: italic;">// mu is an array of means</span>
<span style="color: #006600; font-style: italic;">// sig is an array of standard deviations</span>
<span style="color: #006600; font-style: italic;">// rho is a 2 dim correlation array</span>
<span style="color: #003366; font-weight: bold;">function</span> rmultinorm<span style="color: #009900;">&#40;</span>n<span style="color: #339933;">,</span> mu<span style="color: #339933;">,</span> sig<span style="color: #339933;">,</span> rho<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> N <span style="color: #339933;">=</span> mu.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> A <span style="color: #339933;">=</span> cholesky<span style="color: #009900;">&#40;</span>rho<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// create x which will be the return values</span>
  <span style="color: #003366; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span>
  <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>n<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    x<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span>N<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> j<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> j<span style="color: #339933;">&lt;</span>N<span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      x<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> k<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> k<span style="color: #339933;">&lt;</span>n<span style="color: #339933;">;</span> k<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// generate standard normals</span>
    <span style="color: #003366; font-weight: bold;">var</span> z <span style="color: #339933;">=</span> rnorm<span style="color: #009900;">&#40;</span>N<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #006600; font-style: italic;">// tranform to correlated standard normals</span>
    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>N<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #003366; font-weight: bold;">var</span> ss <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> j<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> j<span style="color: #339933;">&lt;</span>N<span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        ss <span style="color: #339933;">=</span> ss <span style="color: #339933;">+</span> z<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span>A<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      x<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> ss<span style="color: #339933;">*</span>sig<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> mu<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">return</span><span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://stotastic.com/wordpress/2010/06/multivariate-normal-generato-in-javascript/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The only thing smiling today is Volatility</title>
		<link>http://stotastic.com/wordpress/2010/05/smiling-volatility/</link>
		<comments>http://stotastic.com/wordpress/2010/05/smiling-volatility/#comments</comments>
		<pubDate>Fri, 21 May 2010 21:52:03 +0000</pubDate>
		<dc:creator>Lee</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Black-Scholes]]></category>
		<category><![CDATA[implied volatility]]></category>
		<category><![CDATA[options]]></category>
		<category><![CDATA[R]]></category>

		<guid isPermaLink="false">http://stotastic.com/wordpress/?p=990</guid>
		<description><![CDATA[With the stock market freaking out and all, I figured I should take a look at how volatility was being priced in the option market.  The CBOE generously provides snapshots of market data for anyone interested to download.  By using this data, we can calculate the markets &#8216;implied volatility&#8217;, or level of &#8216;freaking [...]]]></description>
			<content:encoded><![CDATA[<p>With the stock market freaking out and all, I figured I should take a look at how volatility was being priced in the option market.  The <a href="http://www.cboe.com/">CBOE</a> generously provides snapshots of market data for anyone interested to download.  By using this data, we can calculate the markets &#8216;implied volatility&#8217;, or level of &#8216;freaking out&#8217;.  For those not familiar with the concept of implied volatility, essentially we can take the prices of options in the market and back out the volatility implied by those prices using the <a href="http://stotastic.com/wordpress/2010/05/black-scholes-without-the-junk/">Black-Scholes formula</a>.  Its been shown over and over again that the assumptions of the Black-Scholes model don&#8217;t hold up to empirical data; but its an easy calculation to perform, and so implied volatility is a widely used metric.  Anyway, below is my Black-Scholes option pricing function and the function used to back out implied volatility (written in R of course).  Since implied volatility can only be found numerically, I used the <a href="http://en.wikipedia.org/wiki/Bisection_method">Bisection Method</a> to calculate it since it was easy to implement, but there are faster methods out there.</p>
<p><span id="more-990"></span></p>

<div class="wp_syntax"><div class="code"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">## Black-Scholes Function</span>
BS <span style="color: #080;">&lt;-</span>
<span style="color: #0000FF; font-weight: bold;">function</span><span style="color: #080;">&#40;</span>S, K, <span style="color: #0000FF; font-weight: bold;">T</span>, r, sig, type<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;C&quot;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
  d1 <span style="color: #080;">&lt;-</span> <span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">log</span><span style="color: #080;">&#40;</span>S<span style="color: #080;">/</span>K<span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> <span style="color: #080;">&#40;</span>r <span style="color: #080;">+</span> sig<span style="color: #080;">^</span><span style="color: #ff0000;">2</span><span style="color: #080;">/</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">*</span><span style="color: #0000FF; font-weight: bold;">T</span><span style="color: #080;">&#41;</span> <span style="color: #080;">/</span> <span style="color: #080;">&#40;</span>sig<span style="color: #080;">*</span><span style="color: #0000FF; font-weight: bold;">sqrt</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">T</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
  d2 <span style="color: #080;">&lt;-</span> d1 <span style="color: #080;">-</span> sig<span style="color: #080;">*</span><span style="color: #0000FF; font-weight: bold;">sqrt</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">T</span><span style="color: #080;">&#41;</span>
  <span style="color: #0000FF; font-weight: bold;">if</span><span style="color: #080;">&#40;</span>type<span style="color: #080;">==</span><span style="color: #ff0000;">&quot;C&quot;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
    value <span style="color: #080;">&lt;-</span> S<span style="color: #080;">*</span><span style="color: #0000FF; font-weight: bold;">pnorm</span><span style="color: #080;">&#40;</span>d1<span style="color: #080;">&#41;</span> <span style="color: #080;">-</span> K<span style="color: #080;">*</span><span style="color: #0000FF; font-weight: bold;">exp</span><span style="color: #080;">&#40;</span><span style="color: #080;">-</span>r<span style="color: #080;">*</span><span style="color: #0000FF; font-weight: bold;">T</span><span style="color: #080;">&#41;</span><span style="color: #080;">*</span><span style="color: #0000FF; font-weight: bold;">pnorm</span><span style="color: #080;">&#40;</span>d2<span style="color: #080;">&#41;</span>
  <span style="color: #080;">&#125;</span>
  <span style="color: #0000FF; font-weight: bold;">if</span><span style="color: #080;">&#40;</span>type<span style="color: #080;">==</span><span style="color: #ff0000;">&quot;P&quot;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
    value <span style="color: #080;">&lt;-</span> K<span style="color: #080;">*</span><span style="color: #0000FF; font-weight: bold;">exp</span><span style="color: #080;">&#40;</span><span style="color: #080;">-</span>r<span style="color: #080;">*</span><span style="color: #0000FF; font-weight: bold;">T</span><span style="color: #080;">&#41;</span><span style="color: #080;">*</span><span style="color: #0000FF; font-weight: bold;">pnorm</span><span style="color: #080;">&#40;</span><span style="color: #080;">-</span>d2<span style="color: #080;">&#41;</span> <span style="color: #080;">-</span> S<span style="color: #080;">*</span><span style="color: #0000FF; font-weight: bold;">pnorm</span><span style="color: #080;">&#40;</span><span style="color: #080;">-</span>d1<span style="color: #080;">&#41;</span>
  <span style="color: #080;">&#125;</span>
  <span style="color: #0000FF; font-weight: bold;">return</span><span style="color: #080;">&#40;</span>value<span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #228B22;">## Function to find BS Implied Vol using Bisection Method</span>
implied.<span style="">vol</span> <span style="color: #080;">&lt;-</span>
<span style="color: #0000FF; font-weight: bold;">function</span><span style="color: #080;">&#40;</span>S, K, <span style="color: #0000FF; font-weight: bold;">T</span>, r, market, type<span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
  sig <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">0.20</span>
  sig.<span style="">up</span> <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">1</span>
  sig.<span style="">down</span> <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">0.001</span>
  count <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">0</span>
  err <span style="color: #080;">&lt;-</span> BS<span style="color: #080;">&#40;</span>S, K, <span style="color: #0000FF; font-weight: bold;">T</span>, r, sig, type<span style="color: #080;">&#41;</span> <span style="color: #080;">-</span> market 
&nbsp;
  <span style="color: #228B22;">## repeat until error is sufficiently small or counter hits 1000</span>
  <span style="color: #0000FF; font-weight: bold;">while</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">abs</span><span style="color: #080;">&#40;</span>err<span style="color: #080;">&#41;</span> <span style="color: #080;">&gt;</span> <span style="color: #ff0000;">0.00001</span> <span style="color: #080;">&amp;&amp;</span> count<span style="color: #080;">&lt;</span><span style="color: #ff0000;">1000</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
    <span style="color: #0000FF; font-weight: bold;">if</span><span style="color: #080;">&#40;</span>err <span style="color: #080;">&lt;</span> <span style="color: #ff0000;">0</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
      sig.<span style="">down</span> <span style="color: #080;">&lt;-</span> sig
      sig <span style="color: #080;">&lt;-</span> <span style="color: #080;">&#40;</span>sig.<span style="">up</span> <span style="color: #080;">+</span> sig<span style="color: #080;">&#41;</span><span style="color: #080;">/</span><span style="color: #ff0000;">2</span>
    <span style="color: #080;">&#125;</span><span style="color: #0000FF; font-weight: bold;">else</span><span style="color: #080;">&#123;</span>
      sig.<span style="">up</span> <span style="color: #080;">&lt;-</span> sig
      sig <span style="color: #080;">&lt;-</span> <span style="color: #080;">&#40;</span>sig.<span style="">down</span> <span style="color: #080;">+</span> sig<span style="color: #080;">&#41;</span><span style="color: #080;">/</span><span style="color: #ff0000;">2</span>
    <span style="color: #080;">&#125;</span>
    err <span style="color: #080;">&lt;-</span> BS<span style="color: #080;">&#40;</span>S, K, <span style="color: #0000FF; font-weight: bold;">T</span>, r, sig, type<span style="color: #080;">&#41;</span> <span style="color: #080;">-</span> market
    count <span style="color: #080;">&lt;-</span> count <span style="color: #080;">+</span> <span style="color: #ff0000;">1</span>
  <span style="color: #080;">&#125;</span>
&nbsp;
  <span style="color: #228B22;">## return NA if counter hit 1000</span>
  <span style="color: #0000FF; font-weight: bold;">if</span><span style="color: #080;">&#40;</span>count<span style="color: #080;">==</span><span style="color: #ff0000;">1000</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
    <span style="color: #0000FF; font-weight: bold;">return</span><span style="color: #080;">&#40;</span>NA<span style="color: #080;">&#41;</span>
  <span style="color: #080;">&#125;</span><span style="color: #0000FF; font-weight: bold;">else</span><span style="color: #080;">&#123;</span>
    <span style="color: #0000FF; font-weight: bold;">return</span><span style="color: #080;">&#40;</span>sig<span style="color: #080;">&#41;</span>
  <span style="color: #080;">&#125;</span>
<span style="color: #080;">&#125;</span></pre></div></div>

<p>To apply the calculations above, I downloaded option data on the S&#038;P 500 (SPX) as of midday Friday, May 21st.  Data is available for multiple expiration dates, but I narrowed my analysis to only the near dated contract, which was the June contract.  As with all market data, I had to do a bit of cleaning first, but you can find my clean data <a href="http://stotastic.com/data/SPX_data.csv">here</a>.  Once cleaned, the data was read into R and cranked though the implied volatility function.  Note that the S&#038;P 500 was at 1082 at the time of these quotes.</p>

<div class="wp_syntax"><div class="code"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">## read in data</span>
dat <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">read.<span style="">csv</span></span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;SPX_data.csv&quot;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">## calculate implied vol for Call</span>
S <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">1082.74</span>
<span style="color: #0000FF; font-weight: bold;">T</span> <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">28</span><span style="color: #080;">/</span><span style="color: #ff0000;">365</span>
r <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">0.01</span>
&nbsp;
n <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">dim</span><span style="color: #080;">&#40;</span>dat<span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span>
c.<span style="">vol</span>.<span style="">Ask</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">rep</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">0</span>,n<span style="color: #080;">&#41;</span>
c.<span style="">vol</span>.<span style="">Bid</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">rep</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">0</span>,n<span style="color: #080;">&#41;</span>
p.<span style="">vol</span>.<span style="">Ask</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">rep</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">0</span>,n<span style="color: #080;">&#41;</span>
p.<span style="">vol</span>.<span style="">Bid</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">rep</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">0</span>,n<span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #0000FF; font-weight: bold;">for</span><span style="color: #080;">&#40;</span>i <span style="color: #0000FF; font-weight: bold;">in</span> <span style="color: #ff0000;">1</span><span style="color: #080;">:</span>n<span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
  c.<span style="">vol</span>.<span style="">Ask</span><span style="color: #080;">&#91;</span>i<span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> implied.<span style="">vol</span><span style="color: #080;">&#40;</span>S, dat$K<span style="color: #080;">&#91;</span>i<span style="color: #080;">&#93;</span>, <span style="color: #0000FF; font-weight: bold;">T</span>, r, dat$C.<span style="">Ask</span><span style="color: #080;">&#91;</span>i<span style="color: #080;">&#93;</span>, <span style="color: #ff0000;">&quot;C&quot;</span><span style="color: #080;">&#41;</span>
  c.<span style="">vol</span>.<span style="">Bid</span><span style="color: #080;">&#91;</span>i<span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> implied.<span style="">vol</span><span style="color: #080;">&#40;</span>S, dat$K<span style="color: #080;">&#91;</span>i<span style="color: #080;">&#93;</span>, <span style="color: #0000FF; font-weight: bold;">T</span>, r, dat$C.<span style="">Bid</span><span style="color: #080;">&#91;</span>i<span style="color: #080;">&#93;</span>, <span style="color: #ff0000;">&quot;C&quot;</span><span style="color: #080;">&#41;</span>
  p.<span style="">vol</span>.<span style="">Ask</span><span style="color: #080;">&#91;</span>i<span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> implied.<span style="">vol</span><span style="color: #080;">&#40;</span>S, dat$K<span style="color: #080;">&#91;</span>i<span style="color: #080;">&#93;</span>, <span style="color: #0000FF; font-weight: bold;">T</span>, r, dat$P.<span style="">Ask</span><span style="color: #080;">&#91;</span>i<span style="color: #080;">&#93;</span>, <span style="color: #ff0000;">&quot;P&quot;</span><span style="color: #080;">&#41;</span>
  p.<span style="">vol</span>.<span style="">Bid</span><span style="color: #080;">&#91;</span>i<span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> implied.<span style="">vol</span><span style="color: #080;">&#40;</span>S, dat$K<span style="color: #080;">&#91;</span>i<span style="color: #080;">&#93;</span>, <span style="color: #0000FF; font-weight: bold;">T</span>, r, dat$P.<span style="">Bid</span><span style="color: #080;">&#91;</span>i<span style="color: #080;">&#93;</span>, <span style="color: #ff0000;">&quot;P&quot;</span><span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span></pre></div></div>

<p>Finally the fun part, I plotted the implied volatility vs the strikes for both the calls and puts.  I also plotted the open interest (number of open contracts held in the market) vs strike.  For reference, I put the current S&#038;P 500 index value (at the time of these quotes) as a vertical dashed red line.</p>

<div class="wp_syntax"><div class="code"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">## plot Volatility Smile and Open Interest</span>
<span style="color: #0000FF; font-weight: bold;">par</span><span style="color: #080;">&#40;</span>mfrow<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">2</span>,<span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">plot</span><span style="color: #080;">&#40;</span>dat$K, c.<span style="">vol</span>.<span style="">Bid</span>, typ<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;l&quot;</span>, <span style="color: #0000FF; font-weight: bold;">col</span><span style="color: #080;">=</span><span style="color: #ff0000;">&quot;green&quot;</span>, xlim<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">800</span>, <span style="color: #ff0000;">1350</span><span style="color: #080;">&#41;</span>, ylim<span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span>.2, .60<span style="color: #080;">&#41;</span>,
     main<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;S&amp;P 500 June Call&quot;</span>, <span style="color: #ff0000;">&quot;Volatility Smile&quot;</span><span style="color: #080;">&#41;</span>, xlab<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;Strike&quot;</span>, ylab<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;Implied Vol&quot;</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">lines</span><span style="color: #080;">&#40;</span>dat$K, c.<span style="">vol</span>.<span style="">Ask</span>, <span style="color: #0000FF; font-weight: bold;">col</span><span style="color: #080;">=</span><span style="color: #ff0000;">&quot;blue&quot;</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">abline</span><span style="color: #080;">&#40;</span>v<span style="color: #080;">=</span>S, <span style="color: #0000FF; font-weight: bold;">col</span><span style="color: #080;">=</span><span style="color: #ff0000;">&quot;red&quot;</span>, lty<span style="color: #080;">=</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">legend</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;bottomleft&quot;</span>, cex<span style="color: #080;">=</span><span style="color: #ff0000;">0.9</span>, <span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;Ask&quot;</span>, <span style="color: #ff0000;">&quot;Bid&quot;</span><span style="color: #080;">&#41;</span>, lty<span style="color: #080;">=</span><span style="color: #ff0000;">1</span>, <span style="color: #0000FF; font-weight: bold;">col</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;blue&quot;</span>, <span style="color: #ff0000;">&quot;green&quot;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #0000FF; font-weight: bold;">plot</span><span style="color: #080;">&#40;</span>dat$K, p.<span style="">vol</span>.<span style="">Bid</span>, typ<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;l&quot;</span>, <span style="color: #0000FF; font-weight: bold;">col</span><span style="color: #080;">=</span><span style="color: #ff0000;">&quot;green&quot;</span>, xlim<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">800</span>, <span style="color: #ff0000;">1350</span><span style="color: #080;">&#41;</span>, ylim<span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span>.2, .6<span style="color: #080;">&#41;</span>,
     main<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;S&amp;P 500 June Put&quot;</span>, <span style="color: #ff0000;">&quot;Volatility Smile&quot;</span><span style="color: #080;">&#41;</span>, xlab<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;Strike&quot;</span>, ylab<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;Implied Vol&quot;</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">lines</span><span style="color: #080;">&#40;</span>dat$K, p.<span style="">vol</span>.<span style="">Ask</span>, <span style="color: #0000FF; font-weight: bold;">col</span><span style="color: #080;">=</span><span style="color: #ff0000;">&quot;blue&quot;</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">abline</span><span style="color: #080;">&#40;</span>v<span style="color: #080;">=</span>S, <span style="color: #0000FF; font-weight: bold;">col</span><span style="color: #080;">=</span><span style="color: #ff0000;">&quot;red&quot;</span>, lty<span style="color: #080;">=</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">legend</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;bottomleft&quot;</span>, cex<span style="color: #080;">=</span><span style="color: #ff0000;">0.9</span>, <span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;Ask&quot;</span>, <span style="color: #ff0000;">&quot;Bid&quot;</span><span style="color: #080;">&#41;</span>, lty<span style="color: #080;">=</span><span style="color: #ff0000;">1</span>, <span style="color: #0000FF; font-weight: bold;">col</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;blue&quot;</span>, <span style="color: #ff0000;">&quot;green&quot;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
&nbsp;
 <span style="color: #0000FF; font-weight: bold;">plot</span><span style="color: #080;">&#40;</span>dat$K, dat$C.<span style="">Open</span>, xlim<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">800</span>, <span style="color: #ff0000;">1350</span><span style="color: #080;">&#41;</span>, ylim<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">0</span>, <span style="color: #ff0000;">260000</span><span style="color: #080;">&#41;</span>, type<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;h&quot;</span>,
     main<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;Open Interest&quot;</span>, xlab<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;Strike&quot;</span>, ylab<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;Open Interest&quot;</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">abline</span><span style="color: #080;">&#40;</span>v<span style="color: #080;">=</span>S, <span style="color: #0000FF; font-weight: bold;">col</span><span style="color: #080;">=</span><span style="color: #ff0000;">&quot;red&quot;</span>, lty<span style="color: #080;">=</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span>
&nbsp;
 <span style="color: #0000FF; font-weight: bold;">plot</span><span style="color: #080;">&#40;</span>dat$K, dat$P.<span style="">Open</span>, xlim<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">800</span>, <span style="color: #ff0000;">1350</span><span style="color: #080;">&#41;</span>, ylim<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">0</span>, <span style="color: #ff0000;">260000</span><span style="color: #080;">&#41;</span>, type<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;h&quot;</span>,
     main<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;Open Interest&quot;</span>, xlab<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;Strike&quot;</span>, ylab<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;Open Interest&quot;</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">abline</span><span style="color: #080;">&#40;</span>v<span style="color: #080;">=</span>S, <span style="color: #0000FF; font-weight: bold;">col</span><span style="color: #080;">=</span><span style="color: #ff0000;">&quot;red&quot;</span>, lty<span style="color: #080;">=</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span></pre></div></div>

<p><a href="http://stotastic.com/wordpress/wp-content/uploads/2010/05/SPX_smile.png"><img src="http://stotastic.com/wordpress/wp-content/uploads/2010/05/SPX_smile.png" alt="" title="SPX_smile" width="642" height="642" class="alignnone size-full wp-image-998" /></a></p>
<p>The most obvious thing you should notice is the phenomenon known as the &#8216;volatility smile&#8217;. The assumptions of the Black-Scholes model tell us that volatility should be constant for all strikes.  Clearly the market doesn&#8217;t believe this assumption and there are numerous theories on why this happens. Something you may have missed, but is rather shocking, most of the put contracts held are at a strike of 800.  These are essential insurance contracts for people who are worried about the S&#038;P 500 dropping over 25% within the next month. Scary!</p>
]]></content:encoded>
			<wfw:commentRss>http://stotastic.com/wordpress/2010/05/smiling-volatility/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Black-Scholes Without the Junk</title>
		<link>http://stotastic.com/wordpress/2010/05/black-scholes-without-the-junk/</link>
		<comments>http://stotastic.com/wordpress/2010/05/black-scholes-without-the-junk/#comments</comments>
		<pubDate>Mon, 17 May 2010 21:51:32 +0000</pubDate>
		<dc:creator>Lee</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Black-Scholes]]></category>
		<category><![CDATA[finance]]></category>
		<category><![CDATA[options]]></category>
		<category><![CDATA[Stochastic Calculus]]></category>

		<guid isPermaLink="false">http://stotastic.com/wordpress/?p=928</guid>
		<description><![CDATA[If you go to Google and search for “Black Scholes” you are bound to come across a long list of articles that derive the Black-Scholes PDE and Call Price formula. Before I learned about the more technical issues of stochastic calculus and martingale measures, I would read these derivations and assume the authors were the [...]]]></description>
			<content:encoded><![CDATA[<p>If you go to Google and search for “Black Scholes” you are bound to come across a long list of articles that derive the Black-Scholes PDE and Call Price formula. Before I learned about the more technical issues of stochastic calculus and martingale measures, I would read these derivations and assume the authors were the experts. There always seemed to be some hand waving going on, but I figured it was just a complex subject and I just didn’t fully understand all the details.  Now, some years later and after having formally learned the material, I find myself in disbelief at how sloppy and often wrong some of these derivations are.  As an example, take a look at <a href="http://en.wikipedia.org/wiki/Black_scholes">Wikipedia’s article on the Black Scholes model</a>.  Now forget everything you just read.  This article is my attempt to straighten things out.  I will try to be more rigorous than most, but I may skip over some of the regularity conditions which concern the pure math types.</p>
<p><span id="more-928"></span></p>
<h1>The Basics</h1>
<p>As in all models, the first thing we need to do is describe the assets we have available for us to trade.  I like to think of this as setting up the &#8216;rules of the game&#8217;.  These rules will tell us what assets we have available and how those assets interact with each other.  The Black-Scholes model gives us two assets to play with, the money market account and the stock we will base our option payoff on.  The Black-Scholes model describes these assets using Brownian Motion, with the following ‘real world’ dynamics.<br />
<br />
<img src='http://s.wordpress.com/latex.php?latex=dS_t%5E%7B%280%29%7D%3DS_t%5E%7B%280%29%7Drdt%20%5C%5C%20dS_t%3DS_t%28%5Cmu%20dt%2B%5Csigma%20dW_t%29%20%5C%5C%20S_0%5E%7B%280%29%7D%3D1&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='dS_t^{(0)}=S_t^{(0)}rdt \\ dS_t=S_t(\mu dt+\sigma dW_t) \\ S_0^{(0)}=1' title='dS_t^{(0)}=S_t^{(0)}rdt \\ dS_t=S_t(\mu dt+\sigma dW_t) \\ S_0^{(0)}=1' class='latex' /><br />
<br />
Where <img src='http://s.wordpress.com/latex.php?latex=S_t%5E%7B%280%29%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='S_t^{(0)}' title='S_t^{(0)}' class='latex' /> is the price of the money market account ‘share’ at time t and <img src='http://s.wordpress.com/latex.php?latex=S_t&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='S_t' title='S_t' class='latex' /> is the price of the stock.  The ‘Big Idea’ of the Black-Scholes model is that we can use these tradable assets to replicate the payoff of an option.  By continuously trading in these two assets, we will create a portfolio <img src='http://s.wordpress.com/latex.php?latex=X_t&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='X_t' title='X_t' class='latex' /> which will exactly replicate the final payoff of the option in all final states.  Since this portfolio will have the same cash flows as the option in all possible states, the value of this portfolio must be the same as the value of the option at all times.  If this wasn&#8217;t the case, there would be an arbitrage opportunity, which shouldn&#8217;t exist in an efficient market.  The following describes the replicating formula in math terms.<br />
<br />
<img src='http://s.wordpress.com/latex.php?latex=X_t%20%3D%20%5CDelta_t%5E%7B%280%29%7DS_t%5E%7B%280%29%7D%20%2B%20%5CDelta_tS_t%20%5C%5C%20X_T%20%3D%20%5Ctext%7BPayoff%20at%20Expiration%7D%20%5C%5C%20X_0%20%3D%20%5Ctext%7BPrice%20Today%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='X_t = \Delta_t^{(0)}S_t^{(0)} + \Delta_tS_t \\ X_T = \text{Payoff at Expiration} \\ X_0 = \text{Price Today}' title='X_t = \Delta_t^{(0)}S_t^{(0)} + \Delta_tS_t \\ X_T = \text{Payoff at Expiration} \\ X_0 = \text{Price Today}' class='latex' /><br />
<br />
Where <img src='http://s.wordpress.com/latex.php?latex=%5CDelta_t%5E%7B%280%29%7D%2C%20%5C%3A%20%5CDelta_t&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\Delta_t^{(0)}, \: \Delta_t' title='\Delta_t^{(0)}, \: \Delta_t' class='latex' /> are the trading strategies defining the number of ‘shares’ of each asset to own at time t.  Also, this replicating portfolio must satisfy the self financing condition, which means no money is added or removed from the portfolio.  The self financing condition is stated as <img src='http://s.wordpress.com/latex.php?latex=dX_t%20%3D%20%5CDelta_t%5E%7B%280%29%7DdS_t%5E%7B%280%29%7D%20%2B%20%5CDelta_t%20dS_t&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='dX_t = \Delta_t^{(0)}dS_t^{(0)} + \Delta_t dS_t' title='dX_t = \Delta_t^{(0)}dS_t^{(0)} + \Delta_t dS_t' class='latex' />.  If we select the money market asset as the numeraire (a.k.a. we are discounting), we can define the following variables as the discounted asset and portfolio prices.<br />
<br />
<img src='http://s.wordpress.com/latex.php?latex=%5Cbar%20S_t%5E%7B0%7D%20%3D%20%5Cfrac%7BS_t%5E%7B%280%29%7D%7D%7BS_t%5E%7B%280%29%7D%7D%3D1%20%5C%5C%20%5Cbar%20S_t%20%3D%20%5Cfrac%7BS_t%7D%7BS_t%5E%7B%280%29%7D%7D%5C%5C%20%5Cbar%20X_t%20%3D%20%5Cfrac%7BX_t%7D%7BS_t%5E%7B%280%29%7D%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\bar S_t^{0} = \frac{S_t^{(0)}}{S_t^{(0)}}=1 \\ \bar S_t = \frac{S_t}{S_t^{(0)}}\\ \bar X_t = \frac{X_t}{S_t^{(0)}}' title='\bar S_t^{0} = \frac{S_t^{(0)}}{S_t^{(0)}}=1 \\ \bar S_t = \frac{S_t}{S_t^{(0)}}\\ \bar X_t = \frac{X_t}{S_t^{(0)}}' class='latex' /><br />
 <br />
And the dynamics of the discounted assets under the ‘real world’ measure are<br />
<br />
<img src='http://s.wordpress.com/latex.php?latex=d%5Cbar%20S_t%5E%7B0%7D%20%3D%200%20%5C%5C%20d%5Cbar%20S_t%20%3D%20%5Cbar%20S_t%28%28%5Cmu-r%29dt%2B%5Csigma%20dW_t%29&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='d\bar S_t^{0} = 0 \\ d\bar S_t = \bar S_t((\mu-r)dt+\sigma dW_t)' title='d\bar S_t^{0} = 0 \\ d\bar S_t = \bar S_t((\mu-r)dt+\sigma dW_t)' class='latex' /><br />
<br />
Using the Numeraire Invariance Theorem, such that the replicating portfolio remains a self financing portfolio when the money market asset is the selected numeraire, the dynamics of the replicating portfolio can be written as<br />
<br />
<img src='http://s.wordpress.com/latex.php?latex=d%5Cbar%20X_t%20%3D%20%5CDelta_t%5E%7B%280%29%7Dd%20%5Cbar%20S_t%5E%7B%280%29%7D%20%2B%20%5CDelta_t%20d%20%5Cbar%20S_t%20%3D%20%5CDelta_t%20d%20%5Cbar%20S_t&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='d\bar X_t = \Delta_t^{(0)}d \bar S_t^{(0)} + \Delta_t d \bar S_t = \Delta_t d \bar S_t' title='d\bar X_t = \Delta_t^{(0)}d \bar S_t^{(0)} + \Delta_t d \bar S_t = \Delta_t d \bar S_t' class='latex' /><br />
<br />
Using Garsinov’s Theorem, we can change the measure to the ‘risk neutral’ measure (which I&#8217;ll identify with a <img src='http://s.wordpress.com/latex.php?latex=%5Cmathbb%20Q&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\mathbb Q' title='\mathbb Q' class='latex' />).<br />
<br />
<img src='http://s.wordpress.com/latex.php?latex=d%5Ctilde%7BW_t%7D%3DdW_t%20%2B%20%5Ctheta%20dt%20%5C%5C%20%5Ctext%7Bwhere%20%7D%20%5Ctheta%20%3D%20%5Cfrac%7B%5Cmu-r%7D%7B%5Csigma%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='d\tilde{W_t}=dW_t + \theta dt \\ \text{where } \theta = \frac{\mu-r}{\sigma}' title='d\tilde{W_t}=dW_t + \theta dt \\ \text{where } \theta = \frac{\mu-r}{\sigma}' class='latex' /><br />
<br />
Therefore, the risk neutral dynamics of the discounted stock price is now <img src='http://s.wordpress.com/latex.php?latex=d%20%5Cbar%20S_t%3D%20%5Cbar%20S_t%5Csigma%20d%20%5Ctilde%7BW%7D_t&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='d \bar S_t= \bar S_t\sigma d \tilde{W}_t' title='d \bar S_t= \bar S_t\sigma d \tilde{W}_t' class='latex' /> which is a Martingale under the risk neutral measure.  Plugging this into the discounted portfolio process, we get <img src='http://s.wordpress.com/latex.php?latex=d%5Cbar%20X_t%20%3D%20%5CDelta_t%20%5Cbar%20S_t%20%5Csigma%20d%20%5Ctilde%7BW%7D_t&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='d\bar X_t = \Delta_t \bar S_t \sigma d \tilde{W}_t' title='d\bar X_t = \Delta_t \bar S_t \sigma d \tilde{W}_t' class='latex' /> which is also a Martingale under the risk neutral measure.  This is a key property, because we can write the expectation under the risk neutral measure as the following.<br />
<br />
<img src='http://s.wordpress.com/latex.php?latex=%5Cmathbb%20E%5E%7B%5Cmathbb%20Q%7D%5B%5Cbar%20X_T%5D%20%3D%20%5Cbar%20X_0%20%20%5C%5C%20%5Cmathbb%20E%5E%7B%5Cmathbb%20Q%7D%5B%5Cfrac%7BX_T%7D%7BS_T%5E%7B%280%29%7D%7D%5D%3D%20%5Cfrac%7BX_0%7D%7BS_0%5E%7B%280%29%7D%7D%20%5C%5C%20%5Cmathbb%20E%5E%7B%5Cmathbb%20Q%7D%5B%5Cfrac%7BX_T%7D%7BS_T%5E%7B%280%29%7D%7D%5D%3D%20X_0%20%5C%5C%20%5Cmathbb%20E%5E%7B%5Cmathbb%20Q%7D%5B%5Cfrac%7B1%7D%7BS_T%5E%7B%280%29%7D%7D%20%5Ctext%7BPayoff%7D%5D%3D%20%5Ctext%7BPrice%20Today%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\mathbb E^{\mathbb Q}[\bar X_T] = \bar X_0  \\ \mathbb E^{\mathbb Q}[\frac{X_T}{S_T^{(0)}}]= \frac{X_0}{S_0^{(0)}} \\ \mathbb E^{\mathbb Q}[\frac{X_T}{S_T^{(0)}}]= X_0 \\ \mathbb E^{\mathbb Q}[\frac{1}{S_T^{(0)}} \text{Payoff}]= \text{Price Today}' title='\mathbb E^{\mathbb Q}[\bar X_T] = \bar X_0  \\ \mathbb E^{\mathbb Q}[\frac{X_T}{S_T^{(0)}}]= \frac{X_0}{S_0^{(0)}} \\ \mathbb E^{\mathbb Q}[\frac{X_T}{S_T^{(0)}}]= X_0 \\ \mathbb E^{\mathbb Q}[\frac{1}{S_T^{(0)}} \text{Payoff}]= \text{Price Today}' class='latex' /><br />
<br />
Since we can solve the SDE for the money market account as <img src='http://s.wordpress.com/latex.php?latex=S_T%5E%7B%280%29%7D%3DS_0%5E%7B%280%29%7De%5E%7BrT%7D%3D%20e%5E%7BrT%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='S_T^{(0)}=S_0^{(0)}e^{rT}= e^{rT}' title='S_T^{(0)}=S_0^{(0)}e^{rT}= e^{rT}' class='latex' />, then we can plug this into the expectation and get our concluding result.<br />
<br /> <img src='http://s.wordpress.com/latex.php?latex=%20%5Cmathbb%20E%5E%7B%5Cmathbb%20Q%7D%5B%20%5Cfrac%7B1%7D%7Be%5E%7BrT%7D%7D%5Ctext%7BPayoff%7D%5D%20%3D%20e%5E%7B-rT%7D%20%5Ccdot%20%5Cmathbb%20E%5E%7B%5Cmathbb%20Q%7D%5B%20%5Ctext%7BPayoff%7D%5D%3D%20%5Ctext%7BPrice%20Today%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt=' \mathbb E^{\mathbb Q}[ \frac{1}{e^{rT}}\text{Payoff}] = e^{-rT} \cdot \mathbb E^{\mathbb Q}[ \text{Payoff}]= \text{Price Today}' title=' \mathbb E^{\mathbb Q}[ \frac{1}{e^{rT}}\text{Payoff}] = e^{-rT} \cdot \mathbb E^{\mathbb Q}[ \text{Payoff}]= \text{Price Today}' class='latex' />.<br />
<br />
Many people jump to this conclusion when they state “The price of the option is the discounted expected payoff under the risk neutral measure”.  As a side note, when the interest rate is random, we can’t pull the discounting term out of the expectation, but the logic we followed is the same.  The Black Scholes model assumes a constant interest rate which allows us to find formula for this expectation, which I&#8217;ll show in a bit. </p>
<h1>The Black-Scholes PDE</h1>
<p>As I mentioned earlier, there are a lot of derivations of the Black-Scholes PDE posted on the internet. Some of which make bold assertions rather than using the mathematical properties developed from stochastic calculus.  These derivations basically assume a portfolio is delta-hedged and should therefore grow at the risk free rate.  While this seems intuitively correct, the underlying mathematical properties called upon are not very clear.  This approach is sometimes called the ‘Classical’ derivation.  Since we now have more developed theories today than in the 1970’s, doesn’t it make since to put them to use and simplify the derivation?  Below I present a derivation that relies on more precise mathematical properties, which is sometimes called the ‘Martingale’ derivation.  It makes use of the connection between SDEs and PDEs via the Feynman-Kac Theorem.<br />
<br />
First, using the definitions from above, we need to define the following function <img src='http://s.wordpress.com/latex.php?latex=f&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='f' title='f' class='latex' />.<br />
<br />
<img src='http://s.wordpress.com/latex.php?latex=%5Cbar%20X_t%20%3D%20%5Cfrac%7BX_t%7D%7BS_t%5E%7B%280%29%7D%7D%20%5C%5C%20%5Ctext%7Bwhere%20%7D%20X_t%20%3D%20f%28t%2CS_t%29&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\bar X_t = \frac{X_t}{S_t^{(0)}} \\ \text{where } X_t = f(t,S_t)' title='\bar X_t = \frac{X_t}{S_t^{(0)}} \\ \text{where } X_t = f(t,S_t)' class='latex' /><br />
<br />
The function <img src='http://s.wordpress.com/latex.php?latex=f&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='f' title='f' class='latex' /> is simply a function which gives use the value of the replicating portfolio by inputting the time until expiration and the current stock price.  Since the stock price is modeled as Geometric Brownian Motion, it is a Markov process.  There is no added information from knowing the stock price history; therefore, we only need the current stock price. The next step is to write out the dynamics of <img src='http://s.wordpress.com/latex.php?latex=%5Cbar%20X_t&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\bar X_t' title='\bar X_t' class='latex' /> using Ito’s Lemma.<br />
<br />
<img src='http://s.wordpress.com/latex.php?latex=d%20%5Cbar%20X_t%20%3D%20f%20d%20%5Cfrac%7B1%7D%7BS_t%5E%7B%280%29%7D%7D%20%2B%20%5Cfrac%7B1%7D%7BS_t%5E%7B%280%29%7D%7D%20df%20%2B%20d%20%5Cfrac%7B1%7D%7BS_t%5E%7B%280%29%7D%7D%20df%20%5C%5C%20d%20%5Cbar%20X_t%20%3D%20-f%20%5Cfrac%7B1%7D%7BS_t%5E%7B%280%29%7D%7D%20r%20dt%20%2B%20%5Cfrac%7B1%7D%7BS_t%5E%7B%280%29%7D%7D%20%5Cleft%28%20f_tdt%20%2B%20f_sdS_t%20%2B%20%5Cfrac%7Bf_%7Bss%7D%7D%7B2%7DdS_tdS_t%20%20%5Cright%29%20%5C%5C%20d%20%5Cbar%20X_t%20%3D%20-f%20%5Cfrac%7B1%7D%7BS_t%5E%7B%280%29%7D%7D%20r%20dt%20%2B%20%5Cfrac%7B1%7D%7BS_t%5E%7B%280%29%7D%7D%20%5Cleft%28%20f_tdt%20%2B%20f_sS_t%20r%20dt%20%2B%20f_s%20S_t%20%5Csigma%20d%20%5Ctilde%7BW%7D_t%20%2B%20%5Cfrac%7Bf_%7Bss%7D%7D%7B2%7D%20S_t%5E2%20%5Csigma%5E2%20dt%20%20%5Cright%29%20%5C%5C%20d%20%5Cbar%20X_t%20%3D%20%5Cfrac%7B1%7D%7BS_t%5E%7B%280%29%7D%7D%20%5Cleft%28-fr%2Bf_t%2Bf_s%20S_t%20r%20%2B%20%5Cfrac%7B1%7D%7B2%7D%20f_%7Bss%7D%20S_t%5E2%20%5Csigma%5E2%20%5Cright%29%20dt%20%2B%20%5Cfrac%7B1%7D%7BS_t%5E%7B%280%29%7D%7D%20f_s%20S_t%20%5Csigma%20d%20%5Ctilde%7BW%7D_t&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='d \bar X_t = f d \frac{1}{S_t^{(0)}} + \frac{1}{S_t^{(0)}} df + d \frac{1}{S_t^{(0)}} df \\ d \bar X_t = -f \frac{1}{S_t^{(0)}} r dt + \frac{1}{S_t^{(0)}} \left( f_tdt + f_sdS_t + \frac{f_{ss}}{2}dS_tdS_t  \right) \\ d \bar X_t = -f \frac{1}{S_t^{(0)}} r dt + \frac{1}{S_t^{(0)}} \left( f_tdt + f_sS_t r dt + f_s S_t \sigma d \tilde{W}_t + \frac{f_{ss}}{2} S_t^2 \sigma^2 dt  \right) \\ d \bar X_t = \frac{1}{S_t^{(0)}} \left(-fr+f_t+f_s S_t r + \frac{1}{2} f_{ss} S_t^2 \sigma^2 \right) dt + \frac{1}{S_t^{(0)}} f_s S_t \sigma d \tilde{W}_t' title='d \bar X_t = f d \frac{1}{S_t^{(0)}} + \frac{1}{S_t^{(0)}} df + d \frac{1}{S_t^{(0)}} df \\ d \bar X_t = -f \frac{1}{S_t^{(0)}} r dt + \frac{1}{S_t^{(0)}} \left( f_tdt + f_sdS_t + \frac{f_{ss}}{2}dS_tdS_t  \right) \\ d \bar X_t = -f \frac{1}{S_t^{(0)}} r dt + \frac{1}{S_t^{(0)}} \left( f_tdt + f_sS_t r dt + f_s S_t \sigma d \tilde{W}_t + \frac{f_{ss}}{2} S_t^2 \sigma^2 dt  \right) \\ d \bar X_t = \frac{1}{S_t^{(0)}} \left(-fr+f_t+f_s S_t r + \frac{1}{2} f_{ss} S_t^2 \sigma^2 \right) dt + \frac{1}{S_t^{(0)}} f_s S_t \sigma d \tilde{W}_t' class='latex' /><br />
<br />
Since we showed earlier that <img src='http://s.wordpress.com/latex.php?latex=%5Cbar%20X_t&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\bar X_t' title='\bar X_t' class='latex' /> is a Martingale under the risk neutral measure, the <img src='http://s.wordpress.com/latex.php?latex=dt&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='dt' title='dt' class='latex' /> term must be equal to 0.  Setting this term to 0, we get the following PDE.<br />
<br />
<img src='http://s.wordpress.com/latex.php?latex=-fr%2Bf_t%2Bf_s%20s%20r%2B%20%5Cfrac%7Bf_%7Bss%7D%7D%7B2%7D%20s%5E2%20%5Csigma%5E2%20%3D0%20%5C%5C%20%5Ctext%7Bwhere%20%7D%20f%28T%2Cs%29%3D%5Ctext%7BPayoff%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='-fr+f_t+f_s s r+ \frac{f_{ss}}{2} s^2 \sigma^2 =0 \\ \text{where } f(T,s)=\text{Payoff}' title='-fr+f_t+f_s s r+ \frac{f_{ss}}{2} s^2 \sigma^2 =0 \\ \text{where } f(T,s)=\text{Payoff}' class='latex' /><br />
<br />
And there you have it folks, the Black-Scholes PDE derived in a few simple steps using some basic properties of stochastic calculus.</p>
<h1>The Black-Scholes Call Price Formula</h1>
<p>We could solve this PDE to get a formula for the price of a call under this Black-Scholes model, but an easier way (in my opinion) is to calculate the expectation under the risk neutral measure.  First, recall that<br />
<br />
<img src='http://s.wordpress.com/latex.php?latex=e%5E%7B-rT%7D%20%5Ccdot%20%5Cmathbb%20E%5E%7B%5Cmathbb%20Q%7D%5B%20%5Ctext%7BPayoff%7D%5D%3D%20%5Ctext%7BPrice%7D%20%5C%5C%20%5Ctext%7Bwhere%20the%20call%20Payoff%7D%3D%5BS_T-K%5D%5E%2B&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='e^{-rT} \cdot \mathbb E^{\mathbb Q}[ \text{Payoff}]= \text{Price} \\ \text{where the call Payoff}=[S_T-K]^+' title='e^{-rT} \cdot \mathbb E^{\mathbb Q}[ \text{Payoff}]= \text{Price} \\ \text{where the call Payoff}=[S_T-K]^+' class='latex' /><br />
<br />
Also, the risk neutral dynamics of the stock price are <img src='http://s.wordpress.com/latex.php?latex=dS_t%3DS_t%20%28rdt%20%2B%20%5Csigma%20d%20%5Ctilde%7BW%7D_t%29&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='dS_t=S_t (rdt + \sigma d \tilde{W}_t)' title='dS_t=S_t (rdt + \sigma d \tilde{W}_t)' class='latex' />.  We can solve this SDE such that <img src='http://s.wordpress.com/latex.php?latex=S_T%3DS_0%20e%5E%7B%28r-%5Cfrac%7B%5Csigma%5E2%7D%7B2%7D%29T%2B%5Csigma%20%5Ctilde%7BW%7D_t%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='S_T=S_0 e^{(r-\frac{\sigma^2}{2})T+\sigma \tilde{W}_t}' title='S_T=S_0 e^{(r-\frac{\sigma^2}{2})T+\sigma \tilde{W}_t}' class='latex' />.  If we define <img src='http://s.wordpress.com/latex.php?latex=y%20%5Csim%20N%280%2C1%29&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='y \sim N(0,1)' title='y \sim N(0,1)' class='latex' />, then <img src='http://s.wordpress.com/latex.php?latex=%5Ctilde%7BW%7D_t%20%3D%20-y%5Csqrt%7BT%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\tilde{W}_t = -y\sqrt{T}' title='\tilde{W}_t = -y\sqrt{T}' class='latex' />, and so we can write the price of the option as the following.<br />
<br />
<img src='http://s.wordpress.com/latex.php?latex=%5Ctext%7BPrice%7D%20%3D%20e%5E%7B-rT%7D%20%5Ccdot%20%5Cmathbb%20E%5E%7B%5Cmathbb%20Q%7D%5B%5Ctext%7BPayoff%7D%5D%20%5C%5C%20%5Ctext%7BPrice%7D%20%3D%20e%5E%7B-rT%7D%20%5Ccdot%20%5Cmathbb%20E%5E%7B%5Cmathbb%20Q%7D%5B%5BS_T%20-%20K%5D%5E%2B%5D%20%5C%5C%20%5Ctext%7BPrice%7D%20%3D%20e%5E%7B-rT%7D%20%5Ccdot%20%5Cmathbb%20E%5E%7B%5Cmathbb%20Q%7D%5B%5BS_0%20e%5E%7B%28r-%5Cfrac%7B%5Csigma%5E2%7D%7B2%7D%29T%2B%5Csigma%20%5Ctilde%7BW%7D_T%7D-K%5D%5E%2B%5D%20%5C%5C%20%5Ctext%7BPrice%7D%20%3D%20e%5E%7B-rT%7D%20%5Ccdot%20%5Cmathbb%20E%5E%7B%5Cmathbb%20Q%7D%5B%5BS_0%20e%5E%7B%28r-%5Cfrac%7B%5Csigma%5E2%7D%7B2%7D%29T-%5Csigma%20y%5Csqrt%7BT%7D%7D-K%5D%5E%2B%5D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\text{Price} = e^{-rT} \cdot \mathbb E^{\mathbb Q}[\text{Payoff}] \\ \text{Price} = e^{-rT} \cdot \mathbb E^{\mathbb Q}[[S_T - K]^+] \\ \text{Price} = e^{-rT} \cdot \mathbb E^{\mathbb Q}[[S_0 e^{(r-\frac{\sigma^2}{2})T+\sigma \tilde{W}_T}-K]^+] \\ \text{Price} = e^{-rT} \cdot \mathbb E^{\mathbb Q}[[S_0 e^{(r-\frac{\sigma^2}{2})T-\sigma y\sqrt{T}}-K]^+]' title='\text{Price} = e^{-rT} \cdot \mathbb E^{\mathbb Q}[\text{Payoff}] \\ \text{Price} = e^{-rT} \cdot \mathbb E^{\mathbb Q}[[S_T - K]^+] \\ \text{Price} = e^{-rT} \cdot \mathbb E^{\mathbb Q}[[S_0 e^{(r-\frac{\sigma^2}{2})T+\sigma \tilde{W}_T}-K]^+] \\ \text{Price} = e^{-rT} \cdot \mathbb E^{\mathbb Q}[[S_0 e^{(r-\frac{\sigma^2}{2})T-\sigma y\sqrt{T}}-K]^+]' class='latex' /><br />
<br />
If we note that the payoff is only positive if<br />
<br />
<img src='http://s.wordpress.com/latex.php?latex=y%20%3C%20d_2%20%5C%5C%20%5Ctext%7Bwhere%20%7D%20d_2%3D%5Cfrac%7Bln%5Cleft%28%5Cfrac%7BS_0%7D%7BK%7D%5Cright%29%20%2B%20%28r-%5Cfrac%7B%5Csigma%5E2%7D%7B2%7D%29T%7D%7B%5Csigma%20%5Csqrt%7BT%7D%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='y &lt; d_2 \\ \text{where } d_2=\frac{ln\left(\frac{S_0}{K}\right) + (r-\frac{\sigma^2}{2})T}{\sigma \sqrt{T}}' title='y &lt; d_2 \\ \text{where } d_2=\frac{ln\left(\frac{S_0}{K}\right) + (r-\frac{\sigma^2}{2})T}{\sigma \sqrt{T}}' class='latex' /><br />
<br />
then by introducing an indicator variable and breaking the expectation into two terms<br />
<br />
<img src='http://s.wordpress.com/latex.php?latex=%5Ctext%7BPrice%7D%20%3D%20e%5E%7B-rT%7D%20%5Ccdot%20%5Cmathbb%20E%5E%7B%5Cmathbb%20Q%7D%5B%28S_0%20e%5E%7B%28r-%5Cfrac%7B%5Csigma%5E2%7D%7B2%7D%29T%20-%20%5Csigma%20y%20%5Csqrt%7BT%7D%7D-K%291_%7By%3Cd_2%7D%5D%20%5C%5C%20%5Ctext%7BPrice%7D%3D%20e%5E%7B-rT%7D%20%5Ccdot%20%5Cmathbb%20E%5E%7B%5Cmathbb%20Q%7D%5BS_0%20e%5E%7B%28r-%5Cfrac%7B%5Csigma%5E2%7D%7B2%7D%29T%20-%20%5Csigma%20y%20%5Csqrt%7BT%7D%7D%20%5Ccdot%201_%7By%3Cd_2%7D%5D%20-%20%20%20e%5E%7B-rT%7D%20%5Ccdot%20%5Cmathbb%20E%5E%7B%5Cmathbb%20Q%7D%5BK%20%5Ccdot%201_%7By%3Cd_2%7D%5D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\text{Price} = e^{-rT} \cdot \mathbb E^{\mathbb Q}[(S_0 e^{(r-\frac{\sigma^2}{2})T - \sigma y \sqrt{T}}-K)1_{y&lt;d_2}] \\ \text{Price}= e^{-rT} \cdot \mathbb E^{\mathbb Q}[S_0 e^{(r-\frac{\sigma^2}{2})T - \sigma y \sqrt{T}} \cdot 1_{y&lt;d_2}] -   e^{-rT} \cdot \mathbb E^{\mathbb Q}[K \cdot 1_{y&lt;d_2}]' title='\text{Price} = e^{-rT} \cdot \mathbb E^{\mathbb Q}[(S_0 e^{(r-\frac{\sigma^2}{2})T - \sigma y \sqrt{T}}-K)1_{y&lt;d_2}] \\ \text{Price}= e^{-rT} \cdot \mathbb E^{\mathbb Q}[S_0 e^{(r-\frac{\sigma^2}{2})T - \sigma y \sqrt{T}} \cdot 1_{y&lt;d_2}] -   e^{-rT} \cdot \mathbb E^{\mathbb Q}[K \cdot 1_{y&lt;d_2}]' class='latex' /><br />
<br />
We can simplify the second term.<br />
<br />
<img src='http://s.wordpress.com/latex.php?latex=-e%5E%7B-rT%7D%20%5Ccdot%20%5Cmathbb%20E%5E%7B%5Cmathbb%20Q%7D%5BK%20%5Ccdot%201_%7By%3Cd_2%7D%5D%20%5C%5C%20%3D%20-%20%20e%5E%7B-rT%7D%20K%20%5CPr%28y%3Cd_2%29%20%5C%5C%20%20%3D%20-%20%20e%5E%7B-rT%7D%20K%20%5CPhi%28d_2%29&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='-e^{-rT} \cdot \mathbb E^{\mathbb Q}[K \cdot 1_{y&lt;d_2}] \\ = -  e^{-rT} K \Pr(y&lt;d_2) \\  = -  e^{-rT} K \Phi(d_2)' title='-e^{-rT} \cdot \mathbb E^{\mathbb Q}[K \cdot 1_{y&lt;d_2}] \\ = -  e^{-rT} K \Pr(y&lt;d_2) \\  = -  e^{-rT} K \Phi(d_2)' class='latex' /><br />
<br />
Where <img src='http://s.wordpress.com/latex.php?latex=%5CPhi&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\Phi' title='\Phi' class='latex' /> is the standard normal CDF.  We can simplify the first term as well.<br />
<br />
<img src='http://s.wordpress.com/latex.php?latex=e%5E%7B-rT%7D%20%5Ccdot%20%5Cmathbb%20E%5E%7B%5Cmathbb%20Q%7D%5BS_0%20e%5E%7B%28r-%5Cfrac%7B%5Csigma%5E2%7D%7B2%7D%29T%20-%20%5Csigma%20y%20%5Csqrt%7BT%7D%7D%20%5Ccdot%201_%7By%3Cd_2%7D%5D%20%5C%5C%20%3D%20e%5E%7B-rT%7D%20%5Cint_%7B-%5Cinfty%7D%5E%7Bd_2%7D%20S_0%20e%5E%7B%28r-%5Cfrac%7B%5Csigma%5E2%7D%7B2%7D%29T%20-%20%5Csigma%20y%20%5Csqrt%7BT%7D%7D%20%5Cfrac%7B1%7D%7B%5Csqrt%7B2%5Cpi%7D%7De%5E%7B%5Cfrac%7B-y%5E2%7D%7B2%7D%7Ddy%20%5C%5C%20%3D%20S_0%20%5Cint_%7B-%5Cinfty%7D%5E%7Bd_2%7D%5Cfrac%7B1%7D%7B%5Csqrt%7B2%5Cpi%7D%7De%5E%5Cfrac%7B%7B-%28y%2B%5Csigma%20%5Csqrt%7BT%7D%29%5E2%7D%7D%7B2%7Ddy&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='e^{-rT} \cdot \mathbb E^{\mathbb Q}[S_0 e^{(r-\frac{\sigma^2}{2})T - \sigma y \sqrt{T}} \cdot 1_{y&lt;d_2}] \\ = e^{-rT} \int_{-\infty}^{d_2} S_0 e^{(r-\frac{\sigma^2}{2})T - \sigma y \sqrt{T}} \frac{1}{\sqrt{2\pi}}e^{\frac{-y^2}{2}}dy \\ = S_0 \int_{-\infty}^{d_2}\frac{1}{\sqrt{2\pi}}e^\frac{{-(y+\sigma \sqrt{T})^2}}{2}dy' title='e^{-rT} \cdot \mathbb E^{\mathbb Q}[S_0 e^{(r-\frac{\sigma^2}{2})T - \sigma y \sqrt{T}} \cdot 1_{y&lt;d_2}] \\ = e^{-rT} \int_{-\infty}^{d_2} S_0 e^{(r-\frac{\sigma^2}{2})T - \sigma y \sqrt{T}} \frac{1}{\sqrt{2\pi}}e^{\frac{-y^2}{2}}dy \\ = S_0 \int_{-\infty}^{d_2}\frac{1}{\sqrt{2\pi}}e^\frac{{-(y+\sigma \sqrt{T})^2}}{2}dy' class='latex' /><br />
<br />
By setting <img src='http://s.wordpress.com/latex.php?latex=z%3Dy%2B%5Csigma%20%5Csqrt%7BT%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='z=y+\sigma \sqrt{T}' title='z=y+\sigma \sqrt{T}' class='latex' /> and changing the upper limit of integration to <img src='http://s.wordpress.com/latex.php?latex=d_1%3Dd_2%20%2B%20%5Csigma%20%5Csqrt%7BT%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='d_1=d_2 + \sigma \sqrt{T}' title='d_1=d_2 + \sigma \sqrt{T}' class='latex' />.<br />
<br />
<img src='http://s.wordpress.com/latex.php?latex=S_0%20%5Cint_%7B-%5Cinfty%7D%5E%7Bd_2%7D%5Cfrac%7B1%7D%7B%5Csqrt%7B2%5Cpi%7D%7De%5E%5Cfrac%7B%7B-%28y%2B%5Csigma%20%5Csqrt%7BT%7D%29%5E2%7D%7D%7B2%7Dd%20%5C%5C%20%3DS_0%20%5Cint_%7B-%5Cinfty%7D%5E%7Bd_1%7D%20%5Cfrac%7B1%7D%7B%5Csqrt%7B2%5Cpi%7D%7De%5E%5Cfrac%7B-z%5E2%7D%7B2%7Ddz%20%5C%5C%20%3D%20S_0%5CPhi%28d_1%29&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='S_0 \int_{-\infty}^{d_2}\frac{1}{\sqrt{2\pi}}e^\frac{{-(y+\sigma \sqrt{T})^2}}{2}d \\ =S_0 \int_{-\infty}^{d_1} \frac{1}{\sqrt{2\pi}}e^\frac{-z^2}{2}dz \\ = S_0\Phi(d_1)' title='S_0 \int_{-\infty}^{d_2}\frac{1}{\sqrt{2\pi}}e^\frac{{-(y+\sigma \sqrt{T})^2}}{2}d \\ =S_0 \int_{-\infty}^{d_1} \frac{1}{\sqrt{2\pi}}e^\frac{-z^2}{2}dz \\ = S_0\Phi(d_1)' class='latex' /><br />
<br />
Putting the first and second terms together<br />
<br />
<img src='http://s.wordpress.com/latex.php?latex=%5Ctext%7BPrice%7D%20%3D%20S_0%5CPhi%28d_1%29-e%5E%7B-rT%7DK%5CPhi%28d_2%29%20%5C%5C%20%5Ctext%7Bwhere%20%7D%20d_2%3D%5Cfrac%7Bln%5Cleft%28%5Cfrac%7BS_0%7D%7BK%7D%5Cright%29%20%2B%20%28r-%5Cfrac%7B%5Csigma%5E2%7D%7B2%7D%29T%7D%7B%5Csigma%20%5Csqrt%7BT%7D%7D%20%5C%5C%20%5Ctext%7Band%20%7D%20d_1%3Dd_2%20%2B%20%5Csigma%20%5Csqrt%7BT%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\text{Price} = S_0\Phi(d_1)-e^{-rT}K\Phi(d_2) \\ \text{where } d_2=\frac{ln\left(\frac{S_0}{K}\right) + (r-\frac{\sigma^2}{2})T}{\sigma \sqrt{T}} \\ \text{and } d_1=d_2 + \sigma \sqrt{T}' title='\text{Price} = S_0\Phi(d_1)-e^{-rT}K\Phi(d_2) \\ \text{where } d_2=\frac{ln\left(\frac{S_0}{K}\right) + (r-\frac{\sigma^2}{2})T}{\sigma \sqrt{T}} \\ \text{and } d_1=d_2 + \sigma \sqrt{T}' class='latex' /><br />
<br />
Boom goes the dynamite!</p>
<h1>Concluding Remarks</h1>
<p>I hope that by using consistent notation, showing intermediate steps, and by stating various mathematical theorems used; that the reader will find this article presents a clear derivation of the Black Scholes model as compared to most other articles on the internet.  To fully appreciate some of the mathematics, a basic knowledge of stochastic calculus is required. I suggest <a href="http://www.amazon.com/Stochastic-Calculus-Finance-II-Continuous-Time/dp/0387401016/ref=sr_1_1?ie=UTF8&#038;s=books&#038;qid=1274131740&#038;sr=8-1-catcorr">Shreve</a> as a good place to look for more rigorous details on some of the underlying math.</p>
]]></content:encoded>
			<wfw:commentRss>http://stotastic.com/wordpress/2010/05/black-scholes-without-the-junk/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Basket Option Pricing: Step by Step</title>
		<link>http://stotastic.com/wordpress/2010/05/basket-option-pricing/</link>
		<comments>http://stotastic.com/wordpress/2010/05/basket-option-pricing/#comments</comments>
		<pubDate>Sat, 08 May 2010 15:06:36 +0000</pubDate>
		<dc:creator>Lee</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[basket option]]></category>
		<category><![CDATA[option pricing]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[risk neutral]]></category>

		<guid isPermaLink="false">http://stotastic.com/wordpress/?p=841</guid>
		<description><![CDATA[I find options fascinating because they deal with the abstract ideas of volatility and correlation, both of which are unobservable and can often seem like wild animal spirits (take the current stock market as an example).  Understanding these subtle concepts is never easy, but it is essential in pricing some of the more exotic [...]]]></description>
			<content:encoded><![CDATA[<p>I find options fascinating because they deal with the abstract ideas of volatility and correlation, both of which are unobservable and can often seem like wild animal spirits (take the current stock market as an example).  Understanding these subtle concepts is never easy, but it is essential in pricing some of the more exotic options which involve multiple underlying stocks.  To set the scene, let’s pretend that your neighbor wants to make a bet with you where he will pay you $100 if Google (GOOG) and Apple (APPL) are above 500 and 240 respectively after 1 year, but you have to pay him $25 today.  How would we determine if $25 is a good deal or not?</p>
<p><span id="more-841"></span></p>
<h1>The Model</h1>
<p>The first things we need to do is to define the dynamics for all the tradable assets.  We have at our disposal a money market account, as well as the stocks of Google and Apple (it is not a coincidence that I picked companies that don’t pay dividends). Below describes how we will model the dynamics of these assets.<br />
<br />
<img src='http://s.wordpress.com/latex.php?latex=dS_t%5E%7B%280%29%7D%20%3D%20S_t%5E%7B%280%29%7D%20r_t%20dt%20%5C%5C%20dS_t%5E%7B%281%29%7D%20%3D%20S_t%5E%7B%281%29%7D%20%28%5Cmu_t%5E%7B%281%29%7D%20dt%20%2B%20%5Csigma_t%5E%7B%281%29%7D%20dW_t%5E%7B%281%29%7D%29%20%5C%5C%20dS_t%5E%7B%282%29%7D%20%3D%20S_t%5E%7B%282%29%7D%20%28%5Cmu_t%5E%7B%282%29%7D%20dt%20%2B%20%5Csigma_t%5E%7B%282%29%7D%20dW_t%5E%7B%282%29%7D%29%20%5C%5C%20%5Ctext%7Bwhere%20%7D%20%20dW_t%5E%7B%281%29%7DdW_t%5E%7B%282%29%7D%3D%5Crho%20dt&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='dS_t^{(0)} = S_t^{(0)} r_t dt \\ dS_t^{(1)} = S_t^{(1)} (\mu_t^{(1)} dt + \sigma_t^{(1)} dW_t^{(1)}) \\ dS_t^{(2)} = S_t^{(2)} (\mu_t^{(2)} dt + \sigma_t^{(2)} dW_t^{(2)}) \\ \text{where }  dW_t^{(1)}dW_t^{(2)}=\rho dt' title='dS_t^{(0)} = S_t^{(0)} r_t dt \\ dS_t^{(1)} = S_t^{(1)} (\mu_t^{(1)} dt + \sigma_t^{(1)} dW_t^{(1)}) \\ dS_t^{(2)} = S_t^{(2)} (\mu_t^{(2)} dt + \sigma_t^{(2)} dW_t^{(2)}) \\ \text{where }  dW_t^{(1)}dW_t^{(2)}=\rho dt' class='latex' /><br />
<br />
In this model, <img src='http://s.wordpress.com/latex.php?latex=S_t%5E%7B%280%29%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='S_t^{(0)}' title='S_t^{(0)}' class='latex' /> is the price of a share in the money market account, and <img src='http://s.wordpress.com/latex.php?latex=S_t%5E%7B%281%29%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='S_t^{(1)}' title='S_t^{(1)}' class='latex' /> and <img src='http://s.wordpress.com/latex.php?latex=S_t%5E%7B%282%29%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='S_t^{(2)}' title='S_t^{(2)}' class='latex' /> are the stock prices.  Notice that we have correlated the Brownian Motions <img src='http://s.wordpress.com/latex.php?latex=W_t%5E%7B%281%29%7D%2C%20%5C%3A%20W_t%5E%7B%282%29%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='W_t^{(1)}, \: W_t^{(2)}' title='W_t^{(1)}, \: W_t^{(2)}' class='latex' /> such that the stock prices will tend to move up and down together just like in the real world.  To make this model easier to work with, we will first convert the correlated Brownian Motions into independent Brownian Motions <img src='http://s.wordpress.com/latex.php?latex=B_t%5E%7B%281%29%7D%2C%20%5C%3A%20B_t%5E%7B%282%29%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='B_t^{(1)}, \: B_t^{(2)}' title='B_t^{(1)}, \: B_t^{(2)}' class='latex' /> by defining the following.<br />
<br />
<img src='http://s.wordpress.com/latex.php?latex=dW_t%5E%7B%281%29%7D%3DdB_t%5E%7B%281%29%7D%20%5C%5C%20dW_t%5E%7B%282%29%7D%3D%5Crho%20dB_t%5E%7B%281%29%7D%20%2B%20%5Csqrt%7B1-%5Crho%5E2%7DdB_t%5E%7B%282%29%7D%20%5C%5C%20%5Ctext%7Bwhere%20%7D%20dB_t%5E%7B%281%29%7DdB_t%5E%7B%282%29%7D%3D0&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='dW_t^{(1)}=dB_t^{(1)} \\ dW_t^{(2)}=\rho dB_t^{(1)} + \sqrt{1-\rho^2}dB_t^{(2)} \\ \text{where } dB_t^{(1)}dB_t^{(2)}=0' title='dW_t^{(1)}=dB_t^{(1)} \\ dW_t^{(2)}=\rho dB_t^{(1)} + \sqrt{1-\rho^2}dB_t^{(2)} \\ \text{where } dB_t^{(1)}dB_t^{(2)}=0' class='latex' /><br />
<br />
By doing this, we can rewrite the &#8216;real world&#8217; dynamics as<br />
<br />
<img src='http://s.wordpress.com/latex.php?latex=dS_t%5E%7B%281%29%7D%20%3D%20S_t%5E%7B%281%29%7D%20%28%5Cmu_t%5E%7B%281%29%7D%20dt%20%2B%20%5Csigma_t%5E%7B%281%29%7D%20dB_t%5E%7B%281%29%7D%29%20%5C%5C%20dS_t%5E%7B%282%29%7D%20%3D%20S_t%5E%7B%282%29%7D%20%28%5Cmu_t%5E%7B%282%29%7D%20dt%20%2B%20%5Csigma_t%5E%7B%282%29%7D%20%28%5Crho%20dB_t%5E%7B%281%29%7D%20%2B%20%5Csqrt%7B1-%5Crho%5E2%7DdB_t%5E%7B%282%29%7D%29%29&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='dS_t^{(1)} = S_t^{(1)} (\mu_t^{(1)} dt + \sigma_t^{(1)} dB_t^{(1)}) \\ dS_t^{(2)} = S_t^{(2)} (\mu_t^{(2)} dt + \sigma_t^{(2)} (\rho dB_t^{(1)} + \sqrt{1-\rho^2}dB_t^{(2)}))' title='dS_t^{(1)} = S_t^{(1)} (\mu_t^{(1)} dt + \sigma_t^{(1)} dB_t^{(1)}) \\ dS_t^{(2)} = S_t^{(2)} (\mu_t^{(2)} dt + \sigma_t^{(2)} (\rho dB_t^{(1)} + \sqrt{1-\rho^2}dB_t^{(2)}))' class='latex' /><br />
 </p>
<h1>Calibration</h1>
<p>Writing down a bunch of stochastic differential equations was pretty mechanical, but now comes the artsy part of modeling.  The first thing we need is some stock price data, which can easily be downloaded from <a href="http://finance.yahoo.com/">Yahoo Finance</a>. This data gives us the stock price over discreet time intervals (I picked daily close prices).  To use this data, lets define the discreet return <img src='http://s.wordpress.com/latex.php?latex=r_n&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='r_n' title='r_n' class='latex' /> as the solution to the following <img src='http://s.wordpress.com/latex.php?latex=S_%7Bn%2B1%7D%3DS_n%20e%5E%7Br_n%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='S_{n+1}=S_n e^{r_n}' title='S_{n+1}=S_n e^{r_n}' class='latex' />, in which case we can write <img src='http://s.wordpress.com/latex.php?latex=r_n%3Dlog%20%5Cleft%28%20%5Cfrac%7BS_%7Bn%2B1%7D%7D%7BS_n%7D%20%5Cright%29&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='r_n=log \left( \frac{S_{n+1}}{S_n} \right)' title='r_n=log \left( \frac{S_{n+1}}{S_n} \right)' class='latex' />.  By assuming the drift and volatility terms are constants, we can use our model of the stock price dynamics to write the discreet returns as the following.<br />
<br />
<img src='http://s.wordpress.com/latex.php?latex=r_n%5E%7B%281%29%7D%3D%5Cmu%5E%7B%281%29%7D%20%5Cdelta%20t%20%2B%20%5Csigma%5E%7B%281%29%7D%20%5Csqrt%7B%5Cdelta%20t%7D%20z_1%20%5C%5C%20r_n%5E%7B%282%29%7D%3D%5Cmu%5E%7B%282%29%7D%20%5Cdelta%20t%20%2B%20%5Csigma%5E%7B%282%29%7D%20%5Csqrt%7B%5Cdelta%20t%7D%20%28%20%5Crho%20z_1%20%2B%20%5Csqrt%7B1-%5Crho%5E2%7D%20z_2%29%20%5C%5C%20%5Ctext%7Bwhere%20%7D%20z_1%2C%20z_2%20%5Csim%20iid%20%5C%3A%20N%280%2C1%29&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='r_n^{(1)}=\mu^{(1)} \delta t + \sigma^{(1)} \sqrt{\delta t} z_1 \\ r_n^{(2)}=\mu^{(2)} \delta t + \sigma^{(2)} \sqrt{\delta t} ( \rho z_1 + \sqrt{1-\rho^2} z_2) \\ \text{where } z_1, z_2 \sim iid \: N(0,1)' title='r_n^{(1)}=\mu^{(1)} \delta t + \sigma^{(1)} \sqrt{\delta t} z_1 \\ r_n^{(2)}=\mu^{(2)} \delta t + \sigma^{(2)} \sqrt{\delta t} ( \rho z_1 + \sqrt{1-\rho^2} z_2) \\ \text{where } z_1, z_2 \sim iid \: N(0,1)' class='latex' /><br />
<br />
Where <img src='http://s.wordpress.com/latex.php?latex=%5Cdelta%20t&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\delta t' title='\delta t' class='latex' /> is the discreet time interval.  By doing this, we can show that the discreet returns are distributed iid normal.<br />
<br />
<img src='http://s.wordpress.com/latex.php?latex=r_n%5E%7B%281%29%7D%20%5Csim%20iid%20%5C%3A%20N%28%5Cmu%5E%7B%281%29%7D%20%5Cdelta%20t%2C%20%5C%3A%20%5Csigma%5E%7B%281%29%7D%20%5Csqrt%7B%5Cdelta%20t%7D%29%20%5C%5C%20r_n%5E%7B%282%29%7D%20%5Csim%20iid%20%5C%3A%20N%28%5Cmu%5E%7B%282%29%7D%20%5Cdelta%20t%2C%20%5C%3A%20%5Csigma%5E%7B%282%29%7D%20%5Csqrt%7B%5Cdelta%20t%7D%29&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='r_n^{(1)} \sim iid \: N(\mu^{(1)} \delta t, \: \sigma^{(1)} \sqrt{\delta t}) \\ r_n^{(2)} \sim iid \: N(\mu^{(2)} \delta t, \: \sigma^{(2)} \sqrt{\delta t})' title='r_n^{(1)} \sim iid \: N(\mu^{(1)} \delta t, \: \sigma^{(1)} \sqrt{\delta t}) \\ r_n^{(2)} \sim iid \: N(\mu^{(2)} \delta t, \: \sigma^{(2)} \sqrt{\delta t})' class='latex' /><br />
<br />
We can use the iid normal distribution to calibrate all the parameters.  <img src='http://s.wordpress.com/latex.php?latex=%5Csigma%5E%7B%28i%29%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\sigma^{(i)}' title='\sigma^{(i)}' class='latex' /> are simply the sample standard deviations of the discreet returns divided by <img src='http://s.wordpress.com/latex.php?latex=%20%5Csqrt%7B%5Cdelta%20t%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt=' \sqrt{\delta t}' title=' \sqrt{\delta t}' class='latex' />, <img src='http://s.wordpress.com/latex.php?latex=%5Crho&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\rho' title='\rho' class='latex' /> is the sample correlation of the discreet returns, and <img src='http://s.wordpress.com/latex.php?latex=%5Cmu%5E%7B%28i%29%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\mu^{(i)}' title='\mu^{(i)}' class='latex' /> are the sample means of the discreet returns divided by <img src='http://s.wordpress.com/latex.php?latex=%20%5Cdelta%20t&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt=' \delta t' title=' \delta t' class='latex' /> (although I should note that we don&#8217;t actually need to know <img src='http://s.wordpress.com/latex.php?latex=%5Cmu%5E%7B%28i%29%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\mu^{(i)}' title='\mu^{(i)}' class='latex' /> to price this option).</p>
<h1>The Data</h1>
<p>Now that we have the math worked out for calibrating the model, let’s take a look at some real data and see if the data agrees with our assumptions.  First we need to read the data into R.  The data I used can be found <a href="http://stotastic.com/data/GOOG_AAPL.csv">here</a>. (I used R&#8217;s scan function since I’m lazy).</p>

<div class="wp_syntax"><div class="code"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">## Scan in close prices</span>
GOOG <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">scan</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
AAPL <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">scan</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span></pre></div></div>

<p>Let’s take a look at the time series of stock prices.</p>

<div class="wp_syntax"><div class="code"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">## regular stock price chart</span>
<span style="color: #0000FF; font-weight: bold;">par</span><span style="color: #080;">&#40;</span>mfrow<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">2</span>,<span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">plot</span><span style="color: #080;">&#40;</span>GOOG, type<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;l&quot;</span>, main<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;GOOG closing Price&quot;</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">plot</span><span style="color: #080;">&#40;</span>AAPL, type<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;l&quot;</span>, main<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;AAPL closing Price&quot;</span><span style="color: #080;">&#41;</span></pre></div></div>

<p><a href="http://stotastic.com/wordpress/wp-content/uploads/2010/05/close_price.png"><img src="http://stotastic.com/wordpress/wp-content/uploads/2010/05/close_price.png" alt="" title="close_price" width="642" height="642" class="alignnone size-full wp-image-874" /></a></p>
<p>Now calculate and plot the daily returns.</p>

<div class="wp_syntax"><div class="code"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">## convert to returns</span>
n <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span>GOOG<span style="color: #080;">&#41;</span>
GOOG.<span style="">r</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">log</span><span style="color: #080;">&#40;</span>GOOG<span style="color: #080;">&#91;</span><span style="color: #ff0000;">2</span><span style="color: #080;">:</span>n<span style="color: #080;">&#93;</span><span style="color: #080;">/</span>GOOG<span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #080;">&#40;</span>n<span style="color: #080;">-</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
AAPL.<span style="">r</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">log</span><span style="color: #080;">&#40;</span>AAPL<span style="color: #080;">&#91;</span><span style="color: #ff0000;">2</span><span style="color: #080;">:</span>n<span style="color: #080;">&#93;</span><span style="color: #080;">/</span>AAPL<span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #080;">&#40;</span>n<span style="color: #080;">-</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">## plot returns</span>
<span style="color: #0000FF; font-weight: bold;">matplot</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">cbind</span><span style="color: #080;">&#40;</span>GOOG.<span style="">r</span>, AAPL.<span style="">r</span><span style="color: #080;">&#41;</span>, type<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;l&quot;</span>, lty<span style="color: #080;">=</span><span style="color: #ff0000;">1</span>, main<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;Daily Returns&quot;</span>, ylab<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;Daily Return&quot;</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">abline</span><span style="color: #080;">&#40;</span>h<span style="color: #080;">=</span><span style="color: #ff0000;">0</span>, <span style="color: #0000FF; font-weight: bold;">col</span><span style="color: #080;">=</span><span style="color: #ff0000;">&quot;blue&quot;</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">legend</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;bottomleft&quot;</span>, <span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;GOOG&quot;</span>, <span style="color: #ff0000;">&quot;AAPL&quot;</span><span style="color: #080;">&#41;</span>, lty<span style="color: #080;">=</span><span style="color: #ff0000;">1</span>, <span style="color: #0000FF; font-weight: bold;">col</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span>,<span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span></pre></div></div>

<p><a href="http://stotastic.com/wordpress/wp-content/uploads/2010/05/daily_return.png"><img src="http://stotastic.com/wordpress/wp-content/uploads/2010/05/daily_return.png" alt="" title="daily_return" width="642" height="481" class="alignnone size-full wp-image-877" /></a></p>
<p>To check the iid normal assumption, we should do an autocorrelation plot and a QQ normal plot.</p>

<div class="wp_syntax"><div class="code"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">## autocorrelation plot</span>
<span style="color: #0000FF; font-weight: bold;">par</span><span style="color: #080;">&#40;</span>mfrow<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">2</span>,<span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">acf</span><span style="color: #080;">&#40;</span>GOOG.<span style="">r</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">acf</span><span style="color: #080;">&#40;</span>AAPL.<span style="">r</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">## QQ normal plots</span>
<span style="color: #0000FF; font-weight: bold;">qqnorm</span><span style="color: #080;">&#40;</span>GOOG.<span style="">r</span>, main<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;GOOG&quot;</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">qqline</span><span style="color: #080;">&#40;</span>GOOG.<span style="">r</span>, <span style="color: #0000FF; font-weight: bold;">col</span><span style="color: #080;">=</span><span style="color: #ff0000;">&quot;red&quot;</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">qqnorm</span><span style="color: #080;">&#40;</span>AAPL.<span style="">r</span>, main<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;AAPL&quot;</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">qqline</span><span style="color: #080;">&#40;</span>AAPL.<span style="">r</span>, <span style="color: #0000FF; font-weight: bold;">col</span><span style="color: #080;">=</span><span style="color: #ff0000;">&quot;red&quot;</span><span style="color: #080;">&#41;</span></pre></div></div>

<p><a href="http://stotastic.com/wordpress/wp-content/uploads/2010/05/return_dist.png"><img src="http://stotastic.com/wordpress/wp-content/uploads/2010/05/return_dist.png" alt="" title="return_dist" width="642" height="642" class="alignnone size-full wp-image-881" /></a></p>
<p>Lastly, lets also take a look at the joint distribution of the returns.</p>

<div class="wp_syntax"><div class="code"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">## pairs plot</span>
<span style="color: #0000FF; font-weight: bold;">plot</span><span style="color: #080;">&#40;</span>GOOG.<span style="">r</span>, AAPL.<span style="">r</span>, main<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;Daily Returns&quot;</span><span style="color: #080;">&#41;</span></pre></div></div>

<p><a href="http://stotastic.com/wordpress/wp-content/uploads/2010/05/joint_dist.png"><img src="http://stotastic.com/wordpress/wp-content/uploads/2010/05/joint_dist.png" alt="" title="joint_dist" width="642" height="642" class="alignnone size-full wp-image-882" /></a></p>
<p>The ACF plot suggests that the daily returns are iid, but they certainly have heavier tails than what would be produced by a normal distribution. In order to keep the model simple, we’ll assume normality of the returns holds, and give ourselves a ‘buffer’ when we calculate the fair price.  Finally, time for the calibration!</p>
<p><code><br />
GOOG vol: 0.2262006<br />
AAPL vol: 0.2756421<br />
Rho: 0.5413732<br />
</code></p>

<div class="wp_syntax"><div class="code"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">## calculate annualized vol</span>
GOOG.<span style="">vol</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">sd</span><span style="color: #080;">&#40;</span>GOOG.<span style="">r</span><span style="color: #080;">&#41;</span><span style="color: #080;">/</span><span style="color: #0000FF; font-weight: bold;">sqrt</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">/</span><span style="color: #ff0000;">252</span><span style="color: #080;">&#41;</span>
AAPL.<span style="">vol</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">sd</span><span style="color: #080;">&#40;</span>AAPL.<span style="">r</span><span style="color: #080;">&#41;</span><span style="color: #080;">/</span><span style="color: #0000FF; font-weight: bold;">sqrt</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">/</span><span style="color: #ff0000;">252</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">## calculate rho</span>
rho <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">cor</span><span style="color: #080;">&#40;</span>GOOG.<span style="">r</span>, AAPL.<span style="">r</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #0000FF; font-weight: bold;">cat</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;GOOG vol:&quot;</span>, GOOG.<span style="">vol</span>, <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">cat</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;AAPL vol:&quot;</span>, AAPL.<span style="">vol</span>, <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">cat</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;Rho:&quot;</span>, rho, <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #080;">&#41;</span></pre></div></div>

<h1>Price Using Simulation</h1>
<p>We are now at the point where we can price this option.  As usual, the price of the option is the expected discounted payoff under the risk neutral measure.  This particular option can be priced as <img src='http://s.wordpress.com/latex.php?latex=Price%20%3D%20%5Cmathbb%20E%5EQ%5Be%5E%7B-%5Cint_0%5ET%20r_u%20%5C%3A%20du%7D%5Ccdot%201_%7BS_T%5E%7B%281%29%7D%3EK_1%7D%5Ccdot%201_%7BS_T%5E%7B%282%29%7D%3EK_2%7D%5D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='Price = \mathbb E^Q[e^{-\int_0^T r_u \: du}\cdot 1_{S_T^{(1)}&gt;K_1}\cdot 1_{S_T^{(2)}&gt;K_2}]' title='Price = \mathbb E^Q[e^{-\int_0^T r_u \: du}\cdot 1_{S_T^{(1)}&gt;K_1}\cdot 1_{S_T^{(2)}&gt;K_2}]' class='latex' /> where the risk neutral dynamics of the stock prices are the following.<br />
<br />
<img src='http://s.wordpress.com/latex.php?latex=dS_t%5E%7B%281%29%7D%20%3D%20S_t%5E%7B%281%29%7D%20%28r_t%5E%7B%281%29%7D%20dt%20%2B%20%5Csigma%5E%7B%281%29%7D%20d%5Ctilde%7BB%7D_t%5E%7B%281%29%7D%29%20%5C%5C%20dS_t%5E%7B%282%29%7D%20%3D%20S_t%5E%7B%282%29%7D%20%28r_t%5E%7B%282%29%7D%20dt%20%2B%20%5Csigma%5E%7B%282%29%7D%20%28%5Crho%20d%5Ctilde%7BB%7D_t%5E%7B%281%29%7D%20%2B%20%5Csqrt%7B1-%5Crho%5E2%7Dd%5Ctilde%7BB%7D_t%5E%7B%282%29%7D%29%29&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='dS_t^{(1)} = S_t^{(1)} (r_t^{(1)} dt + \sigma^{(1)} d\tilde{B}_t^{(1)}) \\ dS_t^{(2)} = S_t^{(2)} (r_t^{(2)} dt + \sigma^{(2)} (\rho d\tilde{B}_t^{(1)} + \sqrt{1-\rho^2}d\tilde{B}_t^{(2)}))' title='dS_t^{(1)} = S_t^{(1)} (r_t^{(1)} dt + \sigma^{(1)} d\tilde{B}_t^{(1)}) \\ dS_t^{(2)} = S_t^{(2)} (r_t^{(2)} dt + \sigma^{(2)} (\rho d\tilde{B}_t^{(1)} + \sqrt{1-\rho^2}d\tilde{B}_t^{(2)}))' class='latex' /><br />
<br />
Where <img src='http://s.wordpress.com/latex.php?latex=%5Ctilde%7BB%7D_t%5E%7B%281%29%7D%2C%20%5C%3A%20%5Ctilde%7BB%7D_t%5E%7B%282%29%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\tilde{B}_t^{(1)}, \: \tilde{B}_t^{(2)}' title='\tilde{B}_t^{(1)}, \: \tilde{B}_t^{(2)}' class='latex' /> are independent Brownian Motions under the risk neutral measure. We will estimate the expectation through Monte Carlo simulation under a Euler discretization scheme. Rather than assume a constant interest rate, lets make things a tad more interesting by modeling the interest rate using the <a href="http://stotastic.com/wordpress/2010/04/vasicek-interest-rate-model/">Vasicek Model</a> (I’ll assume a calibration, but we could have calibrated it to zero coupon bond prices).<br />
<br />
<img src='http://s.wordpress.com/latex.php?latex=dr_t%20%3D%20%5Ckappa%28%5Ctheta-r_t%29dt%20%2B%20%5Cbeta%20d%5Ctilde%7BB%7D_t%5E%7B%283%29%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='dr_t = \kappa(\theta-r_t)dt + \beta d\tilde{B}_t^{(3)}' title='dr_t = \kappa(\theta-r_t)dt + \beta d\tilde{B}_t^{(3)}' class='latex' /><br />
<br />
The following R code simulates the risk neutral dynamics of this model and estimates the expectation. By doing so, we find that the fair price of this option is $0.31 per $1 of notional.  Thus, buying it from our neighbor for $25 seems like a deal if we think the $6 difference is a sufficient buffer to cover the simplifying assumptions we made.</p>

<div class="wp_syntax"><div class="code"><pre class="rsplus" style="font-family:monospace;">trials <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">10000</span>     <span style="color: #228B22;"># simulation trials</span>
<span style="color: #0000FF; font-weight: bold;">T</span> <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">1</span>              <span style="color: #228B22;"># time until expiration (years)</span>
m <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">200</span>            <span style="color: #228B22;"># subintervals</span>
<span style="color: #0000FF; font-weight: bold;">dt</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">T</span><span style="color: #080;">/</span>m           <span style="color: #228B22;"># time per subinterval (years)</span>
&nbsp;
<span style="color: #228B22;">## set interest rate model parameters</span>
k <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">0.4</span>
theta <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">0.05</span>
<span style="color: #0000FF; font-weight: bold;">beta</span> <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">0.03</span>
&nbsp;
<span style="color: #228B22;">## set strikes</span>
K1 <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">500</span>
K2 <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">240</span>
&nbsp;
<span style="color: #228B22;">## create storage vectors</span>
s1 <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">rep</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">0</span>,m<span style="color: #080;">+</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span>       <span style="color: #228B22;"># stock price path 1</span>
s2 <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">rep</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">0</span>,m<span style="color: #080;">+</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span>       <span style="color: #228B22;"># stock price path 2</span>
r <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">rep</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">0</span>,m<span style="color: #080;">+</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span>        <span style="color: #228B22;"># interest rate</span>
<span style="color: #0000FF; font-weight: bold;">c</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">rep</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">0</span>, trials<span style="color: #080;">&#41;</span>    <span style="color: #228B22;"># payoff</span>
&nbsp;
<span style="color: #228B22;">## set initial stock prices and interest rate</span>
s1<span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">500</span>
s2<span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">240</span>
r<span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span>  <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">0.01</span>
&nbsp;
<span style="color: #228B22;">## begin simulation</span>
<span style="color: #0000FF; font-weight: bold;">for</span><span style="color: #080;">&#40;</span>j <span style="color: #0000FF; font-weight: bold;">in</span> <span style="color: #ff0000;">1</span><span style="color: #080;">:</span>trials<span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
  <span style="color: #0000FF; font-weight: bold;">for</span><span style="color: #080;">&#40;</span>i <span style="color: #0000FF; font-weight: bold;">in</span> <span style="color: #ff0000;">2</span><span style="color: #080;">:</span><span style="color: #080;">&#40;</span>m<span style="color: #080;">+</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
    z1 <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">rnorm</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span>,<span style="color: #ff0000;">0</span>,<span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span>
    z2 <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">rnorm</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span>,<span style="color: #ff0000;">0</span>,<span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span>
    z3 <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">rnorm</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span>,<span style="color: #ff0000;">0</span>,<span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span>
&nbsp;
    ds1 <span style="color: #080;">&lt;-</span> s1<span style="color: #080;">&#91;</span>i<span style="color: #080;">-</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">*</span><span style="color: #080;">&#40;</span>r<span style="color: #080;">&#91;</span>i<span style="color: #080;">-</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">*</span><span style="color: #0000FF; font-weight: bold;">dt</span> <span style="color: #080;">+</span> GOOG.<span style="">vol</span><span style="color: #080;">*</span><span style="color: #0000FF; font-weight: bold;">sqrt</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">dt</span><span style="color: #080;">&#41;</span><span style="color: #080;">*</span>z1<span style="color: #080;">&#41;</span>
    ds2 <span style="color: #080;">&lt;-</span> s2<span style="color: #080;">&#91;</span>i<span style="color: #080;">-</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">*</span><span style="color: #080;">&#40;</span>r<span style="color: #080;">&#91;</span>i<span style="color: #080;">-</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">*</span><span style="color: #0000FF; font-weight: bold;">dt</span> <span style="color: #080;">+</span> AAPL.<span style="">vol</span><span style="color: #080;">*</span><span style="color: #0000FF; font-weight: bold;">sqrt</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">dt</span><span style="color: #080;">&#41;</span><span style="color: #080;">*</span><span style="color: #080;">&#40;</span>rho<span style="color: #080;">*</span>z1 <span style="color: #080;">+</span> <span style="color: #0000FF; font-weight: bold;">sqrt</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">-</span>rho<span style="color: #080;">^</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">*</span>z2<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>  
    dr  <span style="color: #080;">&lt;-</span> k<span style="color: #080;">*</span><span style="color: #080;">&#40;</span>theta <span style="color: #080;">-</span> r<span style="color: #080;">&#91;</span>i<span style="color: #080;">-</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">*</span><span style="color: #0000FF; font-weight: bold;">dt</span> <span style="color: #080;">+</span> <span style="color: #0000FF; font-weight: bold;">beta</span><span style="color: #080;">*</span><span style="color: #0000FF; font-weight: bold;">sqrt</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">dt</span><span style="color: #080;">&#41;</span><span style="color: #080;">*</span>z3
&nbsp;
    s1<span style="color: #080;">&#91;</span>i<span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> s1<span style="color: #080;">&#91;</span>i<span style="color: #080;">-</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span> <span style="color: #080;">+</span>  ds1
    s2<span style="color: #080;">&#91;</span>i<span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> s2<span style="color: #080;">&#91;</span>i<span style="color: #080;">-</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span> <span style="color: #080;">+</span>  ds2
    r<span style="color: #080;">&#91;</span>i<span style="color: #080;">&#93;</span>  <span style="color: #080;">&lt;-</span> r<span style="color: #080;">&#91;</span>i<span style="color: #080;">-</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span>  <span style="color: #080;">+</span>  dr
  <span style="color: #080;">&#125;</span>
  ss <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">sum</span><span style="color: #080;">&#40;</span>r<span style="color: #080;">&#91;</span><span style="color: #ff0000;">2</span><span style="color: #080;">:</span><span style="color: #080;">&#40;</span>m<span style="color: #080;">+</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span><span style="color: #080;">*</span><span style="color: #0000FF; font-weight: bold;">dt</span><span style="color: #080;">&#41;</span>
  <span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#91;</span>j<span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">ifelse</span><span style="color: #080;">&#40;</span>s1<span style="color: #080;">&#91;</span>m<span style="color: #080;">+</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&gt;</span>K1 <span style="color: #080;">&amp;&amp;</span> s2<span style="color: #080;">&#91;</span>m<span style="color: #080;">+</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&gt;</span>K2, <span style="color: #0000FF; font-weight: bold;">exp</span><span style="color: #080;">&#40;</span><span style="color: #080;">-</span>ss<span style="color: #080;">&#41;</span>, <span style="color: #ff0000;">0</span><span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span>
&nbsp;
<span style="color: #0000FF; font-weight: bold;">cat</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;Option Price Estimate:&quot;</span>, <span style="color: #0000FF; font-weight: bold;">round</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">mean</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#41;</span>,<span style="color: #ff0000;">3</span><span style="color: #080;">&#41;</span>, <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">cat</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;Standard Error:&quot;</span>, <span style="color: #0000FF; font-weight: bold;">round</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">sd</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#41;</span><span style="color: #080;">/</span><span style="color: #0000FF; font-weight: bold;">sqrt</span><span style="color: #080;">&#40;</span>trials<span style="color: #080;">&#41;</span>,<span style="color: #ff0000;">3</span><span style="color: #080;">&#41;</span>, <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #080;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://stotastic.com/wordpress/2010/05/basket-option-pricing/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>5 Minute Analysis in R: Case-Shiller Indices</title>
		<link>http://stotastic.com/wordpress/2010/04/case-shiller/</link>
		<comments>http://stotastic.com/wordpress/2010/04/case-shiller/#comments</comments>
		<pubDate>Thu, 29 Apr 2010 22:19:31 +0000</pubDate>
		<dc:creator>Lee</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Case-Shiller]]></category>
		<category><![CDATA[R]]></category>

		<guid isPermaLink="false">http://stotastic.com/wordpress/?p=805</guid>
		<description><![CDATA[The Case-Shiller Home Price Indices measure residential home values for 20 cities in the US, with some indices going all the way back to the 80s. With housing prices all the rage these days, we should perform a quick-and-dirty analysis using R to see what we can glean from this rich dataset.  First things [...]]]></description>
			<content:encoded><![CDATA[<p>The Case-Shiller Home Price Indices measure residential home values for 20 cities in the US, with some indices going all the way back to the 80s. With housing prices all the rage these days, we should perform a quick-and-dirty analysis using R to see what we can glean from this rich dataset.  First things first, the data needs to be <a href="http://www.standardandpoors.com/indices/sp-case-shiller-home-price-indices/en/us/?indexId=spusa-cashpidff--p-us----">downloaded from S&#038;P&#8217;s website</a>, converted into a CSV format, and then imported into R.</p>
<p><span id="more-805"></span></p>

<div class="wp_syntax"><div class="code"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">## read in data</span>
dat <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">read.<span style="">csv</span></span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;CSHomePrice_History.csv&quot;</span><span style="color: #080;">&#41;</span></pre></div></div>

<p>Now that the data is loaded, lets start by simply plotting the time series of the Indices.</p>

<div class="wp_syntax"><div class="code"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">## save dataset dimensions</span>
n <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">dim</span><span style="color: #080;">&#40;</span>dat<span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span>
m <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">dim</span><span style="color: #080;">&#40;</span>dat<span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span>
&nbsp;
<span style="color: #228B22;">## plot time series</span>
<span style="color: #0000FF; font-weight: bold;">col</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">seq</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span>, m<span style="color: #080;">-</span><span style="color: #ff0000;">1</span>, <span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">matplot</span><span style="color: #080;">&#40;</span>dat<span style="color: #080;">&#91;</span>,<span style="color: #ff0000;">2</span><span style="color: #080;">:</span>m<span style="color: #080;">&#93;</span>, type<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;l&quot;</span>, xaxt<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;n&quot;</span>, main<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;Case-Shiller Indices&quot;</span>, ylab<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;Index Value&quot;</span>, lty<span style="color: #080;">=</span><span style="color: #ff0000;">1</span>, <span style="color: #0000FF; font-weight: bold;">col</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">col</span><span style="color: #080;">&#41;</span>
xticks <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">seq</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span>, n, <span style="color: #ff0000;">12</span><span style="color: #080;">&#41;</span>
xlabels <span style="color: #080;">&lt;-</span> dat$YEAR<span style="color: #080;">&#91;</span>xticks<span style="color: #080;">&#93;</span>
<span style="color: #0000FF; font-weight: bold;">axis</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span>, at <span style="color: #080;">=</span> xticks, las <span style="color: #080;">=</span> <span style="color: #ff0000;">2</span>, cex.<span style="">axis</span> <span style="color: #080;">=</span> <span style="color: #ff0000;">0.6</span>, <span style="color: #0000FF; font-weight: bold;">labels</span> <span style="color: #080;">=</span> xlabels<span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">legend</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;topleft&quot;</span>, <span style="color: #0000FF; font-weight: bold;">names</span><span style="color: #080;">&#40;</span>dat<span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">2</span><span style="color: #080;">:</span>m<span style="color: #080;">&#93;</span>, lty<span style="color: #080;">=</span><span style="color: #ff0000;">1</span>, cex<span style="color: #080;">=</span><span style="color: #ff0000;">0.6</span>, <span style="color: #0000FF; font-weight: bold;">col</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">col</span><span style="color: #080;">&#41;</span></pre></div></div>

<p><a href="http://stotastic.com/wordpress/wp-content/uploads/2010/04/cs_ts.png"><img src="http://stotastic.com/wordpress/wp-content/uploads/2010/04/cs_ts.png" alt="" title="cs_ts" width="673" height="674" class="alignnone size-full wp-image-813" /></a></p>
<p>There&#8217;s alot of &#8217;stuff&#8217; going on which makes it hard to distinguish one index from another. To simplify things, lets just plot a subset of the indices. For no particular reason, I&#8217;ll pick New York, Las Vegas, and San Francisco.</p>

<div class="wp_syntax"><div class="code"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">## plot NY, LV, SF</span>
<span style="color: #0000FF; font-weight: bold;">col</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">seq</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span>, <span style="color: #ff0000;">3</span>, <span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">matplot</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">cbind</span><span style="color: #080;">&#40;</span>dat$NYXR, dat$LVXR, dat$SFXR<span style="color: #080;">&#41;</span>, type<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;l&quot;</span>, xaxt<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;n&quot;</span>, 
              main<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;Case-Shiller Indices&quot;</span>, ylab<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;Index Value&quot;</span>, lty<span style="color: #080;">=</span><span style="color: #ff0000;">1</span>, <span style="color: #0000FF; font-weight: bold;">col</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">col</span><span style="color: #080;">&#41;</span>
xticks <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">seq</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span>, n, <span style="color: #ff0000;">12</span><span style="color: #080;">&#41;</span>
xlabels <span style="color: #080;">&lt;-</span> dat$YEAR<span style="color: #080;">&#91;</span>xticks<span style="color: #080;">&#93;</span>
<span style="color: #0000FF; font-weight: bold;">axis</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span>, at <span style="color: #080;">=</span> xticks, las <span style="color: #080;">=</span> <span style="color: #ff0000;">2</span>, cex.<span style="">axis</span> <span style="color: #080;">=</span> <span style="color: #ff0000;">0.6</span>, <span style="color: #0000FF; font-weight: bold;">labels</span> <span style="color: #080;">=</span> xlabels<span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">legend</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;topleft&quot;</span>, <span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;New York&quot;</span>, <span style="color: #ff0000;">&quot;Las Vegas&quot;</span>, <span style="color: #ff0000;">&quot;San Francisco&quot;</span><span style="color: #080;">&#41;</span>, lty<span style="color: #080;">=</span><span style="color: #ff0000;">1</span>, cex<span style="color: #080;">=</span><span style="color: #ff0000;">0.6</span>, <span style="color: #0000FF; font-weight: bold;">col</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">col</span><span style="color: #080;">&#41;</span></pre></div></div>

<p><a href="http://stotastic.com/wordpress/wp-content/uploads/2010/04/cs_ts_subset.png"><img src="http://stotastic.com/wordpress/wp-content/uploads/2010/04/cs_ts_subset.png" alt="" title="cs_ts_subset" width="673" height="674" class="alignnone size-full wp-image-814" /></a></p>
<p>Much better, but all this really shows us is that there was a pretty substantial run-up in home values starting in the late 90s, followed by a bust in 2006 (not exactly new news). What would be more interesting would be to analyze the monthly returns in the indices, which I suspect would be somewhat stationary. If we define <img src='http://s.wordpress.com/latex.php?latex=r_t&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='r_t' title='r_t' class='latex' /> as the monthly return in the form <img src='http://s.wordpress.com/latex.php?latex=x_%7Bt%2B1%7D%20%3D%20x_t%20e%5E%7Br_t%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='x_{t+1} = x_t e^{r_t}' title='x_{t+1} = x_t e^{r_t}' class='latex' />, we can calculate it as <img src='http://s.wordpress.com/latex.php?latex=r_t%3Dln%20%28%5Cfrac%7Bx_%7Bt%2B1%7D%7D%7Bx_t%7D%29&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='r_t=ln (\frac{x_{t+1}}{x_t})' title='r_t=ln (\frac{x_{t+1}}{x_t})' class='latex' />. At this point we haven&#8217;t made any assumption about the distribution of <img src='http://s.wordpress.com/latex.php?latex=r_t&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='r_t' title='r_t' class='latex' />.</p>

<div class="wp_syntax"><div class="code"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">## calculate the monthly returns</span>
r <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">log</span><span style="color: #080;">&#40;</span>dat<span style="color: #080;">&#91;</span><span style="color: #ff0000;">2</span><span style="color: #080;">:</span>n, <span style="color: #ff0000;">2</span><span style="color: #080;">:</span>m<span style="color: #080;">&#93;</span> <span style="color: #080;">/</span> dat<span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #080;">&#40;</span>n<span style="color: #080;">-</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span>, <span style="color: #ff0000;">2</span><span style="color: #080;">:</span>m<span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">## plot monthly returns time series</span>
<span style="color: #0000FF; font-weight: bold;">col</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">seq</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span>, <span style="color: #ff0000;">3</span>, <span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">matplot</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">cbind</span><span style="color: #080;">&#40;</span>r$NYXR, r$LVXR, r$SFXR<span style="color: #080;">&#41;</span>, type<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;b&quot;</span>, pch<span style="color: #080;">=</span><span style="color: #ff0000;">21</span>, 
              xaxt<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;n&quot;</span>, main<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;Monthly Returns&quot;</span>, ylab<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;Monthly Return&quot;</span>, lty<span style="color: #080;">=</span><span style="color: #ff0000;">1</span>, <span style="color: #0000FF; font-weight: bold;">col</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">col</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">abline</span><span style="color: #080;">&#40;</span>h<span style="color: #080;">=</span><span style="color: #ff0000;">0</span><span style="color: #080;">&#41;</span>
xticks <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">seq</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">2</span>, n, <span style="color: #ff0000;">12</span><span style="color: #080;">&#41;</span>
xlabels <span style="color: #080;">&lt;-</span> dat$YEAR<span style="color: #080;">&#91;</span>xticks<span style="color: #080;">&#93;</span>
<span style="color: #0000FF; font-weight: bold;">axis</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span>, at <span style="color: #080;">=</span> xticks, las <span style="color: #080;">=</span> <span style="color: #ff0000;">2</span>, cex.<span style="">axis</span> <span style="color: #080;">=</span> <span style="color: #ff0000;">0.6</span>, <span style="color: #0000FF; font-weight: bold;">labels</span> <span style="color: #080;">=</span> xlabels<span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">legend</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;bottomleft&quot;</span>, <span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;New York&quot;</span>, <span style="color: #ff0000;">&quot;Las Vegas&quot;</span>, <span style="color: #ff0000;">&quot;San Francisco&quot;</span><span style="color: #080;">&#41;</span>, lty<span style="color: #080;">=</span><span style="color: #ff0000;">1</span>, cex<span style="color: #080;">=</span><span style="color: #ff0000;">0.6</span>, <span style="color: #0000FF; font-weight: bold;">col</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">col</span><span style="color: #080;">&#41;</span></pre></div></div>

<p><a href="http://stotastic.com/wordpress/wp-content/uploads/2010/04/cs_returns_ts.png"><img src="http://stotastic.com/wordpress/wp-content/uploads/2010/04/cs_returns_ts.png" alt="" title="cs_returns_ts" width="673" height="674" class="alignnone size-full wp-image-819" /></a></p>
<p>Now things are starting to get interesting.  Clearly there is some seasonality going on and the returns appear to be correlated. To investigate the correlation a bit more, lets do a pairs plot.</p>

<div class="wp_syntax"><div class="code"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">## pairs plot of monthly returns</span>
<span style="color: #0000FF; font-weight: bold;">pairs</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">cbind</span><span style="color: #080;">&#40;</span>r$NYXR, r$LVXR, r$SFXR<span style="color: #080;">&#41;</span>, main<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;Monthly Returns&quot;</span>, 
          <span style="color: #0000FF; font-weight: bold;">labels</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;New York&quot;</span>, <span style="color: #ff0000;">&quot;Las Vegas&quot;</span>, <span style="color: #ff0000;">&quot;San Francisco&quot;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span></pre></div></div>

<p><a href="http://stotastic.com/wordpress/wp-content/uploads/2010/04/cs_returns_pairs.png"><img src="http://stotastic.com/wordpress/wp-content/uploads/2010/04/cs_returns_pairs.png" alt="" title="cs_returns_pairs" width="673" height="674" class="alignnone size-full wp-image-821" /></a></p>
<p>This confirms our suspicions about correlation. The monthly return almost appear bivariate normal.  Lets produce some boxplots to investigate the distribution of <img src='http://s.wordpress.com/latex.php?latex=r_t&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='r_t' title='r_t' class='latex' />.</p>

<div class="wp_syntax"><div class="code"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">## boxplot</span>
<span style="color: #0000FF; font-weight: bold;">boxplot</span><span style="color: #080;">&#40;</span>r, xaxt<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;n&quot;</span>, main<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;Monthly Returns&quot;</span>, ylab<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;Monthly Return&quot;</span>, <span style="color: #0000FF; font-weight: bold;">col</span><span style="color: #080;">=</span><span style="color: #ff0000;">&quot;light blue&quot;</span><span style="color: #080;">&#41;</span> 
<span style="color: #0000FF; font-weight: bold;">abline</span><span style="color: #080;">&#40;</span>h<span style="color: #080;">=</span><span style="color: #ff0000;">0</span><span style="color: #080;">&#41;</span>
xticks <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">seq</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span>, m<span style="color: #080;">-</span><span style="color: #ff0000;">1</span>, <span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span>
xlabels <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">names</span><span style="color: #080;">&#40;</span>r<span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">axis</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span>, at <span style="color: #080;">=</span> xticks, las <span style="color: #080;">=</span> <span style="color: #ff0000;">2</span>, cex.<span style="">axis</span> <span style="color: #080;">=</span> <span style="color: #ff0000;">0.6</span>, <span style="color: #0000FF; font-weight: bold;">labels</span> <span style="color: #080;">=</span> xlabels<span style="color: #080;">&#41;</span></pre></div></div>

<p><a href="http://stotastic.com/wordpress/wp-content/uploads/2010/04/cs_returns_box.png"><img src="http://stotastic.com/wordpress/wp-content/uploads/2010/04/cs_returns_box.png" alt="" title="cs_returns_box" width="673" height="674" class="alignnone size-full wp-image-820" /></a></p>
<p>It appears that the returns are roughly normal, with the mean return just above 0, but some appear to have much fatter tails than others (compare New York to Las Vegas for instance). We should perform some QQ Normal plots to see how normal the monthly returns really are.</p>

<div class="wp_syntax"><div class="code"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">## qqnorm plots</span>
<span style="color: #0000FF; font-weight: bold;">par</span><span style="color: #080;">&#40;</span>mfrow<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">3</span>,<span style="color: #ff0000;">4</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">for</span><span style="color: #080;">&#40;</span>i <span style="color: #0000FF; font-weight: bold;">in</span> <span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #ff0000;">12</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
  <span style="color: #0000FF; font-weight: bold;">qqnorm</span><span style="color: #080;">&#40;</span>r<span style="color: #080;">&#91;</span>,i<span style="color: #080;">&#93;</span>, main<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">names</span><span style="color: #080;">&#40;</span>r<span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span>i<span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
  <span style="color: #0000FF; font-weight: bold;">qqline</span><span style="color: #080;">&#40;</span>r<span style="color: #080;">&#91;</span>,i<span style="color: #080;">&#93;</span>, <span style="color: #0000FF; font-weight: bold;">col</span><span style="color: #080;">=</span><span style="color: #ff0000;">&quot;red&quot;</span><span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span>
windows<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">par</span><span style="color: #080;">&#40;</span>mfrow<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">3</span>,<span style="color: #ff0000;">4</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">for</span><span style="color: #080;">&#40;</span>i <span style="color: #0000FF; font-weight: bold;">in</span> <span style="color: #ff0000;">13</span><span style="color: #080;">:</span><span style="color: #080;">&#40;</span>m<span style="color: #080;">-</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
  <span style="color: #0000FF; font-weight: bold;">qqnorm</span><span style="color: #080;">&#40;</span>r<span style="color: #080;">&#91;</span>,i<span style="color: #080;">&#93;</span>, main<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">names</span><span style="color: #080;">&#40;</span>r<span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span>i<span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
  <span style="color: #0000FF; font-weight: bold;">qqline</span><span style="color: #080;">&#40;</span>r<span style="color: #080;">&#91;</span>,i<span style="color: #080;">&#93;</span>, <span style="color: #0000FF; font-weight: bold;">col</span><span style="color: #080;">=</span><span style="color: #ff0000;">&quot;red&quot;</span><span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span></pre></div></div>

<p><a href="http://stotastic.com/wordpress/wp-content/uploads/2010/04/cs_returns_qq1.png"><img src="http://stotastic.com/wordpress/wp-content/uploads/2010/04/cs_returns_qq1.png" alt="" title="cs_returns_qq1" width="673" height="674" class="alignnone size-full wp-image-822" /></a></p>
<p><a href="http://stotastic.com/wordpress/wp-content/uploads/2010/04/cs_returns_qq2.png"><img src="http://stotastic.com/wordpress/wp-content/uploads/2010/04/cs_returns_qq2.png" alt="" title="cs_returns_qq2" width="673" height="674" class="alignnone size-full wp-image-823" /></a></p>
<p>This confirms our suspicions that the return are &#8216;normal like&#8217;, but have some pretty fat tails (as do most financial assets).  Although, New York and Boston appear to be much more normal than the rest. This analysis really begs for an ARMA model that incorporates the correlation across housing markets.</p>
]]></content:encoded>
			<wfw:commentRss>http://stotastic.com/wordpress/2010/04/case-shiller/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Fun with the Vasicek Interest Rate Model</title>
		<link>http://stotastic.com/wordpress/2010/04/vasicek-interest-rate-model/</link>
		<comments>http://stotastic.com/wordpress/2010/04/vasicek-interest-rate-model/#comments</comments>
		<pubDate>Fri, 23 Apr 2010 00:01:29 +0000</pubDate>
		<dc:creator>Lee</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[interest rate]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[Vasicek Model]]></category>

		<guid isPermaLink="false">http://stotastic.com/wordpress/?p=683</guid>
		<description><![CDATA[A common model used in the financial industry for modelling the short rate (think overnight rate, but actually an infinitesimally short amount of time) is the Vasicek model.  Although it is unlikely to perfectly fit the yield curve, it has some nice properties that make it a good model to work with.  The [...]]]></description>
			<content:encoded><![CDATA[<p>A common model used in the financial industry for modelling the short rate (think overnight rate, but actually an infinitesimally short amount of time) is the Vasicek model.  Although it is unlikely to perfectly fit the yield curve, it has some nice properties that make it a good model to work with.  The dynamics of the Vasicek model are describe below.<br />
<br \><img src='http://s.wordpress.com/latex.php?latex=dr_t%3D%5Ckappa%28%5Ctheta-r_t%29dt%2B%5Cbeta%20dW_t%5E%5Cmathbb%7BQ%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='dr_t=\kappa(\theta-r_t)dt+\beta dW_t^\mathbb{Q}' title='dr_t=\kappa(\theta-r_t)dt+\beta dW_t^\mathbb{Q}' class='latex' /><br />
<span id="more-683"></span><br />
In this model, the parameters <img src='http://s.wordpress.com/latex.php?latex=%5Ckappa%20%2C%20%5Ctheta%2C%20%5C%3A%20%5Ctext%7Band%7D%20%5C%3A%20%5Cbeta&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\kappa , \theta, \: \text{and} \: \beta' title='\kappa , \theta, \: \text{and} \: \beta' class='latex' /> are constants, and the random motion is generated by the Q measure Brownian motion <img src='http://s.wordpress.com/latex.php?latex=W_t%5E%5Cmathbb%7BQ%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='W_t^\mathbb{Q}' title='W_t^\mathbb{Q}' class='latex' />.  An important property of the Vasicek model is that the interest rate is mean reverting to <img src='http://s.wordpress.com/latex.php?latex=%5Ctheta&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\theta' title='\theta' class='latex' />, and the tendency to revert is controlled by <img src='http://s.wordpress.com/latex.php?latex=%5Ckappa&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\kappa' title='\kappa' class='latex' />.  Also, this process is a diffusion process, hence Markovian, which will lead to some nice closed form formulas.  Finally, the future value of the interest rate is normally distributed with the distribution <img src='http://s.wordpress.com/latex.php?latex=r_T%20%5Csim%20N%20%5Cleft%28%20%5Ctheta%2B%28r_0-%5Ctheta%29e%5E%7B-%5Ckappa%20T%7D%2C%20%5C%3A%20%5Cfrac%7B%5Cbeta%5E2%7D%7B2%5Ckappa%7D%281-e%5E%7B-2%5Ckappa%20T%7D%29%20%5Cright%29&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='r_T \sim N \left( \theta+(r_0-\theta)e^{-\kappa T}, \: \frac{\beta^2}{2\kappa}(1-e^{-2\kappa T}) \right)' title='r_T \sim N \left( \theta+(r_0-\theta)e^{-\kappa T}, \: \frac{\beta^2}{2\kappa}(1-e^{-2\kappa T}) \right)' class='latex' />.  </p>
<h1>Simulated Paths</h1>
<p>To give you an idea of what this process looks like, I have generate some sample paths using the Euler discretization method.</p>
<p><a href="http://stotastic.com/wordpress/wp-content/uploads/2010/04/vasicekPaths.png"><img src="http://stotastic.com/wordpress/wp-content/uploads/2010/04/vasicekPaths.png" alt="" title="vasicekPaths" width="673" height="462" class="alignnone size-full wp-image-705" /></a></p>
<p>One important things you will notice is that this process starts at 0.03, but is pulled towards 0.10, which is the value of <img src='http://s.wordpress.com/latex.php?latex=%5Ctheta&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\theta' title='\theta' class='latex' />. I&#8217;ve added dashed lines to highlight this, as well as lines for the expected value of the process and confidence bands representing two standard deviations. Also, you may notice the major problem with this process is that the interest rate can drop below 0%. In the real world, this shouldn&#8217;t happen.  The Cox-Ross-Ingersoll model, or CIR model, actually corrects for this, but the process is no longer normally distributed. The following is the R code used to generate the chart above.</p>

<div class="wp_syntax"><div class="code"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">## Simulate Sample Paths ##</span>
&nbsp;
<span style="color: #228B22;">## define model parameters</span>
r0 <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">0.03</span>
theta <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">0.10</span>
k <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">0.3</span>
<span style="color: #0000FF; font-weight: bold;">beta</span> <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">0.03</span>
&nbsp;
<span style="color: #228B22;">## simulate short rate paths</span>
n <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">10</span>    <span style="color: #228B22;"># MC simulation trials</span>
<span style="color: #0000FF; font-weight: bold;">T</span> <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">10</span>    <span style="color: #228B22;"># total time</span>
m <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">200</span>   <span style="color: #228B22;"># subintervals</span>
<span style="color: #0000FF; font-weight: bold;">dt</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">T</span><span style="color: #080;">/</span>m  <span style="color: #228B22;"># difference in time each subinterval</span>
&nbsp;
r <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">matrix</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">0</span>,m<span style="color: #080;">+</span><span style="color: #ff0000;">1</span>,n<span style="color: #080;">&#41;</span>  <span style="color: #228B22;"># matrix to hold short rate paths</span>
r<span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span>,<span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> r0
&nbsp;
<span style="color: #0000FF; font-weight: bold;">for</span><span style="color: #080;">&#40;</span>j <span style="color: #0000FF; font-weight: bold;">in</span> <span style="color: #ff0000;">1</span><span style="color: #080;">:</span>n<span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
  <span style="color: #0000FF; font-weight: bold;">for</span><span style="color: #080;">&#40;</span>i <span style="color: #0000FF; font-weight: bold;">in</span> <span style="color: #ff0000;">2</span><span style="color: #080;">:</span><span style="color: #080;">&#40;</span>m<span style="color: #080;">+</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
    dr <span style="color: #080;">&lt;-</span> k<span style="color: #080;">*</span><span style="color: #080;">&#40;</span>theta<span style="color: #080;">-</span>r<span style="color: #080;">&#91;</span>i<span style="color: #080;">-</span><span style="color: #ff0000;">1</span>,j<span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">*</span><span style="color: #0000FF; font-weight: bold;">dt</span> <span style="color: #080;">+</span> <span style="color: #0000FF; font-weight: bold;">beta</span><span style="color: #080;">*</span><span style="color: #0000FF; font-weight: bold;">sqrt</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">dt</span><span style="color: #080;">&#41;</span><span style="color: #080;">*</span><span style="color: #0000FF; font-weight: bold;">rnorm</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span>,<span style="color: #ff0000;">0</span>,<span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span>
    r<span style="color: #080;">&#91;</span>i,j<span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> r<span style="color: #080;">&#91;</span>i<span style="color: #080;">-</span><span style="color: #ff0000;">1</span>,j<span style="color: #080;">&#93;</span> <span style="color: #080;">+</span> dr
  <span style="color: #080;">&#125;</span>
<span style="color: #080;">&#125;</span> 
&nbsp;
<span style="color: #228B22;">## plot paths</span>
<span style="color: #0000FF; font-weight: bold;">t</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">seq</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">0</span>, <span style="color: #0000FF; font-weight: bold;">T</span>, <span style="color: #0000FF; font-weight: bold;">dt</span><span style="color: #080;">&#41;</span>
rT.<span style="">expected</span> <span style="color: #080;">&lt;-</span> theta <span style="color: #080;">+</span> <span style="color: #080;">&#40;</span>r0<span style="color: #080;">-</span>theta<span style="color: #080;">&#41;</span><span style="color: #080;">*</span><span style="color: #0000FF; font-weight: bold;">exp</span><span style="color: #080;">&#40;</span><span style="color: #080;">-</span>k<span style="color: #080;">*</span><span style="color: #0000FF; font-weight: bold;">t</span><span style="color: #080;">&#41;</span>
rT.<span style="">stdev</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">sqrt</span><span style="color: #080;">&#40;</span> <span style="color: #0000FF; font-weight: bold;">beta</span><span style="color: #080;">^</span><span style="color: #ff0000;">2</span><span style="color: #080;">/</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">2</span><span style="color: #080;">*</span>k<span style="color: #080;">&#41;</span><span style="color: #080;">*</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">-</span><span style="color: #0000FF; font-weight: bold;">exp</span><span style="color: #080;">&#40;</span><span style="color: #080;">-</span><span style="color: #ff0000;">2</span><span style="color: #080;">*</span>k<span style="color: #080;">*</span><span style="color: #0000FF; font-weight: bold;">t</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">matplot</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">t</span>, r<span style="color: #080;">&#91;</span>,<span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #ff0000;">10</span><span style="color: #080;">&#93;</span>, type<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;l&quot;</span>, lty<span style="color: #080;">=</span><span style="color: #ff0000;">1</span>, main<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;Short Rate Paths&quot;</span>, ylab<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;rt&quot;</span><span style="color: #080;">&#41;</span> 
<span style="color: #0000FF; font-weight: bold;">abline</span><span style="color: #080;">&#40;</span>h<span style="color: #080;">=</span>theta, <span style="color: #0000FF; font-weight: bold;">col</span><span style="color: #080;">=</span><span style="color: #ff0000;">&quot;red&quot;</span>, lty<span style="color: #080;">=</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">lines</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">t</span>, rT.<span style="">expected</span>, lty<span style="color: #080;">=</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span> 
<span style="color: #0000FF; font-weight: bold;">lines</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">t</span>, rT.<span style="">expected</span> <span style="color: #080;">+</span> <span style="color: #ff0000;">2</span><span style="color: #080;">*</span>rT.<span style="">stdev</span>, lty<span style="color: #080;">=</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span> 
<span style="color: #0000FF; font-weight: bold;">lines</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">t</span>, rT.<span style="">expected</span> <span style="color: #080;">-</span> <span style="color: #ff0000;">2</span><span style="color: #080;">*</span>rT.<span style="">stdev</span>, lty<span style="color: #080;">=</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span> 
<span style="color: #0000FF; font-weight: bold;">points</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">0</span>,r0<span style="color: #080;">&#41;</span></pre></div></div>

<h1>Bond Pricing and Yield Curve</h1>
<p>One can show that a zero coupon bond with a maturity at time T can be found by calculating the following expectation under the risk neutral measure <img src='http://s.wordpress.com/latex.php?latex=B%280%2CT%29%3D%5Cmathbb%7BE%7D%5E%5Cmathbb%7BQ%7D%20%5Cleft%5B%20e%5E%7B-%5Cint_0%5ET%7Br_u%20du%7D%7D%20%5Cright%5D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='B(0,T)=\mathbb{E}^\mathbb{Q} \left[ e^{-\int_0^T{r_u du}} \right]' title='B(0,T)=\mathbb{E}^\mathbb{Q} \left[ e^{-\int_0^T{r_u du}} \right]' class='latex' /> where the short rate process <img src='http://s.wordpress.com/latex.php?latex=r_u&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='r_u' title='r_u' class='latex' /> can be pretty much any process.  We could estimate this expectation using Monte Carlo simulation, but the Vasicek model allows us to calculate the value of the zero coupon bond by using the Markov property and PDE techniques.  Using this method, we can price the bond by the following  equations.<br />
<br \><img src='http://s.wordpress.com/latex.php?latex=B%280%2CT%29%3De%5E%7B-a%28T%29-b%28T%29r_0%7D%20%5C%5C%20%5Ctext%20%7Bwhere%7D%20%5C%5C%20b%28%5Ctau%29%3D%5Cfrac%7B1-e%5E%7B-%5Ctau%20%5Ckappa%7D%7D%7B%5Ckappa%7D%20%5C%5C%20a%28%5Ctau%29%3D%28%5Ctheta%20-%20%5Cfrac%7B%5Cbeta%5E2%7D%7B2%5Ckappa%7D%29%20%28%5Ctau-b%28%5Ctau%29%29%2B%5Cfrac%7B%5Cbeta%5E2%7D%7B4%5Ckappa%7Db%28%5Ctau%29%5E2%20&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='B(0,T)=e^{-a(T)-b(T)r_0} \\ \text {where} \\ b(\tau)=\frac{1-e^{-\tau \kappa}}{\kappa} \\ a(\tau)=(\theta - \frac{\beta^2}{2\kappa}) (\tau-b(\tau))+\frac{\beta^2}{4\kappa}b(\tau)^2 ' title='B(0,T)=e^{-a(T)-b(T)r_0} \\ \text {where} \\ b(\tau)=\frac{1-e^{-\tau \kappa}}{\kappa} \\ a(\tau)=(\theta - \frac{\beta^2}{2\kappa}) (\tau-b(\tau))+\frac{\beta^2}{4\kappa}b(\tau)^2 ' class='latex' /><br \><br />
This closed form solution for a zero coupon bond makes our lives much easier since we don&#8217;t need to compute the expectation under the martingale measure to find the price of a bond.  Additionally, it will allow us to easily calculate the yield curve implied by the model.  If we note that <img src='http://s.wordpress.com/latex.php?latex=B%280%2CT%29%3De%5E%7B-y_T%20T%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='B(0,T)=e^{-y_T T}' title='B(0,T)=e^{-y_T T}' class='latex' /> where <img src='http://s.wordpress.com/latex.php?latex=y_T&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='y_T' title='y_T' class='latex' /> is the continuous time yield for a zero coupon bond with maturity T, then we can use our model estimate of the bond price to rearrange this formula and solve for the yield by <img src='http://s.wordpress.com/latex.php?latex=%5Cfrac%7B-ln%28B%280%2CT%29%29%7D%7BT%7D%3Dy_T&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\frac{-ln(B(0,T))}{T}=y_T' title='\frac{-ln(B(0,T))}{T}=y_T' class='latex' />. Finally, to demonstrate some of the possible curve shapes allowed by the Vasicek model, I produced the following chart.  Notice that the yield curve can be both increasing or decreasing with maturity.</p>
<p><a href="http://stotastic.com/wordpress/wp-content/uploads/2010/04/vasicekYieldCurves.png"><img src="http://stotastic.com/wordpress/wp-content/uploads/2010/04/vasicekYieldCurves.png" alt="" title="vasicekYieldCurves" width="673" height="421" class="alignnone size-full wp-image-783" /></a></p>
<p>The following R code implements the closed form pricing function for a bond under the Vasicek model, and then uses the pricing function to generate the chart above.</p>

<div class="wp_syntax"><div class="code"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">## function to find ZCB price using Vasicek model</span>
VasicekZCBprice <span style="color: #080;">&lt;-</span> 
<span style="color: #0000FF; font-weight: bold;">function</span><span style="color: #080;">&#40;</span>r0, k, theta, <span style="color: #0000FF; font-weight: bold;">beta</span>, <span style="color: #0000FF; font-weight: bold;">T</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
  b.<span style="">vas</span> <span style="color: #080;">&lt;-</span> <span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">/</span>k<span style="color: #080;">&#41;</span><span style="color: #080;">*</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">-</span><span style="color: #0000FF; font-weight: bold;">exp</span><span style="color: #080;">&#40;</span><span style="color: #080;">-</span><span style="color: #0000FF; font-weight: bold;">T</span><span style="color: #080;">*</span>k<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> 
  a.<span style="">vas</span> <span style="color: #080;">&lt;-</span> <span style="color: #080;">&#40;</span>theta<span style="color: #080;">-</span><span style="color: #0000FF; font-weight: bold;">beta</span><span style="color: #080;">^</span><span style="color: #ff0000;">2</span><span style="color: #080;">/</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">2</span><span style="color: #080;">*</span>k<span style="color: #080;">^</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">*</span><span style="color: #080;">&#40;</span>T<span style="color: #080;">-</span>b.<span style="">vas</span><span style="color: #080;">&#41;</span><span style="color: #080;">+</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">beta</span><span style="color: #080;">^</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">/</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">4</span><span style="color: #080;">*</span>k<span style="color: #080;">&#41;</span><span style="color: #080;">*</span>b.<span style="">vas</span><span style="color: #080;">^</span><span style="color: #ff0000;">2</span>
  <span style="color: #0000FF; font-weight: bold;">return</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">exp</span><span style="color: #080;">&#40;</span><span style="color: #080;">-</span>a.<span style="">vas</span><span style="color: #080;">-</span>b.<span style="">vas</span><span style="color: #080;">*</span>r0<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span>
&nbsp;
<span style="color: #228B22;">## define model parameters for plotting yield curves</span>
theta <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">0.10</span>
k <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">0.5</span>
<span style="color: #0000FF; font-weight: bold;">beta</span> <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">0.03</span>
&nbsp;
r0 <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">seq</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">0.00</span>, <span style="color: #ff0000;">0.20</span>, <span style="color: #ff0000;">0.05</span><span style="color: #080;">&#41;</span>
n <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span>r0<span style="color: #080;">&#41;</span>
yield <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">matrix</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">0</span>, <span style="color: #ff0000;">10</span>, n<span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">for</span><span style="color: #080;">&#40;</span>i <span style="color: #0000FF; font-weight: bold;">in</span> <span style="color: #ff0000;">1</span><span style="color: #080;">:</span>n<span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
  <span style="color: #0000FF; font-weight: bold;">for</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">T</span> <span style="color: #0000FF; font-weight: bold;">in</span> <span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #ff0000;">10</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
    yield<span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">T</span>,i<span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> <span style="color: #080;">-</span><span style="color: #0000FF; font-weight: bold;">log</span><span style="color: #080;">&#40;</span>VasicekZCBprice<span style="color: #080;">&#40;</span>r0<span style="color: #080;">&#91;</span>i<span style="color: #080;">&#93;</span>, k, theta, <span style="color: #0000FF; font-weight: bold;">beta</span>, <span style="color: #0000FF; font-weight: bold;">T</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">/</span><span style="color: #0000FF; font-weight: bold;">T</span>
  <span style="color: #080;">&#125;</span>
<span style="color: #080;">&#125;</span>
&nbsp;
maturity <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">seq</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span>, <span style="color: #ff0000;">10</span>, <span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">matplot</span><span style="color: #080;">&#40;</span>maturity, yield, type<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;l&quot;</span>, <span style="color: #0000FF; font-weight: bold;">col</span><span style="color: #080;">=</span><span style="color: #ff0000;">&quot;black&quot;</span>, lty<span style="color: #080;">=</span><span style="color: #ff0000;">1</span>, main<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;Yield Curve Shapes&quot;</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">abline</span><span style="color: #080;">&#40;</span>h<span style="color: #080;">=</span>theta, <span style="color: #0000FF; font-weight: bold;">col</span><span style="color: #080;">=</span><span style="color: #ff0000;">&quot;red&quot;</span>, lty<span style="color: #080;">=</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span></pre></div></div>

<h1>Formula vs Monte Carlo</h1>
<p>Just to make sure the bond pricing formula was implemented correctly, I compared the price using the formula versus the price using Monte Carlo simulation to estimate the expectation under the martingale measure.  Below are the results and R code.  Seems everything is in order, although the Euler discretization method seems to be causing some error in the Monte Carlo results.</p>
<p><code><br />
Exact Vasicek Price: 0.9614<br />
MC Price: 0.9623<br />
MC Standard Error: 0.0005<br />
</code></p>

<div class="wp_syntax"><div class="code"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">## define model parameters</span>
r0 <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">0.03</span>
theta <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">0.10</span>
k <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">0.3</span>
<span style="color: #0000FF; font-weight: bold;">beta</span> <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">0.03</span>
&nbsp;
<span style="color: #228B22;">## simulate short rate paths</span>
n <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">1000</span>  <span style="color: #228B22;"># MC simulation trials</span>
<span style="color: #0000FF; font-weight: bold;">T</span> <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">1</span>     <span style="color: #228B22;"># total time</span>
m <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">200</span>   <span style="color: #228B22;"># subintervals</span>
<span style="color: #0000FF; font-weight: bold;">dt</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">T</span><span style="color: #080;">/</span>m  <span style="color: #228B22;"># difference in time each subinterval</span>
&nbsp;
r <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">matrix</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">0</span>,m<span style="color: #080;">+</span><span style="color: #ff0000;">1</span>,n<span style="color: #080;">&#41;</span>  <span style="color: #228B22;"># matrix to hold short rate paths</span>
r<span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span>,<span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> r0
<span style="color: #0000FF; font-weight: bold;">for</span><span style="color: #080;">&#40;</span>j <span style="color: #0000FF; font-weight: bold;">in</span> <span style="color: #ff0000;">1</span><span style="color: #080;">:</span>n<span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
  <span style="color: #0000FF; font-weight: bold;">for</span><span style="color: #080;">&#40;</span>i <span style="color: #0000FF; font-weight: bold;">in</span> <span style="color: #ff0000;">2</span><span style="color: #080;">:</span><span style="color: #080;">&#40;</span>m<span style="color: #080;">+</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
    dr <span style="color: #080;">&lt;-</span> k<span style="color: #080;">*</span><span style="color: #080;">&#40;</span>theta<span style="color: #080;">-</span>r<span style="color: #080;">&#91;</span>i<span style="color: #080;">-</span><span style="color: #ff0000;">1</span>,j<span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">*</span><span style="color: #0000FF; font-weight: bold;">dt</span> <span style="color: #080;">+</span> <span style="color: #0000FF; font-weight: bold;">beta</span><span style="color: #080;">*</span><span style="color: #0000FF; font-weight: bold;">sqrt</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">dt</span><span style="color: #080;">&#41;</span><span style="color: #080;">*</span><span style="color: #0000FF; font-weight: bold;">rnorm</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span>,<span style="color: #ff0000;">0</span>,<span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span>
    r<span style="color: #080;">&#91;</span>i,j<span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> r<span style="color: #080;">&#91;</span>i<span style="color: #080;">-</span><span style="color: #ff0000;">1</span>,j<span style="color: #080;">&#93;</span> <span style="color: #080;">+</span> dr
  <span style="color: #080;">&#125;</span>
<span style="color: #080;">&#125;</span> 
&nbsp;
<span style="color: #228B22;">## calculate Monte Carlo bond price and compare to Exact Vasicek solution</span>
ss <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">colSums</span><span style="color: #080;">&#40;</span>r<span style="color: #080;">&#91;</span><span style="color: #ff0000;">2</span><span style="color: #080;">:</span><span style="color: #080;">&#40;</span>m<span style="color: #080;">+</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span>,<span style="color: #080;">&#93;</span><span style="color: #080;">*</span><span style="color: #0000FF; font-weight: bold;">dt</span><span style="color: #080;">&#41;</span>  <span style="color: #228B22;"># integral estimate</span>
<span style="color: #0000FF; font-weight: bold;">c</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">exp</span><span style="color: #080;">&#40;</span><span style="color: #080;">-</span>ss<span style="color: #080;">&#41;</span>
estimate <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">mean</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#41;</span>
stdErr <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">sd</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#41;</span><span style="color: #080;">/</span><span style="color: #0000FF; font-weight: bold;">sqrt</span><span style="color: #080;">&#40;</span>n<span style="color: #080;">&#41;</span>
exact <span style="color: #080;">&lt;-</span> VasicekZCBprice<span style="color: #080;">&#40;</span>r0, k, theta, <span style="color: #0000FF; font-weight: bold;">beta</span>, <span style="color: #0000FF; font-weight: bold;">T</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #0000FF; font-weight: bold;">cat</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'Exact Vasicek Price:'</span>, <span style="color: #0000FF; font-weight: bold;">round</span><span style="color: #080;">&#40;</span>exact,<span style="color: #ff0000;">4</span><span style="color: #080;">&#41;</span>, <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">cat</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'MC Price:'</span>, <span style="color: #0000FF; font-weight: bold;">round</span><span style="color: #080;">&#40;</span>estimate,<span style="color: #ff0000;">4</span><span style="color: #080;">&#41;</span>, <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">cat</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'MC Standard Error:'</span>, <span style="color: #0000FF; font-weight: bold;">round</span><span style="color: #080;">&#40;</span>stdErr,<span style="color: #ff0000;">4</span><span style="color: #080;">&#41;</span>, <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #080;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://stotastic.com/wordpress/2010/04/vasicek-interest-rate-model/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

