Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 457|回复: 0

[算法学习]最小二乘法的JAVA代码

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-11-10 00:02:24 | 显示全部楼层 |阅读模式
    最小二乘法的代码,二个构造方法,一个是不带权的,一个是带权的  
       
        /**
    * 最小二乘法计算类
    *
    * @author Administrator
    *
    */
    public class LeastSquareMethod {
            private double[] x;
            private double[] y;
            private double[] weight;
            private int m;
            private double[] coefficient;
            public LeastSquareMethod(double[] x, double[] y, int m) {
                    if (x == null || y == null || x.length < 2 || x.length != y.length
                                    || m < 2)
                            throw new IllegalArgumentException("无效的参数");
                    this.x = x;
                    this.y = y;
                    this.m = m;
                    weight = new double[x.length];
                    for (int i = 0; i < x.length; i++) {
                            weight = 1;
                    }
            }
            public LeastSquareMethod(double[] x, double[] y, double[] weight, int m) {
                    if (x == null || y == null || weight == null || x.length < 2
                                    || x.length != y.length || x.length != weight.length || m < 2)
                            throw new IllegalArgumentException("无效的参数");
                    this.x = x;
                    this.y = y;
                    this.m = m;
                    this.weight = weight;
            }
            public double[] getCoefficient() {
                    if (coefficient == null)
                            compute();
                    return coefficient;
            }
            public double fit(double v) {
                    if (coefficient == null)
                            compute();
                    if (coefficient == null)
                            return 0;
                    double sum = 0;
                    for (int i = 0; i < coefficient.length; i++) {
                            sum += Math.pow(v, i) * coefficient;
                    }
                    return sum;
            }
            private void compute() {
                    if (x == null || y == null || x.length <= 1 || x.length != y.length
                                    || x.length < m || m < 2)
                            return;
                    double[] s = new double[(m - 1) * 2 + 1];
                    for (int i = 0; i < s.length; i++) {
                            for (int j = 0; j < x.length; j++)
                                    s += Math.pow(x[j], i) * weight[j];
                    }
                    double[] f = new double[m];
                    for (int i = 0; i < f.length; i++) {
                            for (int j = 0; j < x.length; j++)
                                    f += Math.pow(x[j], i) * y[j] * weight[j];
                    }
                    double[][] a = new double[m][m];
                    for (int i = 0; i < m; i++) {
                            for (int j = 0; j < m; j++) {
                                    a[j] = s[i + j];
                            }
                    }
                    coefficient = Algorithm.multiLinearEquationGroup(a, f);
            }
            /**
             * @param args
             */
            public static void main(String[] args) {
                    LeastSquareMethod l = new LeastSquareMethod(
                                    new double[] { 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 },
                                    new double[] { 37.84, 44.55, 45.74, 63.8, 76.67, 105.59, 178.48, 355.27, 409.92 },
                                    new double[] { 11, 12, 13, 14, 15, 16, 17, 18, 19 },
                                    2);
                    double[] x = l.getCoefficient();
                    for (double xx : x) {
                            System.out.println(xx);
                    }
                    System.out.println(l.fit(2009));
            }
    }
                              
        用到了多元一次方程的解法:
         public class Algorithm {
            /**
             * 多元一次方程求解
             * @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[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*a[posx][posy]-b[posx]*a[posy];
                            int count2=0;
                            for (int j=0;j<=len;j++){
                                    if (j==posy)
                                            continue;
                                    aa[count][count2]=a[j]*a[posx][posy]-a[posx][j]*a[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]*result2[count];
                            result=result2[count];
                            count++;
                    }
                    result[posy]=sum/a[posx][posy];
                   
                    return result;
            }
            /**
             * @param args
             */
            public static void main(String[] args) {
                    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);
                    }
            }
    }
                              
       

         
          
          
            
            

             
            

             
            
          
         
       

       
      


    源码下载:http://file.javaxxz.com/2014/11/10/000224546.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-25 07:37 , Processed in 0.299501 second(s), 34 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表