Stochastic Matrix 
      
      Stochastic processes allow us to choose an outcome based on a random 
        number. 
        The possible outcomes of this random process can range from 1 to an infinite 
        number of choices.  
        Most stochastic systems work best by setting the number of possible choices 
        somewhere between the two.  
      Another important quality of stochastic processes is the ability to weight 
        the possible choices. 
        Weighting the choices basically means to rank the possible choices in 
        order of merit (to give them a percent out of 100 where 0.5 would be equal 
        to 50%). Lets say that we wanted to rank the numbers 1-9 such that the 
        most likely result was the number 5 and the least likely numbers were 
        1 and 9. Numbers between are weighted accordingly.  
       
       
        
           
            |   1   | 
              2   | 
              3   | 
              4   | 
              5   | 
              6   | 
              7   | 
              8   | 
              9   | 
           
           
            |   0.0   | 
              0.075   | 
              0.1   | 
              0.15   | 
              0.3   | 
              0.15   | 
              0.1   | 
              0.075   | 
              0.05   | 
           
        
       
      
      There is one small trick in the above example. 
        Full marks to you if you noticed that the number 1 would never be chosen 
        because it has a weighting of 0.0. Watch what happens if we set the table 
        such that 5 is the ONLY available option.  
       
       
        
           
            |   1   | 
              2   | 
              3   | 
              4   | 
              5   | 
              6   | 
              7   | 
              8   | 
              9   | 
           
           
            |   0.0   | 
              0.0   | 
              0.0   | 
              0.0   | 
              1.0   | 
              0.0   | 
              0.0   | 
              0.0   | 
              0.0   | 
           
        
       
      
      This is an important detail about stochastic 
        matrices, the sum of all weighted choices must be equal to 1.0 and therefore 
        if any individual choice is weighted to 1.0 all other choices must be 
        equal to 0.0.  
      Another reason for the popularity of stochastic 
        processes is the amount of data which they produce. A stochastic matrix 
        is capable of generating any number of outcomes. Let's write a simple 
        program which produces output based on matrix number 1.  
      Click here to view/download 
        the source. 
       
       
        
           
             
                1: public final class StochasticMatrix{   2:         public static void main(String[] args){   3:               4:             int[] numbers = {1,2,3,4,5,6,7,8,9};   5:                6:             double[] weightings = {0.0,0.075,0.1,0.15,0.3,0.15,0.1,0.075,0.05};   7:                8:             int numOfResults = 7;   9:               10:             for(int i=0;i<numOfResults;i++){  11:                   12:                 double choice = Math.random();  13:                          14:                 double currentSum = 0.0;  15:                   16:                 int result = 0;  17:                   18:                 for(;result<numbers.length;result++){  19:                     currentSum+=weightings[result];  20:                     if(choice <= currentSum){  21:                          break;   22:                     }  23:                 }  24:                          25:                 System.out.print(numbers[result]+" , ");  26:             }  27:             System.out.println();  28:         }  29: } | 
           
        
       
        
       |