audio MIDI Music Algorithms Interfaces Programming Acoustics Context
> Music Algorithms > Analysing > Stochastic Matrix    
 
   

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: //Our data array
4: int[] numbers = {1,2,3,4,5,6,7,8,9};
5: //Our weightings
6: double[] weightings = {0.0,0.075,0.1,0.15,0.3,0.15,0.1,0.075,0.05};
7: //The number of results to generate
8: int numOfResults = 7;
9: //Loop for the number of numbers we want to generate
10: for(int i=0;i<numOfResults;i++){
11: //Retrieve a random number between 0.0 and 1.0
12: double choice = Math.random();
13: //The current sum of wieghtings
14: double currentSum = 0.0;
15: //The result is an index to the data array
16: int result = 0;
17: //Check the weightings
18: for(;result<numbers.length;result++){
19: currentSum+=weightings[result];
20: if(choice <= currentSum){
21: break; //break when we've chosen the right number
22: }
23: }
24: //Print out the chosen number
25: System.out.print(numbers[result]+" , ");
26: }
27: System.out.println();
28: }
29: }

 

 

jMusic Australia Council Queensland University of Technology Sitemap Contact Home Home http://www.qut.com http://explodingart.com/jmusic http://www.ozco.gov.au

Digital Instrument making Home