Commit 07951839 authored by Jason Zhang's avatar Jason Zhang

v0.3.3.1

parent 9ba03e15
Pipeline #17368600 passed with stages
in 16 minutes and 50 seconds
No preview for this file type
v0.3.3.1 (2018-02-10)
---------------------
* Fixed bug in which MarkovChain.steady_state might use the wrong eigenvector.
* Added warnings if transition probabilities are negative or don't sum to 1.
v0.3.3.0 (2018-02-08)
---------------------
......
Metadata-Version: 1.1
Name: prob140
Version: 0.3.3.0
Version: 0.3.3.1
Summary: A probability library for Berkeley's Prob140 course
Home-page: http://prob140.org/
Author: Jason Zhang, Dibya Ghosh
Author-email: zhang.j@berkeley.edu
License: GPL
Download-URL: https://gitlab.com/probability/prob140/raw/master/dist/prob140-0.3.3.0.tar.gz
Download-URL: https://gitlab.com/probability/prob140/raw/master/dist/prob140-0.3.3.1.tar.gz
Description-Content-Type: UNKNOWN
Description: UNKNOWN
Keywords: data,probability,berkeley,Prob140
......
from collections import OrderedDict
import warnings
from datascience import Table
import matplotlib.pyplot as plt
......@@ -13,6 +14,11 @@ class MarkovChain:
"""
def __init__(self, states, transition_matrix):
transition_matrix = np.array(transition_matrix)
if not np.all(transition_matrix >= 0):
warnings.warn('Transition matrix contains negative value(s).')
if not np.all(np.isclose(np.sum(transition_matrix, axis=1), 1.)):
warnings.warn('Transition probabilities don\'t sum to 1.')
self.states = states
self.matrix = transition_matrix
......@@ -287,7 +293,13 @@ class MarkovChain:
A | 0.666667
B | 0.333333
"""
eigenvector = np.real(scipy.linalg.eig(self.matrix, left=True)[1][:, 0])
# Steady state is the left eigenvector that corresponds to eigenvalue=1.
w, vl = scipy.linalg.eig(self.matrix, left=True, right=False)
# Find index of eigenvalue = 1.
index = np.isclose(w, 1)
eigenvector = np.real(vl[:, index])[:, 0]
probabilities = eigenvector / sum(eigenvector)
return Table().values(self.states).probability(probabilities)
......
__version__ = '0.3.3.0'
__version__ = '0.3.3.1'
......@@ -78,6 +78,14 @@ def test_construction():
mc_from_matrix.get_transition_matrix()
)
# Negative probability.
with pytest.warns(UserWarning):
MarkovChain.from_matrix([1, 2], [[-1, 2], [0.5, 0.5]])
# Transition probability doesn't sum to 1.
with pytest.warns(UserWarning):
MarkovChain.from_matrix([1, 2], [[0.2, 0.3], [1, 2]])
def test_distribution():
assert_dist_equal(MC_SIMPLE.distribution('A'), [0.1, 0.9])
......
......@@ -518,7 +518,7 @@
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
"version": 3.0
},
"file_extension": ".py",
"mimetype": "text/x-python",
......@@ -529,5 +529,5 @@
}
},
"nbformat": 4,
"nbformat_minor": 1
}
"nbformat_minor": 0
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment