Пример #1
0
 public static double duration(Leg leg, double yield, DayCounter dayCounter, Compounding compounding, Frequency frequency,
                               Duration.Type type, bool includeSettlementDateFlows, Date settlementDate = null,
                               Date npvDate = null)
 {
     return(duration(leg, new InterestRate(yield, dayCounter, compounding, frequency),
                     type, includeSettlementDateFlows, settlementDate, npvDate));
 }
        ////! Internal rate of return.
        ///*! The IRR is the interest rate at which the NPV of the cash flows equals the given market price. The function verifies
        //      the theoretical existance of an IRR and numerically establishes the IRR to the desired precision. */
        //public static double irr(List<CashFlow> cashflows, double marketPrice, DayCounter dayCounter, Compounding compounding,
        //                        Frequency frequency, Date settlementDate, double accuracy, int maxIterations, double guess) {
        //      if (settlementDate == null)
        //          settlementDate = Settings.evaluationDate();

        //      // depending on the sign of the market price, check that cash flows of the opposite sign have been specified (otherwise
        //      // IRR is nonsensical.)

        //      int lastSign = Math.Sign(-marketPrice),
        //          signChanges = 0;

        //      foreach (CashFlow cf in cashflows.Where(cf => !cf.hasOccurred(settlementDate))) {
        //          int thisSign = Math.Sign(cf.amount());
        //          if (lastSign * thisSign < 0) // sign change
        //              signChanges++;

        //          if (thisSign != 0)
        //              lastSign = thisSign;
        //      }
        //      if (!(signChanges > 0))
        //          throw new ApplicationException("the given cash flows cannot result in the given market price due to their sign");

        //      /* The following is commented out due to the lack of a QL_WARN macro
        //      if (signChanges > 1) {    // Danger of non-unique solution
        //                                // Check the aggregate cash flows (Norstrom)
        //          Real aggregateCashFlow = marketPrice;
        //          signChanges = 0;
        //          for (Size i = 0; i < cashflows.size(); ++i) {
        //              Real nextAggregateCashFlow =
        //                  aggregateCashFlow + cashflows[i]->amount();

        //              if (aggregateCashFlow * nextAggregateCashFlow < 0.0)
        //                  signChanges++;

        //              aggregateCashFlow = nextAggregateCashFlow;
        //          }
        //          if (signChanges > 1)
        //              QL_WARN( "danger of non-unique solution");
        //      };
        //      */

        //      //Brent solver;
        //      NewtonSafe solver = new NewtonSafe();
        //      solver.setMaxEvaluations(maxIterations);
        //      return solver.solve(new IrrFinder(cashflows, marketPrice, dayCounter, compounding, frequency, settlementDate),
        //                          accuracy, guess, guess / 10.0);
        //  }

        //! Cash-flow duration.

        /*! The simple duration of a string of cash flows is defined as
         *        \f[
         *        D_{\mathrm{simple}} = \frac{\sum t_i c_i B(t_i)}{\sum c_i B(t_i)}
         *        \f]
         *        where \f$ c_i \f$ is the amount of the \f$ i \f$-th cash
         *        flow, \f$ t_i \f$ is its payment time, and \f$ B(t_i) \f$ is the corresponding discount according to the passed yield.
         *
         *        The modified duration is defined as
         *        \f[
         *        D_{\mathrm{modified}} = -\frac{1}{P} \frac{\partial P}{\partial y}
         *        \f]
         *        where \f$ P \f$ is the present value of the cash flows according to the given IRR \f$ y \f$.
         *
         *        The Macaulay duration is defined for a compounded IRR as
         *        \f[
         *        D_{\mathrm{Macaulay}} = \left( 1 + \frac{y}{N} \right)
         *                                D_{\mathrm{modified}}
         *        \f]
         *        where \f$ y \f$ is the IRR and \f$ N \f$ is the number of cash flows per year.
         */
        public static double duration(List <CashFlow> cashflows, InterestRate rate, Duration.Type type = Duration.Type.Modified,
                                      Date settlementDate = null)
        {
            if (cashflows.Count == 0)
            {
                return(0.0);
            }

            if (settlementDate == null)
            {
                settlementDate = Settings.evaluationDate();
            }

            switch (type)
            {
            case Duration.Type.Simple:
                return(simpleDuration(cashflows, rate, settlementDate));

            case Duration.Type.Modified:
                return(modifiedDuration(cashflows, rate, settlementDate));

            case Duration.Type.Macaulay:
                return(macaulayDuration(cashflows, rate, settlementDate));

            default:
                throw new ArgumentException("unknown duration type");
            }
        }
