net.finmath.time.ScheduleFromPeriods Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of finmath-lib Show documentation
Show all versions of finmath-lib Show documentation
finmath lib is a Mathematical Finance Library in Java.
It provides algorithms and methodologies related to mathematical finance.
/*
* (c) Copyright Christian P. Fries, Germany. Contact: [email protected].
*
* Created on 07.09.2013
*/
package net.finmath.time;
import java.io.Serializable;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import net.finmath.time.daycount.DayCountConvention;
/**
* A schedule of interest rate periods with
* a fixing and payment.
*
* The periods have two representations: one a {@link net.finmath.time.Period}
* which contains {@link java.time.LocalDate} dates and
* an alternative representation using doubles.
*
* Within a schedule, the mapping from doubles to dates is one to one.
*
* @author Christian Fries
* @version 1.0
*/
public class ScheduleFromPeriods implements Schedule, Serializable {
private static final long serialVersionUID = 4460864939790714361L;
private final LocalDate referenceDate;
private final List periods;
private final DayCountConvention daycountconvention;
private final double[] fixingTimes;
private final double[] paymentTimes;
private final double[] periodStartTimes;
private final double[] periodEndTimes;
private final double[] periodLength;
public ScheduleFromPeriods(final LocalDate referenceDate, final DayCountConvention daycountconvention, final Period... periods) {
this(referenceDate, Arrays.asList(periods), daycountconvention);
}
public ScheduleFromPeriods(final LocalDate referenceDate, final List periods, final DayCountConvention daycountconvention) {
super();
if(referenceDate == null) {
throw new IllegalArgumentException("referenceDate must not be null.");
}
this.referenceDate = referenceDate;
this.periods = periods;
this.daycountconvention = daycountconvention;
// Precalculate dates to yearfrac doubles
fixingTimes = new double[periods.size()];
paymentTimes = new double[periods.size()];
periodStartTimes = new double[periods.size()];
periodEndTimes = new double[periods.size()];
periodLength = new double[periods.size()];
for(int periodIndex=0; periodIndex < periods.size(); periodIndex++) {
fixingTimes[periodIndex] = FloatingpointDate.getFloatingPointDateFromDate(referenceDate, periods.get(periodIndex).getFixing());
paymentTimes[periodIndex] = FloatingpointDate.getFloatingPointDateFromDate(referenceDate, periods.get(periodIndex).getPayment());
periodStartTimes[periodIndex] = FloatingpointDate.getFloatingPointDateFromDate(referenceDate, periods.get(periodIndex).getPeriodStart());
periodEndTimes[periodIndex] = FloatingpointDate.getFloatingPointDateFromDate(referenceDate, periods.get(periodIndex).getPeriodEnd());
periodLength[periodIndex] = daycountconvention.getDaycountFraction(periods.get(periodIndex).getPeriodStart(), periods.get(periodIndex).getPeriodEnd());
}
}
@Override
public LocalDate getReferenceDate() {
return referenceDate;
}
@Override
public List getPeriods() {
return periods;
}
@Override
public DayCountConvention getDaycountconvention() {
return daycountconvention;
}
@Override
public int getNumberOfPeriods() {
return periods.size();
}
@Override
public Period getPeriod(final int periodIndex) {
return periods.get(periodIndex);
}
@Override
public double getFixing(final int periodIndex) {
return fixingTimes[periodIndex];
}
@Override
public double getPayment(final int periodIndex) {
return paymentTimes[periodIndex];
}
@Override
public double getPeriodStart(final int periodIndex) {
return periodStartTimes[periodIndex];
}
@Override
public double getPeriodEnd(final int periodIndex) {
return periodEndTimes[periodIndex];
}
@Override
public double getPeriodLength(final int periodIndex) {
return periodLength[periodIndex];
}
@Override
public Iterator iterator() {
return periods.iterator();
}
@Override
public int getPeriodIndex(final double time) {
if(time< getPeriodStart(0)|| time>= getPeriodEnd(getNumberOfPeriods()-1)) {
throw new IllegalArgumentException("Time point not included");
}
for(int i=0; i