Guest User

Untitled

a guest
Jul 20th, 2015
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 22.11 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Sun Jul 12 02:42:24 2015
  4. @author: R
  5. """
  6. import scipy
  7. import scipy.linalg
  8. import numpy.linalg
  9. import numpy
  10. from tabulate import tabulate
  11.  
  12.  
  13. precision = 100000000000
  14.  
  15. def MarkovChain(n,s) :
  16.     """
  17.    
  18.    """
  19.     matrix = []
  20.     for l in range(n) :
  21.         lineLst = []
  22.         sum = 0
  23.         crtPrec = precision
  24.         for i in range(n-1) :
  25.             val = random.randrange(crtPrec)
  26.             sum += val
  27.             lineLst.append(float(val)/precision)
  28.             crtPrec -= val
  29.         lineLst.append(float(precision - sum)/precision)
  30.         matrix2 = matrix.append(lineLst)
  31.     print("The intial probability matrix.")    
  32.     print(tabulate(matrix))
  33.     baseprob = []
  34.     for i in range(1000):
  35.         matrix_n = numpy.linalg.matrix_power(matrix, s)
  36.         baseprob.append(matrix_n.item(0))
  37.     print(baseprob)
  38.     print("The final probability matrix.")
  39.     print(tabulate(matrix_n))
  40.     matrixTranspose = zip(*matrix_n)
  41.     evectors = numpy.linalg.eig(matrixTranspose)[1][:,0]
  42.     print("The steady state vector is:")
  43.     print(evectors)
  44.    
  45.    
  46.    
  47.    
  48.    
  49.  
  50. MarkovChain(6, 10000000000)
  51.  
  52. ------------------------------------------------------------------------------
  53. 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