<commit_before>/**
 * Copyright (C) 2009 - 2010 by OpenGamma Inc.
 * 
 * Please see distribution for license.
 */
package com.opengamma.financial.model.cashflow;


/**
 * @author emcleod
 * 
 */
public class MacaulayDurationCalculator {
  private final PresentValueCalculator _pvCalculator = new PresentValueCalculator();

}<commit_msg>Macaulay duration.<commit_after>/**
 * Copyright (C) 2009 - 2010 by OpenGamma Inc.
 * 
 * Please see distribution for license.
 */
package com.opengamma.financial.model.cashflow;

import java.util.Iterator;

import javax.time.calendar.ZonedDateTime;

import com.opengamma.financial.model.bond.BondYieldCalculator;
import com.opengamma.financial.model.bond.DiscretelyCompoundedBondYieldCalculator;
import com.opengamma.financial.model.interestrate.InterestRateModel;
import com.opengamma.timeseries.DoubleTimeSeries;
import com.opengamma.util.time.DateUtil;

/**
 * @author emcleod
 * 
 */
public class MacaulayDurationCalculator {
  private final BondYieldCalculator _yield = new DiscretelyCompoundedBondYieldCalculator();

  public double calculate(final DoubleTimeSeries cashFlows, final InterestRateModel<Double> rates, final double price, final ZonedDateTime date) {
    if (cashFlows == null)
      throw new IllegalArgumentException("Cash flow time series was null");
    if (cashFlows.isEmpty())
      throw new IllegalArgumentException("Cash flow time series was empty");
    if (rates == null)
      throw new IllegalArgumentException("Interest rate model was null");
    if (date == null)
      throw new IllegalArgumentException("Date was null");
    final Iterator<ZonedDateTime> iter = cashFlows.timeIterator();
    double sum = 0, t, pv;
    ZonedDateTime d;
    final double y = _yield.calculate(cashFlows, price, date);
    while (iter.hasNext()) {
      d = iter.next();
      t = DateUtil.getDifferenceInYears(date, d);
      pv = cashFlows.getDataPoint(d) * Math.pow(1 + y, -t);
      sum += t * pv;
    }
    return sum / price;
  }
}
{'fraction_non_alphanumeric': 0.1035175879396985, 'fraction_numerical': 0.009045226130653266, 'mean_word_length': 4.212041884816754, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1118266', 'n_tokens_mistral': 637, 'n_tokens_neox': 589, 'n_words': 155}