Guest User

Untitled

a guest
Jul 16th, 2015
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.57 KB | None | 0 0
  1.  
  2. import random
  3. import math
  4. import numpy
  5. import scipy
  6. import scipy.linalg
  7. import scipy.sparse.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.     matrix_n = numpy.linalg.matrix_power(matrix, s)  
  34.     print("The final probability matrix.")
  35.     print(tabulate(matrix_n))
  36.     matrixTranspose = zip(*matrix_n)
  37.    
  38.     def evecs(matrixTranspose):
  39.          evectors = numpy.linalg.eig(matrixTranspose)[1][:,0]
  40.          return evectors
  41.          
  42.    
  43.     if any(x<0 for x in evectors) == False:
  44.         print(evectors)
  45.    
  46.  
  47. MarkovChain(3, 10000000000)
  48.  
  49. ------------------------------------------------------------------------------------------------------------------------
  50.  
  51. runfile('/home/raleigh/Downloads/Python-3.4.3/MarkovChain.py', wdir='/home/raleigh/Downloads/Python-3.4.3/ipython-3.2.0')
  52. Reloaded modules: tabulate
  53. The intial probability matrix.
  54. --------  ---------  --------
  55. 0.523984  0.324034   0.151982
  56. 0.359689  0.0638557  0.576455
  57. 0.107243  0.304804   0.587953
  58. --------  ---------  --------
  59. The final probability matrix.
  60. --------  --------  --------
  61. 0.292137  0.250149  0.457714
  62. 0.292137  0.250149  0.457714
  63. 0.292137  0.250149  0.457714
  64. --------  --------  --------
  65. Traceback (most recent call last):
  66.  
  67.   File "<ipython-input-174-9b2fc1fd0b8f>", line 1, in <module>
  68.     runfile('/home/raleigh/Downloads/Python-3.4.3/MarkovChain.py', wdir='/home/raleigh/Downloads/Python-3.4.3/ipython-3.2.0')
  69.  
  70.   File "/usr/local/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 685, in runfile
  71.     execfile(filename, namespace)
  72.  
  73.   File "/usr/local/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 78, in execfile
  74.     builtins.execfile(filename, *where)
  75.  
  76.   File "/home/raleigh/Downloads/Python-3.4.3/MarkovChain.py", line 52, in <module>
  77.     MarkovChain(3, 10000000000)
  78.  
  79.   File "/home/raleigh/Downloads/Python-3.4.3/MarkovChain.py", line 48, in MarkovChain
  80.     if any(x<0 for x in evectors) == False:
  81.  
  82. NameError: global name 'evectors' is not defined
Advertisement
Add Comment
Please, Sign In to add comment