本文整理汇总了Java中net.finmath.optimizer.LevenbergMarquardt类的典型用法代码示例。如果您正苦于以下问题:Java LevenbergMarquardt类的具体用法?Java LevenbergMarquardt怎么用?Java LevenbergMarquardt使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
LevenbergMarquardt类属于net.finmath.optimizer包,在下文中一共展示了LevenbergMarquardt类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: sabrCalibrateParameterForImpliedNormalVols
import net.finmath.optimizer.LevenbergMarquardt; //导入依赖的package包/类
public static double[] sabrCalibrateParameterForImpliedNormalVols(final double underlying, final double maturity, final double[] givenStrikes, final double[] givenVolatilities, final double[] parameterInitialValues, double[] parameterSteps, final double[] parameterLowerBound, final double[] parameterUpperBound) throws SolverException {
/*
* Using Levenberg Marquardt to calibrate SABR
*/
double[] targetValues = givenVolatilities;
int maxIteration = 1000;
int numberOfThreads = 8;
LevenbergMarquardt lm = new LevenbergMarquardt(parameterInitialValues, targetValues, maxIteration, numberOfThreads) {
private static final long serialVersionUID = -4481118838855868864L;
@Override
public void setValues(double[] parameters, double[] values) throws SolverException {
for(int parameterIndex = 0; parameterIndex<parameters.length; parameterIndex++) {
parameters[parameterIndex] = Math.min(Math.max(parameters[parameterIndex],parameterLowerBound[parameterIndex]),parameterUpperBound[parameterIndex]);
}
for(int strikeIndex = 0; strikeIndex < givenStrikes.length; strikeIndex++) {
double strike = givenStrikes[strikeIndex];
values[strikeIndex] = AnalyticFormulas.sabrBerestyckiNormalVolatilityApproximation(parameters[0] /* alpha */, parameters[1] /* beta */, parameters[2] /* rho */, parameters[3] /* nu */, parameters[4] /* displacement */, underlying, strike, maturity);
}
}
};
lm.setErrorTolerance(1E-16);
lm.run();
double[] bestParameters = lm.getBestFitParameters();
return bestParameters;
}
示例2: testSABRCalibration
import net.finmath.optimizer.LevenbergMarquardt; //导入依赖的package包/类
@Test
public void testSABRCalibration() throws SolverException {
/*
* Using Levenberg Marquardt to calibrate SABR
*/
final double[] givenStrikes = { -0.01, -0.005, 0.0, 0.005, 0.01, 0.02, 0.03 };
final double[] givenVolatilities = { 0.0055, 0.0059, 0.0060, 0.0061, 0.0063, 0.0066, 0.0070 };
final double underlying = 0.0076;
final double maturity = 20;
double alpha = 0.006;
double beta = 0.05;
double rho = 0.95;
double nu = 0.075;
double displacement = 0.02;
double[] initialParameters = { alpha, beta, rho, nu, displacement };
double[] targetValues = givenVolatilities;
int maxIteration = 500;
int numberOfThreads = 8;
for(double displacement2 = 0.5; displacement2>0; displacement2 -= 0.001) {
givenVolatilities[0] = givenVolatilities[0] + 0.00001;
final double displacement3 = displacement2;
LevenbergMarquardt lm = new LevenbergMarquardt(initialParameters, targetValues, maxIteration, numberOfThreads) {
private static final long serialVersionUID = -4799790311777696204L;
@Override
public void setValues(double[] parameters, double[] values) throws SolverException {
for(int strikeIndex = 0; strikeIndex < givenStrikes.length; strikeIndex++) {
double strike = givenStrikes[strikeIndex];
values[strikeIndex] = AnalyticFormulas.sabrBerestyckiNormalVolatilityApproximation(parameters[0] /* alpha */, parameters[1] /* beta */, parameters[2] /* rho */, parameters[3] /* nu */, parameters[4] /* displacement */, underlying, strike, maturity);
}
}
};
lm.setErrorTolerance(1E-16);
lm.run();
double[] bestParameters = lm.getBestFitParameters();
// System.out.println(lm.getRootMeanSquaredError() + "\t");
System.out.println(givenVolatilities[0] + "\t" + lm.getRootMeanSquaredError() + "\t" + Arrays.toString(bestParameters));
}
}