Skip to content

Instantly share code, notes, and snippets.

@rrguntaka
Created November 17, 2011 16:59
Show Gist options
  • Save rrguntaka/1373735 to your computer and use it in GitHub Desktop.
Save rrguntaka/1373735 to your computer and use it in GitHub Desktop.
Hidden Markov Model - 2 States
# 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