public MissionTimer(StartCriteria startCriteria, EndCriteria endCriteria, string startParameter, string endParameter, string title)
     : base(title)
 {
     this.startCriteria = startCriteria;
     this.endCriteria = endCriteria;
     this.startParameter = startParameter;
     this.endParameter = endParameter;
     
     disableOnStateChange = false;
     checker = new ContractChecker(this);
 }
Beispiel #2
0
 public virtual void calibrate(CalibrationHelperVector arg0, OptimizationMethod arg1, EndCriteria arg2)
 {
     NQuantLibcPINVOKE.CalibratedModel_calibrate__SWIG_3(swigCPtr, CalibrationHelperVector.getCPtr(arg0), OptimizationMethod.getCPtr(arg1), EndCriteria.getCPtr(arg2));
     if (NQuantLibcPINVOKE.SWIGPendingException.Pending)
     {
         throw NQuantLibcPINVOKE.SWIGPendingException.Retrieve();
     }
 }
Beispiel #3
0
 public virtual void calibrate(CalibrationHelperVector arg0, OptimizationMethod arg1, EndCriteria arg2, Constraint constraint, DoubleVector weights)
 {
     NQuantLibcPINVOKE.CalibratedModel_calibrate__SWIG_1(swigCPtr, CalibrationHelperVector.getCPtr(arg0), OptimizationMethod.getCPtr(arg1), EndCriteria.getCPtr(arg2), Constraint.getCPtr(constraint), DoubleVector.getCPtr(weights));
     if (NQuantLibcPINVOKE.SWIGPendingException.Pending)
     {
         throw NQuantLibcPINVOKE.SWIGPendingException.Retrieve();
     }
 }
