Пример #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);
        }
Пример #2
0
 private void SetV(int expiry, int tenor)
 {
     _v    = new double[expiry + 1][];
     _v[0] = new double[tenor + 1];
     for (int i = 0; i < expiry; i++)
     {
         _v[i + 1] = new double[tenor + 1];
     }
     for (int i = 0; i < tenor; i++)
     {
         Pedersen.Debug(_discount[0][i].ToString(CultureInfo.InvariantCulture));
         _v[0][i] = _discount[0][i] - (1 - 0.25 * _shift[i]) * _discount[0][i + 1];
     }
     _v[0][tenor] = _discount[0][tenor];
 }
Пример #3
0
        public void Go()
        {
            _initialGuess = new DenseVector(_p.NumberOfExpiries * _p.NumberOfTenors);
            double initialGuess = _p.AverageSwaptionImpliedVolatility;

            if (initialGuess == 0)
            {
                initialGuess = _p.AverageCapletImpliedVolatility;
            }
            if (initialGuess == 0)
            {
                throw new Exception("The selected time range does not include any calibration targets.");
            }
            if (_objective.ExpForm)
            {
                for (var i = 0; i < _initialGuess.Count; i++)
                {
                    _initialGuess[i] = Math.Log(initialGuess) / 20;
                }
            }
            else
            {
                for (var i = 0; i < _initialGuess.Count; i++)
                {
                    _initialGuess[i] = initialGuess;
                }
            }
            var sw = new StopWatch();

            _objective.StartOtherThreads();
            var solution = BfgsSolver.Solve(_initialGuess, _f, _g);

            _objective.Finished = true;
            sw.Stop();
            TimeSpan ts = sw.GetElapsedTime();

            Pedersen.Write($"  Value x: {solution[0]}\n", "cal");
            _objective.OutputResult(solution);
            Pedersen.Write($"  Process Time: {ts.Hours:d2}:{ts.Minutes:d2}:{ts.Seconds:d2}:{ts.Milliseconds:d3}\n", "cal");
        }
Пример #4
0
        public double Go()
        {
            _discount = _economy.Discount;
            _xi       = _economy.Xi;
            if (_xi == null)
            {
                throw new 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 * 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);
        }