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

net.finmath.montecarlo.GammaProcess 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 Christian P. Fries, Germany. Contact: [email protected].
 *
 * Created on 29.06.2004
 */
package net.finmath.montecarlo;

import java.io.Serializable;

import net.finmath.functions.GammaDistribution;
import net.finmath.randomnumbers.MersenneTwister;
import net.finmath.stochastic.RandomVariable;
import net.finmath.time.TimeDiscretization;

/**
 * Implementation of a time-discrete n-dimensional Gamma process
 * \(
 * \Gamma = (\Gamma_{1},\ldots,\Gamma_{n})
 * \), where \( \Gamma_{i} \) is
 * a Gamma process and \( \Gamma_{i} \), \( \Gamma_{j} \) are
 * independent for i not equal j.
 *
 * The increments \( \Delta \Gamma \) are Gamma distributed with shape parameter shape * (t-s)
 * for \( t-s = \Delta t \) and scale parameter 1.
 *
 * Here the dimension n is called factors since this Gamma process is used to
 * generate multi-dimensional multi-factor Levy processes and there one might
 * use a different number of factors to generate Levy processes of different
 * dimension.
 *
 * The quintruppel (time discretization, number of factors, number of paths, seed, shape)
 * defines the state of an object of this class, i.e., GammaProcess for which
 * there parameters agree, generate the same random numbers.
 *
 * The class is immutable and thread safe. It uses lazy initialization.
 *
 * @author Christian Fries
 * @version 1.6
 */
public class GammaProcess implements IndependentIncrements, Serializable {

	/**
	 *
	 */
	private static final long serialVersionUID = -5430067621669213475L;

	private final double shape;
	private final double scale;

	private final TimeDiscretization						timeDiscretization;

	private final int			numberOfFactors;
	private final int			numberOfPaths;
	private final int			seed;

	private final RandomVariableFactory randomVariableFactory = new RandomVariableFromArrayFactory();

	private transient RandomVariable[][]	gammaIncrements;

	/**
	 * Construct a Gamma process with a given shape parameter.
	 *
	 * @param timeDiscretization The time discretization used for the Gamma increments.
	 * @param numberOfFactors Number of factors.
	 * @param numberOfPaths Number of paths to simulate.
	 * @param seed The seed of the random number generator.
	 * @param shape The shape parameter of the Gamma distribution.
	 * @param scale The scale parameter of the Gamma distribution.
	 */
	public GammaProcess(
			final TimeDiscretization timeDiscretization,
			final int numberOfFactors,
			final int numberOfPaths,
			final int seed,
			final double shape,
			final double scale) {
		super();
		this.timeDiscretization = timeDiscretization;
		this.numberOfFactors	= numberOfFactors;
		this.numberOfPaths		= numberOfPaths;
		this.seed				= seed;
		this.shape				= shape;
		this.scale				= scale;

		gammaIncrements	= null; 	// Lazy initialization
	}

	/**
	 * Construct a Gamma process with a given shape parameter.
	 *
	 * @param timeDiscretization The time discretization used for the Gamma increments.
	 * @param numberOfFactors Number of factors.
	 * @param numberOfPaths Number of paths to simulate.
	 * @param seed The seed of the random number generator.
	 * @param shape The shape parameter of the Gamma distribution.
	 */
	public GammaProcess(
			final TimeDiscretization timeDiscretization,
			final int numberOfFactors,
			final int numberOfPaths,
			final int seed,
			final double shape) {
		this(timeDiscretization, numberOfFactors, numberOfPaths, seed, shape, 1.0);
	}

	@Override
	public IndependentIncrements getCloneWithModifiedSeed(final int seed) {
		return new GammaProcess(getTimeDiscretization(), getNumberOfFactors(), getNumberOfPaths(), seed, shape);
	}

	@Override
	public IndependentIncrements getCloneWithModifiedTimeDiscretization(final TimeDiscretization newTimeDiscretization) {
		/// @TODO This can be improved: a complete recreation of the Gamma process wouldn't be necessary!
		return new GammaProcess(newTimeDiscretization, getNumberOfFactors(), getNumberOfPaths(), getSeed(), shape);
	}

	@Override
	public RandomVariable getIncrement(final int timeIndex, final int factor) {
		// Thread safe lazy initialization
		synchronized(this) {
			if(gammaIncrements == null) {
				doGenerateGammaIncrements();
			}
		}

		/*
		 *  For performance reasons we return directly the stored data (no defensive copy).
		 *  We return an immutable object to ensure that the receiver does not alter the data.
		 */
		return gammaIncrements[timeIndex][factor];
	}

	/**
	 * Lazy initialization of gammaIncrement. Synchronized to ensure thread safety of lazy init.
	 */
	private void doGenerateGammaIncrements() {
		if(gammaIncrements != null) {
			return;	// Nothing to do
		}

		// Create random number sequence generator
		final MersenneTwister			mersenneTwister		= new MersenneTwister(seed);

		// Allocate memory
		final double[][][] gammaIncrementsArray = new double[timeDiscretization.getNumberOfTimeSteps()][numberOfFactors][numberOfPaths];

		// Pre-calculate distributions
		final GammaDistribution[] gammaDistributions = new GammaDistribution[timeDiscretization.getNumberOfTimeSteps()];
		for(int timeIndex=0; timeIndex




© 2015 - 2024 Weber Informatics LLC | Privacy Policy