Exemplo n.º 1
0
        public void CanSolveTameQuadratic2dFact()
        {
            var output         = NelderMead.MethodSolve(QuadraticFunction2d, new double[] { 0, 0 }, new double[] { 0.5, 0.5 }, 1e-8, 10000);
            var functionOutput = QuadraticFunction2d(output);

            Assert.Equal(0, functionOutput, 4);
        }
Exemplo n.º 2
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],
            });
        }
Exemplo n.º 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],
            });
        }