0

Today my task is to make a histogram to represent the operation of A^n where A is a matrix, but only for specific entries in the matrix.

For example, say I have a matrix where the rows sum to one. The first entry is some specific decimal number. However, if I raise that matrix to the 2nd power, that first entry becomes something else, and if I raise that matrix to the 3rd power, it changes again, etc - ad nauseum, and that's what I need to plot.

Right now my attempt is to create an empty list, and then use a for loop to add the entries that result from matrix multiplication to the list. However, all that it does is print the result from the final matrix multiplication into the list, rather than printing its value at each iteration.

Here's the specific bit of code that I'm talking about:

print("The intial probability matrix.")

print(tabulate(matrix))

baseprob = []

for i in range(1000):

     matrix_n = numpy.linalg.matrix_power(matrix, s)

     baseprob.append(matrix_n.item(0))

print(baseprob)

print("The final probability matrix.")

print(tabulate(matrix_n))

Here is the full code, as well as the output I got.

http://pastebin.com/EkfQX2Hu

NightShadeQueen
  • 3,284
  • 3
  • 24
  • 37
Raleigh L.
  • 599
  • 2
  • 13
  • 18

1 Answers1

0

Of course it only prints the final value, you are doing the same operation, matrix^s, 1000 times. You need to have s change each of those 1000 times.

If you want to calculate all values in location matrix(0) for matrix^i where i is each value from 1 to s (your final power) do:

baseprob = []

for i in range(1,s): #changed to do a range 1-s instead of 1000

     #must use the loop variable here, not s (s is always the same)
     matrix_n = numpy.linalg.matrix_power(matrix, i)

     baseprob.append(matrix_n.item(0))

Then baseprob will hold matrix(0) for matrix^1, matrix^2, etc. all the way to matrix^s.

River
  • 8,585
  • 14
  • 54
  • 67
  • Your first answer is exactly what I wanted in terms of the data, however, I'm having trouble representing it in terms of a histogram. Right now, whenever I try it, it prints simply a straight line at the final value. Link to my output: http://i.imgur.com/BNlXTJo.png Also, here's my full code: http://pastebin.com/XhJvQ22d As you can see, the value of baseprob moves between 0.97 to 0.90, but on the histogram, only a line at 0.90 is shown. – Raleigh L. Jul 20 '15 at 23:57
  • @RaleighL. I'm not that familiar with matplotlib, but I think your problem is with `range=[minimum-0.1,minimum+0.1]`. This sets the range to only the minimum. Did you mean to create a `maximum` variable and do `range=[minimum-0.1,maximum+0.1]`? – River Jul 21 '15 at 00:09
  • Even with those changes (the range command you gave), the histogram is still only printing one bar...? My output: http://i.imgur.com/gWfoQw2.png Full code: http://pastebin.com/G7DNUZLq – Raleigh L. Jul 21 '15 at 00:18
  • @RaleighL. Your histogram is not printing one bar. It is printing several, it is just that the bar you are seeing is **so much bigger than the rest**. Try running with `s=10` or `s=30` and you can see this quite clearly. I'm not totally sure what the numbers you generate mean, but you are somehow generating matrixes such that `matrix(0)` becomes closer and closer to a single value as `s` gets larger. Picture of the first 30 values for `matrix(0)`: http://imgur.com/jskeUbh. You can see they are converging on a single final value. With `s=10000` this final value is the only bar you can see. – River Jul 21 '15 at 01:45
  • @RaleighL. if you try a different initial matrix (say with numbers >1), you can see that the `matrix^i` step works fine. It is simply very odd for the specific case you are presenting. – River Jul 21 '15 at 01:52