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

net.finmath.fouriermethod.calibration.CalibratedModel 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
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.CalibrableProcess;
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 CalibrableProcess 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; public CalibratedModel(OptionSurfaceData surface, CalibrableProcess model, OptimizerFactory optimizerFactory, EuropeanOptionSmile pricer, double[] initialParameters, double[] parameterStep) { super(); this.surface = surface; this.model = model; this.optimizerFactory = optimizerFactory; this.pricer = pricer; this.initialParameters = initialParameters; this.lowerBound = model.getParameterLowerBounds(); this.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 { Optimizer.ObjectiveFunction objectiveFunction = new Optimizer.ObjectiveFunction() { @Override public void setValues(double[] parameters, double[] values) { //We change the parameters of the model CalibrableProcess newModel = model.getCloneForModifiedParameters(parameters); CharacteristicFunctionModel newModelFourier = newModel.getCharacteristiFunction(); int numberOfMaturities = surface.getMaturities().length; double mats[] = surface.getMaturities(); QuotingConvention targetConvention = surface.getQuotingConvention(); for(int t = 0; t> currentModelPrices = newPricer.getValue(0.0, newModelFourier); for(int i = 0; i calibrationOutput = outputCalibrationResult(optimizer.getBestFitParameters()); CalibrableProcess 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. int numberOfMaturities = surface.getMaturities().length; double mats[] = surface.getMaturities(); ArrayList vals = new ArrayList(); for(int t = 0; t outputCalibrationResult(double[] parameters) { ArrayList calibrationOutput = new ArrayList(); //We change the parameters of the model CalibrableProcess newModel = model.getCloneForModifiedParameters(parameters); CharacteristicFunctionModel newModelFourier = newModel.getCharacteristiFunction(); int numberOfMaturities = surface.getMaturities().length; double mats[] = surface.getMaturities(); 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(CalibrableProcess model, double[] bestFitParameters, int iterations, double rootMeanSquaredError, ArrayList calibrationOutput) { super(); this.model = model; this.bestFitParameters = bestFitParameters; this.iterations = iterations; this.rootMeanSquaredError = rootMeanSquaredError; this.calibrationOutput = calibrationOutput; } public CalibrableProcess getModel() { return model; } public double[] getBestFitParameters() { return bestFitParameters; } public int getIterations() { return iterations; } public double getRootMeanSquaredError() { return rootMeanSquaredError; } public ArrayList getCalibrationOutput(){ return this.calibrationOutput; } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy