net.finmath.montecarlo.IndependentIncrementsFromICDF 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 09.02.2004
*/
package net.finmath.montecarlo;
import java.io.IOException;
import java.io.Serializable;
import java.util.function.DoubleUnaryOperator;
import java.util.function.IntFunction;
import net.finmath.randomnumbers.MersenneTwister;
import net.finmath.stochastic.RandomVariable;
import net.finmath.time.TimeDiscretization;
/**
* Implementation of a time-discrete n-dimensional sequence of independent increments
* W = (W1,...,Wn) form a given set of inverse
* cumulative distribution functions.
*
* Independent increments is a sequence of independent random variables index
* by the time index associated with the time discretization. At each time step
* the increment is a d-dimensional random variable \( Z(t_{i}) \), where d is numberOfFactors
.
* where each component of \( Z_{j}(t_{i}) \) is given by
* \[
* Z_{j}(t_{i}) = ICDF_{i,j}(U_{i,j})
* \]
* for a sequence of independent uniform distributes random variables U_{i,j}.
*
* The inverse cumulative distribution functions \( ICDF_{i,j} \) are given by
* inverseCumulativeDistributionFunctions
as the
* map \( i \mapsto ( j \mapsto ICDF_{i,j} ) \) (here i is the time index and j is the factor (component).
*
* Each \( U_{i,j} \) is samples using numberOfPaths
.
*
* The class is immutable and thread safe. It uses lazy initialization.
*
* @author Christian Fries
* @version 1.6
*/
public class IndependentIncrementsFromICDF implements IndependentIncrements, Serializable {
/**
*
*/
private static final long serialVersionUID = 6270884840989559532L;
private final TimeDiscretization timeDiscretization;
private final int numberOfFactors;
private final int numberOfPaths;
private final int seed;
private final RandomVariableFactory randomVariableFactory;
private transient RandomVariable[][] increments;
private transient Object incrementsLazyInitLock = new Object();
private final IntFunction> inverseCumulativeDistributionFunctions;
/**
* Construct the simulation of independent increments.
*
* Independent increments is a sequence of independent random variables index
* by the time index associated with the time discretization. At each time step
* the increment is a d-dimensional random variable \( Z(t_{i}) \), where d is numberOfFactors
.
* where each component of \( Z_{j}(t_{i}) \) is given by
* \[
* Z_{j}(t_{i}) = ICDF_{i,j}(U_{i,j})
* \]
* for a sequence of independent uniform distributes random variables U_{i,j}.
*
* The inverse cumulative distribution functions \( ICDF_{i,j} \) are given by
* inverseCumulativeDistributionFunctions
as the
* map \( i \mapsto ( j \mapsto ICDF_{i,j} ) \) (here i is the time index and j is the factor (component).
*
* Each \( U_{i,j} \) is samples using numberOfPaths
.
*
* The constructor allows to set the factory to be used for the construction of
* random variables. This allows to generate increments represented
* by different implementations of the RandomVariable
* (e.g. the RandomVariableFromFloatArray internally using float representations).
*
* @param timeDiscretization The time discretization used for the increments.
* @param numberOfFactors Number of factors.
* @param numberOfPaths Number of paths to simulate.
* @param seed The seed of the random number generator.
* @param inverseCumulativeDistributionFunctions A map from the timeIndices to a map from the from the factors to the corresponding inverse cumulative distribution function.
* @param randomVariableFactory Factory to be used to create random variable.
*/
public IndependentIncrementsFromICDF(
final TimeDiscretization timeDiscretization,
final int numberOfFactors,
final int numberOfPaths,
final int seed,
final IntFunction> inverseCumulativeDistributionFunctions,
final RandomVariableFactory randomVariableFactory) {
super();
this.timeDiscretization = timeDiscretization;
this.numberOfFactors = numberOfFactors;
this.numberOfPaths = numberOfPaths;
this.seed = seed;
this.inverseCumulativeDistributionFunctions = inverseCumulativeDistributionFunctions;
this.randomVariableFactory = randomVariableFactory;
increments = null; // Lazy initialization
}
/**
* Construct the simulation of independet increments.
*
* The independent increments is a sequence of independent random variables index
* by the time index associated with the time discretization. At each time step
* the increment is a d-dimensional random variable \( Z(t_{i}) \), where d is numberOfFactors
.
* where each component of \( Z_{j}(t_{i}) \) is given by
* \[
* Z_{j}(t_{i}) = ICDF_{i,j}(U_{i,j})
* \]
* for a sequence of independent uniform distributes random variables U_{i,j}.
*
* The inverse cumulative distribution functions \( ICDF_{i,j} \) are given by
* inverseCumulativeDistributionFunctions
as the
* map \( i \mapsto ( j \mapsto ICDF_{i,j} ) \) (here i is the time index and j is the factor (component).
*
* Each \( U_{i,j} \) is samples using numberOfPaths
.
*
* @param timeDiscretization The time discretization used for the increments.
* @param numberOfFactors Number of factors.
* @param numberOfPaths Number of paths to simulate.
* @param seed The seed of the random number generator.
* @param inverseCumulativeDistributionFunctions A map from the timeIndices to a map from the from the factors to the corresponding inverse cumulative distribution function.
*/
public IndependentIncrementsFromICDF(
final TimeDiscretization timeDiscretization,
final int numberOfFactors,
final int numberOfPaths,
final int seed,
final IntFunction> inverseCumulativeDistributionFunctions) {
this(timeDiscretization, numberOfFactors, numberOfPaths, seed, inverseCumulativeDistributionFunctions, new RandomVariableFromArrayFactory());
}
@Override
public IndependentIncrements getCloneWithModifiedSeed(final int seed) {
return new IndependentIncrementsFromICDF(getTimeDiscretization(), getNumberOfFactors(), getNumberOfPaths(), seed, inverseCumulativeDistributionFunctions, randomVariableFactory);
}
@Override
public IndependentIncrements getCloneWithModifiedTimeDiscretization(final TimeDiscretization newTimeDiscretization) {
return new IndependentIncrementsFromICDF(newTimeDiscretization, getNumberOfFactors(), getNumberOfPaths(), getSeed(), inverseCumulativeDistributionFunctions, randomVariableFactory);
}
@Override
public RandomVariable getIncrement(final int timeIndex, final int factor) {
// Thread safe lazy initialization
synchronized(incrementsLazyInitLock) {
if(increments == null) {
doGenerateIncrements();
}
}
/*
* We return an immutable object which ensures that the receiver does not alter the data.
*/
return increments[timeIndex][factor];
}
/**
* Lazy initialization of brownianIncrement. Synchronized to ensure thread safety of lazy init.
*/
private void doGenerateIncrements() {
if(increments != null) {
return; // Nothing to do
}
// Create random number sequence generator
final MersenneTwister mersenneTwister = new MersenneTwister(seed);
// Allocate memory
final double[][][] incrementsArray = new double[timeDiscretization.getNumberOfTimeSteps()][numberOfFactors][numberOfPaths];
// Pre-fetch icdfs
final DoubleUnaryOperator[][] inverseCumulativeDistributionFunctions = new DoubleUnaryOperator[timeDiscretization.getNumberOfTimeSteps()][numberOfFactors];
for(int timeIndex=0; timeIndex