Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- Created on Sun Jul 12 02:42:24 2015
- @author: R
- """
- import scipy
- import scipy.linalg
- import numpy.linalg
- import numpy
- from tabulate import tabulate
- precision = 100000000000
- def MarkovChain(n,s) :
- """
- """
- matrix = []
- for l in range(n) :
- lineLst = []
- sum = 0
- crtPrec = precision
- for i in range(n-1) :
- val = random.randrange(crtPrec)
- sum += val
- lineLst.append(float(val)/precision)
- crtPrec -= val
- lineLst.append(float(precision - sum)/precision)
- matrix2 = matrix.append(lineLst)
- 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))
- matrixTranspose = zip(*matrix_n)
- evectors = numpy.linalg.eig(matrixTranspose)[1][:,0]
- print("The steady state vector is:")
- print(evectors)
- MarkovChain(6, 10000000000)
- ------------------------------------------------------------------------------
- runfile('/home/raleigh/Downloads/Python-3.4.3/MarkovChain.py', wdir='/home/raleigh/Downloads/Python-3.4.3/ipython-3.2.0'
Advertisement
Add Comment
Please, Sign In to add comment