net.finmath.montecarlo.assetderivativevaluation.MonteCarloMultiAssetBlackScholesModel 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.
The newest version!
/*
* (c) Copyright Christian P. Fries, Germany. Contact: [email protected].
*
* Created on 09.06.2014
*/
package net.finmath.montecarlo.assetderivativevaluation;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import net.finmath.exception.CalculationException;
import net.finmath.functions.LinearAlgebra;
import net.finmath.montecarlo.BrownianMotion;
import net.finmath.montecarlo.BrownianMotionFromMersenneRandomNumbers;
import net.finmath.montecarlo.RandomVariableFactory;
import net.finmath.montecarlo.RandomVariableFromArrayFactory;
import net.finmath.montecarlo.model.AbstractProcessModel;
import net.finmath.montecarlo.process.EulerSchemeFromProcessModel;
import net.finmath.montecarlo.process.EulerSchemeFromProcessModel.Scheme;
import net.finmath.montecarlo.process.MonteCarloProcess;
import net.finmath.stochastic.RandomVariable;
import net.finmath.time.TimeDiscretization;
/**
* This class implements a multi-asset Black Schole Model as Monte-Carlo simulation implementing AssetModelMonteCarloSimulationModel
.
*
* The model is
* \[
* dS_{i} = r S_{i} dt + \sigma_{i} S_{i} dW_{i}, \quad S_{i}(0) = S_{i,0},
* \]
* \[
* dN = r N dt, \quad N(0) = N_{0},
* \]
* \[
* dW_{i} dW_{j} = \rho_{i,j} dt,
* \]
*
* The class provides the model of \( S_{i} \) to an {@link net.finmath.montecarlo.process.MonteCarloProcess}
via the specification of
* \( f = exp \), \( \mu_{i} = r - \frac{1}{2} \sigma_{i}^2 \), \( \lambda_{i,j} = \sigma_{i} g_{i,j} \), i.e.,
* of the SDE
* \[
* dX_{i} = \mu_{i} dt + \lambda_{i,j} dW, \quad X_{i}(0) = \log(S_{i,0}),
* \]
* with \( S = f(X) \). See {@link net.finmath.montecarlo.process.MonteCarloProcess} for the notation.
*
* @author Christian Fries
* @author Roland Bachl
* @see net.finmath.montecarlo.process.MonteCarloProcess The interface for numerical schemes.
* @see net.finmath.montecarlo.model.ProcessModel The interface for models provinding parameters to numerical schemes.
* @version 1.1
*/
public class MonteCarloMultiAssetBlackScholesModel extends AbstractProcessModel implements AssetModelMonteCarloSimulationModel {
private final MonteCarloProcess process;
private final RandomVariableFactory randomVariableFactory;
private final double[] initialValues;
private final double riskFreeRate; // Actually the same as the drift (which is not stochastic)
private final double[][] factorLoadings;
private static final int defaultSeed = 3141;
private final RandomVariable[] initialStates;
private final RandomVariable[] drift;
private final RandomVariable[][] factorLoadingOnPaths;
/**
* Create a Monte-Carlo simulation using given time discretization.
*
* @param randomVariableFactory The RandomVariableFactory used to construct model parameters as random variables.
* @param brownianMotion The Brownian motion to be used for the numerical scheme.
* @param initialValues Spot values.
* @param riskFreeRate The risk free rate.
* @param factorLoadings The matrix of factor loadings, where factorLoadings[underlyingIndex][factorIndex] is the coefficient of the Brownian driver factorIndex used for the underlying underlyingIndex.
*/
public MonteCarloMultiAssetBlackScholesModel(
final RandomVariableFactory randomVariableFactory,
final BrownianMotion brownianMotion,
final double[] initialValues,
final double riskFreeRate,
final double[][] factorLoadings
) {
this.randomVariableFactory = randomVariableFactory;
this.initialValues = initialValues;
this.riskFreeRate = riskFreeRate;
this.factorLoadings = factorLoadings;
/*
* The interface definition requires that we provide the initial value, the drift and the volatility in terms of random variables.
* We construct the corresponding random variables here and will return (immutable) references to them.
*
* Since the underlying process is configured to simulate log(S),
* the initial value and the drift are transformed accordingly.
*
*/
initialStates = new RandomVariable[getNumberOfComponents()];
drift = new RandomVariable[getNumberOfComponents()];
factorLoadingOnPaths = new RandomVariable[getNumberOfComponents()][];
for(int underlyingIndex = 0; underlyingIndex dataModified) {
BrownianMotion newBrownianMotion = (BrownianMotion) process.getStochasticDriver();
final RandomVariableFactory newRandomVariableFactory = (RandomVariableFactory) dataModified.getOrDefault("randomVariableFactory", randomVariableFactory);
final double[] newInitialValues = (double[]) dataModified.getOrDefault("initialValues", initialValues);
final double newRiskFreeRate = ((Double) dataModified.getOrDefault("riskFreeRate", riskFreeRate)).doubleValue();
double[][] newFactorLoadings = (double[][]) dataModified.getOrDefault("factorLoadings", factorLoadings);
if(dataModified.containsKey("volatilities") || dataModified.containsKey("correlations")) {
if(dataModified.containsKey("factorLoadings")) {
throw new IllegalArgumentException("Inconsistend parameters. Cannot specify volatility or corellation and factorLoadings at the same time.");
}
final double[] newVolatilities = (double[]) dataModified.getOrDefault("volatilities", getVolatilities());
final double[][] newCorrelations = (double[][]) dataModified.getOrDefault("correlations", getCorrelations());
newFactorLoadings = getFactorLoadingsFromVolatilityAnCorrelation(newVolatilities, newCorrelations);
}
if(dataModified.containsKey("seed")) {
newBrownianMotion = newBrownianMotion.getCloneWithModifiedSeed((Integer) dataModified.get("seed"));
}
if(dataModified.containsKey("timeDiscretization")) {
newBrownianMotion = newBrownianMotion.getCloneWithModifiedTimeDiscretization( (TimeDiscretization) dataModified.get("timeDiscretization"));
}
return new MonteCarloMultiAssetBlackScholesModel(
newRandomVariableFactory,
newBrownianMotion,
newInitialValues,
newRiskFreeRate,
newFactorLoadings
);
}
@Override
public AssetModelMonteCarloSimulationModel getCloneWithModifiedSeed(final int seed) {
final Map dataModified = new HashMap<>();
dataModified.put("seed", Integer.valueOf(seed));
return getCloneWithModifiedData(dataModified);
}
@Override
public TimeDiscretization getTimeDiscretization() {
return process.getTimeDiscretization();
}
@Override
public double getTime(int timeIndex) {
return process.getTime(timeIndex);
}
@Override
public int getTimeIndex(double time) {
return process.getTimeIndex(time);
}
@Override
public RandomVariable getMonteCarloWeights(int timeIndex) throws CalculationException {
return process.getMonteCarloWeights(timeIndex);
}
@Override
public int getNumberOfFactors() {
return process.getNumberOfFactors();
}
}