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

net.finmath.marketdata.calibration.Solver 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. All rights reserved. Contact: [email protected].
 *
 * Created on 26.11.2012
 */
package net.finmath.marketdata.calibration;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import net.finmath.marketdata.model.AnalyticModelInterface;
import net.finmath.marketdata.model.curves.CurveInterface;
import net.finmath.marketdata.products.AnalyticProductInterface;
import net.finmath.optimizer.LevenbergMarquardt;
import net.finmath.optimizer.SolverException;

/**
 * Generates a calibrated model for a given set
 * of calibrationProducts with respect to given Curves.
 * 
 * The model and the curve are assumed to be immutable, i.e., the solver
 * will return a calibrate clone, containing clones for every curve
 * which is part of the set of curves to be calibrated.
 * 
 * The calibration is performed as a multi-threaded global optimization.
 * I will greatly profit from a multi-core architecture.
 * 
 * @author Christian Fries
 */
public class Solver {

	private final AnalyticModelInterface			model;
	private final List	calibrationProducts;
	private final double							calibrationAccuracy;

	private	final	double	evaluationTime;
	private final	int		maxIterations	= 1000;

	private 		int		iterations		= 0;
	private 		double	accuracy		= Double.POSITIVE_INFINITY;
	
	/**
	 * Generate a solver for the given parameter objects (independents) and
	 * objective functions (dependents).
	 * 
	 * @param model The model from which a calibrated clone should be created.
	 * @param calibrationProducts The objective functions.
     * @param evaluationTime Evaluation time applied to the calibration products.
	 * @param calibrationAccuracy The error tolerance of the solver.
	 */
    public Solver(AnalyticModelInterface model, Vector calibrationProducts, double evaluationTime, double calibrationAccuracy) {
	    super();
	    this.model = model;
	    this.calibrationProducts = calibrationProducts;
	    this.evaluationTime = evaluationTime;
	    this.calibrationAccuracy = calibrationAccuracy;
    }

	/**
	 * Generate a solver for the given parameter objects (independents) and
	 * objective functions (dependents).
	 * 
	 * @param model The model from which a calibrated clone should be created.
	 * @param calibrationProducts The objective functions.
	 */
    public Solver(AnalyticModelInterface model, Vector calibrationProducts) {
    	this(model, calibrationProducts, 0.0, 0.0);
    }
    
    /**
     * Find the model such that the equation
     * 
* * objectiveFunctions.getValue(model) = 0 * *
* holds. * * @param curvesToCalibrates The set of curve to calibrate. * @return A reference to a calibrated clone of the given model. * @throws net.finmath.optimizer.SolverException Thrown if the underlying optimizer does not find a solution. */ public AnalyticModelInterface getCalibratedModel(Set curvesToCalibrates) throws SolverException { final ParameterAggregation parameterAggregate = new ParameterAggregation(curvesToCalibrates); // Set solver parameters double[] initialParameters = parameterAggregate.getParameter(); double[] zeros = new double[calibrationProducts.size()]; java.util.Arrays.fill(zeros, 0.0); int maxThreads = Math.min(2 * Math.max(Runtime.getRuntime().availableProcessors(), 1), initialParameters.length); LevenbergMarquardt optimizer = new LevenbergMarquardt( initialParameters, zeros, /* targetValues */ maxIterations, maxThreads) { @Override public void setValues(double[] parameters, double[] values) throws SolverException { try { Map curvesParameterPairs = parameterAggregate.getObjectsToModifyForParameter(parameters); AnalyticModelInterface modelClone = model.getCloneForParameter(curvesParameterPairs); for(int i=0; i curvesParameterPairs = parameterAggregate.getObjectsToModifyForParameter(bestParameters); calibratedModel = model.getCloneForParameter(curvesParameterPairs); } catch (CloneNotSupportedException e) { throw new SolverException(e); } accuracy = 0.0; for(int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy