Tag Archives: pricing

The Black Scholes Algorithm: The Greeks in Java

Last time in the Black Scholes series I wrote about how to write the Black Scholes algorithm in Java in a maintainable and readable way by decomposing its constituent parts for easy reference. This time I look at modifying the previous implementation to incorporate The Greeks. Though – this time – there’s isn’t any great degree of decomposition to do as each Greek has its own formula and each one is implemented separately anyway.

What are The Greeks and how do they relate to the Black Scholes algorithm? This is best answered by Risk Glossary. Below I merely present a decomposed implementation of Black Scholes and The Greeks. In terms of performance considerations I’ve ensured that no equivalent operations are called more than once. I’ve also eliminated object allocation completely – the only allocation made is of a result array which contains six doubles. However, I have not applied advanced optimisations such as the use of multiplication in place of the pow() function.

I want to add a disclaimer that there isn’t anything very new in this post that isn’t already out there. I’m sure this has been done lots of times out there but I’m posting this for two reasons: 1) I wanted to post my take on the implementation in a decomposed fashion 2) I didn’t really find anything out there implementing the Greeks in Java. I hope it helps others looking for something similar.

The Greeks in Java

The inputs to the The Greeks formulas are the same as those available to the The Black Scholes algorithm so both can implemented in common scope. Most Greeks have different formulas for call and put options with the exception of gamma and vega which have been implemented common to call and put options. Theta was the most complex formula compared to the rest of them and so I’ve broken that one down into left and right halves. The calculate() method returns a six element double array which contains the following values in order: (1) price (2) delta (3) gamma (4) vega (5) theta (6) rho. If you want to know what the inputs are see my previous article and for the formulas implemented below look here.

package name.dhruba.black.scholes;

import static java.lang.Math.PI;
import static java.lang.Math.abs;
import static java.lang.Math.exp;
import static java.lang.Math.log;
import static java.lang.Math.pow;
import static java.lang.Math.sqrt;

public enum BlackScholesGreeks2 {

    _;

    private static final double P = 0.2316419;
    private static final double B1 = 0.319381530;
    private static final double B2 = -0.356563782;
    private static final double B3 = 1.781477937;
    private static final double B4 = -1.821255978;
    private static final double B5 = 1.330274429;

    public static double[] calculate(boolean c, 
            double s, double k, double r, double t, double v) {

        double[] p = new double[6];

        double d1 = d1(s, k, r, t, v);
        double d2 = d2(s, k, r, t, v);
        
        double sd1 = standardNormalDistribution(d1);
        double cd1 = cumulativeDistribution(d1, sd1);
        double thetaLeft = -(s * sd1 * v) / (2 * sqrt(t));

        if (c) {

            double cd2 = cumulativeDistribution(d2);

            // price
            p[0] = s * cd1 - k * exp(-r * t) * cd2;

            // delta
            p[1] = cd1;

            // theta
            double thetaRight = r * k * exp(-r * t) * cd2;
            p[4] = thetaLeft - thetaRight;

            // rho
            p[5] = k * t * exp(-r * t) * cd2;

        } else {

            double pcd1 = cumulativeDistribution(-d1);
            double pcd2 = cumulativeDistribution(-d2);

            // price
            p[0] = k * exp(-r * t) * pcd2 - s * pcd1;

            // delta
            p[1] = cd1 - 1;

            // theta
            double thetaRight = r * k * exp(-r * t) * pcd2;
            p[4] = thetaLeft + thetaRight;

            // rho
            p[5] = -k * t * exp(-r * t) * pcd2;

        }

        // gamma
        p[2] = sd1 / (s * v * sqrt(t));

        // vega
        p[3] = s * sd1 * sqrt(t);

        return p;

    }

    private static double d1(double s, double k, double r, double t, double v) {
        double top = log(s / k) + (r + pow(v, 2) / 2) * t;
        double bottom = v * sqrt(t);
        return top / bottom;
    }

    private static double d2(double s, double k, double r, double t, double v) {
        return d1(s, k, r, t, v) - v * sqrt(t);
    }

    public static double cumulativeDistribution(double x) {
        return cumulativeDistribution(x, standardNormalDistribution(x));
    }

    public static double cumulativeDistribution(double x, double sdx) {
        double t = 1 / (1 + P * abs(x));
        double t1 = B1 * pow(t, 1);
        double t2 = B2 * pow(t, 2);
        double t3 = B3 * pow(t, 3);
        double t4 = B4 * pow(t, 4);
        double t5 = B5 * pow(t, 5);
        double b = t1 + t2 + t3 + t4 + t5;
        double cd = 1 - sdx * b;
        return x < 0 ? 1 - cd : cd;
    }

    public static double standardNormalDistribution(double x) {
        double top = exp(-0.5 * pow(x, 2));
        double bottom = sqrt(2 * PI);
        return top / bottom;
    }

}

Testing the implementation

I’ve written a simple test based on values assumed to be correct from an online calculator. Rather oddly for some of the values although my answers matched with that online calculator they differ from those on Wolfram Alpha very slightly. I don’t know why Wolfram Alpha is produced different values. If you know let me know.

package name.dhruba.black.scholes;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class TestBlackScholesGreeks {

    @Test
    public void testGreeks() {

        boolean c;
        double s, k, r, t, v;
        double[] p;

        c = true;
        s = 56.25;
        k = 55;
        r = 0.0285;
        t = 0.34;
        v = 0.28;

        p = BlackScholesGreeks2.calculate(c, s, k, r, t, v);

        assertEquals(4.561, round(p[0], 3), 0);
        assertEquals(0.610, round(p[1], 3), 0);
        assertEquals(0.042, round(p[2], 3), 0);
        assertEquals(12.587, round(p[3], 3), 0);
        assertEquals(-6.030, round(p[4], 3), 0);
        assertEquals(10.110, round(p[5], 3), 0);

        c = false;
        p = BlackScholesGreeks2.calculate(c, s, k, r, t, v);

        assertEquals(2.781, round(p[0], 3), 0);
        assertEquals(-0.390, round(p[1], 3), 0);
        assertEquals(0.042, round(p[2], 3), 0);
        assertEquals(12.587, round(p[3], 3), 0);
        assertEquals(-4.478, round(p[4], 3), 0);
        assertEquals(-8.409, round(p[5], 3), 0);

    }

    static double round(double d, int places) {
        int factor = (int) Math.pow(10, places);
        return (double) Math.round(d * factor) / factor;
    }

}

Oddities

One thing I noticed is that for the price formula for a put option reordering certain operations, even though the overall operation was equivalent, produced different digits towards the end of the value; in other words, towards the final decimal places. The following two operations, for example, although equivalent, produce different digits for the final few decimal places. If anyone knows what’s going on here do let me know.

p[0] = pcd2 * k * exp(-r * t) - pcd1 * s;
p[0] = k * exp(-r * t) * pcd2 - s * pcd1;

Did this help you or do you have any improvements or fixes? Let me know in the comments.