커브 클래스를 참조하십시오 : 그 라이브러리/개발자입니다. 참고 : 저는 또한 commons-math를 좋아하지만 커브 피팅에 대해서는 아직 사용하지 않았습니다. 응용 프로그램 (수학 금융)과 관련된 일부 맞춤 속성이 필요합니다.
(편집) 다음
작은 데모입니다 :이 (참고 :이 데모는 finmath-lib 디렉토리 1.2.13 또는 mvn.finmath.net 또는 github.com/finmath/finmath-lib에서 사용할 수있는 현재 1.2.12 - 스냅 샷 (그렇지 않은 필요
package net.finmath.tests.marketdata.curves;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import org.junit.Test;
import net.finmath.marketdata.model.curves.Curve;
import net.finmath.marketdata.model.curves.CurveInterface;
import net.finmath.optimizer.LevenbergMarquardt;
import net.finmath.optimizer.SolverException;
/**
* A short demo on how to use {@link net.finmath.marketdata.model.curves.Curve}.
*
* @author Christian Fries
*/
public class CurveTest {
private static NumberFormat numberFormat = new DecimalFormat("0.0000");
/**
* Run a short demo on how to use {@link net.finmath.marketdata.model.curves.Curve}.
*
* @param args Not used.
* @throws SolverException Thrown if optimizer fails.
* @throws CloneNotSupportedException Thrown if curve cannot be cloned for optimization.
*/
public static void main(String[] args) throws SolverException, CloneNotSupportedException {
(new CurveTest()).testCurveFitting();
}
/**
* Tests fitting of curve to given data.
*
* @throws SolverException Thrown if optimizer fails.
* @throws CloneNotSupportedException Thrown if curve cannot be cloned for optimization.
*/
@Test
public void testCurveFitting() throws SolverException, CloneNotSupportedException {
/*
* Build a curve (initial guess for our fitting problem, defines the times).
*/
Curve.CurveBuilder curveBuilder = new Curve.CurveBuilder();
curveBuilder.setInterpolationMethod(Curve.InterpolationMethod.LINEAR);
curveBuilder.setExtrapolationMethod(Curve.ExtrapolationMethod.LINEAR);
curveBuilder.setInterpolationEntity(Curve.InterpolationEntity.VALUE);
// Add some points - which will not be fitted
curveBuilder.addPoint(-1.0 /* time */, 1.0 /* value */, false /* isParameter */);
curveBuilder.addPoint(0.0 /* time */, 1.0 /* value */, false /* isParameter */);
// Add some points - which will be fitted
curveBuilder.addPoint(0.5 /* time */, 2.0 /* value */, true /* isParameter */);
curveBuilder.addPoint(0.75 /* time */, 2.0 /* value */, true /* isParameter */);
curveBuilder.addPoint(1.0 /* time */, 2.0 /* value */, true /* isParameter */);
curveBuilder.addPoint(2.2 /* time */, 2.0 /* value */, true /* isParameter */);
curveBuilder.addPoint(3.0 /* time */, 2.0 /* value */, true /* isParameter */);
final Curve curve = curveBuilder.build();
/*
* Create data to which the curve should be fitted to
*/
final double[] givenTimes = { 0.0, 0.5, 0.75, 1.0, 1.5, 1.75, 2.5 };
final double[] givenValues = { 3.5, 12.3, 13.2, 7.5, 5.5, 2.9, 4.4 };
/*
* Find a best fitting curve.
*/
// Define the objective function
LevenbergMarquardt optimizer = new LevenbergMarquardt(
curve.getParameter() /* initial parameters */,
givenValues /* target values */,
100, /* max iterations */
Runtime.getRuntime().availableProcessors() /* max number of threads */
) {
@Override
public void setValues(double[] parameters, double[] values) throws SolverException {
CurveInterface curveGuess = null;
try {
curveGuess = curve.getCloneForParameter(parameters);
} catch (CloneNotSupportedException e) {
throw new SolverException(e);
}
for(int valueIndex=0; valueIndex<values.length; valueIndex++) {
values[valueIndex] = curveGuess.getValue(givenTimes[valueIndex]);
}
}
};
// Fit the curve (find best parameters)
optimizer.run();
CurveInterface fittedCurve = curve.getCloneForParameter(optimizer.getBestFitParameters());
// Print out fitted curve
for(double time = -2.0; time < 5.0; time += 0.1) {
System.out.println(numberFormat.format(time) + "\t" + numberFormat.format(fittedCurve.getValue(time)));
}
// Check fitted curve
double errorSum = 0.0;
for(int pointIndex = 0; pointIndex<givenTimes.length; pointIndex++) {
errorSum += fittedCurve.getValue(givenTimes[pointIndex]) - givenValues[pointIndex];
}
System.out.println("Mean deviation: " + errorSum);
/*
* Test: With the given data, the fit cannot over come that at 0.0 we have an error of -2.5.
* Hence we test if the mean deviation is -2.5 (the optimizer reduces the variance)
*/
org.junit.Assert.assertTrue(Math.abs(errorSum - -2.5) < 1E-5);
}
}
하나의 다항식 구분 보간 만들기 --- 당신이 경계에서 차이를 지정하지 않는 한 좋은 시작이다) 1.2.12와 호환. –
당신은 조금 명확히 할 수 . 내가 원하는 것은 (내가 생각하기에) 시간축을 세분화하고 각각이 연속적 이도록 낮은 차수의 다항식을 맞추는 것입니다. 내가 이해하는 바와 같이 높은 차수의 다항식은 피팅에 적합하지 않습니다. –
라이브러리의 보간 지원을 각 세그먼트에 적용 할 수 있습니다. 수동으로 작업하여 파트를 서로 붙입니다. –