Example #1
0
        public SviRawParameters SolveSviRaw(ATMStraddleConstraint atmConstraint, RRBFConstraint[] smileConstraints, DateTime buildDate,
                                            DateTime expiry, double fwd, bool vegaWeightedFit = true)
        {
            _atmConstraint    = atmConstraint;
            _smileConstraints = smileConstraints;

            _numberOfConstraints = smileConstraints.Length * 2 + 1;
            _vegaWeighted        = vegaWeightedFit;

            _fwd       = fwd;
            _buildDate = buildDate;
            _tExp      = (expiry - buildDate).TotalDays / 365.0;

            var startingPoint = new[] { atmConstraint.MarketVol *atmConstraint.MarketVol *_tExp - Sqrt(atmConstraint.MarketVol), 1.0, smileConstraints.Average(x => x.RisykVol) >= 0 ? 0.1 : -0.1, 0, Sqrt(atmConstraint.MarketVol) };
            //var startingPoint = new[] { atmConstraint.MarketVol* atmConstraint.MarketVol *_tExp-Sqrt(atmConstraint.MarketVol), 0.5, smileConstraints.Average(x => x.RisykVol) >= 0 ? 0.25 : -0.25, 0, Sqrt(atmConstraint.MarketVol) };
            var initialStep = new[] { atmConstraint.MarketVol *atmConstraint.MarketVol, 0.5, 0.5, 0.002, Sqrt(atmConstraint.MarketVol) / 2 };

            //var startingPoint = new[] { atmConstraint.MarketVol, 1.0, 0.1, 0, 0.1 };
            //var initialStep = new[] { 0.1, 0.25, 0.25, 0.01, 0.1 };


            var currentError = new Func <double[], double>(x =>
            {
                var currentSVI = new SviRawParameters
                {
                    A     = x[0],
                    B     = x[1],
                    Rho   = x[2],
                    M     = x[3],
                    Sigma = x[4],
                };

                var e = ComputeErrorsSviRaw(currentSVI);
                return(Sqrt(e.Sum()));
            });

            SetupConstraints();

            var optimal = NelderMead.MethodSolve(currentError, startingPoint, initialStep, 1e-10, 50000);

            return(new SviRawParameters
            {
                A = optimal[0],
                B = optimal[1],
                Rho = optimal[2],
                M = optimal[3],
                Sigma = optimal[4],
            });
        }
Example #2
0
        public double[] Solve(ATMStraddleConstraint atmConstraint, RRBFConstraint[] smileConstraints, DateTime buildDate,
                              DateTime expiry, double fwd, double[] strikesToFit, Interpolator1DType interpType)
        {
            _atmConstraint    = atmConstraint;
            _smileConstraints = smileConstraints;

            _numberOfConstraints = smileConstraints.Length * 2 + 1;

            if (strikesToFit.Length != _numberOfConstraints)
            {
                throw new Exception($"{_numberOfConstraints} constraints provided to fit {strikesToFit.Length} strikes");
            }

            _fwd        = fwd;
            _buildDate  = buildDate;
            _tExp       = (expiry - buildDate).TotalDays / 365.0;
            _interpType = interpType;
            _strikes    = strikesToFit;

            if (TrivialSolution(out var vols))
            {
                return(vols);
            }

            _currentGuess = Enumerable.Repeat(atmConstraint.MarketVol, _numberOfConstraints).ToArray();

            SetupConstraints();

            _currentErrors = ComputeErrors(_currentGuess);

            ComputeJacobian();

            for (var i = 0; i < MaxItterations; i++)
            {
                ComputeNextGuess();
                NaNCheck();
                _currentErrors = ComputeErrors(_currentGuess);
                if (_currentErrors.Max(x => Abs(x)) < Tollerance)
                {
                    UsedItterations = i + 1;
                    break;
                }
                ComputeJacobian();
            }

            return(_currentGuess);
        }
Example #3
0
        public SABRParameters SolveSABR(ATMStraddleConstraint atmConstraint, RRBFConstraint[] smileConstraints, DateTime buildDate,
                                        DateTime expiry, double fwd, double beta = 1.0, bool vegaWeightedFit = true)
        {
            _atmConstraint    = atmConstraint;
            _smileConstraints = smileConstraints;

            _numberOfConstraints = smileConstraints.Length * 2 + 1;
            _vegaWeighted        = vegaWeightedFit;

            _fwd       = fwd;
            _buildDate = buildDate;
            _tExp      = (expiry - buildDate).TotalDays / 365.0;

            var startingPoint = new[] { atmConstraint.MarketVol, Sqrt(atmConstraint.MarketVol), smileConstraints.Average(x => x.RisykVol) >= 0 ? 0.1 : -0.1 };
            var initialStep   = new[] { 0.1, 0.25, 0.25 };

            var currentError = new Func <double[], double>(x =>
            {
                var currentSABR = new SABRParameters
                {
                    Alpha = x[0],
                    Beta  = beta,
                    Nu    = x[1],
                    Rho   = x[2],
                };

                var e = ComputeErrorsSABR(currentSABR);
                return(e.Sum(ee => ee * ee));
            });

            SetupConstraints();

            var optimal = NelderMead.MethodSolve(currentError, startingPoint, initialStep, 1e-8, 10000);

            return(new SABRParameters
            {
                Alpha = optimal[0],
                Beta = beta,
                Nu = optimal[1],
                Rho = optimal[2],
            });
        }