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