一千萬個為什麽

搜索

Java中的Black Scholes和Monte Carlo實現

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

任何人都可以推薦一個帶有Black Scholes和Monte Carlo Java實現的庫嗎?理想情況下,只有Java是不需要C ++ dll或.so或.lib等的東西。

我發布了一個關於開源的類似問題,到目前為止,選擇似乎非常有限:

Quantlib - 使用SWIG或類似的C ++與JVM進行通信 JQuantLib - QuantLib的端口為純Java,但現在無法訪問站點2天。 finmath.net - 看起來都是java並且有承諾,但是發現使用它運行applet的問題。

目前,這是定價非常簡單的香草歐元風格的選項。但必須接受可能出現的復雜問題。需要蒙特卡羅或其他定價模式。

提前致謝。

最佳答案

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[] simulations = new Task[NUM_SIMULATIONS];

            for (int i = 0; i < simulations.Length; i++)
            {
                simulations[i] = new Task(() => 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;
        }
    }
}

轉載註明原文: Java中的Black Scholes和Monte Carlo實現