TommasoBelluzzo / PyDTMC

A library for discrete-time Markov chains analysis.
MIT License
81 stars 18 forks source link

ValidationError: The "p" parameter must contain only values between 0 and 1. #5

Closed bwkchee closed 4 years ago

bwkchee commented 4 years ago

Expected Behavior

For a dense 2d matrix, each row in the matrix is normalized [0,1]. This should be a valid transition matrix p for initializing a MarkovChain

Current Behavior

Throwing a validation error: in 2 import networkx as nx 3 ----> 4 mc = MarkovChain(dense)

~/Library/Python/3.7/lib/python/site-packages/pydtmc/markov_chain.py in init(self, p, states) 131 except Exception as e: 132 argument = ''.join(trace()[0][4]).split('=', 1)[0].strip() --> 133 raise ValidationError(str(e).replace('@arg@', argument)) from None 134 135 self._digraph: tgraph = nx.DiGraph(p)

ValidationError: The "p" parameter must contain only values between 0 and 1.

Steps to Reproduce

For a dictionary states = {col:count (int)}

dense = [] for i in range(len(states)): row = np.zeros(len(states)) for k, v in mat[i].items(): row[k] = v row = row/row.sum() dense.append(row.tolist()) Using dense results in the error above. Even if I bound everything in dense in the following way: dense[dense >= 1.] = 0.99999 dense[dense <= 0.] = 0.00001 I still get the error above.

Environment

Possible Solution

bwkchee commented 4 years ago

The input matrix contained NaNs from row normalization.