Skip to content

Instantly share code, notes, and snippets.

@hristo-vrigazov
Created November 6, 2016 22:02
Show Gist options
  • Save hristo-vrigazov/5298269488cba1d64e76688b9d2a9dc8 to your computer and use it in GitHub Desktop.
Save hristo-vrigazov/5298269488cba1d64e76688b9d2a9dc8 to your computer and use it in GitHub Desktop.
Hidden Markov Model in Problog with revealing comments
% Hidden Markov Model example
% The weather can be in state "sun" or "rain" initially
% with 0.5 probabilities for each state
0.5::start(weather, sun);
0.5::start(weather, rain).
% In a given moment in time, if today is sunny - "sun",
% tommorrow will be 0.6 sun or 0.4 rain
0.6::trans(weather, Moment_in_time, sun, sun);
0.4::trans(weather, Moment_in_time, sun, rain).
% In a given moment in time, if today is rainy - "rain",
% tommorrow will be 0.2 sun or 0.8 rain
0.2::trans(weather, Moment_in_time, rain, sun);
0.8::trans(weather, Moment_in_time, rain, rain).
% If the weather is sunny, there is 0.9 probability
% that I will not take an umbrella and 0.1 probability
% that I will take an umbrella
0.9::emit(weather, Moment_in_time, sun, no);
0.1::emit(weather, Moment_in_time, sun, yes).
% If the weather is rainy, there is 0.1 probability
% that I will not take an umbrella and 0.1 probability
% that I will take an umbrella
0.1::emit(weather, Moment_in_time, rain, no);
0.9::emit(weather, Moment_in_time, rain, yes).
% The probability that a given model is in a given state
% in a given moment in time is the probability that in
% the previous moment the model was in a previous state
% and makes a transition from the previous to the current
% state in the previous moment in time
state(Model, Moment_in_time, Current_state) :-
Moment_in_time > 0,
Previous_moment_in_time is Moment_in_time-1,
state(Model, Previous_moment_in_time, Previous_state),
trans(Model, Previous_moment_in_time, Previous_state, Current_state).
% A model in the zeroth moment in time is in state
% accoring to the initial probabilities
state(Model, 0, State) :- start(Model, State).
% The probability that we will observe
% a symbol in a given moment in time from
% a given mode is the same as the probability
% that the model in that moment in time is in
% some state and that in this state it emits
% the given symbol
observe(Model, Moment_in_time, Symbol) :-
state(Model, Moment_in_time, Current_state),
emit(Model, Moment_in_time, Current_state, Symbol).
% The probability that a given model in a state will
% emit a given sequence in a given moment in time
% is the same as the probability that the given model
% in the current moment time will make a transition from
% the current to the next state and in the next moment in time
% will emit the first character in the sequence and then repeat
% for the rest of the list
ob_seq(Model, Current_state, [Symbol|Tail], Current_moment_in_time) :-
trans(Model, Current_moment_in_time, Current_state, Next_state),
Next_moment_in_time is Current_moment_in_time+1,
emit(Model, Next_moment_in_time, Next_state, Symbol),
ob_seq(Model, Next_state, Tail, Next_moment_in_time).
% stop if you get an empty list
ob_seq(_, _, [], _).
% The probability that we will observe a sequence
% from a given model is the same as to start the model
% from a given state, emit the first symbol in that state
% and then observe the rest of the
observe_sequence(Model, [First|List]) :-
start(Model, State),
emit(Model, 0, State, First),
ob_seq(Model, State, List, 0).
% The probability that a model will produce given list
% of states is the same as to start from a state, and then
% to emit a rest of the list starting from the state
% in a moment 0 in time
state_sequence(Model, [State|List]) :-
start(Model, State),
st_seq(Model, State, List, 0).
% The probability that a model will produce given list
% of states in a moment in time is the same as to make
% a transition from the current state to a next state,
% and then from the next state to produce the rest of
% the states
st_seq(Model, Current_state, [Next_state|Tail], Moment_in_time) :-
trans(Model, Moment_in_time, Current_state, Next_state),
Next_moment_in_time is Moment_in_time+1,
st_seq(Model, Next_state, Tail, Next_moment_in_time).
% stop if you get an empty list
st_seq(_, _, [], _).
% What is the probability that I will take my umbrella
% for the next 7 days?
query(observe_sequence(weather, [yes, yes, yes, yes, yes, yes, yes])).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment