All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.finmath.marketdata.model.curves.locallinearregression.CurveEstimation Maven / Gradle / Ivy

Go to download

finmath lib is a Mathematical Finance Library in Java. It provides algorithms and methodologies related to mathematical finance.

There is a newer version: 6.0.19
Show newest version
/*
 * (c) Copyright finmath.net, Germany. Contact: [email protected].
 *
 * Created on 01.06.2018
 */
package net.finmath.marketdata.model.curves.locallinearregression;

import java.time.LocalDate;

import org.apache.commons.math3.distribution.AbstractRealDistribution;
import org.apache.commons.math3.distribution.CauchyDistribution;
import org.apache.commons.math3.distribution.LaplaceDistribution;
import org.apache.commons.math3.distribution.NormalDistribution;
import org.jblas.DoubleMatrix;
import org.jblas.Solve;

import net.finmath.marketdata.model.curves.Curve;
import net.finmath.marketdata.model.curves.CurveInterpolation;
import net.finmath.marketdata.model.curves.DiscountCurveInterpolation;

/**
 * This class implements the method of local linear regression with discrete kernel function, see see https://ssrn.com/abstract=3073942
 *
 * In particular it represents the implementation of proposition 2 and 3 of the paper.
 *
 * This class allows choosing between three different kernel functions, i.e. a normal, a Laplace or a Cauchy kernel.
 *
 * For the kernel types provided see {@link net.finmath.marketdata.model.curves.locallinearregression.CurveEstimation.Distribution}.
 *
 * The resulting curve is piecewise linear. That means, only the knot points of the curve are computed in this algorithm.
 * The final curve is then provided with linear interpolation of the knot points,
 * see {@link net.finmath.marketdata.model.curves.CurveInterpolation}.
 *
 * @author Moritz Scherrmann
 * @author Christian Fries
 * @version 1.0
 */
public class CurveEstimation{

	/**
	 * Possible kernel types.
	 */
	public enum Distribution {
		NORMAL,
		LAPLACE,
		CAUCHY
	}

	private final LocalDate referenceDate;
	private final double bandwidth;
	private final double[] independentValues;
	private final double[] dependentValues;
	private final Partition partition;
	private final DiscountCurveInterpolation regressionCurve=null;
	private AbstractRealDistribution kernel;

	/**
	 * Creates a curve estimation object.
	 *
	 * @param referenceDate The reference date for the resulting regression curve, i.e., the date which defined t=0.
	 * @param bandwidth The bandwidth parameter of the regression.
	 * @param independentValues The realization of a random variable X.
	 * @param dependentValues The realization of a random variable Y.
	 * @param partitionValues The values to create a partition. It is important that min(partition) ≤ min(X) and max(partition) ≥ max(X).
	 * @param weight The weight needed to create a partition.
	 * @param distribution The kernel type.
	 */
	public CurveEstimation(
			final LocalDate referenceDate,
			final double bandwidth,
			final double[] independentValues,
			final double[] dependentValues,
			final double[] partitionValues,
			final double weight,
			final Distribution distribution){
		this.referenceDate = referenceDate;
		this.bandwidth = bandwidth;
		this.independentValues = independentValues;
		this.dependentValues = dependentValues;
		partition = new Partition(partitionValues.clone(), weight);


		switch(distribution) {
		case LAPLACE:
			kernel=new LaplaceDistribution(0,1);
			break;
		case CAUCHY:
			kernel=new CauchyDistribution();
			break;
		case NORMAL:
		default:
			kernel=new NormalDistribution();
			break;
		}

	}

	/**
	 * Creates a curve estimation object with a normal kernel.
	 *
	 * @param referenceDate The reference date for the resulting regression curve, i.e., the date which defined t=0.
	 * @param bandwidth The bandwidth parameter of the regression.
	 * @param independentValues The realization of a random variable X.
	 * @param dependentValues The realization of a random variable Y.
	 * @param partitionValues The values to create a partition. It is important that min(partition) ≤ min(X) and max(partition) ≥ max(X).
	 * @param weight The weight needed to create a partition.
	 */
	public CurveEstimation(
			final LocalDate referenceDate,
			final double bandwidth,
			final double[] independentValues,
			final double[] dependentValues,
			final double[] partitionValues,
			final double weight) {
		this(referenceDate,bandwidth,independentValues,dependentValues,partitionValues,weight,Distribution.NORMAL);
	}

	/**
	 * Returns the curve resulting from the local linear regression with discrete kernel.
	 *
	 * @return The regression curve.
	 */
	public Curve getRegressionCurve(){
		// @TODO Add threadsafe lazy init.
		if(regressionCurve !=null) {
			return regressionCurve;
		}
		final DoubleMatrix a = solveEquationSystem();
		final double[] curvePoints=new double[partition.getLength()];
		curvePoints[0]=a.get(0);
		for(int i=1;i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy