Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import math
- import numpy
- import scipy
- import scipy.linalg
- import scipy.sparse.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))
- matrix_n = numpy.linalg.matrix_power(matrix, s)
- print("The final probability matrix.")
- print(tabulate(matrix_n))
- matrixTranspose = zip(*matrix_n)
- def evecs(matrixTranspose):
- evectors = numpy.linalg.eig(matrixTranspose)[1][:,0]
- return evectors
- if any(x<0 for x in evectors) == False:
- print(evectors)
- MarkovChain(3, 10000000000)
- ------------------------------------------------------------------------------------------------------------------------
- runfile('/home/raleigh/Downloads/Python-3.4.3/MarkovChain.py', wdir='/home/raleigh/Downloads/Python-3.4.3/ipython-3.2.0')
- Reloaded modules: tabulate
- The intial probability matrix.
- -------- --------- --------
- 0.523984 0.324034 0.151982
- 0.359689 0.0638557 0.576455
- 0.107243 0.304804 0.587953
- -------- --------- --------
- The final probability matrix.
- -------- -------- --------
- 0.292137 0.250149 0.457714
- 0.292137 0.250149 0.457714
- 0.292137 0.250149 0.457714
- -------- -------- --------
- Traceback (most recent call last):
- File "<ipython-input-174-9b2fc1fd0b8f>", line 1, in <module>
- runfile('/home/raleigh/Downloads/Python-3.4.3/MarkovChain.py', wdir='/home/raleigh/Downloads/Python-3.4.3/ipython-3.2.0')
- File "/usr/local/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 685, in runfile
- execfile(filename, namespace)
- File "/usr/local/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 78, in execfile
- builtins.execfile(filename, *where)
- File "/home/raleigh/Downloads/Python-3.4.3/MarkovChain.py", line 52, in <module>
- MarkovChain(3, 10000000000)
- File "/home/raleigh/Downloads/Python-3.4.3/MarkovChain.py", line 48, in MarkovChain
- if any(x<0 for x in evectors) == False:
- NameError: global name 'evectors' is not defined
Advertisement
Add Comment
Please, Sign In to add comment