Describes the time-dependent Heston optimization problem.
Inheritance: IOptimizationProblem
Example #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="HestonEstimator.HestonCall"/> class.
 /// </summary>
 /// <param name='problem'>
 /// HestonCallOptimizationProblem at which call price calculations are to be linked.
 /// </param>
 /// <param name='x'>
 /// Vector of Heston model parameters.
 /// </param>
 /// <param name='s0'>
 /// Starting value for the stock process.
 /// </param>
 internal HestonCall(HestonCallOptimizationProblem problem, Vector x, double s0)
     : this(problem)
 {
     this.kappa = x[0];
     this.theta = x[1];
     this.sigma = x[2];
     this.rho   = x[3];
     this.v0    = x[4];
     if (x.Length >= 6)
     {
         this.dividend = x[5];
     }
     this.s0 = s0;
 }
Example #2
0
        /// <summary>
        /// Attempts to solve the Heston optimization problem using
        /// <see cref="Heston.HestonOptimizationProblem"/>.
        /// </summary>
        /// <param name="marketData">Data to be used in order to perform the optimization.</param>
        /// <param name="settings">The parameter is not used.</param>
        /// <param name="controller">IController.</param>
        /// <returns>The results of the optimization.</returns>
        public EstimationResult Estimate(List <object> marketData, IEstimationSettings settings = null, IController controller = null, Dictionary <string, object> properties = null)
        {
            DateTime              t0 = DateTime.Now;
            var                   interestDataSet = (CurveMarketData)marketData[0];
            CallPriceMarketData   callDataSet     = (CallPriceMarketData)marketData[1];
            EquityCalibrationData equityCalData   = new EquityCalibrationData(callDataSet, interestDataSet);
            var                   spotPrice       = (DVPLI.MarketDataTypes.Scalar)marketData[2];


            Setup(equityCalData, settings);

            var calSettings = settings as HestonCalibrationSettings;
            // Creates the context.
            Document   doc = new Document();
            ProjectROV prj = new ProjectROV(doc);

            doc.Part.Add(prj);

            // Optimization problem instance.
            Vector matBound    = new Vector(2);
            Vector strikeBound = new Vector(2);

            if (calSettings != null)
            {
                matBound[0]    = calSettings.MinMaturity;
                matBound[1]    = calSettings.MaxMaturity;
                strikeBound[0] = calSettings.MinStrike;
                strikeBound[1] = calSettings.MaxStrike;
            }
            else
            {
                //use defaults
                matBound[0]    = 1.0 / 12; // .25;
                matBound[1]    = 6;        // 10; //Up to 6Y maturities
                strikeBound[0] = 0.4;
                strikeBound[1] = 1.6;
            }
            Console.WriteLine(callDataSet);

            /*
             * //CBA TEST
             * matBound[0] = 1;// .25;
             * matBound[1] = 3.5;// 10; //Up to 6Y maturities
             * strikeBound[0] = 0.5;// 0.5;
             * strikeBound[1] = 2;//1.5;
             */
            HestonCallOptimizationProblem problem = NewOptimizationProblem(equityCalData, matBound, strikeBound);
            int totalOpts = problem.numCall + problem.numPut;

            Console.WriteLine("Calibration based on " + totalOpts + " options. (" + problem.numCall + " call options and " + problem.numPut + " put options).");

            IOptimizationAlgorithm solver = new  QADE();
            //IOptimizationAlgorithm solver = new MultiLevelSingleLinkage();
            IOptimizationAlgorithm solver2 = new SteepestDescent();

            DESettings o = new DESettings();

            o.controller = controller;

            // If true the optimization algorithm will operate in parallel.
            o.Parallel = Engine.MultiThread;
            o.h        = 10e-8;
            o.epsilon  = 10e-8;

            SolutionInfo solution = null;

            double minObj = double.MaxValue;
            Vector minX   = null;
            int    Z      = 1;

            //if (problem.GetType() == typeof(Heston.HestonCallSimulationOptimizationProblem))
            //    Z = 2;

            for (int z = 0; z < Z; z++)
            {
                if (solver.GetType() == typeof(MultiLevelSingleLinkage))
                {
                    o.NP       = 50;
                    o.MaxIter  = 25;
                    o.MaxGamma = 6;
                }
                else
                {
                    o.NP      = 60;
                    o.MaxIter = 35;
                }
                o.Verbosity = 1;
                Vector x0 = null;// new Vector(new double[] { 0.5, 0.5, 0.8, -0.5, 0.05 });

                // GA
                solution = solver.Minimize(problem, o, x0);
                if (solution.errors)
                {
                    return(null);
                }

                o.options = "qn";
                o.MaxIter = 500;// 1000;

                if (solution != null)
                {
                    solution = solver2.Minimize(problem, o, solution.x);
                }
                else
                {
                    solution = solver2.Minimize(problem, o, x0);
                }
                if (solution.errors)
                {
                    return(null);
                }

                if (solution.obj < minObj)
                {
                    minObj = solution.obj;
                    minX   = solution.x.Clone();
                }
            }



            solution.obj = minObj;
            solution.x   = minX;

            //Displays pricing error structure
            HestonCallOptimizationProblem.displayObjInfo = true;
            problem.Obj(solution.x);
            HestonCallOptimizationProblem.displayObjInfo = false;
            Console.WriteLine("Calibration Time (s)\t" + (DateTime.Now - t0).TotalSeconds);

            return(BuildEstimate(spotPrice, interestDataSet, callDataSet, equityCalData, solution));
        }
