net.finmath.marketdata.model.curves.CurveInterpolation 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 20.05.2005
*/
package net.finmath.marketdata.model.curves;
import java.io.IOException;
import java.io.Serializable;
import java.lang.ref.SoftReference;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import net.finmath.interpolation.RationalFunctionInterpolation;
import net.finmath.marketdata.model.AnalyticModel;
import net.finmath.time.FloatingpointDate;
/**
* This class represents a curve build from a set of points in 2D.
*
* It provides different interpolation and extrapolation methods applied to a transformation of the input point,
* examples are
*
* - linear interpolation of the input points
* - linear interpolation of the log of the input points
* - linear interpolation of the log of the input points divided by their respective time
* - cubic spline interpolation of the input points (or a function of the input points) (the curve will be C1).
* - Akima interpolation of the input points (or a function of the input points).
* - etc.
*
*
*
*
* For the interpolation methods provided see {@link net.finmath.marketdata.model.curves.CurveInterpolation.InterpolationMethod}.
* For the extrapolation methods provided see {@link net.finmath.marketdata.model.curves.CurveInterpolation.ExtrapolationMethod}.
* For the possible interpolation entities see {@link net.finmath.marketdata.model.curves.CurveInterpolation.InterpolationEntity}.
*
* To construct the curve, please use the inner class CurveBuilder (a builder pattern).
*
* For a demo on how to construct and/or calibrate a curve see, e.g.
* net.finmath.tests.marketdata.curves.CurveTest.
*
* @author Christian Fries
* @version 1.0
*/
public class CurveInterpolation extends AbstractCurve implements Serializable, Cloneable {
/**
* Possible interpolation methods.
*
* @author Christian Fries
*/
public enum InterpolationMethod {
/** Constant interpolation. Synonym of PIECEWISE_CONSTANT_LEFTPOINT. **/
PIECEWISE_CONSTANT,
/** Constant interpolation. Right continuous, i.e. using the value of the left end point of the interval. **/
PIECEWISE_CONSTANT_LEFTPOINT,
/** Constant interpolation using the value of the right end point of the interval. **/
PIECEWISE_CONSTANT_RIGHTPOINT,
/** Linear interpolation. **/
LINEAR,
/** Cubic spline interpolation. **/
CUBIC_SPLINE,
/** Akima interpolation (C1 sub-spline interpolation). **/
AKIMA,
/** Akima interpolation (C1 sub-spline interpolation) with a smoothing in the weights. **/
AKIMA_CONTINUOUS,
/** Harmonic spline interpolation (C1 sub-spline interpolation). **/
HARMONIC_SPLINE,
/** Harmonic spline interpolation (C1 sub-spline interpolation) with a monotonic filtering at the boundary points. **/
HARMONIC_SPLINE_WITH_MONOTONIC_FILTERING
}
/**
* Possible extrapolation methods.
*
* @author Christian Fries
*/
public enum ExtrapolationMethod {
/** Extrapolation using the interpolation function of the adjacent interval **/
DEFAULT,
/** Constant extrapolation. **/
CONSTANT,
/** Linear extrapolation. **/
LINEAR
}
/**
* Possible interpolation entities.
*
* @author Christian Fries
*/
public enum InterpolationEntity {
/** Interpolation is performed on the native point values, i.e. value(t) **/
VALUE,
/** Interpolation is performed on the log of the point values, i.e. log(value(t)) **/
LOG_OF_VALUE,
/** Interpolation is performed on the log of the point values divided by their respective time, i.e. log(value(t))/t **/
LOG_OF_VALUE_PER_TIME
}
/**
* Representation of a 2D curve point including the boolean property if the point is fixed or calibrateable.
*
* @author Christian Fries
*/
public static class Point implements Comparable, Serializable {
private static final long serialVersionUID = 8857387999991917430L;
private final double time;
private double value;
private final boolean isParameter;
/**
* @param time The time (or x-value) of the point.
* @param value The value (or y-value) of the point.
* @param isParameter A boolean specifying if this point is considered a "degree of freedom", e.g., in a calibration.
*/
Point(final double time, final double value, final boolean isParameter) {
super();
this.time = time;
this.value = value;
this.isParameter = isParameter;
}
@Override
public int compareTo(final Point point) {
// Ordering of the curve points with respect to time.
if(time < point.time) {
return -1;
}
if(time > point.time) {
return +1;
}
return 0;
}
public double getTime() {
return time;
}
public double getValue() {
return value;
}
public boolean isParameter() {
return isParameter;
}
@Override
public String toString() {
return "Point [time=" + time + ", value=" + value + ", isParameter=" + isParameter + "]";
}
@Override
public Object clone() {
return new Point(time,value,isParameter);
}
}
/**
* A builder (following the builder pattern) for CurveFromInterpolationPoints objects.
* Allows to successively construct a curve object by adding points.
*
* @author Christian Fries
*/
public static class Builder implements CurveBuilder {
private CurveInterpolation curveInterpolation = null;
/**
* Build a curve.
*/
public Builder() {
curveInterpolation = new CurveInterpolation(null, null);
}
/**
* Build a curve with a given name and given reference date.
*
* @param name The name of this curve.
* @param referenceDate The reference date for this curve, i.e., the date which defined t=0.
*/
public Builder(final String name, final LocalDate referenceDate) {
curveInterpolation = new CurveInterpolation(name, referenceDate);
}
/**
* Build a curve by cloning a given curve.
*
* @param curveInterpolation A curve to be used as starting point for the new curve.
* @throws CloneNotSupportedException Thrown, when the curve could not be cloned.
*/
public Builder(final CurveInterpolation curveInterpolation) throws CloneNotSupportedException {
this.curveInterpolation = curveInterpolation.clone();
}
/* (non-Javadoc)
* @see net.finmath.marketdata.model.curves.CurveBuilderInterface#build()
*/
@Override
public Curve build() throws CloneNotSupportedException {
final CurveInterpolation buildCurve = curveInterpolation;
curveInterpolation = null;
return buildCurve;
}
/* (non-Javadoc)
* @see net.finmath.marketdata.model.curves.CurveBuilderInterface#addPoint(double, double, boolean)
*/
@Override
public Builder addPoint(final double time, final double value, final boolean isParameter) {
curveInterpolation.addPoint(time, value, isParameter);
return this;
}
/**
* Set the interpolation method of the curve.
*
* @param interpolationMethod The interpolation method of the curve.
* @return A self reference to this curve build object.
*/
public Builder setInterpolationMethod(final InterpolationMethod interpolationMethod) {
curveInterpolation.interpolationMethod = interpolationMethod;
return this;
}
/**
* Set the extrapolation method of the curve.
*
* @param extrapolationMethod The extrapolation method of the curve.
* @return A self reference to this curve build object.
*/
public Builder setExtrapolationMethod(final ExtrapolationMethod extrapolationMethod) {
curveInterpolation.extrapolationMethod = extrapolationMethod;
return this;
}
/**
* Set the interpolationEntity of the curve.
*
* @param interpolationEntity The interpolation entity of the curve.
* @return A self reference to this curve build object.
*/
public Builder setInterpolationEntity(final InterpolationEntity interpolationEntity) {
curveInterpolation.interpolationEntity = interpolationEntity;
return this;
}
}
private ArrayList points = new ArrayList<>();
private ArrayList pointsBeingParameters = new ArrayList<>();
private InterpolationMethod interpolationMethod = InterpolationMethod.CUBIC_SPLINE;
private ExtrapolationMethod extrapolationMethod = ExtrapolationMethod.CONSTANT;
private InterpolationEntity interpolationEntity = InterpolationEntity.LOG_OF_VALUE;
private RationalFunctionInterpolation rationalFunctionInterpolation = null;
private transient Object rationalFunctionInterpolationLazyInitLock = new Object();
private transient SoftReference