net.finmath.marketdata2.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.marketdata2.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.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import net.finmath.marketdata2.interpolation.RationalFunctionInterpolation;
import net.finmath.marketdata2.model.AnalyticModel;
import net.finmath.montecarlo.RandomVariableFromDoubleArray;
import net.finmath.stochastic.RandomVariable;
import net.finmath.time.FloatingpointDate;
/**
* This class represents a curveFromInterpolationPoints 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 curveFromInterpolationPoints 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 curveFromInterpolationPoints, please use the inner class CurveBuilder (a builder pattern).
*
* For a demo on how to construct and/or calibrate a curveFromInterpolationPoints 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
}
private static class Point implements Comparable, Serializable {
private static final long serialVersionUID = 8857387999991917430L;
private final double time;
private RandomVariable 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 RandomVariable value, final boolean isParameter) {
super();
this.time = time;
this.value = value;
this.isParameter = isParameter;
}
@Override
public int compareTo(final Point point) {
// Ordering of the curveFromInterpolationPoints points with respect to time.
if(time < point.time) {
return -1;
}
if(time > point.time) {
return +1;
}
return 0;
}
@Override
public Object clone() {
return new Point(time,value,isParameter);
}
}
/**
* A builder (following the builder pattern) for CurveFromInterpolationPoints objects.
* Allows to successively construct a curveFromInterpolationPoints object by adding points.
*
* @author Christian Fries
*/
public static class Builder implements CurveBuilder {
private CurveInterpolation curveInterpolation = null;
/**
* Build a curveFromInterpolationPoints.
*/
public Builder() {
curveInterpolation = new CurveInterpolation(null, null);
}
/**
* Build a curveFromInterpolationPoints with a given name and given reference date.
*
* @param name The name of this curveFromInterpolationPoints.
* @param referenceDate The reference date for this curveFromInterpolationPoints, i.e., the date which defined t=0.
*/
public Builder(final String name, final LocalDate referenceDate) {
curveInterpolation = new CurveInterpolation(name, referenceDate);
}
/**
* Build a curveFromInterpolationPoints by cloning a given curveFromInterpolationPoints.
*
* @param curveInterpolation A curveFromInterpolationPoints to be used as starting point for the new curveFromInterpolationPoints.
* @throws CloneNotSupportedException Thrown, when the curveFromInterpolationPoints 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() {
final CurveInterpolation buildCurve = curveInterpolation;
curveInterpolation = null;
return buildCurve;
}
/**
* Set the interpolation method of the curveFromInterpolationPoints.
*
* @param interpolationMethod The interpolation method of the curveFromInterpolationPoints.
* @return A self reference to this curveFromInterpolationPoints build object.
*/
public CurveBuilder setInterpolationMethod(final InterpolationMethod interpolationMethod) {
curveInterpolation.interpolationMethod = interpolationMethod;
return this;
}
/**
* Set the extrapolation method of the curveFromInterpolationPoints.
*
* @param extrapolationMethod The extrapolation method of the curveFromInterpolationPoints.
* @return A self reference to this curveFromInterpolationPoints build object.
*/
public CurveBuilder setExtrapolationMethod(final ExtrapolationMethod extrapolationMethod) {
curveInterpolation.extrapolationMethod = extrapolationMethod;
return this;
}
/**
* Set the interpolationEntity of the curveFromInterpolationPoints.
*
* @param interpolationEntity The interpolation entity of the curveFromInterpolationPoints.
* @return A self reference to this curveFromInterpolationPoints build object.
*/
public CurveBuilder setInterpolationEntity(final InterpolationEntity interpolationEntity) {
curveInterpolation.interpolationEntity = interpolationEntity;
return this;
}
/* (non-Javadoc)
* @see net.finmath.marketdata.model.curves.CurveBuilderInterface#addPoint(double, double, boolean)
*/
@Override
public CurveBuilder addPoint(final double time, final RandomVariable value, final boolean isParameter) {
curveInterpolation.addPoint(time, value, isParameter);
return this;
}
}
private ArrayList points = new ArrayList<>();
private ArrayList pointsBeingParameters = new ArrayList<>();
private InterpolationMethod interpolationMethod = InterpolationMethod.LINEAR; //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 SoftReference