public SABR(double t, double forward, double alpha, double beta, double nu, double rho,
             bool alphaIsFixed, bool betaIsFixed, bool nuIsFixed, bool rhoIsFixed,
             bool vegaWeighted                         = false,
             EndCriteria endCriteria                   = null,
             OptimizationMethod optMethod              = null,
             double errorAccept                        = 0.0020, bool useMaxError = false, int maxGuesses = 50, double shift = 0.0,
             VolatilityType volatilityType             = VolatilityType.ShiftedLognormal,
             SabrApproximationModel approximationModel = SabrApproximationModel.Hagan2002)
 {
     t_                  = t;
     forward_            = forward;
     alpha_              = alpha;
     beta_               = beta;
     nu_                 = nu;
     rho_                = rho;
     alphaIsFixed_       = alphaIsFixed;
     betaIsFixed_        = betaIsFixed;
     nuIsFixed_          = nuIsFixed;
     rhoIsFixed_         = rhoIsFixed;
     vegaWeighted_       = vegaWeighted;
     endCriteria_        = endCriteria;
     optMethod_          = optMethod;
     errorAccept_        = errorAccept;
     useMaxError_        = useMaxError;
     maxGuesses_         = maxGuesses;
     shift_              = shift;
     volatilityType_     = volatilityType;
     approximationModel_ = approximationModel;
 }
Esempio n. 2
0
        public CubicBSplinesFitting(List <double> knots, bool constrainAtZero = true, Vector weights = null,
                                    OptimizationMethod optimizationMethod     = null)
            : base(constrainAtZero, weights, optimizationMethod)
        {
            splines_ = new BSpline(3, knots.Count - 5, knots);

            Utils.QL_REQUIRE(knots.Count >= 8, () => "At least 8 knots are required");
            int basisFunctions = knots.Count - 4;

            if (constrainAtZero)
            {
                size_ = basisFunctions - 1;

                // Note: A small but nonzero N_th basis function at t=0 may
                // lead to an ill conditioned problem
                N_ = 1;

                Utils.QL_REQUIRE(Math.Abs(splines_.value(N_, 0.0)) > Const.QL_EPSILON, () =>
                                 "N_th cubic B-spline must be nonzero at t=0");
            }
            else
            {
                size_ = basisFunctions;
                N_    = 0;
            }
        }
Esempio n. 3
0
 public SVI(double t, double forward, double a, double b, double sigma, double rho, double m,
            bool aIsFixed, bool bIsFixed, bool sigmaIsFixed, bool rhoIsFixed, bool mIsFixed,
            bool vegaWeighted            = false,
            EndCriteria endCriteria      = null,
            OptimizationMethod optMethod = null,
            double errorAccept           = 0.0020, bool useMaxError = false, int maxGuesses = 50, List <double?> addParams = null)
 {
     t_            = t;
     forward_      = forward;
     a_            = a;
     b_            = b;
     sigma_        = sigma;
     rho_          = rho;
     m_            = m;
     aIsFixed_     = aIsFixed;
     bIsFixed_     = bIsFixed;
     sigmaIsFixed_ = sigmaIsFixed;
     rhoIsFixed_   = rhoIsFixed;
     mIsFixed_     = mIsFixed;
     vegaWeighted_ = vegaWeighted;
     endCriteria_  = endCriteria;
     optMethod_    = optMethod;
     errorAccept_  = errorAccept;
     useMaxError_  = useMaxError;
     maxGuesses_   = maxGuesses;
     addParams_    = addParams;
 }
 public NonLinearLeastSquare(Constraint c, double accuracy, int maxiter)
 {
     exitFlag_      = -1;
     accuracy_      = accuracy;
     maxIterations_ = maxiter;
     om_            = new ConjugateGradient();
     c_             = c;
 }
 //! Default constructor
 public NonLinearLeastSquare(Constraint c, double accuracy, int maxiter, OptimizationMethod om)
 {
     exitFlag_      = -1;
     accuracy_      = accuracy;
     maxIterations_ = maxiter;
     om_            = om;
     c_             = c;
 }