Beispiel #4
0
        public void testCachedHullWhite()
        {
            //("Testing Hull-White calibration against cached values...");

            Date today      = new Date(15, Month.February, 2002);
            Date settlement = new Date(19, Month.February, 2002);

            Settings.setEvaluationDate(today);
            Handle <YieldTermStructure> termStructure =
                new Handle <YieldTermStructure>(Utilities.flatRate(settlement, 0.04875825, new Actual365Fixed()));
            //termStructure.link
            HullWhite model = new HullWhite(termStructure);

            CalibrationData[] data = { new CalibrationData(1, 5, 0.1148),
                                       new CalibrationData(2, 4, 0.1108),
                                       new CalibrationData(3, 3, 0.1070),
                                       new CalibrationData(4, 2, 0.1021),
                                       new CalibrationData(5, 1, 0.1000) };
            IborIndex         index = new Euribor6M(termStructure);

            IPricingEngine engine = new JamshidianSwaptionEngine(model);

            List <CalibrationHelper> swaptions = new List <CalibrationHelper>();

            for (int i = 0; i < data.Length; i++)
            {
                Quote             vol    = new SimpleQuote(data[i].volatility);
                CalibrationHelper helper =
                    new SwaptionHelper(new Period(data[i].start, TimeUnit.Years),
                                       new Period(data[i].length, TimeUnit.Years),
                                       new Handle <Quote>(vol),
                                       index,
                                       new Period(1, TimeUnit.Years),
                                       new Thirty360(),
                                       new Actual360(),
                                       termStructure);
                helper.setPricingEngine(engine);
                swaptions.Add(helper);
            }

            // Set up the optimization problem
            // Real simplexLambda = 0.1;
            // Simplex optimizationMethod(simplexLambda);
            LevenbergMarquardt optimizationMethod = new LevenbergMarquardt(1.0e-8, 1.0e-8, 1.0e-8);
            EndCriteria        endCriteria        = new EndCriteria(10000, 100, 1e-6, 1e-8, 1e-8);

            //Optimize
            model.calibrate(swaptions, optimizationMethod, endCriteria, new Constraint(), new List <double>());
            EndCriteria.Type ecType = model.endCriteria();

            // Check and print out results
#if QL_USE_INDEXED_COUPON
            double cachedA = 0.0488199, cachedSigma = 0.00593579;
#else
            double cachedA = 0.0488565, cachedSigma = 0.00593662;
#endif
            double tolerance = 1.120e-5;
            //double tolerance = 1.0e-6;
            Vector xMinCalculated = model.parameters();
            double yMinCalculated = model.value(xMinCalculated, swaptions);
            Vector xMinExpected   = new Vector(2);
            xMinExpected[0] = cachedA;
            xMinExpected[1] = cachedSigma;
            double yMinExpected = model.value(xMinExpected, swaptions);
            if (Math.Abs(xMinCalculated[0] - cachedA) > tolerance ||
                Math.Abs(xMinCalculated[1] - cachedSigma) > tolerance)
            {
                QAssert.Fail("Failed to reproduce cached calibration results:\n"
                             + "calculated: a = " + xMinCalculated[0] + ", "
                             + "sigma = " + xMinCalculated[1] + ", "
                             + "f(a) = " + yMinCalculated + ",\n"
                             + "expected:   a = " + xMinExpected[0] + ", "
                             + "sigma = " + xMinExpected[1] + ", "
                             + "f(a) = " + yMinExpected + ",\n"
                             + "difference: a = " + (xMinCalculated[0] - xMinExpected[0]) + ", "
                             + "sigma = " + (xMinCalculated[1] - xMinExpected[1]) + ", "
                             + "f(a) = " + (yMinCalculated - yMinExpected) + ",\n"
                             + "end criteria = " + ecType);
            }
        }
 public void calibrate(CalibrationHelperVector helper, OptimizationMethod method, EndCriteria endCriteria)
 {
     NQuantLibcPINVOKE.MarkovFunctional_calibrate__SWIG_3(swigCPtr, CalibrationHelperVector.getCPtr(helper), OptimizationMethod.getCPtr(method), EndCriteria.getCPtr(endCriteria));
     if (NQuantLibcPINVOKE.SWIGPendingException.Pending)
     {
         throw NQuantLibcPINVOKE.SWIGPendingException.Retrieve();
     }
 }
 public void calibrate(CalibrationHelperVector instruments, OptimizationMethod method, EndCriteria endCriteria, Constraint constraint, DoubleVector weights, BoolVector fixParameters)
 {
     NQuantLibcPINVOKE.Gsr_calibrate__SWIG_0(swigCPtr, CalibrationHelperVector.getCPtr(instruments), OptimizationMethod.getCPtr(method), EndCriteria.getCPtr(endCriteria), Constraint.getCPtr(constraint), DoubleVector.getCPtr(weights), BoolVector.getCPtr(fixParameters));
     if (NQuantLibcPINVOKE.SWIGPendingException.Pending)
     {
         throw NQuantLibcPINVOKE.SWIGPendingException.Retrieve();
     }
 }
Beispiel #7
0
 public void calibrate(CalibrationHelperVector arg0, OptimizationMethod arg1, EndCriteria arg2) {
   NQuantLibcPINVOKE.ShortRateModelHandle_calibrate__SWIG_2(swigCPtr, CalibrationHelperVector.getCPtr(arg0), OptimizationMethod.getCPtr(arg1), EndCriteria.getCPtr(arg2));
   if (NQuantLibcPINVOKE.SWIGPendingException.Pending) throw NQuantLibcPINVOKE.SWIGPendingException.Retrieve();
 }
 public SVIInterpolation(QlArray x, QlArray y, double expiry, double forward, double a, double b, double rho, double m, double sigma, EndCriteria endCriteria, OptimizationMethod optimizationMethod) : this(NQuantLibcPINVOKE.new_SVIInterpolation__SWIG_1(QlArray.getCPtr(x), QlArray.getCPtr(y), expiry, forward, a, b, rho, m, sigma, EndCriteria.getCPtr(endCriteria), OptimizationMethod.getCPtr(optimizationMethod)), true)
 {
     if (NQuantLibcPINVOKE.SWIGPendingException.Pending)
     {
         throw NQuantLibcPINVOKE.SWIGPendingException.Retrieve();
     }
 }
