Binomial Option Pricing Model In Java

Code

import static java.lang.Math.max;
import static java.lang.Math.pow;
 
import java.util.HashMap;
import java.util.Map;
 
public class BinomialOptionPricingModel
{
 
    private static final String CONTEXT_MAP_FORMAT = "%d%d";
 
    protected  Map<String, Integer> map = new HashMap<String, Integer>();
 
    public final double calculatePutValue()
    {
        return 0;
    }
 
    public static final double calculateAmericanOption(BinomialParameters parameters)
    {
        Map<String, Double> context = new HashMap<String, Double>();
        return _calculateAmericanOption(parameters, context, 0, 0);
    }
 
    private static double _calculateAmericanOption(BinomialParameters bp, Map<String, Double> context, final int level,
            final int node)
    {
 
        Double value;
 
        final String contextIdentifier = String.format(CONTEXT_MAP_FORMAT, level, node);
 
        if ((value = context.get(contextIdentifier)) != null)
        {
            return value;
        }
 
        OptionValuation valuation = bp.getNodeValuation();
        final double down = bp.getDown();
        final double up = bp.getUp();
 
        if (level == bp.getSamplesPerPeriod())
        {
            value = valuation.calculateValue(bp.getExercisePrice(), bp.getCurrentPrice() * pow(down, node)
                    * pow(up, (level - node)));
        } else
        {
            double unexercisedValue = bp.getP() * _calculateAmericanOption(bp, context, level + 1, node) + bp.getQ()
                    * _calculateAmericanOption(bp, context, level + 1, node + 1);
            // S * d^j * u^(i - j)
            double exercisedValue = valuation.calculateValue(bp.getExercisePrice(), bp.getCurrentPrice()
                    * pow(down, node) * pow(up, (level - node)));
            value = max(unexercisedValue, exercisedValue);
        }
 
        context.put(contextIdentifier, value);
 
        return value;
 
    }
}

Commentary

This is an implementation (actually a recursive adaptation) of the binomial options pricing model written in Java. I wrote this is in 2008 when playing around with yahoo financial services .

Snippet Usage

Unless otherwise stated, the content of this page is licensed under GNU Free Documentation License.