Esempio n. 6
0
 public SimplePolynomialFitting(int degree,
                                bool constrainAtZero = true,
                                Vector weights       = null,
                                OptimizationMethod optimizationMethod = null)
     : base(constrainAtZero, weights, optimizationMethod)
 {
     size_ = constrainAtZero ? degree : degree + 1;
 }
Esempio n. 7
0
        public SwaptionVolCube1x(Handle <SwaptionVolatilityStructure> atmVolStructure,
                                 List <Period> optionTenors,
                                 List <Period> swapTenors,
                                 List <double> strikeSpreads,
                                 List <List <Handle <Quote> > > volSpreads,
                                 SwapIndex swapIndexBase,
                                 SwapIndex shortSwapIndexBase,
                                 bool vegaWeightedSmileFit,
                                 List <List <Handle <Quote> > > parametersGuess,
                                 List <bool> isParameterFixed,
                                 bool isAtmCalibrated,
                                 EndCriteria endCriteria      = null,
                                 double?maxErrorTolerance     = null,
                                 OptimizationMethod optMethod = null,
                                 double?errorAccept           = null,
                                 bool useMaxError             = false,
                                 int maxGuesses      = 50,
                                 bool backwardFlat   = false,
                                 double cutoffStrike = 0.0001)
            : base(atmVolStructure, optionTenors, swapTenors, strikeSpreads, volSpreads, swapIndexBase,
                   shortSwapIndexBase, vegaWeightedSmileFit)
        {
            parametersGuessQuotes_ = parametersGuess;
            isParameterFixed_      = isParameterFixed;
            isAtmCalibrated_       = isAtmCalibrated;
            endCriteria_           = endCriteria;
            optMethod_             = optMethod;
            useMaxError_           = useMaxError;
            maxGuesses_            = maxGuesses;
            backwardFlat_          = backwardFlat;
            cutoffStrike_          = cutoffStrike;

            if (maxErrorTolerance != null)
            {
                maxErrorTolerance_ = maxErrorTolerance.Value;
            }
            else
            {
                maxErrorTolerance_ = SWAPTIONVOLCUBE_TOL;
                if (vegaWeightedSmileFit_)
                {
                    maxErrorTolerance_ = SWAPTIONVOLCUBE_VEGAWEIGHTED_TOL;
                }
            }
            if (errorAccept != null)
            {
                errorAccept_ = errorAccept.Value;
            }
            else
            {
                errorAccept_ = maxErrorTolerance_ / 5.0;
            }

            privateObserver_ = new PrivateObserver(this);
            registerWithParametersGuess();
            setParameterGuess();
        }
 //! constructor
 protected FittingMethod(bool constrainAtZero = true,
                         Vector weights       = null,
                         OptimizationMethod optimizationMethod = null)
 {
     constrainAtZero_    = constrainAtZero;
     weights_            = weights ?? new Vector();
     calculateWeights_   = weights_.empty();
     optimizationMethod_ = optimizationMethod;
 }