Beispiel #9
0
 public void calibrate(CalibrationHelperVector arg0, OptimizationMethod arg1, EndCriteria arg2, Constraint constraint)
 {
     NQuantLibcPINVOKE.ShortRateModelHandle_calibrate__SWIG_2(swigCPtr, CalibrationHelperVector.getCPtr(arg0), OptimizationMethod.getCPtr(arg1), EndCriteria.getCPtr(arg2), Constraint.getCPtr(constraint));
     if (NQuantLibcPINVOKE.SWIGPendingException.Pending)
     {
         throw NQuantLibcPINVOKE.SWIGPendingException.Retrieve();
     }
 }
Beispiel #10
0
 public void calibrateVolatilitiesIterative(CalibrationHelperVector helpers, OptimizationMethod method, EndCriteria endCriteria)
 {
     NQuantLibcPINVOKE.Gsr_calibrateVolatilitiesIterative__SWIG_2(swigCPtr, CalibrationHelperVector.getCPtr(helpers), OptimizationMethod.getCPtr(method), EndCriteria.getCPtr(endCriteria));
     if (NQuantLibcPINVOKE.SWIGPendingException.Pending)
     {
         throw NQuantLibcPINVOKE.SWIGPendingException.Retrieve();
     }
 }
Beispiel #11
0
 public void calibrateVolatilitiesIterative(CalibrationHelperVector helpers, OptimizationMethod method, EndCriteria endCriteria, Constraint constraint, DoubleVector weights)
 {
     NQuantLibcPINVOKE.Gsr_calibrateVolatilitiesIterative__SWIG_0(swigCPtr, CalibrationHelperVector.getCPtr(helpers), OptimizationMethod.getCPtr(method), EndCriteria.getCPtr(endCriteria), Constraint.getCPtr(constraint), DoubleVector.getCPtr(weights));
     if (NQuantLibcPINVOKE.SWIGPendingException.Pending)
     {
         throw NQuantLibcPINVOKE.SWIGPendingException.Retrieve();
     }
 }
Beispiel #12
0
        public QlArray solve(CostFunctionDelegate function, Constraint c, OptimizationMethod m, EndCriteria e, QlArray iv)
        {
            QlArray ret = new QlArray(NQuantLibcPINVOKE.Optimizer_solve(swigCPtr, CostFunctionDelegate.getCPtr(function), Constraint.getCPtr(c), OptimizationMethod.getCPtr(m), EndCriteria.getCPtr(e), QlArray.getCPtr(iv)), true);

            if (NQuantLibcPINVOKE.SWIGPendingException.Pending)
            {
                throw NQuantLibcPINVOKE.SWIGPendingException.Retrieve();
            }
            return(ret);
        }
 public SABRInterpolation(QlArray x, QlArray y, double expiry, double forward, double alpha, double beta, double volvol, double rho, EndCriteria endCriteria, OptimizationMethod optimizationMethod) : this(NQuantLibcPINVOKE.new_SABRInterpolation__SWIG_5(QlArray.getCPtr(x), QlArray.getCPtr(y), expiry, forward, alpha, beta, volvol, rho, EndCriteria.getCPtr(endCriteria), OptimizationMethod.getCPtr(optimizationMethod)), true)
 {
     if (NQuantLibcPINVOKE.SWIGPendingException.Pending)
     {
         throw NQuantLibcPINVOKE.SWIGPendingException.Retrieve();
     }
 }
