<?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; alchemy</title>
	<atom:link href="http://www.adhocgeek.com/tag/alchemy/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 Alchemy</title>
		<link>http://www.adhocgeek.com/2009/09/binomial-option-pricing-with-alchemy/</link>
		<comments>http://www.adhocgeek.com/2009/09/binomial-option-pricing-with-alchemy/#comments</comments>
		<pubDate>Thu, 17 Sep 2009 14:30:47 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Geek]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[alchemy]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[financial engineering]]></category>
		<category><![CDATA[mathematics]]></category>
		<category><![CDATA[option pricing]]></category>

		<guid isPermaLink="false">http://www.adhocgeek.com/?p=146</guid>
		<description><![CDATA[Yet another version of the binomial option pricing algorithm (sorry), but this time I&#8217;m using Alchemy! I have to admit to not entirely understanding how Alchemy works, but I understand enough C to write a simple pricer in it. Here&#8217;s the relevant C-code (which is, in part, copied from Mike Chambers&#8217; stringecho.c sample) : #include [...]]]></description>
			<content:encoded><![CDATA[<p>Yet another version of the binomial option pricing algorithm (sorry), but this time I&#8217;m using <a href="http://labs.adobe.com/wiki/index.php/Alchemy">Alchemy</a>!</p>
<p>I have to admit to not entirely understanding how Alchemy works, but I understand enough C to write a simple pricer in it. Here&#8217;s the relevant C-code (which is, in part, copied from <a href="http://www.mikechambers.com/blog/">Mike Chambers&#8217;</a> stringecho.c sample) :</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdlib.h&gt;</span>
<span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#include &lt;math.h&gt;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Header file for AS3 interop APIs</span>
<span style="color: #339933;">#include &quot;AS3.h&quot;</span>
&nbsp;
<span style="color: #993333;">static</span> AS3_Val getPrice<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #339933;">*</span> self<span style="color: #339933;">,</span> AS3_Val args<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">double</span> N<span style="color: #339933;">,</span> T<span style="color: #339933;">,</span> S<span style="color: #339933;">,</span> K<span style="color: #339933;">,</span> v<span style="color: #339933;">,</span> r<span style="color: #339933;">,</span> price<span style="color: #339933;">;</span>
    AS3_ArrayValue<span style="color: #009900;">&#40;</span> args<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;DoubleType, DoubleType, DoubleType, DoubleType, DoubleType, DoubleType&quot;</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>N<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>T<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>S<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>K<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>v<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>r<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #993333;">double</span> dt <span style="color: #339933;">=</span> T<span style="color: #339933;">/</span>N<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//one time step</span>
&nbsp;
    <span style="color: #993333;">double</span> u <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span> <span style="color: #339933;">+</span> v<span style="color: #339933;">*</span>sqrt<span style="color: #009900;">&#40;</span>dt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//up-tick</span>
    <span style="color: #993333;">double</span> d <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span> <span style="color: #339933;">-</span> v<span style="color: #339933;">*</span>sqrt<span style="color: #009900;">&#40;</span>dt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//down-tick</span>
    <span style="color: #993333;">double</span> p <span style="color: #339933;">=</span> <span style="color:#800080;">0.5</span> <span style="color: #339933;">+</span> r<span style="color: #339933;">*</span>sqrt<span style="color: #009900;">&#40;</span>dt<span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #339933;">*</span>v<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//risk-neutral probability of up-tick</span>
    <span style="color: #993333;">double</span> df <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">/</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #339933;">+</span>r<span style="color: #339933;">*</span>dt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//discount factor over 1 time step, dt</span>
&nbsp;
    <span style="color: #993333;">double</span><span style="color: #339933;">*</span> optionValues <span style="color: #339933;">=</span> malloc<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>N<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">double</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;">//populate the tree (for N-steps there will be N+1 values)</span>
    <span style="color: #993333;">int</span> i<span style="color: #339933;">,</span> j<span style="color: #339933;">;</span>
    <span style="color: #993333;">double</span> ST<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> N<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> 
    <span style="color: #009900;">&#123;</span>
        ST <span style="color: #339933;">=</span> S <span style="color: #339933;">*</span> pow<span style="color: #009900;">&#40;</span>u<span style="color: #339933;">,</span> i<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> pow<span style="color: #009900;">&#40;</span>d<span style="color: #339933;">,</span> N<span style="color: #339933;">-</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        optionValues<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>ST <span style="color: #339933;">&gt;</span> K<span style="color: #009900;">&#41;</span><span style="color: #339933;">?</span> ST <span style="color: #339933;">-</span> K <span style="color: #339933;">:</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//now work backwards to get expected </span>
    <span style="color: #666666; font-style: italic;">//option value at each previous stage</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span>N<span style="color: #339933;">;</span> i <span style="color: #339933;">&gt;=</span> <span style="color: #0000dd;">0</span><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: #b1b100;">for</span><span style="color: #009900;">&#40;</span>j<span style="color: #339933;">=</span><span style="color: #0000dd;">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>
            optionValues<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>p<span style="color: #339933;">*</span>optionValues<span style="color: #009900;">&#91;</span>j<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #339933;">-</span>p<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span>optionValues<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span>df<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    price <span style="color: #339933;">=</span> optionValues<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    free<span style="color: #009900;">&#40;</span>optionValues<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> AS3_Number<span style="color: #009900;">&#40;</span>price<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//define the methods exposed to ActionScript</span>
    <span style="color: #666666; font-style: italic;">//typed as an ActionScript Function instance</span>
    AS3_Val getPriceMethod <span style="color: #339933;">=</span> AS3_Function<span style="color: #009900;">&#40;</span> NULL<span style="color: #339933;">,</span> getPrice <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// construct an object that holds references to the functions</span>
    AS3_Val result <span style="color: #339933;">=</span> AS3_Object<span style="color: #009900;">&#40;</span> <span style="color: #ff0000;">&quot;getPrice: AS3ValType&quot;</span><span style="color: #339933;">,</span> getPriceMethod <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Release</span>
    AS3_Release<span style="color: #009900;">&#40;</span> getPriceMethod <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// notify that we initialized -- THIS DOES NOT RETURN!</span>
    AS3_LibInit<span style="color: #009900;">&#40;</span> result <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// should never get here!</span>
    <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>It&#8217;s been a long time since I used C in anger though, so it&#8217;s entirely possible I&#8217;m using malloc and free badly here! If so, please let me know.</p>
<p>Using the compile SWC in a flex project is incredibly simple, here&#8217;s the relevant exerpt :</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">import</span> cmodule.<span style="color: #006600;">binomial_pricer</span>.<span style="color: #006600;">CLibInit</span>;
&nbsp;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> calc_Click<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">start</span>:<span style="color: #0066CC;">Number</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Date</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">time</span>;
    <span style="color: #000000; font-weight: bold;">var</span> result:<span style="color: #0066CC;">Number</span>;
    <span style="color: #000000; font-weight: bold;">var</span> loader:CLibInit = <span style="color: #000000; font-weight: bold;">new</span> CLibInit<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #000000; font-weight: bold;">var</span> lib:<span style="color: #0066CC;">Object</span> = loader.<span style="color: #006600;">init</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span>=<span style="color: #cc66cc;">0</span>; i <span style="color: #66cc66;">&lt;</span> repetitions.<span style="color: #006600;">value</span>; i++<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        result = lib.<span style="color: #006600;">getPrice</span><span style="color: #66cc66;">&#40;</span>    steps.<span style="color: #006600;">value</span>, 
                            timeToExpiry.<span style="color: #006600;">value</span>, 
                            spot.<span style="color: #006600;">value</span>,
                            strike.<span style="color: #006600;">value</span>,
                            volatility.<span style="color: #006600;">value</span>,
                            <span style="color: #0066CC;">rate</span>.<span style="color: #006600;">value</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>                                
    price.<span style="color: #0066CC;">text</span> = result.<span style="color: #0066CC;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;            
    <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">end</span>:<span style="color: #0066CC;">Number</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Date</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">time</span>;
    averageTime.<span style="color: #0066CC;">text</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>end-<span style="color: #0066CC;">start</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">/</span>repetitions.<span style="color: #006600;">value</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">&quot;ms&quot;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>And the finished result, which is about 8x (!) faster than the pixel bender version (on my machine at least) :</p>
<p><object width="400" height="360" data="/flash/AlchemyBinomialPricer/AlchemyBinomialPricer.swf" type="application/x-shockwave-flash"><param name="name" value="AlchemyBinomialPricer" /><param name="bgcolor" value="#ffffff" /><param name="align" value="middle" /><param name="src" value="/flash/AlchemyBinomialPricer/AlchemyBinomialPricer.swf" /><param name="quality" value="high" /></object></p>
]]></content:encoded>
			<wfw:commentRss>http://www.adhocgeek.com/2009/09/binomial-option-pricing-with-alchemy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