Esempio n. 9
0
        // to constrained <- from unconstrained
        public AbcdCalibration(List <double> t,
                               List <double> blackVols,
                               double aGuess             = -0.06,
                               double bGuess             = 0.17,
                               double cGuess             = 0.54,
                               double dGuess             = 0.17,
                               bool aIsFixed             = false,
                               bool bIsFixed             = false,
                               bool cIsFixed             = false,
                               bool dIsFixed             = false,
                               bool vegaWeighted         = false,
                               EndCriteria endCriteria   = null,
                               OptimizationMethod method = null)
        {
            aIsFixed_        = aIsFixed;
            bIsFixed_        = bIsFixed;
            cIsFixed_        = cIsFixed;
            dIsFixed_        = dIsFixed;
            a_               = aGuess;
            b_               = bGuess;
            c_               = cGuess;
            d_               = dGuess;
            abcdEndCriteria_ = QLCore.EndCriteria.Type.None;
            endCriteria_     = endCriteria;
            optMethod_       = method;
            weights_         = new InitializedList <double>(blackVols.Count, 1.0 / blackVols.Count);
            vegaWeighted_    = vegaWeighted;
            times_           = t;
            blackVols_       = blackVols;


            AbcdMathFunction.validate(aGuess, bGuess, cGuess, dGuess);

            Utils.QL_REQUIRE(blackVols.Count == t.Count, () =>
                             "mismatch between number of times (" + t.Count + ") and blackVols (" + blackVols.Count + ")");

            // if no optimization method or endCriteria is provided, we provide one
            if (optMethod_ == null)
            {
                double epsfcn = 1.0e-8;
                double xtol   = 1.0e-8;
                double gtol   = 1.0e-8;
                bool   useCostFunctionsJacobian = false;
                optMethod_ = new LevenbergMarquardt(epsfcn, xtol, gtol, useCostFunctionsJacobian);
            }

            if (endCriteria_ == null)
            {
                int    maxIterations = 10000;
                int    maxStationaryStateIterations = 1000;
                double rootEpsilon         = 1.0e-8;
                double functionEpsilon     = 0.3e-4; // Why 0.3e-4 ?
                double gradientNormEpsilon = 0.3e-4; // Why 0.3e-4 ?
                endCriteria_ = new EndCriteria(maxIterations, maxStationaryStateIterations, rootEpsilon, functionEpsilon,
                                               gradientNormEpsilon);
            }
        }
            // curve optimization called here- adjust optimization parameters here
            internal void calculate()
            {
                FittingCost costFunction = costFunction_;
                Constraint  constraint   = new NoConstraint();

                // start with the guess solution, if it exists
                Vector x = new Vector(size(), 0.0);

                if (!curve_.guessSolution_.empty())
                {
                    x = curve_.guessSolution_;
                }

                if (curve_.maxEvaluations_ == 0)
                {
                    //Don't calculate, simply use given parameters to provide a fitted curve.
                    //This turns the fittedbonddiscountcurve into an evaluator of the parametric
                    //curve, for example allowing to use the parameters for a credit spread curve
                    //calculated with bonds in one currency to be coupled to a discount curve in
                    //another currency.
                    return;
                }

                //workaround for backwards compatibility
                OptimizationMethod optimization = optimizationMethod_;

                if (optimization == null)
                {
                    optimization = new Simplex(curve_.simplexLambda_);
                }

                Problem problem = new Problem(costFunction, constraint, x);

                double rootEpsilon         = curve_.accuracy_;
                double functionEpsilon     = curve_.accuracy_;
                double gradientNormEpsilon = curve_.accuracy_;

                EndCriteria endCriteria = new EndCriteria(curve_.maxEvaluations_,
                                                          curve_.maxStationaryStateIterations_,
                                                          rootEpsilon,
                                                          functionEpsilon,
                                                          gradientNormEpsilon);

                optimization.minimize(problem, endCriteria);
                solution_ = problem.currentValue();

                numberOfIterations_ = problem.functionEvaluation();
                costValue_          = problem.functionValue();

                // save the results as the guess solution, in case of recalculation
                curve_.guessSolution_ = solution_;
            }
Esempio n. 11
0
 public AbcdInterpolationImpl(List <double> xBegin, int size, List <double> yBegin,
                              double a, double b, double c, double d,
                              bool aIsFixed,
                              bool bIsFixed,
                              bool cIsFixed,
                              bool dIsFixed,
                              bool vegaWeighted,
                              EndCriteria endCriteria,
                              OptimizationMethod optMethod)
     : base(xBegin, size, yBegin)
 {
     abcdCoeffHolder_ = new AbcdCoeffHolder(a, b, c, d, aIsFixed, bIsFixed, cIsFixed, dIsFixed);
     endCriteria_     = endCriteria;
     optMethod_       = optMethod;
     vegaWeighted_    = vegaWeighted;
 }