Beispiel #14
0
 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(EndCriteria obj)
 {
     return((obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr);
 }
 public void calibrate(CalibrationHelperVector instruments, OptimizationMethod method, EndCriteria endCriteria)
 {
     NQuantLibcPINVOKE.Gsr_calibrate__SWIG_3(swigCPtr, CalibrationHelperVector.getCPtr(instruments), OptimizationMethod.getCPtr(method), EndCriteria.getCPtr(endCriteria));
     if (NQuantLibcPINVOKE.SWIGPendingException.Pending)
     {
         throw NQuantLibcPINVOKE.SWIGPendingException.Retrieve();
     }
 }
Beispiel #16
0
 public void calibrate(CalibrationHelperVector arg0, OptimizationMethod arg1, EndCriteria arg2, Constraint constraint) {
   NQuantLibcPINVOKE.CalibratedModel_calibrate__SWIG_1(swigCPtr, CalibrationHelperVector.getCPtr(arg0), OptimizationMethod.getCPtr(arg1), EndCriteria.getCPtr(arg2), Constraint.getCPtr(constraint));
   if (NQuantLibcPINVOKE.SWIGPendingException.Pending) throw NQuantLibcPINVOKE.SWIGPendingException.Retrieve();
 }
Beispiel #17
0
        public static string eqModelSVI(
            [ExcelArgument(Description = "id of option to be constructed ")] string ObjectId,
            [ExcelArgument(Description = "ATM forward ")] double forward,
            [ExcelArgument(Description = "expiry (years of fraction ")] double tenor,
            [ExcelArgument(Description = "Strikes ")] double[] strikes,
            [ExcelArgument(Description = "implied vols ")] double[] volatilities,
            [ExcelArgument(Description = "initial parameters ")] double[] initials,
            [ExcelArgument(Description = "trigger ")] object trigger)
        {
            if (ExcelUtil.CallFromWizard())
            {
                return("");
            }

            string callerAddress = "";

            callerAddress = ExcelUtil.getActiveCellAddress();

            try
            {
                if (strikes.Length != volatilities.Length)
                {
                    throw new Exception("SABR input lengths don't match.");
                }

                if (initials.Length != 5)
                {
                    throw new Exception("SVI has five parameters.");
                }

                int[] idx = volatilities.Select((v, Index) => new { V = v, idx = Index })
                            .Where(x => x.V == 0)
                            .Select(x => x.idx)
                            .ToArray();

                QlArray xx = new QlArray((uint)strikes.Length - (uint)idx.Length);
                QlArray yy = new QlArray((uint)volatilities.Length - (uint)idx.Length);
                for (uint i = 0, j = 0; i < strikes.Length; i++)
                {
                    if (volatilities[i] == 0)       // empty
                    {
                        continue;
                    }
                    xx.set(j, strikes[i]);
                    yy.set(j, volatilities[i]);
                    j++;
                }

                EndCriteria        endcriteria = new EndCriteria(100000, 100, 1e-8, 1e-8, 1e-8);
                OptimizationMethod opmodel     = new Simplex(0.01);
                // alpha: ATM Vol, beta: CEV param, rho: underlying/vol correlation, nu: vol of vol
                SVIInterpolation svi = new SVIInterpolation(xx, yy, tenor, forward,
                                                            initials[0], initials[1], initials[2], initials[3], initials[4],
                                                            endcriteria, opmodel, true);

                double err = 0;
                err = svi.update();

                // Store the option and return its id
                string id = "MODEL@" + ObjectId;
                OHRepository.Instance.storeObject(id, svi, callerAddress);
                id += "#" + (String)DateTime.Now.ToString(@"HH:mm:ss");
                return(id);
            }
            catch (Exception e)
            {
                ExcelUtil.logError(callerAddress, System.Reflection.MethodInfo.GetCurrentMethod().Name.ToString(), e.Message);
                return(e.Message);
            }
        }
        protected override void OnParameterLoad(ConfigNode node)
        {
            startCriteria = ConfigNodeUtil.ParseValue<StartCriteria>(node, "startCriteria");
            endCriteria = ConfigNodeUtil.ParseValue<EndCriteria>(node, "endCriteria");
            startParameter = ConfigNodeUtil.ParseValue<string>(node, "startParameter", "");
            endParameter = ConfigNodeUtil.ParseValue<string>(node, "endParameter", "");
            startTime = Convert.ToDouble(node.GetValue("startTime"));
            endTime = Convert.ToDouble(node.GetValue("endTime"));

            if (Root.ContractState == Contract.State.Active || Root.ContractState == Contract.State.Offered)
            {
                checker = new ContractChecker(this);
            }
        }
Beispiel #19
0
 public void calibrate(CalibrationHelperVector arg0, OptimizationMethod arg1, EndCriteria arg2, Constraint constraint, DoubleVector weights) {
   NQuantLibcPINVOKE.ShortRateModelHandle_calibrate__SWIG_0(swigCPtr, CalibrationHelperVector.getCPtr(arg0), OptimizationMethod.getCPtr(arg1), EndCriteria.getCPtr(arg2), Constraint.getCPtr(constraint), DoubleVector.getCPtr(weights));
   if (NQuantLibcPINVOKE.SWIGPendingException.Pending) throw NQuantLibcPINVOKE.SWIGPendingException.Retrieve();
 }
 public void calibrate(CalibrationHelperVector helper, OptimizationMethod method, EndCriteria endCriteria, Constraint constraint, DoubleVector weights)
 {
     NQuantLibcPINVOKE.MarkovFunctional_calibrate__SWIG_1(swigCPtr, CalibrationHelperVector.getCPtr(helper), OptimizationMethod.getCPtr(method), EndCriteria.getCPtr(endCriteria), Constraint.getCPtr(constraint), DoubleVector.getCPtr(weights));
     if (NQuantLibcPINVOKE.SWIGPendingException.Pending)
     {
         throw NQuantLibcPINVOKE.SWIGPendingException.Retrieve();
     }
 }
Beispiel #21
0
 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(EndCriteria obj) {
   return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
 }
        //! Perform line search
        public override double value(Problem P,           // Optimization problem
                                     ref EndCriteria.Type ecType,
                                     EndCriteria endCriteria,
                                     double t_ini)    // initial value of line-search step
        {
            Constraint constraint = P.constraint();

            succeed_ = true;
            bool   maxIter    = false;
            double t          = t_ini;
            int    loopNumber = 0;

            double q0  = P.functionValue();
            double qp0 = P.gradientNormValue();

            double tl = 0.0;
            double tr = 0.0;

            qt_  = q0;
            qpt_ = (gradient_.empty()) ? qp0 : -Vector.DotProduct(gradient_, searchDirection_);

            // Initialize gradient
            gradient_ = new Vector(P.currentValue().size());
            // Compute new point
            xtd_ = P.currentValue();
            t    = update(ref xtd_, searchDirection_, t, constraint);
            // Compute function value at the new point
            qt_ = P.value(xtd_);

            while ((qt_ - q0) < -beta_ * t * qpt_ || (qt_ - q0) > -alpha_ * t * qpt_)
            {
                if ((qt_ - q0) > -alpha_ * t * qpt_)
                {
                    tr = t;
                }
                else
                {
                    tl = t;
                }
                ++loopNumber;

                // calculate the new step
                if (Utils.close_enough(tr, 0.0))
                {
                    t *= extrapolation_;
                }
                else
                {
                    t = (tl + tr) / 2.0;
                }

                // New point value
                xtd_ = P.currentValue();
                t    = update(ref xtd_, searchDirection_, t, constraint);

                // Compute function value at the new point
                qt_ = P.value(xtd_);
                P.gradient(ref gradient_, xtd_);
                // and it squared norm
                maxIter = endCriteria.checkMaxIterations(loopNumber, ref ecType);

                if (maxIter)
                {
                    break;
                }
            }

            if (maxIter)
            {
                succeed_ = false;
            }

            // Compute new gradient
            P.gradient(ref gradient_, xtd_);
            // and it squared norm
            qpt_ = Vector.DotProduct(gradient_, gradient_);

            // Return new step value
            return(t);
        }