net.finmath.marketdata.model.curves.Curve 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. All rights reserved. Contact: [email protected].
*
* Created on 20.05.2005
*/
package net.finmath.marketdata.model.curves;
import java.io.Serializable;
import java.lang.ref.SoftReference;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import net.finmath.interpolation.RationalFunctionInterpolation;
import net.finmath.marketdata.model.AnalyticModelInterface;
/**
* 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.Curve.InterpolationMethod}.
* For the extrapolation methods provided see {@link net.finmath.marketdata.model.curves.Curve.ExtrapolationMethod}.
* For the possible interpolation entities see {@link net.finmath.marketdata.model.curves.Curve.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
*/
public class Curve 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;
public double time;
public double value;
public boolean isParameter;
/**
* @param time
* @param value
*/
public Point(double time, double value, boolean isParameter) {
super();
this.time = time;
this.value = value;
this.isParameter = isParameter;
}
@Override
public int compareTo(Point point) {
if(this.time < point.time) return -1;
if(this.time > point.time) return +1;
return 0;
}
@Override
public Object clone() {
return new Point(time,value,isParameter);
}
}
/**
* A builder (following the builder pattern) for Curve objects.
* Allows to successively construct a curve object by adding points.
*
* @author Christian Fries
*/
public static class CurveBuilder implements CurveBuilderInterface {
private Curve curve = null;
/**
* Build a curve.
*/
public CurveBuilder() {
curve = new Curve(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 CurveBuilder(String name, Calendar referenceDate) {
curve = new Curve(name, referenceDate);
}
/**
* Build a curve by cloning a given curve.
*
* @param curve A curve to be used as starting point for the new curve.
* @throws CloneNotSupportedException Thrown, when the curve could not be cloned.
*/
public CurveBuilder(Curve curve) throws CloneNotSupportedException {
this.curve = (Curve)curve.clone();
}
/* (non-Javadoc)
* @see net.finmath.marketdata.model.curves.CurveBuilderInterface#build()
*/
@Override
public CurveInterface build() throws CloneNotSupportedException {
Curve buildCurve = curve;
curve = null;
return buildCurve;
}
/**
* 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 CurveBuilderInterface setInterpolationMethod(InterpolationMethod interpolationMethod) {
curve.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 CurveBuilderInterface setExtrapolationMethod(ExtrapolationMethod extrapolationMethod) {
curve.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 CurveBuilderInterface setInterpolationEntity(InterpolationEntity interpolationEntity) {
curve.interpolationEntity = interpolationEntity;
return this;
}
/* (non-Javadoc)
* @see net.finmath.marketdata.model.curves.CurveBuilderInterface#addPoint(double, double, boolean)
*/
@Override
public CurveBuilderInterface addPoint(double time, double value, boolean isParameter) {
curve.addPoint(time, value, isParameter);
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 SoftReference
© 2015 - 2025 Weber Informatics LLC | Privacy Policy