Esempio n. 12
0
        //! Calibrate to a set of market instruments (caps/swaptions)

        /*! An additional constraint can be passed which must be
         *  satisfied in addition to the constraints of the model.
         */
        public void calibrate(List <CalibrationHelper> instruments,
                              OptimizationMethod method,
                              EndCriteria endCriteria,
                              Constraint additionalConstraint = null,
                              List <double> weights           = null,
                              List <bool> fixParameters       = null)
        {
            if (weights == null)
            {
                weights = new List <double>();
            }
            if (additionalConstraint == null)
            {
                additionalConstraint = new Constraint();
            }
            Utils.QL_REQUIRE(weights.empty() || weights.Count == instruments.Count, () =>
                             "mismatch between number of instruments (" +
                             instruments.Count + ") and weights(" +
                             weights.Count + ")");

            Constraint c;

            if (additionalConstraint.empty())
            {
                c = constraint_;
            }
            else
            {
                c = new CompositeConstraint(constraint_, additionalConstraint);
            }
            List <double> w = weights.Count == 0 ? new InitializedList <double>(instruments.Count, 1.0) : weights;

            Vector              prms = parameters();
            List <bool>         all  = new InitializedList <bool>(prms.size(), false);
            Projection          proj = new Projection(prms, fixParameters ?? all);
            CalibrationFunction f    = new CalibrationFunction(this, instruments, w, proj);
            ProjectedConstraint pc   = new ProjectedConstraint(c, proj);
            Problem             prob = new Problem(f, pc, proj.project(prms));

            shortRateEndCriteria_ = method.minimize(prob, endCriteria);
            Vector result = new Vector(prob.currentValue());

            setParams(proj.include(result));
            Vector shortRateProblemValues_ = prob.values(result);

            notifyObservers();
        }
Esempio n. 13
0
        public SABRInterpolation(List <double> xBegin, // x = strikes
                                 int xEnd,
                                 List <double> yBegin, // y = volatilities
                                 double t,             // option expiry
                                 double forward,
                                 double?alpha,
                                 double?beta,
                                 double?nu,
                                 double?rho,
                                 bool alphaIsFixed,
                                 bool betaIsFixed,
                                 bool nuIsFixed,
                                 bool rhoIsFixed,
                                 bool vegaWeighted            = true,
                                 EndCriteria endCriteria      = null,
                                 OptimizationMethod optMethod = null,
                                 double errorAccept           = 0.0020,
                                 bool useMaxError             = false,
                                 int maxGuesses = 50,
                                 double shift   = 0.0,
                                 VolatilityType volatilityType             = VolatilityType.ShiftedLognormal,
                                 SabrApproximationModel approximationModel = SabrApproximationModel.Hagan2002)
        {
            List <double?> addParams = new List <double?>();

            addParams.Add(shift);
            addParams.Add(volatilityType == VolatilityType.ShiftedLognormal ? 0.0 : 1.0);
            addParams.Add((double?)approximationModel);

            impl_ = new XABRInterpolationImpl <SABRSpecs>(
                xBegin, xEnd, yBegin, t, forward,
                new List <double?>()
            {
                alpha, beta, nu, rho
            },
                //boost::assign::list_of(alpha)(beta)(nu)(rho),
                new List <bool>()
            {
                alphaIsFixed, betaIsFixed, nuIsFixed, rhoIsFixed
            },
                //boost::assign::list_of(alphaIsFixed)(betaIsFixed)(nuIsFixed)(rhoIsFixed),
                vegaWeighted, endCriteria, optMethod, errorAccept, useMaxError,
                maxGuesses, addParams);
            coeffs_ = (impl_ as XABRInterpolationImpl <SABRSpecs>).coeff_;
        }
