Created
November 17, 2011 16:59
-
-
Save rrguntaka/1373735 to your computer and use it in GitHub Desktop.
Hidden Markov Model - 2 States
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The model here is a two state HMM (States are A and B). | |
# This is a simple network having 4 possible actions, | |
# two for each state (going to the other or staying at the same) | |
# pa and pb are probabilities of having next state as A | |
# from state A and B respectively | |
class Hmm | |
def initialize(pa,pb) | |
@pa = pa; @pb = pb | |
end | |
def prob(n) | |
if n == 0 | |
1 | |
else | |
prev = prob(n-1) # Memoization can be used for optimization | |
prev * @pa + (1-prev) * @pb | |
end | |
end | |
end | |
hmm = Hmm.new(0.6,0.2) | |
puts hmm.prob(3) | |
#(1..20).each{|i| puts hmm.prob(i)} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment