public double getMarketValue(
      DateTime date,
      ITermStructure termStructure) { // todo (cne) add the spread (due to the rating)

    double marketValue = 0;
    int i = 0;
    DateTime tempDate =
        date.plus(installmentPeriod); // todo (cne) check if the coupon occurs immediately of not
    Duration tempDuration = new Duration(date, tempDate);

    while (!isExpired(tempDate)) {
      marketValue +=
          coupon
              / Math.pow(
                  1
                      + termStructure.getRiskFreeRate(
                          convertDurationToPercentageOfAYear(tempDuration)),
                  i);
      i++;
      tempDate = tempDate.plus(installmentPeriod);
      tempDuration = tempDuration.plus(installmentPeriod);
      // int iterationCounter = i;
    }
    // i = iterationCounter;
    return quantity
        * (marketValue
            + (coupon + getFaceValue())
                / Math.pow(
                    1
                        + termStructure.getRiskFreeRate(
                            convertDurationToPercentageOfAYear(tempDuration)),
                    i));
  }
  // the following function is used when one wants to use the term structure as of now to calculate
  // market values for future periods (i.e. forward values)
  public double getMarketValue(
      DateTime startDate, DateTime forwardDate, ITermStructure termStructure) {

    int i = 0;
    double marketValue = 0;
    DateTime tempDate = forwardDate.plus(installmentPeriod);
    Duration tempDuration = new Duration(startDate, tempDate);

    while (!isExpired(tempDate)) {
      marketValue +=
          coupon
              / Math.pow(
                  1
                      + termStructure.getRiskFreeRate(
                          convertDurationToPercentageOfAYear(tempDuration)),
                  i);
      i++;
      tempDate = tempDate.plus(installmentPeriod);
      tempDuration = tempDuration.plus(installmentPeriod);
    }
    return quantity
        * (marketValue
            + (coupon + getFaceValue())
                / Math.pow(
                    1
                        + termStructure.getRiskFreeRate(
                            convertDurationToPercentageOfAYear(tempDuration)),
                    i));
  }
  public double getDurationDiscrete(DateTime date, ITermStructure termStructure) {

    double numerator = 0;
    double denominator = 0;
    // int numberOfCashFlows = (int) installmentPeriod * numberOfPeriods(installmentPeriod, date,
    // maturityDate);
    int i = 0;
    DateTime tempDate = date.plus(installmentPeriod);
    Duration tempDuration = new Duration(date, tempDate);
    while (!isExpired(tempDate)) {

      numerator +=
          i
              * coupon
              / Math.pow(
                  1
                      + termStructure.getRiskFreeRate(
                          convertDurationToPercentageOfAYear(tempDuration)),
                  i);
      denominator +=
          coupon
              / Math.pow(
                  1
                      + termStructure.getRiskFreeRate(
                          convertDurationToPercentageOfAYear(tempDuration)),
                  i);
      i++;
      tempDate = tempDate.plus(installmentPeriod);
      tempDuration = tempDuration.plus(installmentPeriod);
    }
    return (numerator
            + i
                * (coupon + getFaceValue())
                / Math.pow(
                    1
                        + termStructure.getRiskFreeRate(
                            convertDurationToPercentageOfAYear(tempDuration)),
                    i))
        / (denominator
            + (coupon + getFaceValue())
                / Math.pow(
                    1
                        + termStructure.getRiskFreeRate(
                            convertDurationToPercentageOfAYear(tempDuration)),
                    i));
  }