Home > Back-end >  The code can Java implementation markov transition matrix
The code can Java implementation markov transition matrix

Time:11-07

In order to predict the rate of change of next, I need to get set up transfer matrix of markov chain model, using existing data rate calculation, calculate the corresponding state, and then the transition probability of each state, through the transition probability, we can predict the rate of change of the next step, but I get is a problem with the transition probability matrix, the sum total of a few lines of probability is not equal to 1, I don't know how to modify it, please help! The code is as follows:

Import the Java. Util. ArrayList;
Import the Java. Util. Arrays;
Import the Java. Util. List;

Public class TestA {


Public static void main (String [] args) {
//real data of 12 test
Double [] rainFall=new double [] {
1438.646484, 1445.384766, 1884.957031, 1878.256836, 1891.259766,
2364.574219, 2773.780273, 2865.074219, 3365.361328, 3390.108398,
4417.552734, 5636.793945

};
Double Max=rainFall [0];
For (int e=1; EIf (rainFall [e] & gt; Max) {
Max=rainFall [e];
}}

Double min=rainFall [0];
For (int k=1; KIf (rainFall [k] Min=rainFall [k].

}}

Double cha=0;
Cha=Max - min;
System. The out. Print (" total variation: "+ cha +" \ n ");
Double [] arr=new double [rainFall. Length];

List LevelList=new ArrayList (a);
//the state of the corresponding value is 0,1,2,3,4,5
Int [] statusValue=https://bbs.csdn.net/topics/new int [],1,2,3,4,5 {0};
//this array used to store each state corresponding to the number of occurrences of and its status value
Int [] statusValueNum=new int [statusValue length].
//calculate change rate
For (int I=1; I & lt; RainFall. Length; I++) {
Double h=rainFall [I] - rainFall [I - 1);
Arr [I]=new Double (Math. Round (h * 1000/cha)/1000.0);
System. The out. Print (" rate is: "+ arr [I] +" \ n ");
}
//is divided into six state
//change at a rate of 0.35-0.2 for state 0, 0.2 -- 0.1 for state 1, 0.1 0 to 2, 0-0.1 to state 3,0.1,0.2 4-0.35-0.2 for state is state 5
For (int j=1; J & lt; RainFall. Length; J++) {
If (arr [j] & gt;=0.35 & amp; & Arr [j] <0.2) {
StatusValueNum [0] + +;
LevelList. Add (0);
}

Else if (arr [j] & gt;=0.2 & amp; & Arr [j] <0.1) {
StatusValueNum [1] + +;
LevelList. Add (1);

}
Else if (arr [j] & gt;=0.1 & amp; & Arr [j] <0 {
StatusValueNum [2] + +;
LevelList. Add (2);

}
Else if (arr [j] & gt;=0 & amp; & Arr [j] <0.1) {
StatusValueNum [3] + +;
LevelList. Add (3);
}
Else if (arr [j] & gt;=0.1 & amp; & Arr [j] <0.2) {
StatusValueNum [4] + +;
LevelList. Add (4);
}
Else if (arr [j] & gt;=0.2 & amp; & Arr [j] <0.35) {
StatusValueNum [5] + +;
LevelList. Add (5);
}
//}
}
//the state of the corresponding values
System. The out. Println (" the state of the corresponding values: \ n "+ levelList +" \ n ");
System. Out. Println (" state 0,1,2,3,4,5 respectively the number of occurrences of: \ n "+ Arrays. ToString (statusValueNum) +" \ n ");
//get transition probability matrix
Double [] [] transProbablityMatrix=statusTransProbablity (statusValueNum, levelList);
System. The out. Println (" introduction to step transfer matrix results: ");
For (int I=0; I & lt; TransProbablityMatrix. Length; I++) {
for (int j=0; J & lt; TransProbablityMatrix. Length; J++) {
System. The out. Printf (" %. 4 f \ t, "transProbablityMatrix [I] [j]);//output formatted data
}
System.out.println();
}
}

Explanation:/* step transition probability matrix state have 0 1, 2, 3, 4, 5, when the current state, such as 0 and the next state is such as 1, called step state transition */
Public static Double [] [] statusTransProbablity (int [] statusValueNum, List LevelList) {
Double [] [] transProbablityMatrix=new Double [6] [6].
//step transition probability matrix
///* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * iterate through the result step state transition probability matrix is * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
For (int s=0; S & lt; StatusValueNum. Length; S++) {
//change s acted as a control array lines status value
Int status=0;
//state the type of the value of 0 1 2 3 4 5 when every time after traversal, the status to change
For (int I=0; I & lt; StatusValueNum. Length; I++) {
int index=0;//record step transfer state the number of times each traversal is clear 0
for (int j=0; J & lt; LevelList. The size () - 1; J++) {
//levelList. The size () - 1 reason is k + 1 can achieve chain end
Int k=j;
//k value is to use if the current state in the current traversal and traversing the whole data every time the next state values are equal only to find a state change
//such as first find 0 to 0, and then is 0 to 1 so on
While (levelList) get (k)) intValue ()==s & amp; & LevelList. Get (k + 1). IntValue ()=={
the status)Index++;//counter plus 1
If (k & lt; LevelList. The size () - 2) {//minus 2 explain when k=28 levelList) get (k + 1) will subscript crossing the line here, it is more than statements range
k++;//the current position backward a namely to query a status value for the number of occurrences of the
} else {
break;//when does not meet the current conditions, terminate the loop body recently
}
}
}
//System. Out. Println (" Trans/" + s + "/" + I + "" + index);
//calculate probability value
TransProbablityMatrix [s] [I]=Double. The valueOf (index)/Double. The valueOf (statusValueNum [s]);
Status++;
}
}
Return transProbablityMatrix;
}


}
  • Related