<?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>Ad hoc Geek &#187; flash</title>
	<atom:link href="http://www.adhocgeek.com/tag/flash/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.adhocgeek.com</link>
	<description>Approaching geekery in an ad hoc and improvisational manner.</description>
	<lastBuildDate>Fri, 30 Sep 2011 09:43:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Binomial Option Pricing with Pixel Bender</title>
		<link>http://www.adhocgeek.com/2009/09/binomial-option-pricing-with-pixel-bender/</link>
		<comments>http://www.adhocgeek.com/2009/09/binomial-option-pricing-with-pixel-bender/#comments</comments>
		<pubDate>Tue, 15 Sep 2009 11:13:03 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Geek]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[financial engineering]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[mathematics]]></category>
		<category><![CDATA[option pricing]]></category>
		<category><![CDATA[pixel bender]]></category>

		<guid isPermaLink="false">http://www.adhocgeek.com/?p=134</guid>
		<description><![CDATA[Pixel Bender has been around for a little while now and it&#8217;s a very cool idea, promising incredible speed improvements by allowing developers to make the most of multi-core processors and GPUs. It seems as though it was designed primarily for Adobe image manipulation software, but, since images are just arrays of bytes it can [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://labs.adobe.com/technologies/pixelbender/">Pixel Bender</a> has been around for a little while now and it&#8217;s a very cool idea, promising incredible speed improvements by allowing developers to make the most of multi-core processors and GPUs. It seems as though it was designed primarily for Adobe image manipulation software, but, since images are just arrays of bytes it can also be used to speed up any parallelisable code you might have within Flash.</p>
<h3>A fast intro to Pixel Bender</h3>
<p>The main unit of operation in pixel bender is the kernel. Generally, each kernel will take a source image and calculate the value of a single pixel in the output image. i.e. for every pixel in your source image, a single kernel instance will be run which can use that whole image to calculate the value of a single pixel (although you won&#8217;t usually want it to look at the whole image &#8211; the less dependent it is on other pixels, the faster it can run).</p>
<p>The kernel language is intended to be fast and as such is fairly simplistic. A simple kernel which converts every pixel in the source image to black would be written as follows :</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>languageVersion <span style="color: #339933;">:</span> <span style="color:#800080;">1.0</span><span style="color: #339933;">;&gt;</span>
&nbsp;
kernel Gothify
<span style="color: #339933;">&lt;</span>   namespace <span style="color: #339933;">:</span> <span style="color: #ff0000;">&quot;com.example&quot;</span><span style="color: #339933;">;</span>
    vendor <span style="color: #339933;">:</span> <span style="color: #ff0000;">&quot;Example.com&quot;</span><span style="color: #339933;">;</span>
    version <span style="color: #339933;">:</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    description <span style="color: #339933;">:</span> <span style="color: #ff0000;">&quot;Fills every pixel in the source image with black&quot;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&gt;</span>
<span style="color: #009900;">&#123;</span>        
    input image4 source<span style="color: #339933;">;</span>
    output pixel4 result<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #993333;">void</span> evaluatePixel<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        result <span style="color: #339933;">=</span> pixel4<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span><span style="color:#800080;">1.0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The developer&#8217;s guide says to use pixel4(0,0,0,0), but since the last value is the alpha channel, that just ends up with a <em>transparent</em> black image. It also doesn&#8217;t include the input image4, and while this may do something in photoshop, it&#8217;s not going to give you any output in the pixel bender IDE.</p>
<p>Now, an image is basically just an array of pixels, and, within pixel bender, a 4-channel pixel (a pixel4 type) is just a vector of floats. We can use <em>any</em> values within a pixel4, limited only by the bounds of a standard float size. So, we could change the evaluatePixel function above to the following and it wouldn&#8217;t make any difference to the output image :</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> evaluatePixel<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    result <span style="color: #339933;">=</span> pixel4<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color:#800080;">1500.67</span><span style="color: #339933;">,-</span><span style="color:#800080;">45.0</span><span style="color: #339933;">,-</span><span style="color:#800080;">32.333</span><span style="color: #339933;">,</span><span style="color:#800080;">515.47</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>It&#8217;s only the values between 0.0 and 1.0 that can change the output colour, anything above 1.0 is at maximum and anything below 0.0 is at minimum. This means we can use pixel bender to operate on any array of floating point values as long as we&#8217;re happy for it to operate on pixel sized chunks of data in isolation (the pixel bender graph language does allow chaining of kernels, but this isn&#8217;t available to us in the flash player, so I&#8217;m going to ignore it for now).</p>
<h3>Parallelising the Binomial Option Pricing Algorithm</h3>
<p>There&#8217;s a short, fairly readable paper <a href="http://saahpc.ncsa.illinois.edu/papers/Ganesan_paper.pdf">here</a> [Ganesan, Chamberlain, Buhler] which gives some ideas for how to parallelise the algorithm. I&#8217;m only going to implement the scheme which is probably best illustrated by their fig. 2. This requires me to pass to each kernel the input image (array of option values at time T), the risk-neutral probability of an up-tick and the discount factor over one time step.<br />
I&#8217;ll then have to call the resulting kernel (or array of kernels, depending on how you think of it), N-1 times to get the final option price.</p>
<p>The kernel I wrote ended up like this :</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>languageVersion <span style="color: #339933;">:</span> <span style="color:#800080;">1.0</span><span style="color: #339933;">;&gt;</span>
&nbsp;
kernel BinomialOptionPricer
<span style="color: #339933;">&lt;</span>   namespace <span style="color: #339933;">:</span> <span style="color: #ff0000;">&quot;com.adhocgeek&quot;</span><span style="color: #339933;">;</span>
    vendor <span style="color: #339933;">:</span> <span style="color: #ff0000;">&quot;Ad hoc Geek&quot;</span><span style="color: #339933;">;</span>
    version <span style="color: #339933;">:</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    description <span style="color: #339933;">:</span> <span style="color: #ff0000;">&quot;Calculates the one step binomial option price&quot;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&gt;</span>
<span style="color: #009900;">&#123;</span>
    input image3 src<span style="color: #339933;">;</span>
    output pixel3 dst<span style="color: #339933;">;</span>
&nbsp;
    parameter <span style="color: #993333;">float</span> p<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//risk-neutral probability of an up-tick</span>
    parameter <span style="color: #993333;">float</span> df<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//discount factor over one time step</span>
&nbsp;
    <span style="color: #993333;">void</span> evaluatePixel<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        pixel3 down <span style="color: #339933;">=</span> sampleNearest<span style="color: #009900;">&#40;</span>src<span style="color: #339933;">,</span> outCoord<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        pixel3 up <span style="color: #339933;">=</span> sampleNearest<span style="color: #009900;">&#40;</span>src<span style="color: #339933;">,</span> outCoord<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> float2<span style="color: #009900;">&#40;</span><span style="color:#800080;">1.0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #993333;">float</span> V <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>p<span style="color: #339933;">*</span>up.<span style="color: #202020;">x</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color:#800080;">1.0</span><span style="color: #339933;">-</span>p<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span>down.<span style="color: #202020;">x</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span>df<span style="color: #339933;">;</span>
&nbsp;
        dst <span style="color: #339933;">=</span> pixel3<span style="color: #009900;">&#40;</span>V<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Using the Pixel Bender Filter in Actionscript</h3>
<p>This is actually incredibly easy. Flash 10 gives us a new class called the <a href="http://help.adobe.com/en_US/AS3LCR/Flash_10.0/flash/display/Shader.html">Shader</a> which represents a Pixel Bender kernel. You can use this with the new ShaderJob class to trigger a set of kernel operations on any vector or byte array that you&#8217;ve created. I&#8217;m not going to try and write a tutorial, so here&#8217;s the final actionscript code I wrote (you should notice it&#8217;s quite similar to the previous version):</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&#91;</span>Embed<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;BinomialOptionPricer.pbj&quot;</span>, mimeType=<span style="color: #ff0000;">&quot;application/octet-stream&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> BinomialOptionPricer:<span style="color: #000000; font-weight: bold;">Class</span>;
&nbsp;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getPrice<span style="color: #66cc66;">&#40;</span>  N:<span style="color: #0066CC;">Number</span>, <span style="color: #808080; font-style: italic;">//Number of steps</span>
                                    T:<span style="color: #0066CC;">Number</span>, <span style="color: #808080; font-style: italic;">//Time to expiry (in years)</span>
                                    S:<span style="color: #0066CC;">Number</span>, <span style="color: #808080; font-style: italic;">//Spot price of underlying</span>
                                    K:<span style="color: #0066CC;">Number</span>, <span style="color: #808080; font-style: italic;">//Option strike</span>
                                    v:<span style="color: #0066CC;">Number</span>, <span style="color: #808080; font-style: italic;">//Volatility of underlying</span>
                                    r:<span style="color: #0066CC;">Number</span>, <span style="color: #808080; font-style: italic;">//risk-free rate</span>
                                    NUMOPTIONS:<span style="color: #0066CC;">Number</span> 
                                 <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">var</span> dt:<span style="color: #0066CC;">Number</span> = T<span style="color: #66cc66;">/</span>N; <span style="color: #808080; font-style: italic;">//one time step</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">var</span> u:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">1</span> + v<span style="color: #66cc66;">*</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">sqrt</span><span style="color: #66cc66;">&#40;</span>dt<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">//up-tick</span>
    <span style="color: #000000; font-weight: bold;">var</span> d:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">1</span> - v<span style="color: #66cc66;">*</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">sqrt</span><span style="color: #66cc66;">&#40;</span>dt<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">//down-tick</span>
    <span style="color: #000000; font-weight: bold;">var</span> p:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">0.5</span> + r<span style="color: #66cc66;">*</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">sqrt</span><span style="color: #66cc66;">&#40;</span>dt<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">/</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">*</span>v<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">//risk-neutral prob. of up-tick</span>
    <span style="color: #000000; font-weight: bold;">var</span> df:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">/</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>+r<span style="color: #66cc66;">*</span>dt<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">//discount factor over 1 time step, dt</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">var</span> optionValues:Vector.<span style="color: #66cc66;">&lt;</span>Number<span style="color: #66cc66;">&gt;</span> = <span style="color: #000000; font-weight: bold;">new</span> Vector.<span style="color: #66cc66;">&lt;</span>Number<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">//Populate the fake pixels array </span>
    <span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span>, j:<span style="color: #0066CC;">int</span>, ST:<span style="color: #0066CC;">Number</span>;
    <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>j=<span style="color: #cc66cc;">0</span>; j <span style="color: #66cc66;">&lt;</span> NUMOPTIONS; j++<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>i=<span style="color: #cc66cc;">0</span>; i <span style="color: #66cc66;">&lt;</span> N+<span style="color: #cc66cc;">1</span>; i++<span style="color: #66cc66;">&#41;</span> 
        <span style="color: #66cc66;">&#123;</span>
            ST = spot.<span style="color: #006600;">value</span> <span style="color: #66cc66;">*</span> <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">pow</span><span style="color: #66cc66;">&#40;</span>u, i<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">pow</span><span style="color: #66cc66;">&#40;</span>d, N-i<span style="color: #66cc66;">&#41;</span>;
            optionValues.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>ST <span style="color: #66cc66;">&gt;</span> K<span style="color: #66cc66;">&#41;</span>? ST - K : <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
            optionValues.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
            optionValues.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #808080; font-style: italic;">//work backwards to get expected option value at each previous stage</span>
    <span style="color: #000000; font-weight: bold;">var</span> sj:ShaderJob;
    <span style="color: #000000; font-weight: bold;">var</span> shader:Shader = <span style="color: #000000; font-weight: bold;">new</span> Shader<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> BinomialOptionPricer<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> as ByteArray<span style="color: #66cc66;">&#41;</span>;
    shader.<span style="color: #0066CC;">data</span>.<span style="color: #006600;">src</span>.<span style="color: #006600;">input</span> = optionValues;
    shader.<span style="color: #0066CC;">data</span>.<span style="color: #006600;">src</span>.<span style="color: #0066CC;">height</span> = NUMOPTIONS;
    shader.<span style="color: #0066CC;">data</span>.<span style="color: #006600;">p</span>.<span style="color: #006600;">value</span> = <span style="color: #66cc66;">&#91;</span>p<span style="color: #66cc66;">&#93;</span>;
    shader.<span style="color: #0066CC;">data</span>.<span style="color: #006600;">df</span>.<span style="color: #006600;">value</span> = <span style="color: #66cc66;">&#91;</span>df<span style="color: #66cc66;">&#93;</span>;
    <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>i=N; i <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span>; i--<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        shader.<span style="color: #0066CC;">data</span>.<span style="color: #006600;">src</span>.<span style="color: #0066CC;">width</span> = i+<span style="color: #cc66cc;">1</span>;
        sj = <span style="color: #000000; font-weight: bold;">new</span> ShaderJob<span style="color: #66cc66;">&#40;</span>shader, optionValues, i+<span style="color: #cc66cc;">1</span>, NUMOPTIONS<span style="color: #66cc66;">&#41;</span>;
        sj.<span style="color: #0066CC;">start</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> optionValues<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>; 
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Points to note are where I&#8217;ve embedded the kernel in the actionscript itself and where I&#8217;ve added the ability to run the calculation multiple times in parallel (simulating running the calculation for many different options at once). Each line in the source &#8220;image&#8221; represents a different option (although they&#8217;re all using the same source data at the moment).</p>
<p>Here (if you have Flash Player 10 installed) is the resulting swf (view source enabled, of course) :</p>
<p><object width="400" height="360" data="/flash/PixelBenderPricer/PBBinomialPricer.swf" type="application/x-shockwave-flash"><param name="name" value="PBBinomialPricer" /><param name="bgcolor" value="#ffffff" /><param name="align" value="middle" /><param name="src" value="/flash/PixelBenderPricer/PBBinomialPricer.swf" /><param name="quality" value="high" /></object></p>
<p>It&#8217;s slightly faster than the straight actionscript version, but not by a great deal, where it really shines is in calculating multiple options at once. There&#8217;s an interesting non-linear relationship between the number of lines in the source &#8220;image&#8221; and the speed of this particular algorithm. I might graph it at some point&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adhocgeek.com/2009/09/binomial-option-pricing-with-pixel-bender/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

