public static void Task5() { // Variance Process Values double Kappa = 1.5768; double Theta = 0.398; double Sigma = 0.5751; double V0 = 1.0175; double Rho = -0.5711; // Heston Model Params double InitialStockPrice = 100; double RiskFreeRate = 0.025; // Callibration Settings double accuracy = 0.001; int maxIter = 1000; VarianceProcessParameters varParams = new VarianceProcessParameters(Kappa, Theta, Sigma, V0, Rho); HestonModelParameters hestonModel = new HestonModelParameters(InitialStockPrice, RiskFreeRate, varParams); CalibrationSettings calibrationSettings = new CalibrationSettings(accuracy, maxIter); // Market Data LinkedList <IOptionMarketData <IEuropeanOption> > observedOptions = new LinkedList <IOptionMarketData <IEuropeanOption> >(); EuropeanOption eu1 = new EuropeanOption(80, PayoffType.Call, 1); OptionMarketData <IEuropeanOption> marketData1 = new OptionMarketData <IEuropeanOption>(eu1, 25.72); observedOptions.AddLast(marketData1); EuropeanOption eu2 = new EuropeanOption(90, PayoffType.Call, 1); OptionMarketData <IEuropeanOption> marketData2 = new OptionMarketData <IEuropeanOption>(eu2, 18.93); observedOptions.AddLast(marketData2); EuropeanOption eu3 = new EuropeanOption(80, PayoffType.Call, 2); OptionMarketData <IEuropeanOption> marketData3 = new OptionMarketData <IEuropeanOption>(eu3, 30.49); observedOptions.AddLast(marketData3); EuropeanOption eu4 = new EuropeanOption(100, PayoffType.Call, 2); OptionMarketData <IEuropeanOption> marketData4 = new OptionMarketData <IEuropeanOption>(eu4, 19.36); observedOptions.AddLast(marketData4); EuropeanOption eu5 = new EuropeanOption(100, PayoffType.Call, 1.5); OptionMarketData <IEuropeanOption> marketData5 = new OptionMarketData <IEuropeanOption>(eu5, 16.58); observedOptions.AddLast(marketData5); HestonCalibrationResult result; result = (HestonCalibrationResult) Heston.CalibrateHestonParameters(hestonModel, observedOptions, calibrationSettings); Console.WriteLine("Calibration outcome: {0} and error: {1}", result.MinimizerStatus, result.PricingError); }
public static void Task6() { Console.WriteLine("********Task 6*********"); // Variance Process Values double Kappa = 1.5768; double Theta = 0.398; double Sigma = 0.5751; double V0 = 0.0175; double Rho = -0.5711; // Heston Model Params double InitialStockPrice = 100; double RiskFreeRate = 0.025; // Callibration Settings double accuracy = 1.0e-3; int maxIter = 1000; VarianceProcessParameters varParams = new VarianceProcessParameters(Kappa, Theta, Sigma, V0, Rho); HestonModelParameters hestonModel = new HestonModelParameters(InitialStockPrice, RiskFreeRate, varParams); HestonModelParameters guess = new HestonModelParameters(InitialStockPrice, RiskFreeRate, new VarianceProcessParameters(1.55, 0.88, 1.5999, 0.4, -0.55)); CalibrationSettings calibrationSettings = new CalibrationSettings(accuracy, maxIter); // Market Data LinkedList <IOptionMarketData <IEuropeanOption> > observedOptions = new LinkedList <IOptionMarketData <IEuropeanOption> >(); EuropeanOption eu1 = new EuropeanOption(80, PayoffType.Call, 1); EuropeanOptionFormula eu1Form = new EuropeanOptionFormula(hestonModel, eu1); OptionMarketData <IEuropeanOption> marketData1 = new OptionMarketData <IEuropeanOption>(eu1, eu1Form.Price()); observedOptions.AddLast(marketData1); EuropeanOption eu2 = new EuropeanOption(90, PayoffType.Call, 1); EuropeanOptionFormula eu2Form = new EuropeanOptionFormula(hestonModel, eu2); OptionMarketData <IEuropeanOption> marketData2 = new OptionMarketData <IEuropeanOption>(eu2, eu2Form.Price()); observedOptions.AddLast(marketData2); EuropeanOption eu3 = new EuropeanOption(80, PayoffType.Call, 2); EuropeanOptionFormula eu3Form = new EuropeanOptionFormula(hestonModel, eu3); OptionMarketData <IEuropeanOption> marketData3 = new OptionMarketData <IEuropeanOption>(eu3, eu3Form.Price()); observedOptions.AddLast(marketData3); EuropeanOption eu4 = new EuropeanOption(100, PayoffType.Call, 2); EuropeanOptionFormula eu4Form = new EuropeanOptionFormula(hestonModel, eu4); OptionMarketData <IEuropeanOption> marketData4 = new OptionMarketData <IEuropeanOption>(eu4, eu4Form.Price()); observedOptions.AddLast(marketData4); EuropeanOption eu5 = new EuropeanOption(100, PayoffType.Call, 1.5); EuropeanOptionFormula eu5Form = new EuropeanOptionFormula(hestonModel, eu5); OptionMarketData <IEuropeanOption> marketData5 = new OptionMarketData <IEuropeanOption>(eu5, eu5Form.Price()); observedOptions.AddLast(marketData5); HestonCalibrationResult result; result = (HestonCalibrationResult) Heston.CalibrateHestonParameters(guess, observedOptions, calibrationSettings); Console.WriteLine("Calibration outcome: {0} and error: {1}", result.MinimizerStatus, result.PricingError); }
public static object CalibrateHestonParameters(object guessModelParameters, double riskFreeRate, double underlyingPrice, object strikes, object maturities, object type, object observedPrices, double accuracy, int maxIterations, [ExcelArgument(Description = "Timeout set to 10 mins if not specified")] int timeOutInMinutes) { try { if (ExcelDnaUtil.IsInFunctionWizard()) { return(null); } if (timeOutInMinutes.Equals(0)) { timeOutInMinutes = 10; } double[] strikesArray = ConvertToVector <double>(strikes); double[] maturitiesArray = ConvertToVector <double>(maturities); string[] optTypeArray = ConvertToVector <string>(type); double[] observedPricesArray = ConvertToVector <double>(observedPrices); if (strikesArray.Length != maturitiesArray.Length || maturitiesArray.Length != optTypeArray.Length || optTypeArray.Length != observedPricesArray.Length) { // must improve error message display return(null); } HestonParametersGrading guessHestonModelParams = ParseParameters(guessModelParameters, underlyingPrice, riskFreeRate); if (guessHestonModelParams == null) { return(null); } int numObservedOptions = strikesArray.Length; var marketData = new OptionMarketDataGrading[numObservedOptions]; for (int optionIdx = 0; optionIdx < numObservedOptions; ++optionIdx) { marketData[optionIdx] = new OptionMarketDataGrading( new EuropeanOptionGrading( maturitiesArray[optionIdx], strikesArray[optionIdx], GetTypeOfVanillaEuropean(optTypeArray[optionIdx])), observedPricesArray[optionIdx]); } var calibrationSettings = new CalibrationSettingsGrading(accuracy, maxIterations); var task = Task.Run(() => { var result = Heston.CalibrateHestonParameters(guessHestonModelParams, marketData, calibrationSettings); const int numCols = 2; const int numRows = 7; object[,] output = new object[numRows, numCols]; output[0, 0] = "Kappa"; output[0, 1] = result.Parameters.VarianceParameters.Kappa; output[1, 0] = "Theta"; output[1, 1] = result.Parameters.VarianceParameters.Theta; output[2, 0] = "Sigma"; output[2, 1] = result.Parameters.VarianceParameters.Sigma; output[3, 0] = "Rho"; output[3, 1] = result.Parameters.VarianceParameters.Rho; output[4, 0] = "v0"; output[4, 1] = result.Parameters.VarianceParameters.V0; output[5, 0] = "Minimizer Status"; if (result.MinimizerStatus == CalibrationOutcome.FinishedOK) { output[5, 1] = "OK"; } else if (result.MinimizerStatus == CalibrationOutcome.FailedMaxItReached) { output[5, 1] = "Reached max. num. iterations."; } else if (result.MinimizerStatus == CalibrationOutcome.FailedOtherReason) { output[5, 1] = "Failed."; } else { output[5, 1] = "Unknown outcome."; } output[6, 0] = "Pricing error"; output[6, 1] = result.PricingError; return(output); }); if (task.Wait(TimeSpan.FromMinutes(timeOutInMinutes))) { return(task.Result); } else { throw new Exception($"Timed out ({timeOutInMinutes} mins)"); } } catch (Exception e) { return("CalibrateHestonParameters: unknown error: " + e.Message); } }