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

设计构想如下图

         通过历年上证日指数据,对未来数据进行上涨或下降的预测,尚不对成交量及成绩额进行预测。未来实现过程中采用方案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

转载于:https://www.cnblogs.com/sunshinewill/archive/2013/04/07/3003285.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
股神--人工智能股票预测系统是专门为股票投资者开发的一套全新的基于人工智能技术的股票趋势预测软件平台。该软件以基因演化算法(GP)为内核对股票交易历史数据进行自动建模和学习,挖掘出股票交易大数据中隐藏的行为规律,并以此为依据对下一个股票日的最高价和最低价的涨跌趋势进行预测分析。该软件能够帮助您了解何时进入股市,何时退出股市,并在最佳的时机买进或卖出股票,从而获取最大的利润和收益。 支持6种典型的股票类别:上证指数、上证A股、上证B股、深证指数、深证A股和深证B股。 精确的股票预测信息(如上涨、下跌或持平)和买卖推荐信息(如买入、卖出、持股以及买入价、卖出价等)。 基因演化算法参数支持用户自定义,默认设置为种群大小:30,杂交概率:0.8,变异概率:0.1,最大运行代数:1000。 支持批量操作,如股票批量评测、模型批量训练、股票批量预测、批量增加股票代码、批量添加/撤销我的股票池等。 对大多数股票而言,最高价与最低价的涨跌趋势预测准确度达60%-80%;对部分股票而言,预测准确度最高可达90%。 仅需简单的操作即可完成股票评测、智能选股、模型训练以及股票预测等功能。 系统主界面支持从云数据库和本地数据库自动更新最优股票预测信息。 支持流行的微软Windows操作系统,如Windows 98/Me/2000/XP/Vista/7。 股神--人工智能股票预测系统既适用于专业的股票投资者,也适用于股票初学者。您可以通过股神系统轻轻松松地完成股票评测、智能选股、模型训练以及股票预测等功能,所有操作简单易懂,软件界面友好大方。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值