Domanda

I need to make prediction for a next point, based on given set of point samples on 2-d coordinate system.

I am using Best-Fit Straight Line method for such prediction.

Please let me know if there is method better than Best-Fit Straight Line?

My code is below:

public class LineEquation
{
    public double m; //slope
    public double c;  //constant  in y=mx+c
}
public class Point
{
    public double x;
    public double y;
}

public class BestFitLine
{
    public Point[] points = new Point[7];

    public void InputPoints(Point[] points)
    {

        for (int i = 0; i < points.Length; i++)
        {
            points[i] = new Point();
        }

        points[0].x = 12;
        points[0].y = 13;

        points[1].x = 22;
        points[1].y = 23;


        points[2].x = 32;
        points[2].y = 33;


        points[3].x = 42;
        points[0].y = 23;


        points[4].x = 52;
        points[4].y = 33;


        points[5].x = 62;
        points[5].y = 63;

        points[6].x = 72;
        points[6].y = 63;



    }

    public LineEquation CalculateBestFitLine(Point[] points)
    {
        double constant = 0;
        double slope=0;
        for (int i = 0; i < points.Length - 1; i++)
        {
            for (int j = i + 1; j < points.Length; j++)
            {

                double m = (points[j].y - points[i].y) / (points[j].x - points[i].x);
                double c = points[j].y - (m * points[j].x);
                constant += c;
                slope += m;
            }
        }
        int lineCount =((points.Length-1)*points.Length)/2;

        slope = slope / lineCount;
        constant = constant / lineCount;
        LineEquation eq = new LineEquation();
        eq.c = constant;
        eq.m = slope;
        return eq;

    }}
È stato utile?

Soluzione

If your x coordinate is composed of dates, you can rely on generalized additive models with the following components : - trend - yearly profile - weekly profile - daily profile

GAM models are available in R, so I would advice you to use JRI in order to interface your java code with R.

Cheers

Altri suggerimenti

I think you could consider smoothing algorithms like Exponential Moving average to predict the near future data points,

http://en.wikipedia.org/wiki/Exponential_smoothing

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top