多元线性回归

package com.smart.test;

public class Algorithm
{
    /**
     * 一元线性回归分析
     * 
     * @param x[n] 存放自变量x的n个取值
     * @param y[n] 存放与自变量x的n个取值相对应的随机变量y的观察值
     * @param n    观察点数
     * @param a[2]   a(0) 返回回归系数b ,a(1)返回回归系数a
     */
    public static void lineareEquation(double[] x, double[] y, int n, double[] a)
    {
        int i;
        double xx, yy, e, f, q, s;
        xx = 0.0;
        yy = 0.0;
        for (i = 0; i <= n - 1; i++)
        {
            xx = xx + x[i] / n;
            yy = yy + y[i] / n;
        }
        e = 0.0;
        f = 0.0;
        for (i = 0; i <= n - 1; i++)
        {
            q = x[i] - xx;
            e = e + q * q;
            f = f + q * (y[i] - yy);
        }
        a[1] = f / e;
        a[0] = yy - a[1] * xx;
        q = 0.0;
        for (i = 0; i <= n - 1; i++)
        {
            s = a[1] * x[i] + a[0];
            q = q + (y[i] - s) * (y[i] - s);
            e = Math.abs(y[i] - s);
        }
    }
    /**
     * 多元一次方程求解
     * 
     * @param a 系数数组
     * @param b 结果数组
     * @return 解答
     */
    public static double[] multiLinearEquationGroup(double[][] a, double[] b)
    {
        if (a == null || b == null || a.length == 0 || a.length != b.length) return null;
        for (double[] x : a)
        {
            if (x == null || x.length != a.length) return null;
        }

        int len = a.length - 1;
        double[] result = new double[a.length];

        if (len == 0)
        {
            result[0] = b[0] / a[0][0];
            return result;
        }

        double[][] aa = new double[len][len];
        double[] bb = new double[len];
        int posx = -1, posy = -1;
        for (int i = 0; i <= len; i++)
        {
            for (int j = 0; j <= len; j++)
                if (a[i][j] != 0.0d)
                {
                    posy = j;
                    break;
                }
            if (posy != -1)
            {
                posx = i;
                break;
            }
        }
        if (posx == -1) return null;

        int count = 0;
        for (int i = 0; i <= len; i++)
        {
            if (i == posx) continue;
            bb[count] = b[i] * a[posx][posy] - b[posx] * a[i][posy];
            int count2 = 0;
            for (int j = 0; j <= len; j++)
            {
                if (j == posy) continue;
                aa[count][count2] = a[i][j] * a[posx][posy] - a[posx][j] * a[i][posy];
                count2++;
            }
            count++;
        }

        double[] result2 = multiLinearEquationGroup(aa, bb);

        double sum = b[posx];
        count = 0;
        for (int i = 0; i <= len; i++)
        {
            if (i == posy) continue;
            sum -= a[posx][i] * result2[count];
            result[i] = result2[count];
            count++;
        }
        result[posy] = sum / a[posx][posy];

        return result;
    }

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        
        /**
         * 一元回归
         */
        double[] a = new double[2];
        double[] x = { 0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0};
        double[] y = { 2.75,2.84,2.965,3.01,3.20,3.25,3.38,3.43,3.55,3.66,3.74};
        Algorithm.lineareEquation(x,y,11,a);
        System.out.println("a="+a[1]+" b="+a[0]);
        /**
         * 多元回归
         */
//        double[] r = multiLinearEquationGroup(new double[][] { { 1, 2, 3, 1 }, { 2, 0, 1, 0 }, { 5, 2, 0, 0 }, { 7, 1, 1, 0 } }, new double[] { 18, 5, 9, 12 });
//        for (int i = 0; i < r.length; i++)
//        {
//            System.out.println(r[i]);
//        }
    }
}

 

posted @ 2013-02-26 17:05  何长春  阅读(233)  评论(0编辑  收藏  举报