Esempio n. 14
0
        //! \name Constructors
        //@{
        //! all market data are quotes
        public SviInterpolatedSmileSection(
            Date optionDate,
            Handle <Quote> forward,
            List <double> strikes,
            bool hasFloatingStrikes,
            Handle <Quote> atmVolatility,
            List <Handle <Quote> > volHandles,
            double a, double b, double sigma, double rho, double m,
            bool isAFixed, bool isBFixed, bool isSigmaFixed, bool isRhoFixed, bool isMFixed,
            bool vegaWeighted,
            EndCriteria endCriteria   = null,
            OptimizationMethod method = null,
            DayCounter dc             = null)
            : base(optionDate, dc)
        {
            forward_            = forward;
            atmVolatility_      = atmVolatility;
            volHandles_         = volHandles;
            strikes_            = strikes;
            actualStrikes_      = strikes;
            hasFloatingStrikes_ = hasFloatingStrikes;
            a_            = a;
            b_            = b;
            sigma_        = sigma;
            rho_          = rho;
            m_            = m;
            isAFixed_     = isAFixed;
            isBFixed_     = isBFixed;
            isSigmaFixed_ = isSigmaFixed;
            isRhoFixed_   = isRhoFixed;
            isMFixed_     = isMFixed;
            vegaWeighted_ = vegaWeighted;
            endCriteria_  = endCriteria;
            method_       = method;

            forward_.registerWith(update);
            atmVolatility_.registerWith(update);

            for (int i = 0; i < volHandles_.Count; ++i)
            {
                volHandles_[i].registerWith(update);
            }
        }
        //! \name Constructors
        //@{
        //! all market data are quotes
        public SabrInterpolatedSmileSection(
            Date optionDate,
            Handle <Quote> forward,
            List <double> strikes,
            bool hasFloatingStrikes,
            Handle <Quote> atmVolatility,
            List <Handle <Quote> > volHandles,
            double alpha, double beta, double nu, double rho,
            bool isAlphaFixed, bool isBetaFixed, bool isNuFixed, bool isRhoFixed,
            bool vegaWeighted,
            EndCriteria endCriteria   = null,
            OptimizationMethod method = null,
            DayCounter dc             = null,
            double shift = 0.0)
            : base(optionDate, dc, null, VolatilityType.ShiftedLognormal, shift)
        {
            forward_            = forward;
            atmVolatility_      = atmVolatility;
            volHandles_         = volHandles;
            strikes_            = strikes;
            actualStrikes_      = strikes;
            hasFloatingStrikes_ = hasFloatingStrikes;
            alpha_        = alpha;
            beta_         = beta;
            nu_           = nu;
            rho_          = rho;
            isAlphaFixed_ = isAlphaFixed;
            isBetaFixed_  = isBetaFixed;
            isNuFixed_    = isNuFixed;
            isRhoFixed_   = isRhoFixed;
            vegaWeighted_ = vegaWeighted;
            endCriteria_  = endCriteria;
            method_       = method;

            forward_.registerWith(update);
            atmVolatility_.registerWith(update);

            for (int i = 0; i < volHandles_.Count; ++i)
            {
                volHandles_[i].registerWith(update);
            }
        }
Esempio n. 16
0
 public Abcd(double a, double b, double c, double d,
             bool aIsFixed, bool bIsFixed,
             bool cIsFixed, bool dIsFixed,
             bool vegaWeighted            = false,
             EndCriteria endCriteria      = null,
             OptimizationMethod optMethod = null)
 {
     a_            = a;
     b_            = b;
     c_            = c;
     d_            = d;
     aIsFixed_     = aIsFixed;
     bIsFixed_     = bIsFixed;
     cIsFixed_     = cIsFixed;
     dIsFixed_     = dIsFixed;
     vegaWeighted_ = vegaWeighted;
     endCriteria_  = endCriteria;
     optMethod_    = optMethod;
     global        = true;
 }
