Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

bio

a guest Dec 17th, 2017 65 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. # -*- coding: utf-8 -*-
  2.  
  3. #  Ukryte modele Markova - odtworzenie i prezentacja eksperymentu z „nieuczciwym
  4. # kasynem” (generator danych tj. „sztuczny krupier” zachowujący się zgodnie z podanymi
  5. # przez użytkownika prawdopodobieństwami i próba odtworzenia przebiegu podmian kostki na
  6. # podstawie sekwencji wyników wyjściowych). Typ A. Implementacja algorytmów z wykładu i
  7. # porównanie predykcji (tj. Viterbi oraz ciąg prawdopodobieństw a'posteriori) z
  8. # rzeczywistości
  9. import random
  10. import numpy as np
  11.  
  12. class Dice:
  13.  
  14.     def __init__(self, name, sequenceStateTable):
  15.         self.name = name
  16.         self.stateNum = len(sequenceStateTable)
  17.         self.tab = sequenceStateTable
  18.         self.actualState = random.randint(1, 6)
  19.  
  20.     def throwDice(self):
  21.         fortune = random.random()
  22.         result = 0
  23.         for i in range(self.stateNum):
  24.             result += self.tab[i]
  25.             if(result > fortune):
  26.                 self.actualState = i
  27.                 return i + 1
  28.         return self.stateNum
  29.  
  30.  
  31.  
  32. class Cassino:
  33.  
  34.     def __init__(self, diceTab, replaceDiceTable):
  35.         self.diceThrow = diceTab
  36.         self.diceNum = len(diceTab)
  37.         self.replaceDiceTable = replaceDiceTable
  38.         self.actualMainState = 0
  39.         self.timeStep = 0
  40.         pass
  41.  
  42.     def tryReplaceDice(self):
  43.         fortune = random.random()
  44.         result = 0
  45.         for i in range(len(self.replaceDiceTable)):
  46.             result += self.replaceDiceTable[self.actualMainState][i]
  47.             if (result > fortune):
  48.                 self.actualMainState = i
  49.                 return i
  50.         pass
  51.  
  52.     def generateNextState(self):
  53.         # TODO Tu zamieniłem kolejnosc
  54.         diceState = self.tryReplaceDice()
  55.         diceThrowResult = self.diceThrow[self.actualMainState].throwDice()
  56.         return [diceState, diceThrowResult]
  57.  
  58. def initCassino():
  59.     fairSequenceStateTable = [np.log(1./6), 1./6, 1./6, 1./6, 1./6, 1./6]
  60.     fairDice = Dice("fair", fairSequenceStateTable)
  61.  
  62.     loadedSequenceStateTable = [0.1, 0.1, 0.1, 0.1, 0.1, 0.5] # ToDO tu bylo 0.9, zamienilem na 0.5
  63.     loadedDice = Dice("loaded", loadedSequenceStateTable)
  64.     diceTab = [fairDice, loadedDice]
  65.     replaceDiceTable = [[0.95, 0.05], [0.1, 0.9]]#[[0.95, 0.05], [0.1, 0.9]]
  66.  
  67.     cassino = Cassino(diceTab, replaceDiceTable)
  68.     return cassino
  69.  
  70. # ToDo dodana funkcja - na razie tylko wypisuje stany i prawdopodobienstwa
  71. def countProbability(cassino, throwSequence, startState):
  72.     if len(throwSequence) == 1:
  73.         priorPropability =1
  74.         state = startState
  75.     else:
  76.         state, priorPropability = countProbability(cassino, throwSequence[0:-1], startState)
  77.  
  78.  
  79.     max_propability = 0
  80.     for i in range(cassino.diceNum):
  81.         temp_propability = cassino.replaceDiceTable[state][i] * cassino.diceThrow[i].tab[throwSequence[-1]-1]
  82.         if temp_propability>max_propability:
  83.             max_propability = temp_propability
  84.             state = i
  85.     max_propability *= priorPropability
  86.     print (state, max_propability)
  87.     return state, max_propability
  88.  
  89. def viterbi2(cassino, throwSequence):
  90.     for i in range (cassino.diceNum):
  91.         print ("sekwencja nr:", i)
  92.         countProbability(cassino, throwSequence, i)
  93.  
  94.  
  95.  
  96.  
  97.  
  98. def viterbi(cassino, throwSequence):
  99.     V = [[1], [0]]
  100.     ptr = [[],[]]
  101.  
  102.     # for throw in range(1, len(throwSequence) + 1):
  103.     for throw in range(len(throwSequence)):
  104.         for state in range(2):
  105.             acctualThrowIndex = throwSequence[throw] - 1
  106.  
  107.             ei = cassino.diceThrow[state].tab[acctualThrowIndex]
  108.             pkp0state = V[0][throw] * cassino.replaceDiceTable[0][state]
  109.             pkp1state = V[1][throw] * cassino.replaceDiceTable[1][state]
  110.  
  111.             maxk = max(pkp0state, pkp1state)
  112.             ptr[state].append(np.argmax([pkp0state, pkp1state]))
  113.             V[state].append( ei*maxk)
  114.  
  115.     print(V[0])
  116.     print(V[1])
  117.     print(np.argmax(V, 0)[-1])
  118.     print(ptr[0])
  119.     print(ptr[1])
  120.  
  121.  
  122.     stateWay = [np.argmax(V, 0)[-1]]
  123.     for i in range(len(throwSequence)-1):
  124.         stateWay.append(ptr[stateWay[-1]][-1-i])
  125.  
  126.     stateWay = list(reversed(stateWay))
  127.     return stateWay
  128.    
  129. def aposterioti(cassino, throwSequence):
  130.     b = [[1],[1]]
  131.     for throwI in range(len(throwSequence)):
  132.         for state in range(2):
  133.             sumBk = 0
  134.             acctualThrowIndex = throwSequence[throwI] - 1
  135.             ei = cassino.diceThrow[state].tab[acctualThrowIndex]
  136.             for i in range(2):
  137.                 sumBk = ei*cassino.replaceDiceTable[i][state]*b[state][throwI]
  138.             b[state].append(sumBk)
  139.     print(b)
  140.     pass
  141.  
  142. if __name__ == "__main__":
  143.     cassino = initCassino()
  144.     throwSequence = []
  145.     stateSequence = []
  146.     sequenceLen = 30
  147.     for i in range(sequenceLen):
  148.         state, throwResult = cassino.generateNextState()
  149.         throwSequence.append(throwResult)
  150.         stateSequence.append(state)
  151.     throwSequence =[6,1, 2, 6,6,6]
  152.     # 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]
  153.     # 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]
  154.       #viterbi(cassino, throwSequence, first)
  155.     print("z aposterioti :  ",aposterioti(cassino, throwSequence))
  156.  
  157.     # countProbability(cassino, throwSequence) #ToDo dodalem
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top