samedi 11 juillet 2015

Creating a matrix of arbitrary size where rows sum to 1?

My task is to create a program that simulates a discrete time Markov Chain, for an arbitrary number of events. However, right now the part I'm struggling with is creating the right stochastic matrix that will represent the probabilities. A right stochastic matrix is a matrix that has row entries that sum to 1. And for a given size, I kind of know how to write the matrix that does that, however, the problem is that I don't know how to do that for an arbitrary size.

For example: here is my code for a 3x3 matrix, along with example of the output I was getting.

http://ift.tt/1CwKYsk

However, my code doesn't work every time -- there are certain times when the third entry in the row is negative because the first two are too large. And I don't know how to get around that, as far as I know, there isn't a function in Python that makes it so that you can generate random numbers that sum to something in particular.

Any help is appreciated.

(Note that this isn't a homework problem, it's only for extra credit in my Math class and the professor doesn't mind the use of outside sources.)




Aucun commentaire:

Enregistrer un commentaire