Esempio n. 17
0
        public XABRInterpolationImpl(List <double> xBegin, int size, List <double> yBegin, double t,
                                     double forward, List <double?> _params,
                                     List <bool> paramIsFixed, bool vegaWeighted,
                                     EndCriteria endCriteria,
                                     OptimizationMethod optMethod,
                                     double errorAccept, bool useMaxError, int maxGuesses, List <double?> addParams = null,
                                     XABRConstraint constraint = null)
            : base(xBegin, size, yBegin)
        {
            endCriteria_  = endCriteria ?? new EndCriteria(60000, 100, 1e-8, 1e-8, 1e-8);
            optMethod_    = optMethod ?? new LevenbergMarquardt(1e-8, 1e-8, 1e-8);
            errorAccept_  = errorAccept;
            useMaxError_  = useMaxError;
            maxGuesses_   = maxGuesses;
            forward_      = forward;
            vegaWeighted_ = vegaWeighted;
            constraint_   = constraint ?? new NoXABRConstraint();

            coeff_          = new XABRCoeffHolder <Model>(t, forward, _params, paramIsFixed, addParams);
            coeff_.weights_ = new InitializedList <double>(size, 1.0 / size);
        }
Esempio n. 18
0
 /*! Constructor */
 public AbcdInterpolation(List <double> xBegin, int size, List <double> yBegin,
                          double a                     = -0.06,
                          double b                     = 0.17,
                          double c                     = 0.54,
                          double d                     = 0.17,
                          bool aIsFixed                = false,
                          bool bIsFixed                = false,
                          bool cIsFixed                = false,
                          bool dIsFixed                = false,
                          bool vegaWeighted            = false,
                          EndCriteria endCriteria      = null,
                          OptimizationMethod optMethod = null)
 {
     impl_ = new AbcdInterpolationImpl(xBegin, size, yBegin,
                                       a, b, c, d,
                                       aIsFixed, bIsFixed,
                                       cIsFixed, dIsFixed,
                                       vegaWeighted,
                                       endCriteria,
                                       optMethod);
     impl_.update();
     coeffs_ = ((AbcdInterpolationImpl)impl_).AbcdCoeffHolder();
 }
Esempio n. 19
0
 public SviInterpolation(List <double> xBegin, // x = strikes
                         int size,
                         List <double> yBegin, // y = volatilities
                         double t,             // option expiry
                         double forward,
                         double?a,
                         double?b,
                         double?sigma,
                         double?rho,
                         double?m,
                         bool aIsFixed,
                         bool bIsFixed,
                         bool sigmaIsFixed,
                         bool rhoIsFixed,
                         bool mIsFixed, bool vegaWeighted = true,
                         EndCriteria endCriteria          = null,
                         OptimizationMethod optMethod     = null,
                         double errorAccept       = 0.0020,
                         bool useMaxError         = false,
                         int maxGuesses           = 50,
                         List <double?> addParams = null)
 {
     impl_ = new XABRInterpolationImpl <SVISpecs>(
         xBegin, size, yBegin, t, forward,
         new List <double?>()
     {
         a, b, sigma, rho, m
     },
         new List <bool>()
     {
         aIsFixed, bIsFixed, sigmaIsFixed, rhoIsFixed, mIsFixed
     },
         vegaWeighted, endCriteria, optMethod, errorAccept, useMaxError,
         maxGuesses, addParams);
     coeffs_ = (impl_ as XABRInterpolationImpl <SVISpecs>).coeff_;
 }
Esempio n. 20
0
 public SvenssonFitting(Vector weights = null, OptimizationMethod optimizationMethod = null)
     : base(true, weights, optimizationMethod)
 {
 }
Esempio n. 21
0
 public NelsonSiegelFitting(Vector weights = null, OptimizationMethod optimizationMethod = null)
     : base(true, weights, optimizationMethod)
 {
 }
Esempio n. 22
0
 public ExponentialSplinesFitting(bool constrainAtZero = true,
                                  Vector weights       = null,
                                  OptimizationMethod optimizationMethod = null)
     : base(constrainAtZero, weights, optimizationMethod)
 {
 }