⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bptest.java

📁 销售预测的实现,包括移动平均,指数平滑,时间序列分解,自回归
💻 JAVA
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package forcast;/** * * @author Administrator */public class bptest{    double error,lhi,lho;    int step,num;    double[][] w,v;    double[] /*input,hi,ho,oi,oo,output,*/mh,mo,fh,fo,forcast,real,forcastchange,realchange;    public bptest(double[] get,double[] put,int number )    {      w = new double[3][4];//输入层到隐层的权值      v = new double[4][1];//隐层到输出层的权值      /*input = new double[3];      hi = new double[4];      ho = new double[4];      oi = new double[1];      oo = new double[1];      output = new double[1];*/      mh = new double[4];//隐层的修改系数      mo = new double[1];//输出层的修改系数      fh = new double[4];//隐层的阀值      fo = new double[1];//输出层的阀值      this.assaign(w,0.05);      this.assaign(v,0.05);      for(int i=0;i<4;i++)      {          fh[i]=0.01;      }      fo[0]=0.01;      lhi = 0.1;//输入层到隐层的学习率      lho = 0.1;//隐层到输出层的学习率      error = 0.02;      step = 10000;      real=get;      forcast=put;      num=number;    }    public void bp_main()    {        int n=0;        double max,min;        max=min=0;        realchange=new double[real.length];//处理后的实际值        forcastchange=new double[forcast.length];//用处理后的实际值得出的预测值        double[] forcasttemp=new double[3];//预测值        for(int i=0;i<real.length;i++)        {            if(max<real[i])            {                max=real[i];            }            if(min>real[i])            {                min=real[i];            }        }        for(int i=0;i<real.length;i++)        {            realchange[i]=(real[i]-min)/(2*(max-min));        }        while(n<step)        {            double err=0;            double[] input,hi,ho,oi,oo,output,mh,mo;            input = new double[3];            hi = new double[4];            ho = new double[4];            oi = new double[1];            oo = new double[1];            output = new double[1];            mh = new double[4];            mo = new double[1];            for(int i=0;i<realchange.length-3;i++)            {                for(int j=0 ; j<input.length ; j++)                {                    input[j]=realchange[j+i];                }                output[0]=realchange[i+3];                for(int k=0;k<4;k++)                {                    hi[k]=0;                    ho[k]=0;                }                oi[0]=0;                oo[0]=0;                this.computeOnLevel(w,input,hi,fh);                this.inToOut(hi,ho);                this.computeOnLevel(v,ho,oi,fo);                this.inToOut(oi,oo);                this.computeMo(mo,output,oo,oi);                this.computeMh(v,mh,mo,hi);                this.modifyWeight(v,mo,ho,fo,lho);                this.modifyWeight(w,mh,input,fh,lhi);                this.printArray(w, "w");                this.printArray(v, "v");            }            n++;            /*for(int i=0;i<realchange.length-3;i++)            {                for(int j=0 ; j<input.length ; j++)                {                    input[j]=realchange[j+i];                }                output[0]=realchange[i+3];                oo[0]=bpforcast(input);                if(err<((output[0]-oo[0])*(output[0]-oo[0])/2))                {                    err=(output[0]-oo[0])*(output[0]-oo[0])/2;                }            }*/            for(int i=0;i<realchange.length-3;i++)            {                for(int j=0 ; j<input.length ; j++)                {                    input[j]=realchange[j+i];                }                output[0]=realchange[i+3];                oo[0]=bpforcast(input);                err+=(output[0]-oo[0])*(output[0]-oo[0]);            }            err/=2;            if(err<error)            {                break;            }        }                for(int i=0;i<3;i++)        {            forcastchange[i]=0;            forcast[i]=0;        }        for(int i=3;i<realchange.length+1;i++)        {            forcasttemp[0]=realchange[i-3];            forcasttemp[1]=realchange[i-2];            forcasttemp[2]=realchange[i-1];            this.printArray(forcasttemp, "temp");            forcastchange[i]=bpforcast(forcasttemp);        }        for(int i=realchange.length+1;i<forcastchange.length;i++)        {            forcasttemp[0]=forcastchange[i-3];            forcasttemp[1]=forcastchange[i-2];            forcasttemp[2]=forcastchange[i-1];            this.printArray(forcasttemp, "temp");            forcastchange[i]=bpforcast(forcasttemp);        }        for(int i=0;i<forcast.length;i++)        {            forcast[i]=2*(max-min)*forcastchange[i]+min;        }        this.printArray(w, "w");        this.printArray(v, "v");        this.printArray(real, "实际值");        this.printArray(forcast, "预测值");        this.printArray(realchange, "实际值c");        this.printArray(forcastchange, "预测值c");    }    public double sigMoid(double x)//激励函数    {        return 1.0/(1.0 + Math.exp(-x));    }    public double sigMoid_diff(double x)//激励函数的求导    {        return sigMoid(x)*(1-sigMoid(x));    }    private void assaign(double[][] x,double r)//给权值赋值    {        for(int i=0 ; i<x.length ; i++)        {            for(int j=0 ; j<x[0].length ; j++)            {                x[i][j]=r;            }        }    }    private void printArray(double[] x,String str)//打印一个数组    {        System.out.println(str+ ":");         for(int j=0 ; j<x.length ; j++)         {               System.out.print(x[j]+" ");         }            System.out.println();    }    private void computeOnLevel(double[][] weight,double[] from,double[] to,double[] fx)//计算隐层和输出层的输入    {        for(int i=0 ; i<to.length ; i++)        {            to[i]=0;            for(int j=0 ; j<from.length ; j++)            {                to[i] += weight[j][i]*from[j];            }            to[i]-=fx[i];        }    }    private void inToOut(double[] in , double[] out)//将输入经过激励函数转化为输出    {        for(int i=0 ; i<in.length && i<out.length ; i++)        {            out[i]= sigMoid(in[i]);        }    }    private void computeMo(double[] mo,double[] output,double[] oo,double[] oi)//计算输出层的修改系数    {        for(int i=0 ; i<mo.length ; i++)        {            mo[i]=(output[i]-oo[i])*sigMoid_diff(oi[i]);        }    }    private void computeMh(double[][] v,double[] mh,double[] mo,double[] hi)//计算隐层的修改系数    {        for(int i=0 ; i<hi.length ; i++)        {            mh[i]=0;            for(int j=0 ; j<v[0].length ; j++)            {                mh[i] += mo[j]*v[i][j];            }            mh[i]=mh[i]*sigMoid_diff(hi[i]);        }    }    private void  modifyWeight(double[][] weight,double[] mx, double[] out,double[] fx,double learn)//修改权值和阀值    {        for(int i=0 ; i<weight.length ; i++)        {            for(int j=0 ; j<weight[0].length ; j++)            {                weight[i][j] =learn* mx[j]*out[i];            }        }        for(int i=0;i<mx.length;i++)        {            fx[i]=learn*mx[i];        }    }    public double bpforcast(double[] in)//预测函数    {        double[] hi=new double[4];        double[] ho=new double[4];        double[] oi=new double[1];        this.computeOnLevel(w,in,hi,fh);        this.inToOut(hi, ho);        this.computeOnLevel(v,ho,oi,fo);        return sigMoid(oi[0]);    }    private void printArray(double[][] x , String str)    {         System.out.println(str+ ":");        for(int i=0 ; i<x.length ; i++)        {            for(int j=0 ; j<x[0].length ; j++)            {               System.out.print(x[i][j]+" ");            }            System.out.println();        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -