Esempio n. 1
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");
        }
Esempio n. 2
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);
        }