
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if a Given Matrix is a Markov Matrix Using Python
In this article we are going to learn how to check whether the input matrix is a Markov Matrix using nested for loops and sum() function. Before that let us understand what is Markov Matrix with an example?
The matrix is said to be a Markov matrix if the sum of each row is equal to 1.
Example
[ [ 0, 1, 0 ], [ 0, 0.3, 0.7 ], [ 0, 0, 1 ]]
In the above example, the sum of each row is 1. Hence the above matrix is an example of a Markov matrix.
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task .
Create a function isMarkovMatrix() to check whether the input matrix is a Markov matrix by accepting the input matrix, no of rows as arguments.
Use the for loop, to traverse through the rows of a matrix.
Create a variable to store the sum of elements of the current row and initialize its value with 0.
Use another nested for loop, to traverse through all the columns of a current row by using len() function(returns the number of items in an object).
Get the sum of the current row by adding the above current row sum with the current element
Use the if conditional statement to check whether the sum of the current row is not equal to 1.
Return false if the condition is true i.e, the sum of the current row is not equal to 1.
Else return True.
Create a variable to store the input matrix.
Call the above isMarkovMatrix() function by passing the input matrix, no of rows as arguments to it, and check whether the function returns true using an if conditional statement.
Print the input matrix is a Markov Matrix if the condition is true.
Else print the input matrix is NOT a Markov Matrix.
Example
The following program the function iterates through each row of the matrix, calculating the sum of its elements. If any row's sum is not equal to 1, the function returns False, indicating that the matrix is not a Markov matrix. If all rows pass the sum check, the function returns True, confirming that the matrix satisfies the Markov property.
# creating a function to check whether the input matrix # is a Markov matrix by accepting input matrix as an argument def isMarkovMatrix(inputMatrix): # traversing through the rows of a matrix for p in range(0, len(inputMatrix)): # Taking a variable to store the sum of elements of current row currentrow_sum = 0 # traversing through all the columns of a current row for q in range(0, len(inputMatrix[p])): # getting the sum of current row currentrow_sum = currentrow_sum + inputMatrix[p][q] # checking whether the sum of current row is not equal to 1 if (currentrow_sum != 1): # returning false if sum is not equal to 1 return False # else returning True return True # input matrix inputMatrix = [[0, 1, 0], [0, 0.3, 0.7], [0, 0, 1]] if (isMarkovMatrix(inputMatrix)): # printing Markov matrix it function returns true print("The input matrix is a Markov matrix") else: # else printing Not a Markov matrix print("The input matrix is Not a Markov matrix")
Output
On executing, the above program will generate the following output
The input matrix is a Markov matrix
Method 2: Using sum() function
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task -
Define a function named isMarkovMatrix that takes the input matrix as an argument.
Traverse through each row of the matrix using a for loop.
Initialize a variable currentrow_sum to store the sum of elements in the current row.
Within the nested loop, traverse through each column of the current row.
Add the element at the current row and column to currentrow_sum.
After the inner loop completes, check if currentrow_sum is not equal to 1.
If the sum is not equal to 1, return False to indicate that the matrix is not a Markov matrix.
If all rows pass the sum check, return True to indicate that the matrix is a Markov matrix.
Create an input matrix using a 2D list.
Call the isMarkovMatrix function with the input matrix as an argument.
Use an if-else statement to check the return value of the function.
If the function returns True, print "The input matrix is a Markov matrix".
If the function returns False, print "The input matrix is not a Markov matrix".
Example
The following program checks whether the input matrix is a Markov matrix using sum function
def isMarkovMatrix(inputMatrix): for row in inputMatrix: rowSum = sum(row) if rowSum != 1: return False return True # Input matrix inputMatrix = [[0, 1, 0], [0, 0.3, 0.7], [0, 0, 1]] if isMarkovMatrix(inputMatrix): print("The input matrix is a Markov matrix") else: print("The input matrix is not a Markov matrix")
Output
The input matrix is a Markov matrix
Conclusion
In conclusion, the Python programs presented above provide two different methods to check if an input matrix is a Markov matrix. The first method uses nested for loops to iterate through the matrix and calculate the sum of each row, while the second method utilizes the sum() function to calculate row sums. Both methods yield the same result and can be used to determine whether a matrix satisfies the Markov property. These programs offer a practical approach to validate Markov matrices and can be applied in various fields that involve probability and stochastic processes.