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

📄 bp.java

📁 销售预测的实现,包括移动平均,指数平滑,时间序列分解,自回归
💻 JAVA
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package forcast;/** * * @author Administrator */public class bp{    double E;    double rate;    int step,number;    double result;    double[][] W1,W2;    double[] Input,Output,HI,HO,M1,M2,data,datachange,forcastchange,forcast;    public bp(double[] d )    {      W1 = new double[3][4];      W2 = new double[4][1];      Input = new double[3];      Output = new double[1];      HI = new double[4];      HO = new double[4];      M1 = new double[4];      M2 = new double[1];      //this.assaign(W1,0.1);      //this.assaign(W2,0.1);      rate = 0.5;      number=5;      E = 0.02;      step = 1000;      data=d;    }    public void bp_main()    {        int n=0;        double max,min;        max=min=0;        datachange=new double[data.length];        forcastchange=new double[data.length+number];        forcast=new double[data.length+number];        double[] forcasttemp=new double[3];        for(int i=0;i<data.length;i++)        {            if(max<data[i])            {                max=data[i];            }            if(min>data[i])            {                min=data[i];            }        }        for(int i=0;i<data.length;i++)        {            datachange[i]=(data[i]-min)/(2*(max-min));        }        int k=0;        while(n<step)        {            double err=0;            for(int i=0;i<data.length-3;i++)            {                double[] in = new double[4];                for(int j=0 ; j<in.length ; j++)                {                    in[j]=datachange[j+i];                }               this.inToOut(in, Input);               double real = in[in.length-1];               this.computeOnLevel(W1, Input, HI);               this.inToOut(HI, HO);               this.computeOnLevel(W2, HO,Output);                result = this.sigMoid(Output[0]);                double temp = real - result;                M2[0] = - temp*this.sigMoid_diff(Output[0]);                this.modifyWeight(W2, M2, HO);                this.computeMu(W2, M1, M2, HI);                this.modifyWeight(W1, M1, Input);                this.printArray(W1, "W1");                this.printArray(W2, "W2");                n++;                //double err = temp*temp*0.5;                //System.out.println("Result:"+ result+" "+ in[in.length-1]+" " +k);            }            for(int i=0;i<data.length-3;i++)            {                double[] in = new double[4];                for(int j=0 ; j<in.length ; j++)                {                    in[j]=datachange[j+i];                }                this.inToOut(in, Input);                double real = in[in.length-1];                this.computeOnLevel(W1, Input, HI);                this.inToOut(HI, HO);                this.computeOnLevel(W2, HO,Output);                result = this.sigMoid(Output[0]);                double temp = real - result;                if(err<(temp*temp*0.5))                {                    err=temp*temp*0.5;                }            }                                if(err<E)                {                    System.out.println("ERR:"+err);                    break;                }                    }        for(int i=0;i<3;i++)        {            forcastchange[i]=0;            forcast[i]=0;        }        for(int i=3;i<datachange.length+1;i++)        {            forcasttemp[0]=datachange[i-1];            forcasttemp[1]=datachange[i-2];            forcasttemp[2]=datachange[i-3];            forcastchange[i]=bpforcast(forcasttemp);        }        for(int i=datachange.length+1;i<forcastchange.length;i++)        {            forcasttemp[0]=forcastchange[i-1];            forcasttemp[1]=forcastchange[i-2];            forcasttemp[2]=forcastchange[i-3];            forcastchange[i]=bpforcast(forcasttemp);        }        for(int i=0;i<forcast.length;i++)        {            forcast[i]=2*(max-min)*forcastchange[i]+min;        }        this.printArray(data, "实际值");        this.printArray(forcast, "预测值");    }    public double bpforcast(double[] in)    {        this.inToOut(in, Input);        this.computeOnLevel(W1, Input, HI);        this.inToOut(HI, HO);        this.computeOnLevel(W2, HO,Output);        //this.printArray(W1, "W1");        //this.printArray(W2, "W2");        //System.out.println("E:"+ Output[0]);        return this.sigMoid(Output[0]);    }    private void computeOnLevel(double[][] w,double[] from,double[] to)    {        for(int i=0 ; i<to.length ; i++)        {            to[i]=0;            for(int j=0 ; j<from.length ; j++)            {                to[i] += w[j][i]*from[j];            }        }    }    private void computeMu(double[][] w,double[] m1,double[] m2,double[] in)    {        for(int i=0 ; i<in.length ; i++)        {            m1[i]=0;            for(int j=0 ; j<w[0].length ; j++)            {                m1[i] += m2[j]*w[i][j];            }            m1[i]=m1[i]*sigMoid_diff(in[i]);        }    }    private void  modifyWeight(double[][] w,double[] m, double[] out)    {        for(int i=0 ; i<w.length ; i++)        {            for(int j=0 ; j<w[0].length ; j++)            {                //w[i][j] += rate * m[j]*out[i];//-号?                w[i][j] += (-rate * m[j]*out[i]);            }        }    }    private void inToOut(double[] in , double[] out)    {        for(int i=0 ; i<in.length && i<out.length ; i++)        {            out[i]= sigMoid(in[i]);        }    }    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 i=0 ; i<x.length ; i++)        {            for(int j=0 ; j<x[0].length ; j++)            {               System.out.print(x[i][j]+" ");            }            System.out.println();        }    }    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();    }}

⌨️ 快捷键说明

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