Esempio n. 1
0
        public double ObjFun(Vector <double> x)
        {
            var xMatrix = VecToMat(x);

            PopulateXi(xMatrix);
            double temp = Bound(x);

            if (temp > 0 && !ExpForm)
            {
                return(temp);
            }
            if (_param.NumberOfCaplets > 0)
            {
                temp += CWeight * QOFCplt();
            }
            if (_param.NumberOfSwaptions > 0)
            {
                temp += SWeight * QOFSwpn();
            }
            temp         += HWeight * SmoothH(xMatrix) + VWeight * SmoothV(xMatrix);
            FunctionValue = temp;
            _iterationCount++;
            Pedersen.WriteRange(1, 1, $"{_iterationCount}, {temp:f9}, {_hit}");
            return(temp);
        }
Esempio n. 2
0
        public double Go()
        {
            _discount = _economy.Discount;
            _xi       = _economy.Xi;
            if (_xi == null)
            {
                throw new System.Exception("Run Calibrator first!");
            }
            _shift = _economy.Shift;
            SetV(_param.UnderlyingExpiry, _param.UnderlyingTenor);
            if (Derivative == Derivative.Custom)
            {
                Payoff = new PayoffParser(_economy, PayoffFunction);
            }
            double temp  = 0;
            double temp2 = 0;
            var    p     = new[] { Exp, Ten };
            double k     = SetStrike(p);

            for (int i = 0; i < Iterate; i++)
            {
                if (i % 100 == 0)
                {
                    Pedersen.WriteRange($"Iteration # {i}");
                }
                Simulate(_param.UnderlyingExpiry, _param.UnderlyingTenor);
                double c = FindPayoff(p, k);
                temp  += c;
                temp2 += c * c;
            }
            double actual = Notional * FindTheoretical(p, k);
            double mean   = Notional * temp / Iterate;
            double sd     = Notional * System.Math.Sqrt((temp2 / Iterate - (temp / Iterate) * (temp / Iterate)) / (Iterate - 1.0));
            double error  = (mean - actual) / sd;

            if (Derivative == Derivative.Caplet)
            {
                Pedersen.Write($"Caplet: {Exp}, ", "sim");
            }
            else if (Derivative == Derivative.Swaption)
            {
                Pedersen.Write($"Swaption: ({Exp}, {Ten}), ", "sim");
            }
            if (actual == 0)
            {
                Pedersen.Write($"Notional: {Notional}, Iterations: {Iterate}\n", "sim");
                Pedersen.Write("Theoretical: N/A\n", "sim");
                Pedersen.Write($"Sim Average: {mean}\n", "sim");
                Pedersen.Write($"Sim Std.Dev: {sd}\n", "sim");
                Pedersen.Write("Error / S.D: N/A\n", "sim");
            }
            else
            {
                Pedersen.Write($"Notional: {Notional}, Iterations: {Iterate}\n", "sim");
                Pedersen.Write($"Theoretical: {actual}\n", "sim");
                Pedersen.Write($"Sim Average: {mean}\n", "sim");
                Pedersen.Write($"Sim Std.Dev: {sd}\n", "sim");
                Pedersen.Write($"Error / S.D: {error}\n", "sim");
            }
            return(mean);
        }