Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- # Ukryte modele Markova - odtworzenie i prezentacja eksperymentu z „nieuczciwym
- # kasynem” (generator danych tj. „sztuczny krupier” zachowujący się zgodnie z podanymi
- # przez użytkownika prawdopodobieństwami i próba odtworzenia przebiegu podmian kostki na
- # podstawie sekwencji wyników wyjściowych). Typ A. Implementacja algorytmów z wykładu i
- # porównanie predykcji (tj. Viterbi oraz ciąg prawdopodobieństw a'posteriori) z
- # rzeczywistości
- import random
- import numpy as np
- class Dice:
- def __init__(self, name, sequenceStateTable):
- self.name = name
- self.stateNum = len(sequenceStateTable)
- self.tab = sequenceStateTable
- self.actualState = random.randint(1, 6)
- def throwDice(self):
- fortune = random.random()
- result = 0
- for i in range(self.stateNum):
- result += self.tab[i]
- if(result > fortune):
- self.actualState = i
- return i + 1
- return self.stateNum
- class Cassino:
- def __init__(self, diceTab, replaceDiceTable):
- self.diceThrow = diceTab
- self.diceNum = len(diceTab)
- self.replaceDiceTable = replaceDiceTable
- self.actualMainState = 0
- self.timeStep = 0
- pass
- def tryReplaceDice(self):
- fortune = random.random()
- result = 0
- for i in range(len(self.replaceDiceTable)):
- result += self.replaceDiceTable[self.actualMainState][i]
- if (result > fortune):
- self.actualMainState = i
- return i
- pass
- def generateNextState(self):
- # TODO Tu zamieniłem kolejnosc
- diceState = self.tryReplaceDice()
- diceThrowResult = self.diceThrow[self.actualMainState].throwDice()
- return [diceState, diceThrowResult]
- def initCassino():
- fairSequenceStateTable = [np.log(1./6), 1./6, 1./6, 1./6, 1./6, 1./6]
- fairDice = Dice("fair", fairSequenceStateTable)
- loadedSequenceStateTable = [0.1, 0.1, 0.1, 0.1, 0.1, 0.5] # ToDO tu bylo 0.9, zamienilem na 0.5
- loadedDice = Dice("loaded", loadedSequenceStateTable)
- diceTab = [fairDice, loadedDice]
- replaceDiceTable = [[0.95, 0.05], [0.1, 0.9]]#[[0.95, 0.05], [0.1, 0.9]]
- cassino = Cassino(diceTab, replaceDiceTable)
- return cassino
- # ToDo dodana funkcja - na razie tylko wypisuje stany i prawdopodobienstwa
- def countProbability(cassino, throwSequence, startState):
- if len(throwSequence) == 1:
- priorPropability =1
- state = startState
- else:
- state, priorPropability = countProbability(cassino, throwSequence[0:-1], startState)
- max_propability = 0
- for i in range(cassino.diceNum):
- temp_propability = cassino.replaceDiceTable[state][i] * cassino.diceThrow[i].tab[throwSequence[-1]-1]
- if temp_propability>max_propability:
- max_propability = temp_propability
- state = i
- max_propability *= priorPropability
- print (state, max_propability)
- return state, max_propability
- def viterbi2(cassino, throwSequence):
- for i in range (cassino.diceNum):
- print ("sekwencja nr:", i)
- countProbability(cassino, throwSequence, i)
- def viterbi(cassino, throwSequence):
- V = [[1], [0]]
- ptr = [[],[]]
- # for throw in range(1, len(throwSequence) + 1):
- for throw in range(len(throwSequence)):
- for state in range(2):
- acctualThrowIndex = throwSequence[throw] - 1
- ei = cassino.diceThrow[state].tab[acctualThrowIndex]
- pkp0state = V[0][throw] * cassino.replaceDiceTable[0][state]
- pkp1state = V[1][throw] * cassino.replaceDiceTable[1][state]
- maxk = max(pkp0state, pkp1state)
- ptr[state].append(np.argmax([pkp0state, pkp1state]))
- V[state].append( ei*maxk)
- print(V[0])
- print(V[1])
- print(np.argmax(V, 0)[-1])
- print(ptr[0])
- print(ptr[1])
- stateWay = [np.argmax(V, 0)[-1]]
- for i in range(len(throwSequence)-1):
- stateWay.append(ptr[stateWay[-1]][-1-i])
- stateWay = list(reversed(stateWay))
- return stateWay
- def aposterioti(cassino, throwSequence):
- b = [[1],[1]]
- for throwI in range(len(throwSequence)):
- for state in range(2):
- sumBk = 0
- acctualThrowIndex = throwSequence[throwI] - 1
- ei = cassino.diceThrow[state].tab[acctualThrowIndex]
- for i in range(2):
- sumBk = ei*cassino.replaceDiceTable[i][state]*b[state][throwI]
- b[state].append(sumBk)
- print(b)
- pass
- if __name__ == "__main__":
- cassino = initCassino()
- throwSequence = []
- stateSequence = []
- sequenceLen = 30
- for i in range(sequenceLen):
- state, throwResult = cassino.generateNextState()
- throwSequence.append(throwResult)
- stateSequence.append(state)
- throwSequence =[6,1, 2, 6,6,6]
- # throwSequence = [6, 5,1,1,6, 6,4,5,3,1,3,2,6,5,1,2,4,5,6,3,6,6,6,4,6,3,1,6,3,6,6,6,3,1,6,2,3,2,6,4,5,5,2,3,6,2,6,6,6,6,6,6,2,5,1,5,1,6,3,1]
- # throwSequence = [2, 2, 2, 5, 5, 5, 4, 4, 1 , 6, 6, 6, 5, 6, 6, 5, 6 ,3, 5, 6, 4, 3, 2, 4, 3, 6, 4, 1, 3, 1, 5, 1, 3, 4, 6, 5, 1, 4, 6, 3, 5,3,4,1,1,1,2,6,4,1,4,6,2,6,2,5,3,3,5,6]
- #viterbi(cassino, throwSequence, first)
- print("z aposterioti : ",aposterioti(cassino, throwSequence))
- # countProbability(cassino, throwSequence) #ToDo dodalem
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement