net.finmath.montecarlo.BrownianMotion 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 09.02.2004
*/
package net.finmath.montecarlo;
import java.io.Serializable;
import net.finmath.stochastic.RandomVariableInterface;
import net.finmath.time.TimeDiscretizationInterface;
import cern.jet.random.engine.MersenneTwister64;
/**
* Implementation of a time-discrete n-dimensional Brownian motion
* W = (W1,...,Wn) where Wi is
* a Brownian motion and Wi, Wj are
* independent for i not equal j.
*
* For a correlated Brownian motion with see
* {@link net.finmath.montecarlo.CorrelatedBrownianMotion}.
*
* Here the dimension n is called factors since this Brownian motion is used to
* generate multi-dimensional multi-factor Ito processes and there one might
* use a different number of factors to generate Ito processes of different
* dimension.
*
* The quadruppel (time discretization, number of factors, number of paths, seed)
* defines the state of an object of this class, i.e., BrownianMotion 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 BrownianMotion implements BrownianMotionInterface, Serializable {
/**
*
*/
private static final long serialVersionUID = -5430067621669213475L;
private final TimeDiscretizationInterface timeDiscretization;
private final int numberOfFactors;
private final int numberOfPaths;
private final int seed;
private AbstractRandomVariableFactory randomVariableFactory = new RandomVariableFactory();
private transient RandomVariableInterface[][] brownianIncrements;
/**
* Construct a Brownian motion.
*
* @param timeDiscretization The time discretization used for the Brownian increments.
* @param numberOfFactors Number of factors.
* @param numberOfPaths Number of paths to simulate.
* @param seed The seed of the random number generator.
*/
public BrownianMotion(
TimeDiscretizationInterface timeDiscretization,
int numberOfFactors,
int numberOfPaths,
int seed) {
super();
this.timeDiscretization = timeDiscretization;
this.numberOfFactors = numberOfFactors;
this.numberOfPaths = numberOfPaths;
this.seed = seed;
this.brownianIncrements = null; // Lazy initialization
}
/* (non-Javadoc)
* @see net.finmath.montecarlo.BrownianMotionInterface#getCloneWithModifiedSeed(int)
*/
@Override
public BrownianMotionInterface getCloneWithModifiedSeed(int seed) {
return new BrownianMotion(getTimeDiscretization(), getNumberOfFactors(), getNumberOfPaths(), seed);
}
/* (non-Javadoc)
* @see net.finmath.montecarlo.BrownianMotionInterface#getCloneWithModifiedTimeDiscretization(net.finmath.time.TimeDiscretizationInterface)
*/
@Override
public BrownianMotionInterface getCloneWithModifiedTimeDiscretization(TimeDiscretizationInterface newTimeDiscretization) {
/// @TODO This can be improved: a complete recreation of the Brownian motion wouldn't be necessary!
return new BrownianMotion(newTimeDiscretization, getNumberOfFactors(), getNumberOfPaths(), getSeed());
}
/* (non-Javadoc)
* @see net.finmath.montecarlo.BrownianMotionInterface#getBrownianIncrement(int, int)
*/
@Override
public RandomVariableInterface getBrownianIncrement(int timeIndex, int factor) {
// Thread safe lazy initialization
synchronized(this) {
if(brownianIncrements == null) doGenerateBrownianMotion();
}
/*
* 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 brownianIncrements[timeIndex][factor];
}
/**
* Lazy initialization of brownianIncrement. Synchronized to ensure thread safety of lazy init.
*/
private void doGenerateBrownianMotion() {
if(brownianIncrements != null) return; // Nothing to do
// Create random number sequence generator (we use MersenneTwister64 from colt)
MersenneTwister64 mersenneTwister = new MersenneTwister64(seed);
// Allocate memory
double[][][] brownianIncrementsArray = new double[timeDiscretization.getNumberOfTimeSteps()][numberOfFactors][numberOfPaths];
// Pre-calculate square roots of deltaT
double[] sqrtOfTimeStep = new double[timeDiscretization.getNumberOfTimeSteps()];
for(int timeIndex=0; timeIndex
© 2015 - 2025 Weber Informatics LLC | Privacy Policy