Apache commons math curve fitting to log periodic power law
I want to fit Didier Sornetts Crash indicator using apache commons math
curve fitting solver. But I do not have any idea what to provide to the
gradient function. Can anyone help me out or guide me to a good tutorial
using commom math curve fitting?
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package tryout;
import java.sql.Date;
import org.apache.commons.math.FunctionEvaluationException;
import org.apache.commons.math.optimization.fitting.CurveFitter;
import org.apache.commons.math.optimization.fitting.ParametricRealFunction;
import
org.apache.commons.math.optimization.general.LevenbergMarquardtOptimizer;
/**
*
* @author Krusty
*/
public class Fitting {
static String[] date = new
String[]{"2012-07-31","2012-07-30","2012-07-27","2012-07-26","2012-07-25","2012-07-24","2012-07-23","2012-07-20","2012-07-19","2012-07-18","2012-07-17","2012-07-16","2012-07-13","2012-07-12","2012-07-11","2012-07-10","2012-07-09","2012-07-06","2012-07-05","2012-07-03","2012-07-02","2012-06-29","2012-06-28","2012-06-27","2012-06-26","2012-06-25","2012-06-22","2012-06-21","2012-06-20","2012-06-19","2012-06-18","2012-06-15","2012-06-14","2012-06-13","2012-06-12","2012-06-11","2012-06-08","2012-06-07","2012-06-06","2012-06-05","2012-06-04","2012-06-01","2012-05-31","2012-05-30","2012-05-29","2012-05-25","2012-05-24","2012-05-23","2012-05-22","2012-05-21","2012-05-18","2012-05-17","2012-05-16","2012-05-15","2012-05-14","2012-05-11","2012-05-10","2012-05-09","2012-05-08","2012-05-07","2012-05-04","2012-05-03","2012-05-02","2012-05-01","2012-04-30","2012-04-27","2012-04-26","2012-04-25","2012-04-24","2012-04-23","2012-04-20","2012-04-19","2012-04-18","2012-04-17","2012-04-16","2012-
04-13","2012-04-12","2012-04-11","2012-04-10","2012-04-09","2012-04-05","2012-04-04","2012-04-03","2012-04-02"};
static double[] value = new
double[]{594,578.7,569.1,559.1,559.19,584.43,587.26,587.71,597.46,589.62,590.28,590.25,588.37,582.46,587.84,591.52,597.04,589.25,593.2,582.96,576.26,567.97,553.43,558.73,556.33,555.1,566.12,561.81,569.66,571.29,569.7,558.37,555.84,556.46,560.35,555.49,564.39,556.03,555.78,547.38,548.8,545.59,561.87,563.27,556.56,546.86,549.8,554.9,541.68,545.87,515.82,515.57,531.09,537.99,542.9,551.16,554.86,553.56,552.59,553.85,549.74,565.85,569.9,566.15,567.95,586.45,591.02,593.26,544.9,556.01,557.25,571.32,591.64,592.97,564.21,588.62,605.68,609.01,611.19,618.77,616.29,607.17,612.05,601.65};
public static void main (String args[]) throws Exception {
final CurveFitter fitter = new CurveFitter(new
LevenbergMarquardtOptimizer());
for (int i=0;i<date.length;i++) {
fitter.addObservedPoint(Date.valueOf(date[0]).getTime(),
value[i]);
}
double[] initialguess2 = new double[7];
initialguess2[0] = value[value.length-1]; // A
initialguess2[1] = -1.0d; // B
initialguess2[2] = 0.0d; // C
initialguess2[3] = Date.valueOf(date[date.length-1]).getTime(); // tc
initialguess2[4] = 9d; // w
initialguess2[5] = 1.0d; // p
initialguess2[6] = 0.5d; // m
fitter.fit(new Sornette(), initialguess2);
}
// y = A + B * Math.pow((tc − x),m) + C * Math.pow((tc −
x),m) * Math.cos(w * log(tc − x) - pi );
public static class Sornette implements ParametricRealFunction {
@Override
public double value(double x, double[] parameters ) throws
FunctionEvaluationException {
double A = parameters[0];
double B = parameters[1];
double C = parameters[2];
double tc = parameters[3];
double w = parameters[4];
double p = parameters[5];
double m = parameters[6];
// y = A + B * Math.pow((tc - x),m) + C * Math.pow((tc - x),m)
* Math.cos(w * log(tc - x) - p );
return A + B * Math.pow((tc - x),m) + C * Math.pow((tc - x),m)
* Math.cos(w * log(tc - x) - p );
}
public double log(double d) {
return Math.log(d);
//return Math.log10(d);
}
@Override
public double[] gradient(double d, double[] parameters ) throws
FunctionEvaluationException {
/*
* no idea what to do here lt;lt; ---------------
*/
}
}
}
/code/pre
No comments:
Post a Comment