<a href="http://blog.accelereyes.com/blog/wp-content/uploads/2012/08/Koch-Supply-and-Trading1.jpg"><img class="size-full wp-image-2906 alignright" title="Koch Supply and Trading" src="http://blog.accelereyes.com/blog/wp-content/uploads/2012/08/Koch-Supply-and-Trading1.jpg" alt="" width="216" height="60" /></a>

Andrew Shin, Market Risk Manager of Koch Supply &amp; Trading, achieves significant performance increases on option pricing algorithms using Jacket to accelerate his MATLAB® code with GPUs.

Andrew says, "My buddy and I are, at best, novice programmers and we couldn't imagine having to figure out how to code all this in CUDA." But he found Jacket to be straight-forward. With these results, he says he can see Jacket and GPUs populating Koch's mark-to-futures cube, which contains its assets, simulations, and simulated asset prices.

Modern option pricing techniques are often considered among the most mathematically complex of all applied areas of finance. Andrew shared some exemplary code to demonstrate how much leverage you can get out of Jacket and GPUs for financial computing in MATLAB® and C/C++.

All option pricing algorithms in this post use a Monte Carlo simulation. The <code>stockpath B</code> version is the option pricing algorithm with a barrier option, and the <code>stockpath A</code> version is without the barrier.

The execution times and speedups for these algorithms are shown in the charts below:

<a href="http://blog.accelereyes.com/blog/wp-content/uploads/2012/08/plot2.jpg"><img title="plot2" src="http://blog.accelereyes.com/blog/wp-content/uploads/2012/08/plot2.jpg" alt="" width="760" height="529" /></a>

<a href="http://blog.accelereyes.com/blog/wp-content/uploads/2012/08/plots01.jpg"><img title="plots0" src="http://blog.accelereyes.com/blog/wp-content/uploads/2012/08/plots01.jpg" alt="" width="640" height="471" /></a>

For <code>stockpath A</code>, Jacket and ArrayFire reach<strong> 40.5x</strong> and <strong>51.8x</strong> speedups respectively over the baseline CPU MATLAB® code. For <code>stockpath B</code>, Jacket and ArrayFire achieve <strong>38.4x</strong> and <strong>47.5x</strong> speedups respectively over the baseline CPU MATLAB® code.

System specs:

A single workstation with a 2.66 GHz Intel Core 2 Quad CPU and an 6GB NVIDIA Quadro 6000 GPU, with MATLAB® R2010b.

Below is example Jacket code of option pricers:
<pre lang="matlab">function P=stockpath_A(N, K, steps, t, r, vol, cls)
  payoff = zeros(N, 1, cls);
  dt = t / (steps - 1);
  s = K * ones(N, 1, cls);

  randmat = randn(N, steps - 1, cls);
  d = exp((r - (vol * vol * 0.5)) * dt + vol * sqrt(dt) * randmat);
  S = [s, d];
  S = cumprod(S, 2);

  payoff = max(S(:, steps) - K, 0.0);
  P = double(mean(payoff) * exp(-r * t));
end

function P=stockpath_B(N, K, steps, t, r, vol, cls)
  payoff = zeros(N, 1, cls);
  B = 1.15 * K;
  dt = t / (steps - 1);
  s = K * ones(N, 1, cls);

  randmat = randn(N, steps - 1, cls);
  d = exp((r - (vol * vol * 0.5)) * dt + vol * sqrt(dt) * randmat);

  S = [s, d];
  S = cumprod(S, 2);
  S(:, steps) = S(:, steps) .* all(S &lt; B, 2);

  payoff = max(S(:, steps) - K, 0.0);
  P = double(mean(payoff) * exp(-r * t));
end</pre>
Where <code>K</code> is strike price, <code>t</code> is maturity, <code>r</code> is risk free rate, <code>B</code> is barrier and <code>vol</code> is volatility. The <code>cls</code>, which is either <code>'double'</code> or <code>gdouble</code>, enables us to run same pieces of code <code>stockpath_A</code> and <code>stockpath_B</code> both on CPU and GPU without actually changing the code. The <code>cls</code> serves as a flag that lets the code know whether it is run on a CPU or a GPU. When <code>cls</code> is set as <code>gdouble</code>, matrix initialization, assignments, calculations including <code>cumprod</code>, <code>mean</code> and <code>all</code>, are all executed in parallel on GPU and therefore you will get a good speedup.

Below is example ArrayFire code of option pricers:
<pre>static double stockpath_A(int N)
{
    dtype pres = f64;
    int steps = 180;
    array payoff = zeros(N, 1, pres);
    double K = 100.0;

    double t = 0.5;
    double dt = t / (double)(steps - 1);
    double vol = .30;
    double r = .01;
    array s = 100 * ones(N, 1, pres);

    array randmat = randn(N, steps - 1, pres);
    randmat = exp((r - (vol * vol * 0.5)) * dt + vol * sqrt(dt) * randmat);
    array S = join(1, s, randmat);
    S = accum(S, 1, PROD_T);

    payoff = max(0.0, S.col(steps - 1) - K);
    double P = mean&lt;double&gt;(payoff) * exp(-r * t);

    return P;
}

static double stockpath_B(int N)
{
    dtype pres = f64;
    int steps = 180;
    array payoff = zeros(N, 1, pres);
    double K = 100.0;
    double B = 115.0;

    double t = 0.5;
    double dt = t / (double)(steps - 1);
    double vol = .30;
    double r = .01;
    array s = 100 * ones(N, 1, pres);

    array randmat = randn(N, steps - 1, pres);
    randmat = exp((r - (vol * vol * 0.5)) * dt + vol * sqrt(dt) * randmat);

    array S = join(1, s, randmat);
    S = accum(S, 1, PROD_T);
    S(span, end) *= all(S &lt; B, 1);

    payoff = max(0.0, S.col(steps - 1) - K);
    double P = mean&lt;double&gt;(payoff) * exp(-r * t);

    return P;
}</pre>
Where <code>array</code> is matrix, <code>f64</code> denotes all elements of arrays should be doubles, <code>join(1, s, randmat)</code> joins matrix <code>s</code> and <code>randmat</code> as <code>[s, d]</code> does, and <code>accum(S, 1, PROD_T)</code> calculates cumulative product as <code>cumprod(S, 2)</code> does. ArrayFire functions are straightforward to understand as their names indicate and of course they will be run on GPUs. The ArrayFire code is faster than Jacket because it is C/C++ code and it does not suffer from overheads introduced by MATLAB®.

From the above code and charts, you can easily see the simplicity of achieving great speedups. Grab a free <a href="http://accelereyes.com/jacket_tour">Jacket</a> or <a href="http://accelereyes.com/arrayfire_tour">ArrayFire Pro</a> 15-day trial today!