如果Best Fit Straight Line是预测的最佳方法

时间:2013-07-17 12:06:04

标签: c# approximation best-fit-curve

我需要根据二维坐标系上给定的点样本集对下一个点进行预测。

我正在使用Best-Fit Straight Line方法进行此类预测。

如果有比Best-Fit Straight Line更好的方法,请告诉我?

我的代码如下:

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;

    }}

2 个答案:

答案 0 :(得分:0)

我认为你可以考虑像指数移动平均线这样的平滑算法来预测近期的数据点,

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

答案 1 :(得分:0)

如果x坐标由日期组成,则可以依赖具有以下组件的广义加法模型: - 趋势 - 年度简介 - 每周简介 - 每日简介

GAM模型在R中可用,因此我建议您使用JRI将Java代码与R接口。

干杯