Example #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="HestonEstimator.HestonCall"/> class.
 /// </summary>
 /// <param name='problem'>
 /// HestonCallOptimizationProblem at which call price calculations are to be linked.
 /// </param>
 /// <param name='x'>
 /// Vector of Heston model parameters.
 /// </param>
 /// <param name='s0'>
 /// Starting value for the stock process.
 /// </param>
 internal HestonCall(HestonCallOptimizationProblem problem, Vector x, double s0)
     : this(problem)
 {
     this.kappa = x[0];
     this.theta = x[1];
     this.sigma = x[2];
     this.rho = x[3];
     this.v0 = x[4];
     if(x.Length>=6)
         this.dividend = x[5];
     this.s0 = s0;
 }
Example #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="HestonEstimator.HestonCall"/> class.
 /// </summary>
 /// <param name='problem'>
 /// HestonCallOptimizationProblem at which call price calculations are to be linked.
 /// </param>
 internal HestonCall(HestonCallOptimizationProblem problem)
 {
     hestonCallPrice = new Matrix(problem.callMarketPrice.R, problem.callMarketPrice.C);
     hestonPutPrice = new Matrix(problem.callMarketPrice.R, problem.callMarketPrice.C);
 }
        public static void Main(string[] args)
        {
            int Caso = 1;

            if (Caso == 0)
            {
                InterestRateMarketData MData   = InterestRateMarketData.FromFile("../../../TestData/InterestRatesModels/05052009-EU.xml");
                CallPriceMarketData    test    = CallPriceMarketData.FromFile("../../../TestData/Heston/05052009-SX5E-HestonData.xml");
                EquityCalibrationData  CalData = new EquityCalibrationData(test, MData);

                Matrix CallMarketPrice = (Matrix)test.CallPrice;
                Vector Maturity        = (Vector)test.Maturity;
                Vector Strike          = (Vector)test.Strike;
                Vector DividendYield   = (Vector)test.DividendYield;
                Vector Drift           = CalData.Rate - CalData.DividendYield;
                Vector Rate            = CalData.Rate;

                double u, kappa, theta, sigma, rho, v0, s0, r, q, T, K, val;
                u     = 1.0;
                kappa = 19.4;
                theta = 0.235;
                sigma = 0.00500999;
                rho   = -0.96;
                v0    = 0.664;
                s0    = 3872.15;
                r     = -0.0867303549580581;
                q     = 0;
                T     = 0.50;
                K     = 6000;
                Vector MatBound    = new Vector(2);
                Vector StrikeBound = new Vector(2);
                MatBound[0]    = 0.0;
                MatBound[1]    = 2.0;
                StrikeBound[0] = 0.7;
                StrikeBound[1] = 1.3;
                Matrix Volatility = new Matrix(test.CallPrice.R, test.CallPrice.C);
                HestonCallOptimizationProblem HP = new HestonCallOptimizationProblem(CallMarketPrice, Maturity, Strike, Rate, DividendYield, test.S0, MatBound, StrikeBound, Volatility);
                Complex Cval, Cu;
                Cu = u - Complex.I;
                HestonCall hc = new HestonCall(HP);

                Cval = hc.phi(u, kappa, theta, sigma, rho, s0, v0, r, T);
                Console.WriteLine("phi1 = {0}", Cval);
                Cval = hc.phi(Cu, kappa, theta, sigma, rho, s0, v0, r, T);
                Console.WriteLine("phi2 = {0}", Cval);
                val = hc.IntegrandFunc(u, kappa, theta, sigma, rho, s0, v0, r, q, T, K);
                Console.WriteLine("IntFunc = {0}", val);

                Vector x = new Vector(5);
                x[0] = kappa;
                x[1] = theta;
                x[2] = sigma;
                x[3] = rho;
                x[4] = v0;

                DateTime T1, T2;
                TimeSpan ElapsedTime;
                double   Time, Time2, Time3;

                T1          = DateTime.Now;
                val         = hc.HestonCallPrice(x, s0, T, K, r, q);
                T2          = DateTime.Now;
                ElapsedTime = T2 - T1;
                Time        = (double)ElapsedTime.Milliseconds;
                Time2       = (double)ElapsedTime.Seconds;
                Console.WriteLine("Price = {0}", val);
                Console.WriteLine("Elapsed Time = {0}", Time2 + Time / 1000);

                int    NProve = 10;
                int    NPassi = 1000;
                double val2;
                Random CasNum = new Random();
                for (int i = 0; i < NProve; i++)
                {
                    for (int j = 0; j < 5; j++)
                    {
                        val2 = ((double)CasNum.Next(0, NPassi)) / ((double)NPassi);
                        x[j] = HP.Bounds.Lb[j] + (HP.Bounds.Ub[j] - HP.Bounds.Lb[j]) * val2;
                    }
                    Console.Write("Trial {0}  x = " + x.ToString(), i + 1);
                    T1          = DateTime.Now;
                    val         = HP.Obj(x);
                    T2          = DateTime.Now;
                    ElapsedTime = T2 - T1;
                    Time        = (double)ElapsedTime.Milliseconds;
                    Time2       = (double)ElapsedTime.Seconds;
                    Time3       = (double)ElapsedTime.Minutes;
                    Console.WriteLine("  Time = {0}' {1}'' Val = {2}", Time3, Time2 + Time / 1000, val);
                }
            }
            if (Caso == 1)
            {
                TestHestonCallEstimation NewTest = new TestHestonCallEstimation();
                bool Result = NewTest.Run();
            }
        }
Example #6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="HestonEstimator.HestonCall"/> class.
 /// </summary>
 /// <param name='problem'>
 /// HestonCallOptimizationProblem at which call price calculations are to be linked.
 /// </param>
 internal HestonCall(HestonCallOptimizationProblem problem)
 {
     hestonCallPrice = new Matrix(problem.callMarketPrice.R, problem.callMarketPrice.C);
     hestonPutPrice  = new Matrix(problem.callMarketPrice.R, problem.callMarketPrice.C);
 }