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

net.finmath.montecarlo.conditionalexpectation.MonteCarloConditionalExpectationRegression Maven / Gradle / Ivy

Go to download

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

The newest version!
/*
 * (c) Copyright Christian P. Fries, Germany. Contact: [email protected].
 *
 * Created on 13.08.2004
 */
package net.finmath.montecarlo.conditionalexpectation;

import org.apache.commons.math3.linear.Array2DRowRealMatrix;
import org.apache.commons.math3.linear.ArrayRealVector;
import org.apache.commons.math3.linear.DecompositionSolver;
import org.apache.commons.math3.linear.SingularValueDecomposition;

import net.finmath.stochastic.ConditionalExpectationEstimator;
import net.finmath.stochastic.RandomVariable;

/**
 * A service that allows to estimate conditional expectation via regression.
 *
 * In oder to estimate the conditional expectation, basis functions have to be
 * specified.
 *
 * The class can either estimate and predict the conditional expectation within
 * the same simulation (which will eventually introduce a small foresight bias)
 * or use a different simulation for estimation (using basisFunctionsEstimator)
 * to predict conditional expectation within another simulation
 * (using basisFunctionsPredictor). In the latter case, the
 * basis functions have to correspond to the same entities, however, generated in
 * different simulations (number of path, etc., may be different).
 *
 * @author Christian Fries
 * @version 1.0
 */
public class MonteCarloConditionalExpectationRegression implements ConditionalExpectationEstimator {

	/**
	 * Interface for objects specifying regression basis functions (a vector of random variables).
	 *
	 * @author Christian Fries
	 */
	public interface RegressionBasisFunctions {
		RandomVariable[] getBasisFunctions();
	}

	/**
	 * Wrapper to an array of RandomVariable[] implementing RegressionBasisFunctions
	 * @author Christian Fries
	 */
	public static class RegressionBasisFunctionsGiven implements RegressionBasisFunctions {
		private final RandomVariable[] basisFunctions;

		public RegressionBasisFunctionsGiven(final RandomVariable[] basisFunctions) {
			super();
			this.basisFunctions = basisFunctions;
		}

		@Override
		public RandomVariable[] getBasisFunctions() {
			return basisFunctions;
		}
	}


	private RegressionBasisFunctions basisFunctionsEstimator		= null;
	private RegressionBasisFunctions basisFunctionsPredictor		= null;

	private transient DecompositionSolver solver;
	private final transient Object solverLock;

	public MonteCarloConditionalExpectationRegression() {
		super();
		solverLock = new Object();	// Lock for LazyInit of solver.
	}

	/**
	 * Creates a class for conditional expectation estimation.
	 *
	 * @param basisFunctions A vector of random variables to be used as basis functions.
	 */
	public MonteCarloConditionalExpectationRegression(final RandomVariable[] basisFunctions) {
		this();
		basisFunctionsEstimator = new RegressionBasisFunctionsGiven(getNonZeroBasisFunctions(basisFunctions));
		basisFunctionsPredictor = basisFunctionsEstimator;
	}

	/**
	 * Creates a class for conditional expectation estimation.
	 *
	 * @param basisFunctionsEstimator A vector of random variables to be used as basis functions for estimation.
	 * @param basisFunctionsPredictor A vector of random variables to be used as basis functions for prediction.
	 */
	public MonteCarloConditionalExpectationRegression(final RandomVariable[] basisFunctionsEstimator, final RandomVariable[] basisFunctionsPredictor) {
		this();
		this.basisFunctionsEstimator = new RegressionBasisFunctionsGiven(getNonZeroBasisFunctions(basisFunctionsEstimator));
		this.basisFunctionsPredictor = new RegressionBasisFunctionsGiven(getNonZeroBasisFunctions(basisFunctionsPredictor));
	}

	@Override
	public RandomVariable getConditionalExpectation(final RandomVariable randomVariable) {
		// Get regression parameters x as the solution of XTX x = XT y
		final double[] linearRegressionParameters = getLinearRegressionParameters(randomVariable);

		// Calculate estimate, i.e. X x
		final RandomVariable[] basisFunctions = basisFunctionsPredictor.getBasisFunctions();
		RandomVariable conditionalExpectation = basisFunctions[0].mult(linearRegressionParameters[0]);
		for(int i=1; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy