net.finmath.fouriermethod.calibration.CalibratedModel 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.
package net.finmath.fouriermethod.calibration;
import java.util.ArrayList;
import java.util.Map;
import java.util.function.Function;
import org.apache.commons.lang3.ArrayUtils;
import net.finmath.exception.CalculationException;
import net.finmath.fouriermethod.calibration.models.CalibratableProcess;
import net.finmath.fouriermethod.models.CharacteristicFunctionModel;
import net.finmath.fouriermethod.products.smile.EuropeanOptionSmile;
import net.finmath.marketdata.model.volatilities.OptionSmileData;
import net.finmath.marketdata.model.volatilities.OptionSurfaceData;
import net.finmath.marketdata.model.volatilities.VolatilitySurface.QuotingConvention;
import net.finmath.optimizer.Optimizer;
import net.finmath.optimizer.OptimizerFactory;
import net.finmath.optimizer.SolverException;
/**
* This class solves a calibration problem. The problem is defined in terms of:
*
* - a generic container of market data OptionSurfaceData.
*
- a generic pricing model.
*
- a generic calibration algorithm.
*
- a generic pricer for claims.
*
*
* The class supports both calibration in terms of:
*
* - Prices
*
- Log-normal implied volatilities.
*
- Normal implied volatilities.
*
*
* To change the calibration entity please change the convention in the option surface.
* The calibration entity (i.e. price/vol/normal vol) is directly detected from market data.
*
* @author Alessandro Gnoatto
*/
public class CalibratedModel {
private final OptionSurfaceData surface; //target calibration instruments. They dictate the calibration entity: vol/price.
private final CalibratableProcess model; //Pricing model
private final OptimizerFactory optimizerFactory; //construct the instance of the optimization algorithm inside the class.
private final EuropeanOptionSmile pricer; //How do we compute prices: Carr Madan, Cos, Conv, Lewis...
//Optimizer parameters
private final double[] initialParameters;
private final double[] lowerBound;
private final double[] upperBound;
private final double[] parameterStep;
/**
* Create the calibration from data.
*
* @param surface The target calibration instruments. They dictate the calibration entity: vol/price.
* @param model The model to calibrate.
* @param optimizerFactory Factory providing the optimizer to use.
* @param pricer How do we compute prices: Carr Madan, Cos, Conv, Lewis...
* @param initialParameters Initial parameters
* @param parameterStep Parameter steps.
*/
public CalibratedModel(final OptionSurfaceData surface, final CalibratableProcess model,
final OptimizerFactory optimizerFactory, final EuropeanOptionSmile pricer, final double[] initialParameters,
final double[] parameterStep) {
super();
this.surface = surface;
this.model = model;
this.optimizerFactory = optimizerFactory;
this.pricer = pricer;
this.initialParameters = initialParameters;
lowerBound = model.getParameterLowerBounds();
upperBound = model.getParameterUpperBounds();
this.parameterStep = parameterStep;
}
/**
* Solves the calibration problem thus providing a calibrated model.
*
* @return the calibrated model wrapped in an {@link OptimizationResult}.
* @throws SolverException Thrown if the calibration problem cannot be solved.
*/
public OptimizationResult getCalibration() throws SolverException {
final Optimizer.ObjectiveFunction objectiveFunction = new Optimizer.ObjectiveFunction() {
@Override
public void setValues(final double[] parameters, final double[] values) {
//We change the parameters of the model
final CalibratableProcess newModel = model.getCloneForModifiedParameters(parameters);
final CharacteristicFunctionModel newModelFourier = newModel.getCharacteristicFunctionModel();
final int numberOfMaturities = surface.getMaturities().length;
final double[] mats = surface.getMaturities();
final QuotingConvention targetConvention = surface.getQuotingConvention();
final ArrayList vals = new ArrayList<>();
for(int t = 0; t> currentModelPrices = newPricer.getValue(0.0, newModelFourier);
for(int i = 0; i calibrationOutput = outputCalibrationResult(optimizer.getBestFitParameters());
final CalibratableProcess calibratedModel = model.getCloneForModifiedParameters(optimizer.getBestFitParameters());
return new OptimizationResult(calibratedModel, optimizer.getBestFitParameters(), optimizer.getIterations(), optimizer.getRootMeanSquaredError(), calibrationOutput);
}
/**
* This is a service method that takes care of putting al the target values in a single array.
* @return
*/
private double[] formatTargetValuesForOptimizer() {
//Put all values in an array for the optimizer.
final int numberOfMaturities = surface.getMaturities().length;
final double[] mats = surface.getMaturities();
final ArrayList vals = new ArrayList<>();
for(int t = 0; t outputCalibrationResult(final double[] parameters) {
final ArrayList calibrationOutput = new ArrayList<>();
//We change the parameters of the model
final CalibratableProcess newModel = model.getCloneForModifiedParameters(parameters);
final CharacteristicFunctionModel newModelFourier = newModel.getCharacteristicFunctionModel();
final int numberOfMaturities = surface.getMaturities().length;
final double[] mats = surface.getMaturities();
final QuotingConvention targetConvention = surface.getQuotingConvention();
double value;
double targetValue;
double T;
double K;
calibrationOutput.add("Strike"+ "\t" + "Maturity"+ "\t" + "Market Value" + "\t" + "Model Value" + "\t" + "Squared Error");
for(int t = 0; t> currentModelPrices = newPricer.getValue(0.0, newModelFourier);
for(int i = 0; i calibrationOutput;
public OptimizationResult(final CalibratableProcess model, final double[] bestFitParameters,
final int iterations, final double rootMeanSquaredError, final ArrayList calibrationOutput) {
super();
this.model = model;
this.bestFitParameters = bestFitParameters;
this.iterations = iterations;
this.rootMeanSquaredError = rootMeanSquaredError;
this.calibrationOutput = calibrationOutput;
}
public CalibratableProcess getModel() {
return model;
}
public double[] getBestFitParameters() {
return bestFitParameters;
}
public int getIterations() {
return iterations;
}
public double getRootMeanSquaredError() {
return rootMeanSquaredError;
}
public ArrayList getCalibrationOutput(){
return calibrationOutput;
}
}
}