アットウィキロゴ

ffg

package p4;
import tool.*;
 
public class pro {
 
    int[] point=new int[31];
    String[] data=new String[500];
   int s,number,datanumber;
   double[] mu=new double[11];
  double[] sig=new double[11];
  double[] p=new double[11];
 
 
  double[] mus=new double[11];
  double[] sigs=new double[11];
  double[] ps=new double[11];
 
  double[] dmu=new double[11];
  double[] dsig=new double[11];
 
  double[] c=new double[30];
 
    public static void main(String[] args) {
        pro t=new pro();
    }
 
    pro(){
 
 
 
 
         readfile sub2=new readfile();
        sub2.makedata("data.csv","UTF-8");
        data=sub2.data;
        datanumber=sub2.datanumber;     
 
        for(s=2;s<datanumber+1;s++){
        data[s]=rev_csv(data[s]);
        String[] x=data[s].split(",");
        point[s-1]=num_int(x[1]);
       c[s-1]=50*s-75;
        }
 
 
        datanumber=datanumber-1;
 
        c[datanumber]=1200;
 
 
        number=10;
 
 
        for(s=1;s<number+1;s++){
        mu[s]=100*s;
        sig[s]=100;
        p[s]=0.1;
        }
 
        int mode=0;
        while(mode<3000){
 
        cal sub=new cal();
        sub.point=point;
        sub.number=number;
        sub.datanumber=datanumber;
        sub.mu=mu;
        sub.sig=sig;
        sub.p=p;
        sub.c=c;
        double u1=sub.makedata();
 
 
 
        double h=0.01;
 
        for(s=1;s<number+1;s++){
 
           int sx; 
         for(sx=1;sx<number+1;sx++){
         mus[sx]=mu[sx];    
         }   
 
 
         mus[s]=mus[s]+h;   
 
 
        cal sub3=new cal();
        sub3.point=point;
        sub3.number=number;
        sub3.datanumber=datanumber;
        sub3.mu=mus;
        sub3.sig=sig;
        sub3.p=p;
        sub3.c=c;
        double u3=sub3.makedata();
 
         dmu[s]=(u3-u1)/h;   
 
 
 
        }
 
 
 
        for(s=1;s<number+1;s++){
 
           int sx; 
         for(sx=1;sx<number+1;sx++){
         sigs[sx]=sig[sx];    
         }   
 
 
         sigs[s]=sigs[s]+h;   
 
 
        cal sub4=new cal();
        sub4.point=point;
        sub4.number=number;
        sub4.datanumber=datanumber;
        sub4.mu=mu;
        sub4.sig=sigs;
        sub4.p=p;
        sub4.c=c;
        double u4=sub4.makedata();
 
         dsig[s]=(u4-u1)/h;   
 
 
        }
 
 
        double du=u1/100;
        double dz=0;
        for(s=1;s<number+1;s++){
        dz=dz+dmu[s]*dmu[s];
        dz=dz+dsig[s]*dsig[s];
        }
 
        double t=du/dz;
 
 
         for(s=1;s<number+1;s++){
         mus[s]=mu[s]+dmu[s]*t;
         sigs[s]=sig[s]+dsig[s]*t;
         }   
 
           cal sub5=new cal();
        sub5.point=point;
        sub5.number=number;
        sub5.datanumber=datanumber;
        sub5.mu=mus;
        sub5.sig=sigs;
        sub5.p=p;
        sub5.c=c;
        double u5=sub5.makedata();
 
 
 
        if(u5>u1){
 
         for(s=1;s<number+1;s++){
         mu[s]=mu[s]+dmu[s]*t;
         sig[s]=sig[s]+dsig[s]*t;
         }       
 
 
        }
 
      if(u1>u5)mode=10000;   
     mode=mode+1;    
    }
 
    for(s=1;s<number+1;s++){
    System.out.println(mu[s]+","+sig[s]);
    }    
 
 
 
 
    }
 
       int num_int(String str){
 
    int nx;
 
    nx=0;
 
 try{
 
    nx=Integer.parseInt(str);
 
 }catch (Exception e) {}
 
 return nx;
 
 
 
}    
 
    String rev_csv(String str){
 
        String str5=str;
        int px=str5.indexOf("\"");
 
 
 
        while(px>-1){
       str5=rev(str5);
       px=str5.indexOf("\"");   
        }
        return str5;
    }
 
    String rev(String str){
 
        int p1=str.indexOf("\"");
        int p2=str.indexOf("\"",p1+1);
 
     String str1=str.substring(0,p1);   
        String str2=str.substring(p1+1,p2);  
        String str3=str.substring(p2+1,str.length());
        str2=str2.replace(",","");
 
        return str1+str2+str3;
 
    }
 
 
 
    int randx(){
 
 
    int s;
    double[] px=new double[100];
    px[1]=p[1];
    for(s=2;s<number+1;s++){
    px[s]=px[s-1]+p[s];
    }
 
    double m1=Math.random();
    int nn=1;
    for(s=1;s<number;s++){
    if(m1>px[s])nn=s;
    }
 
 
 
    return nn;
}  
 
 
 
 
 
 
       double f(double x,double mu,double sig){
double pi,f1,f2;
 
        pi=Math.PI;
 
        f2=Math.sqrt(2*pi*sig*sig);
        f1=-(x-mu)*(x-mu)/(2*sig*sig);
 
        return Math.exp(f1)/f2;
}
 
}
 
 
 
package p4;
 
public class cal{
 
   int datanumber;
    int[] point=new int[50];
 
    double[] mu=new double[11];
  double[] sig=new double[11];
  double[] p=new double[11];
  double[] c=new double[50];
  int number;
 
    double makedata(){
 
 
        int s,sx;
 
 
 
 
 
 
    double v1=0;
    for(s=1;s<number+1;s++){
    for(sx=1;sx<datanumber+1;sx++){
    v1=v1+p[s]*point[sx]*f(c[sx],mu[s],sig[s]);
    }
    }
 
 
    return v1;
 
 
 
 
 
 
 
    }
 
        double f(double x,double mux,double sigx){
double pi,f1,f2;
 
        pi=Math.PI;
 
        f2=Math.sqrt(2*pi*sigx*sigx);
        f1=-(x-mux)*(x-mux)/(2*sigx*sigx);
 
        return Math.exp(f1)/f2;
}
 
 
 
 
}
 
最終更新:2016年12月20日 07:19