• 股票预测程序设计思路与实现 <持续更新>


    设计构想如下图

             通过历年上证日指数据,对未来数据进行上涨或下降的预测,尚不对成交量及成绩额进行预测。未来实现过程中采用方案2,代码编写中…

    模型说明:

     

    对于一由0/1组成的字符串,按照每一个字符为一个单位,统计从状态0转换到状态0 or 1的概率,以及从状态1转换到状态0/1的概率,并将其转化成矩阵,上图可转化为矩阵:

    当考虑字符串按照两个字符为一组进行组合时,按照三个字符为一组,四个…考虑到股票交易市场,连续5天交易的情况,将分组上限划分为五个元素为一组(如000001可被划分为两组:00000 && 00001),五种划分方式分别求出状态转换间对应的概率,并对转换的矩阵求矩阵的n次方,直到矩阵收敛(前提矩阵是收敛的)。

    通过以上操作得到五个模型,采用某些算法(如EM可得到多个高斯分布函数的权重)减少与实际值误差,得到拟合曲线。

     

    设计草稿及简单陈述如上,持续更新中。因08年金融危机,选择从09年1月开始,到11年4月21日,共551条记录。

    现阶段源码如下:

    View Code
      1 import java.io.BufferedReader;
      2 import java.io.FileReader;
      3 import java.util.ArrayList;
      4 
      5 
      6 public class Z_gess_stock {
      7 /**
      8  * using up--related to calculate the situation, when UP appear, one UP, two UP...
      9  * to predict what next when this day we get a rise.
     10  *  
     11  * using down--related to calculate the situation, when DOWN happens, 
     12  * that is to say, to predict what will happen, when this day we get a fall.
     13  */
     14     private Vector[] points_perday;//used to pridict the quantity
     15     static int limit=5;//to set the biggest days after today
     16     double[][] UP;//the probability of A[i][j], i days up then j days down
     17     double[][] DOWN;//i days down then j days up
     18     double[] upprob;
     19     double[] downprob;
     20     
     21     
     22     public Z_gess_stock(int k) {    
     23         points_perday=new Vector[k];
     24         UP=new double[limit][limit];
     25         upprob=new double[limit];
     26         
     27     }
     28     
     29     public void init_up(ArrayList<Double> points){
     30         
     31         int A=0,B=0;
     32         double temp=points.get(0);
     33         
     34         double sum_upprob=0;
     35         
     36         for(int i=0;i<points.size();i++){
     37 
     38             for(;i<points.size();i++)//calculate value i
     39             {
     40                 if(temp<points.get(i)){
     41                     temp=points.get(i);
     42                     A++;
     43                 }
     44                 else if(temp>points.get(i)){
     45                     temp=points.get(i);
     46                     B++;
     47                     i++;
     48                     break;
     49                 }
     50             }
     51             
     52 
     53             for(;i<points.size();i++)//calculate value j
     54             {
     55                 if(temp<points.get(i)){
     56                     temp=points.get(i);
     57                     A++;
     58                     i++;
     59                     break;
     60                 }
     61                 else if(temp>points.get(i)){
     62                     temp=points.get(i);
     63                     B++;
     64                 }
     65             }
     66             if(A-1<limit&&B-1<limit)
     67             {
     68                 UP[A-1][B-1]+=1;
     69                 upprob[B-1]++;
     70             }
     71             else {
     72             //    UP[limit-1][limit-1]++;
     73             //    upprob[limit-1]++;
     74             }
     75             sum_upprob++;
     76             
     77             B=0;
     78             A=1;    
     79         }
     80         printMatrix(UP);
     81         
     82         for(int i=0;i<upprob.length;i++){
     83             for(int j=0;j<upprob.length&&upprob[i]!=0;j++)
     84                 UP[j][i]/=upprob[i];
     85         upprob[i]/=sum_upprob;
     86         }
     87         
     88         
     89         
     90     }
     91     
     92     public void printMatrix(double[][] A){
     93         for(int i=0;i<A.length;i++){
     94             for(int j=0;j<A[0].length;j++)
     95                 System.out.print(A[i][j]+" ");
     96             System.out.println();
     97         }
     98         System.out.println();
     99     }
    100         
    101     public void init_down(){
    102         
    103     }    
    104     /**
    105      * making matrix converges to a steady state
    106      * @param n to calculate A^n, the powers of the transition
    107      */
    108     public double[][] Matrix_mul(double[][] A,double[][] B){
    109         if(A[0].length!=B.length){
    110             System.out.println("wrong size");
    111             return A;
    112         }
    113         
    114         double[][] temp =new double[A.length][B[0].length];
    115         
    116         int n=A[0].length;//column
    117         int m=A.length;//row
    118         for(int i=0,x,y;i<m;i++){
    119             for(x=0;x<m;x++){
    120                 double sum=0;
    121                 for(y=0;y<n;y++)
    122                     sum+=A[i][y]*B[y][x];
    123                 temp[i][x]=sum;
    124             }            
    125         }
    126         
    127         return temp;    
    128     }
    129     
    130     private double[][] iterature(int n, double[][] A){
    131         if(n==1){
    132             return A;
    133         }
    134         
    135         if(n%2.0==0)
    136             {
    137             double[][] B=iterature(n/2,A);
    138             return Matrix_mul(B,B);
    139             }
    140         else{
    141             double[][] B=iterature((n-1)/2,A);
    142             return Matrix_mul(Matrix_mul(B,B),B);
    143         }
    144         
    145         
    146     }
    147     
    148      public static ArrayList<Double> read_data(String filename,int column){
    149         ArrayList<Double> list =new ArrayList<Double>();
    150         
    151         FileReader fr;
    152         BufferedReader bfr;
    153         try{
    154             fr =new FileReader(filename);
    155             bfr =new BufferedReader(fr);
    156             String str =bfr.readLine();
    157 
    158             while(str != null){
    159                 String[] temp =str.split(" ");
    160                 list.add(Double.valueOf(temp[column-1]));
    161                 str =bfr.readLine();
    162             }
    163             
    164         }catch(Exception e){
    165             //System.out.println("\n??");
    166             e.printStackTrace();
    167         }
    168         return list;
    169     }
    170     
    171     public static void main(String[] args) {
    172         //limit=7;
    173         ArrayList<Double> points=read_data("stock.txt",5);//只计算收盘价格
    174 
    175         //For every 5 days we name it one cluster
    176         Z_gess_stock stock=new Z_gess_stock(points.size()-4);
    177         System.out.println(points.size());
    178 //        for(int i=0;i<points.size();i++)
    179 //            System.out.print(points.get(i)+" ");
    180         
    181         stock.init_up(points);
    182         
    183         stock.printMatrix(stock.UP);
    184                 
    185         stock.UP=stock.iterature(100, stock.UP);
    186         stock.printMatrix(stock.UP);
    187                     
    188         for(int j=0;j<stock.UP.length;j++)
    189             System.out.print(stock.upprob[j]+" ");
    190         
    191         
    192         System.out.println("\n  ");
    193 
    194         double test[][]={
    195                 {0.1, 0.2, 0.3, 0.4},
    196                 {0.9, 0.0, 0.0, 0.0},
    197                 {0.0, 0.8, 0.0, 0.0},
    198                 {0.0, 0.0, 0.7, 0.6}};
    199         test=stock.iterature(1000, test);
    200         System.out.print("\n");
    201         //System.out.println("\n"+test[0].length+" "+test.length);
    202         for(int i=0;i<4;i++){
    203             for(int j=0;j<4;j++)
    204                 System.out.print(test[i][j]+" ");
    205             System.out.println();
    206         }
    207         
    208 
    209     }
    210 
    211 }

    运行结果为:

    Ps:源码采用“看涨”方式,即今日为涨时,未来几天的情况,更新中^_^

    551

    25.0 13.0 8.0 1.0 1.0
    9.0 5.0 0.0 3.0 0.0
    7.0 11.0 0.0 2.0 0.0
    9.0 1.0 1.0 0.0 0.0

    0.5 0.43333333333333335 0.8888888888888888 0.16666666666666666 1.0
    0.18 0.16666666666666666 0.0 0.5 0.0
    0.14 0.36666666666666664 0.0 0.3333333333333333 0.0
    0.18 0.03333333333333333 0.1111111111111111 0.0 0.0

    0.4821428571428557 0.4821428571428557 0.4821428571428557 0.4821428571428557 0.4821428571428557
    0.2129464285714279 0.2129464285714279 0.2129464285714279 0.2129464285714279 0.21294642857142793
    0.2651785714285706 0.26517857142857054 0.2651785714285706 0.2651785714285706 0.2651785714285706
    0.03973214285714274 0.03973214285714274 0.03973214285714274 0.03973214285714274 0.03973214285714274

    0.45871559633027525 0.27522935779816515 0.08256880733944955 0.05504587155963303 0.009174311926605505

    0.25773195876291055 0.25773195876291055 0.25773195876291055 0.25773195876291055
    0.23195876288661954 0.23195876288661954 0.23195876288661954 0.23195876288661954
    0.18556701030929562 0.18556701030929562 0.18556701030929562 0.18556701030929562
    0.3247422680412674 0.3247422680412674 0.3247422680412674 0.3247422680412674

  • 相关阅读:
    报表插件
    开发工具安装运行bug总结
    UML学习笔记
    Asp.net MVC中的ViewData与ViewBag
    mvc 4 Razor (@html.xx)语法大全以及应用
    数据大并发处理
    vs2010常用快捷方式
    asp.net mvc3+EF4.1项目实战
    jquery,javascript常用
    vs2010常见错误
  • 原文地址:https://www.cnblogs.com/sunshinewill/p/3003285.html
Copyright © 2020-2023  润新知