Black-Scholes
Java 中的 Black Scholes 和 Monte Carlo 實現
可能重複:
誰能推荐一個在 Java 中實現 Black Scholes 和 Monte Carlo 的庫?理想情況下,只有 Java 並且不需要 C++ dll 或 .so 或 .lib 等。
我已經發布了一個關於開源的類似問題,到目前為止,選擇似乎非常有限:
Quantlib - 使用 SWIG 或類似方法與 JVM 通信的 C++ JQuantLib - 將 QuantLib 移植到純 Java,但現在 2 天無法訪問站點。finmath.net - 似乎都是 java 並且有希望,但發現使用它執行小程序時遇到問題。
目前,這是為非常簡單的香草歐元式期權定價。但必須對肯定會出現的複雜性持開放態度。需要蒙地卡羅或其他定價模型。
提前致謝。
這個是用 C# 編寫的,但它可以幫助你用 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; } } }
它不是免費的,但我認為它是解決任何復雜問題的好工具。
雖然我沒有親自測試過。