Skip to content
All library documents

Java Options Pricing Libraries for Black–Scholes and Monte Carlo

Article Quant Q&A · Author: colin

Summary

The document asks for Java-only libraries that implement Black–Scholes and Monte Carlo pricing, initially for simple European vanilla options but with room for more complex models. It mentions QuantLib through a Java interface, the pure-Java JQuantLib port, and finmath.net, while noting access or runtime difficulties encountered by the questioner. The responses suggest adapting a Monte Carlo example from another language and mention a commercial numerical library as another possibility.

The exchange does not provide a verified Java implementation or compare the suggested options in depth. The included example is written in C#, so it illustrates a simulation approach but is not directly usable as Java code. One response also says the commercial library was not personally tested. Readers should treat the recommendations as leads to investigate rather than evidence of library suitability, current availability, or pricing accuracy.

Key ideas

  • The request is for Java implementations of Black–Scholes and Monte Carlo option pricing.
  • The desired scope starts with European vanilla options and may expand to more complex models.
  • The document mentions a Java port, a Java-focused finance library, and access to QuantLib through a Java interface.
  • The Monte Carlo example is in C#, so it is not a ready-to-use Java implementation.
  • The suggestions are not supported by a systematic comparison or verified testing.

Tags

Full text
# Black Scholes and Monte Carlo implementations in Java


# Black Scholes and Monte Carlo implementations in Java












> Possible Duplicate: Is there an all Java options-pricing library (preferably open source) besides jquantlib?

Can anyone recommend a library with an implementation of Black Scholes and Monte Carlo in Java? Ideally something that is Java only and doesn't require a C++ dll or .so or .lib etc..

I've posted a similar question about open source and the choices appear to be quite limited thus far:

Quantlib - C++ using SWIG or similar to communicate with JVM JQuantLib - port of QuantLib to pure Java, but can't access site for 2 days now. finmath.net - Appears to be all java and has promise, but finding problems running applets using it.

Presently, this is for pricing very simple vanilla euro-style options. But must be open to the complexity that's sure to come prob. requiring Monte Carlo or other pricing models.

Thanks in advance.

## Answer by Clebson Derivan (score 1)

https://quant.stackexchange.com/a/1928

This one is in C#, but it could help you create yours in Java: Divergence issue with my monte carlo pricer...

```
using System;
using System.Threading.Tasks;
using MathNet.Numerics.Distributions;
using MathNet.Numerics.Random;

namespace MonteCarlo
{
    class VanillaEuropeanCallMonteCarlo
    {
        static void Main(string[] args)
        {
            const int NUM_SIMULATIONS = 10000000;
            const decimal strike = 50m;
            const decimal initialStockPrice = 52m;
            const decimal volatility = 0.2m;
            const decimal riskFreeRate = 0.05m;
            const decimal maturity = 0.5m;
            Normal n = new Normal();
            n.RandomSource = new MersenneTwister();

            VanillaEuropeanCallMonteCarlo vanillaCallMonteCarlo = new VanillaEuropeanCallMonteCarlo();

            Task<decimal>[] simulations = new Task<decimal>[NUM_SIMULATIONS];

            for (int i = 0; i < simulations.Length; i++)
            {
                simulations[i] = new Task<decimal>(() => vanillaCallMonteCarlo.RunMonteCarloSimulation(strike, initialStockPrice, volatility, riskFreeRate, maturity, n));
                simulations[i].Start();
            }

            Task.WaitAll(simulations);

            decimal total = 0m;

            for (int i = 0; i < simulations.Length; i++)
            {
                total += simulations[i].Result;
            }

            decimal callPrice = (decimal)(Math.Exp((double)(-riskFreeRate * maturity)) * (double)total / (NUM_SIMULATIONS * 2));

            Console.WriteLine("Call Price: " + callPrice);
            Console.WriteLine("Difference: " + Math.Abs(callPrice - 4.744741008m));
        }

        decimal RunMonteCarloSimulation(decimal strike, decimal initialStockPrice, decimal volatility, decimal riskFreeRate, decimal maturity, Normal n)
        {
            decimal randGaussian = (decimal)n.Sample();
            decimal endStockPriceA = initialStockPrice * (decimal)Math.Exp((double)((riskFreeRate - (decimal)(0.5 * Math.Pow((double)volatility, 2))) * maturity + volatility * (decimal)Math.Sqrt((double)maturity) * randGaussian));
            decimal endStockPriceB = initialStockPrice * (decimal)Math.Exp((double)((riskFreeRate - (decimal)(0.5 * Math.Pow((double)volatility, 2))) * maturity + volatility * (decimal)Math.Sqrt((double)maturity) * (-randGaussian)));
            decimal sumPayoffs = (decimal)(Math.Max(0, endStockPriceA - strike) + Math.Max(0, endStockPriceB - strike));
            return sumPayoffs;
        }
    }
}
```

## Answer by SRKX (score 0)

https://quant.stackexchange.com/a/1927

You could try to see if NAG could suit your needs as I suggested in this post.

It's not free, but I think it's a pretty good tool to tackle problems of any complexity.

I haven't personally tested it though.

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.