// Objective function ===========================================================================
        public double f(double[] param, OFSet ofset)
        {
            Bisection   BA   = new Bisection();
            HestonPrice HP   = new HestonPrice();
            double      S    = ofset.opsettings.S;
            double      r    = ofset.opsettings.r;
            double      q    = ofset.opsettings.q;
            int         trap = ofset.opsettings.trap;

            double[,] MktIV    = ofset.data.MktIV;
            double[,] MktPrice = ofset.data.MktPrice;
            string[,] PutCall  = ofset.data.PutCall;
            double[] K = ofset.data.K;
            double[] T = ofset.data.T;

            int NK = PutCall.GetLength(0);
            int NT = PutCall.GetLength(1);

            HParam param2 = new HParam();

            param2.kappa = param[0];
            param2.theta = param[1];
            param2.sigma = param[2];
            param2.v0    = param[3];
            param2.rho   = param[4];

            // Settings for the Bisection algorithm
            double a       = 0.001;
            double b       = 3.0;
            double Tol     = 1e5;
            int    MaxIter = 10000;

            // Initialize the model price and model implied vol vectors, and the objective function value
            double[,] ModelPrice = new double[NK, NT];
            double[,] ModelIV    = new double[NK, NT];
            double Vega  = 0.0;
            double Error = 0.0;
            double pi    = Math.PI;

            double[] lb = ofset.lb;
            double[] ub = ofset.ub;

            double kappaLB = lb[0]; double kappaUB = ub[0];
            double thetaLB = lb[1]; double thetaUB = ub[1];
            double sigmaLB = lb[2]; double sigmaUB = ub[2];
            double v0LB = lb[3]; double v0UB = ub[3];
            double rhoLB = lb[4]; double rhoUB = ub[4];

            int LossFunction = ofset.LossFunction;

            double[] X = ofset.X;
            double[] W = ofset.W;

            // Penalty for inadmissible parameter values
            if ((param2.kappa <= kappaLB) || (param2.theta <= thetaLB) || (param2.sigma <= sigmaLB) || (param2.v0 <= v0LB) || (param2.rho <= rhoLB) ||
                (param2.kappa >= kappaUB) || (param2.theta >= thetaUB) || (param2.sigma >= sigmaUB) || (param2.v0 >= v0UB) || (param2.rho >= rhoUB))
            {
                Error = 1e50;
            }
            else
            {
                for (int k = 0; k < NK; k++)
                {
                    for (int t = 0; t < NT; t++)
                    {
                        ModelPrice[k, t] = HP.HestonPriceGaussLaguerre(param2, S, K[k], r, q, T[t], trap, PutCall[k, t], X, W);
                        switch (LossFunction)
                        {
                        case 1:
                            // MSE Loss Function
                            Error += Math.Pow(ModelPrice[k, t] - MktPrice[k, t], 2) / Convert.ToDouble(NT * NK);
                            break;

                        case 2:
                            // RMSE Loss Function
                            Error += Math.Pow(ModelPrice[k, t] - MktPrice[k, t], 2) / MktPrice[k, t] / Convert.ToDouble(NT * NK);
                            break;

                        case 3:
                            // IVMSE Loss Function
                            ModelIV[k, t] = BA.BisecBSIV(PutCall[k, t], S, K[k], r, q, T[t], a, b, ModelPrice[k, t], Tol, MaxIter);
                            Error        += Math.Pow(ModelIV[k, t] - MktIV[k, t], 2) / Convert.ToDouble(NT * NK);;
                            break;

                        case 4:
                            // IVRMSE Christoffersen, Heston, Jacobs proxy
                            double d       = (Math.Log(S / K[k]) + (r - q + MktIV[k, t] * MktIV[k, t] / 2.0) * T[t]) / MktIV[k, t] / Math.Sqrt(T[t]);
                            double NormPDF = Math.Exp(-0.5 * d * d) / Math.Sqrt(2.0 * pi);
                            Vega   = S * NormPDF * Math.Sqrt(T[t]);
                            Error += Math.Pow(ModelPrice[k, t] - MktPrice[k, t], 2) / Vega / Vega / Convert.ToDouble(NT * NK);
                            break;
                        }
                    }
                }
            }
            return(Error);
        }
        static void Main(string[] args)
        {
            // 32-point Gauss-Laguerre Abscissas and weights
            double[] X = new Double[32];
            double[] W = new Double[32];
            using (TextReader reader = File.OpenText("../../GaussLaguerre32.txt"))
            {
                for (int k = 0; k <= 31; k++)
                {
                    string   text = reader.ReadLine();
                    string[] bits = text.Split(' ');
                    X[k] = double.Parse(bits[0]);
                    W[k] = double.Parse(bits[1]);
                }
            }

            // Bounds on the parameter estimates
            // kappa theta sigma v0 rho
            double e = 1e-5;

            double[] lb = new double[5] {
                e, e, e, e, -0.99
            };
            double[] ub = new double[5] {
                20.0, 2.0, 2.0, 2.0, 0.99
            };

            // Option settings
            OPSet opsettings;

            opsettings.S    = 137.14;
            opsettings.r    = 0.0010;
            opsettings.q    = 0.0068;
            opsettings.trap = 1;

            // Read in SP500 implied volatilities
            int NT = 4;
            int NK = 7;

            double[,] MktIV = new Double[7, 4] {
                { 0.2780, 0.2638, 0.2532, 0.2518 }, { 0.2477, 0.2402, 0.2364, 0.2369 },
                { 0.2186, 0.2158, 0.2203, 0.2239 }, { 0.1878, 0.1930, 0.2047, 0.2098 },
                { 0.1572, 0.1712, 0.1894, 0.1970 }, { 0.1334, 0.1517, 0.1748, 0.1849 },
                { 0.1323, 0.1373, 0.1618, 0.1736 }
            };
            double[] K = new Double[7] {
                120.0, 125.0, 130.0, 135.0, 140.0, 145.0, 150.0
            };
            double[] T = new Double[4] {
                0.123287671232877, 0.268493150684932, 0.715068493150685, 0.953424657534247
            };

            // PutCall identifiers
            string[,] PutCall = new String[NK, NT];
            for (int k = 0; k <= NK - 1; k++)
            {
                for (int t = 0; t <= NT - 1; t++)
                {
                    PutCall[k, t] = "C";
                }
            }

            // Obtain the market prices
            BlackScholesPrice BS = new BlackScholesPrice();

            double[,] MktPrice = new Double[NK, NT];
            for (int k = 0; k <= NK - 1; k++)
            {
                for (int t = 0; t <= NT - 1; t++)
                {
                    MktPrice[k, t] = BS.BlackScholes(opsettings.S, K[k], T[t], opsettings.r, opsettings.q, MktIV[k, t], PutCall[k, t]);
                }
            }

            // Place the market data in the structure
            MktData data = new MktData();

            data.MktIV    = MktIV;
            data.MktPrice = MktPrice;
            data.K        = K;
            data.T        = T;
            data.PutCall  = PutCall;

            // Settings for the objective function
            OFSet ofsettings;

            ofsettings.opsettings   = opsettings;
            ofsettings.data         = data;
            ofsettings.X            = X;
            ofsettings.W            = W;
            ofsettings.LossFunction = 1;
            ofsettings.lb           = lb;
            ofsettings.ub           = ub;

            // Settings for the Nelder Mead algorithm
            NMSet nmsettings;

            nmsettings.N          = 5;              // Number of Heston parameters
            nmsettings.MaxIters   = 1000;           // Maximum number of iterations
            nmsettings.Tolerance  = 1e-3;           // Tolerance on best and worst function values
            nmsettings.ofsettings = ofsettings;

            // Starting values (vertices) in vector form.  Add random increment about each starting value
            double kappaS = 9.00;
            double thetaS = 0.05;
            double sigmaS = 0.30;
            double v0S    = 0.05;
            double rhoS   = -0.80;
            int    N      = nmsettings.N;

            double[,] x = new double[N, N + 1];
            for (int j = 0; j <= N; j++)
            {
                x[0, j] = kappaS + RandomNum(-0.01, 0.01);
                x[1, j] = thetaS + RandomNum(-0.01, 0.01);
                x[2, j] = sigmaS + RandomNum(-0.01, 0.01);
                x[3, j] = v0S + RandomNum(-0.01, 0.01);
                x[4, j] = rhoS + RandomNum(-0.01, 0.01);
            }

            // Obtain the parameter estimates
            NelderMeadAlgo    NM = new NelderMeadAlgo();
            ObjectiveFunction OF = new ObjectiveFunction();

            double[] B = NM.NelderMead(OF.f, nmsettings, x);

            // Output the estimation result
            Console.WriteLine("  ");
            Console.WriteLine("Parameter Estimates --------------------");
            Console.WriteLine("  ");
            Console.WriteLine("kappa   =  {0:F5}", B[0]);
            Console.WriteLine("theta   =  {0:F5}", B[1]);
            Console.WriteLine("sigma   =  {0:F5}", B[2]);
            Console.WriteLine("v0      =  {0:F5}", B[3]);
            Console.WriteLine("rho     =  {0:F5}", B[4]);
            Console.WriteLine("  ");
            Console.WriteLine("Value of the objective function is  {0:F5}", B[5]);
            Console.WriteLine("  ");
            Console.WriteLine("Number of iterations required       {0:0}", B[6]);
            Console.WriteLine("  ");
            Console.WriteLine("----------------------------------------");

            // Obtain the model prices and model implied volatilities
            HParam paramNM = new HParam();

            paramNM.kappa = B[0];
            paramNM.theta = B[1];
            paramNM.sigma = B[2];
            paramNM.v0    = B[3];
            paramNM.rho   = B[4];

            // Settings for bisection algorithm
            double a       = 0.01;
            double b       = 3.00;
            double Tol     = 1e-4;
            int    MaxIter = 1000;

            double[,] ModelPrice = new double[NK, NT];
            double[,] ModelIV    = new double[NK, NT];
            double IVMSE = 0.0;

            HestonPrice HP = new HestonPrice();
            Bisection   BA = new Bisection();

            for (int k = 0; k < NK; k++)
            {
                for (int t = 0; t < NT; t++)
                {
                    ModelPrice[k, t] = HP.HestonPriceGaussLaguerre(paramNM, opsettings.S, K[k], opsettings.r, opsettings.q, T[t], opsettings.trap, PutCall[k, t], X, W);
                    ModelIV[k, t]    = BA.BisecBSIV(PutCall[k, t], opsettings.S, K[k], opsettings.r, opsettings.q, T[t], a, b, ModelPrice[k, t], Tol, MaxIter);
                    IVMSE           += Math.Pow(MktIV[k, t] - ModelIV[k, t], 2) / Convert.ToDouble(NT * NK);
                }
            }

            // Output the results
            Console.Write("MSE between model and market implied vols  = {0}", IVMSE);
            Console.WriteLine("  ");
            Console.WriteLine("----------------------------------------");
            Console.WriteLine("Market implied volatilities");
            Console.WriteLine("  ");
            for (int k = 0; k < NK; k++)
            {
                for (int t = 0; t < NT; t++)
                {
                    if (t < NT - 1)
                    {
                        Console.Write("{0:F4}   ", MktIV[k, t]);
                    }
                    else
                    {
                        Console.WriteLine("{0:F4}   ", MktIV[k, t]);
                    }
                }
            }

            Console.WriteLine("  ");
            Console.WriteLine("----------------------------------------");
            Console.WriteLine("Model implied volatilities");
            Console.WriteLine("  ");
            for (int k = 0; k < NK; k++)
            {
                for (int t = 0; t < NT; t++)
                {
                    if (t < NT - 1)
                    {
                        Console.Write("{0:F4}   ", ModelIV[k, t]);
                    }
                    else
                    {
                        Console.WriteLine("{0:F4}   ", ModelIV[k, t]);
                    }
                }
            }

            Console.WriteLine("----------------------------------------");
        }