def __init__(self,stateObjLea,chain): ''' initializes StateAlea instance's attributes corresponding to the probability distribution given in stateObjLea and referring to the given chain, instance of Chain ''' Alea.__init__(self,stateObjLea.getAlea().genVPs()) self._chain = chain
def fromMatrix(stateObjs,*transProbsPerState): ''' returns a new Chain instance from given arguments stateObjs is a sequence of objects representing states (strings, usually); transProbsPerState arguments contain the transiiton probability weights; there is one such argument per state, it is a tuple (stateObj,transProbs) where transProbs is the sequence of probability weights of transition from stateObj to each declared state, in the order of their declarations ''' nextStateLeas = (Alea.fromValFreqs(*zip(stateObjs,transProbs)) for (stateObj,transProbs) in transProbsPerState) nextStateLeaPerState = tuple(zip(stateObjs,nextStateLeas)) return Chain(nextStateLeaPerState)
def fromSeq(stateObjSeq): ''' returns a new Chain instance from given sequence of state objects the probabilities of state transitions are set according to transition frequencies in the given sequence ''' (fromStateObjIter,toStateObjIter) = tee(stateObjSeq); for _ in toStateObjIter: break nextStateObjsDict = dict() for (fromStateObj,toStateObj) in zip(fromStateObjIter,toStateObjIter): nextStateObjs = nextStateObjsDict.get(fromStateObj) if nextStateObjs is None: nextStateObjs = [] nextStateObjsDict[fromStateObj] = nextStateObjs nextStateObjs.append(toStateObj) nextStateNameAndObjs = list(nextStateObjsDict.items()) nextStateNameAndObjs.sort() nextStateLeaPerState = tuple((stateObj,Alea.fromVals(*nextStateObjs)) for (stateObj,nextStateObjs) in nextStateNameAndObjs) return Chain(nextStateLeaPerState)