Пример #3
0
        //! Cash-flow duration.
        public static double duration(Leg leg, InterestRate rate, Duration.Type type, bool includeSettlementDateFlows,
                                      Date settlementDate = null, Date npvDate = null)
        {
            if (leg.empty())
            {
                return(0.0);
            }

            if (settlementDate == null)
            {
                settlementDate = Settings.evaluationDate();
            }

            if (npvDate == null)
            {
                npvDate = settlementDate;
            }

            switch (type)
            {
            case Duration.Type.Simple:
                return(simpleDuration(leg, rate, includeSettlementDateFlows, settlementDate, npvDate));

            case Duration.Type.Modified:
                return(modifiedDuration(leg, rate, includeSettlementDateFlows, settlementDate, npvDate));

            case Duration.Type.Macaulay:
                return(macaulayDuration(leg, rate, includeSettlementDateFlows, settlementDate, npvDate));

            default:
                Utils.QL_FAIL("unknown duration type");
                break;
            }
            return(0.0);
        }
Пример #4
0
        public static double duration(Bond bond, InterestRate yield, Duration.Type type)
        {
            double ret = NQuantLibcPINVOKE.BondFunctions_duration__SWIG_1(Bond.getCPtr(bond), InterestRate.getCPtr(yield), (int)type);

            if (NQuantLibcPINVOKE.SWIGPendingException.Pending)
            {
                throw NQuantLibcPINVOKE.SWIGPendingException.Retrieve();
            }
            return(ret);
        }
        public static double duration(Leg arg0, InterestRate arg1, Duration.Type type, bool includeSettlementDateFlows)
        {
            double ret = NQuantLibcPINVOKE.CashFlows_duration__SWIG_1(Leg.getCPtr(arg0), InterestRate.getCPtr(arg1), (int)type, includeSettlementDateFlows);

            if (NQuantLibcPINVOKE.SWIGPendingException.Pending)
            {
                throw NQuantLibcPINVOKE.SWIGPendingException.Retrieve();
            }
            return(ret);
        }
Пример #6
0
        public static double duration(Bond bond, InterestRate yield, Duration.Type type = Duration.Type.Modified,
                                      Date settlementDate = null)
        {
            if (settlementDate == null)
            {
                settlementDate = bond.settlementDate();
            }

            Utils.QL_REQUIRE(BondFunctions.isTradable(bond, settlementDate), () =>
                             "non tradable at " + settlementDate +
                             " (maturity being " + bond.maturityDate() + ")");

            return(CashFlows.duration(bond.cashflows(), yield, type, false, settlementDate));
        }
Пример #7
0
        public static double duration(Bond bond, double yield, DayCounter dayCounter, Compounding compounding, Frequency frequency, Duration.Type type)
        {
            double ret = NQuantLibcPINVOKE.BondFunctions_duration__SWIG_4(Bond.getCPtr(bond), yield, DayCounter.getCPtr(dayCounter), (int)compounding, (int)frequency, (int)type);

            if (NQuantLibcPINVOKE.SWIGPendingException.Pending)
            {
                throw NQuantLibcPINVOKE.SWIGPendingException.Retrieve();
            }
            return(ret);
        }
Пример #8
0
 public static double duration(Bond bond, double yield, DayCounter dayCounter, Compounding compounding, Frequency frequency,
                               Duration.Type type = Duration.Type.Modified, Date settlementDate = null)
 {
     return(duration(bond, new InterestRate(yield, dayCounter, compounding, frequency), type, settlementDate));
 }
        public static double duration(Leg arg0, double yield, DayCounter dayCounter, Compounding compounding, Frequency frequency, Duration.Type type, bool includeSettlementDateFlows)
        {
            double ret = NQuantLibcPINVOKE.CashFlows_duration__SWIG_4(Leg.getCPtr(arg0), yield, DayCounter.getCPtr(dayCounter), (int)compounding, (int)frequency, (int)type, includeSettlementDateFlows);

            if (NQuantLibcPINVOKE.SWIGPendingException.Pending)
            {
                throw NQuantLibcPINVOKE.SWIGPendingException.Retrieve();
            }
            return(ret);
        }