Introduction To Hidden Markov Models

A Hidden Markov Model is a statistical model that can be used to determine the underlying processes that affect a particular observed outcome. A HMM can be presented as the simplest dynamic Bayesian network. The mathematics behind the HMM were developed by L. E. Baum and coworkers. It is closely related to an earlier work on the optimal nonlinear filtering problem by Ruslan L. Stratonovich, who was the first to describe the forward-backward procedure.

In simpler Markov models (like a Markov chain), the state is directly visible to the observer, and therefore the state transition probabilities are the only parameters. In a hidden Markov model, the state is not directly visible, but the output, dependent on the state, is visible. Each state has a probability distribution over the possible output tokens. Therefore, the sequence of tokens generated by an HMM gives some information about the sequence of states. The adjective ‘hidden’ refers to the state sequence through which the model passes, not to the parameters of the model; the model is still referred to as a ‘hidden’ Markov model even if these parameters are known exactly.

Consider two friends, Alice and Bob, who live far apart from each other and who talk together daily over the telephone about what they did that day. Bob is only interested in three activities: walking in the park, shopping, and cleaning his apartment. The choice of what to do is determined exclusively by the weather on a given day. Alice has no definite information about the weather where Bob lives, but she knows general trends. Based on what Bob tells her he did each day, Alice tries to guess what the weather must have been like.

states = ('Rainy', 'Sunny')
observations = ('walk', 'shop', 'clean')
start_probability = {'Rainy': 0.6, 'Sunny': 0.4}
transition_probability = {
                           'Rainy': {'Rainy': 0.7, 'Sunny': 0.3}
                           'Sunny': {'Rainy': 0.4, 'Sunny': 0.6}
emission_probability = {
                         'Rainy': {'walk': 0.1, 'shop': 0.4, 'clean': 0.5}
                         'Sunny': {'walk': 0.6, 'shop': 0.3, 'clean': 0.1}

Implementation in Python:



Activate Virtual Environment In PyCharm

I create python projects so infrequently that whenever I need to create a new project or add a package to my Python base project I completely forget how to do it; so here goes…

In the terminal window of Pycharm type: source activate [environment-name]

Then you can pip install the package info your environment

The end.

From MATLAB To Python


For many years MATLAB has been my primary tool for prototyping algorithms, because of its rich set of optimization functions and the AI tool box it has proven to be a valuable tool to have. However, It is not cheap and if you do not have a company to pay for license or attend a university that provides you with a license then you will have to find an alternative.

MATLAB is not a programming language rather its a tool that has as part of its framework a programming language called M language, this language has a lot of quirks and takes some getting use to, the other issue I found with MATLAB is that the functions while well documented do not seem to follow a standard in terms of parameters; on the whole while MATLAB is a good tool for prototyping and is used a lot in engineering and medical fields which are my core domain; However,I am forced to look for a cheaper/free alternative that will give me as much if not more tools than MATLAB now provides.

Python to the rescue:

Python is powerful… and fast;
plays well with others;
runs everywhere;
is friendly & easy to learn;
is Open

All these wonderful things make Python a big contender for my MATLAB replacement.

The first thing we want to do is install Python.
Next install my favurite Python IDE PyCharm
Create a new Python Project using PyCharm
Python project

How do we add packages to our project?
Python is nothing without its packages and two of my favourites are numpy and scipy. To add these packages simply download the Anaconda distribution and configure it to be your default python implementation.


And here is my first piece of python code as taken from the python website 🙂


All I need now is a good Python book and 2-4 months to delve into the language. Stay tuned for more posts on my Python journey. Happy coding!!

How To Unit Test A LINQPad Code Snippet

Writing code is a programmers life; sometimes it becomes necessary to write pieces of code that you can conveniently run and evaluate  without spinning up a full fledged IDE. For those tasks there is a tool called LINQPad. LINQPad allows you to write/run snippets in C#, SQL and a few other languages. In oder to run the example in this tutorial you will need to download LINQPad and NUnit Lite.

After installing LINQPad and NUnit Lite open an instance of LINQPad and change the language to C# Program.


Next hit the F4 key on you keyboard to bring up the additional references dialog and browse to the location where you installed NUnit Lite. You will need to add NUnit Lite as a reference.


Copy preceding code into the LINQPad edit window then hit the run button, the results of the test will be displayed in the console window, enjoy 🙂

Running Microsoft SQL Server From A VS2012 Command Prompt

Having a connection to Microsoft SQL Server in the command prompt provides you with an easily disposable window that you can use to query a database without resorting to the SQL Server Management Studio. Personally, I like to have it tucked into a little corner of my screen where It can be easily accessed; for tasks such as checking if i have a connection to the server,  listing tables in a database and running simple queries. Lets see how we can get a connection to SQL Server up and running in a VS2012 DOS Box.


  1. Open a visual studio command prompt dos box.
  2. Type CD/ at the command prompt.
  3. At the command line enter sqlcmd -S [SERVERNAME|IP] -U [USERNAME] -P [PASSWORD]

*NB: The parameters in [] should be replaced with the actual authentication details of the server that you want to connect to. Each command should be followed by the GO directive in order for them to be executed.

Here is an example of a connection to a server running on my machine using sqlcmd -S .

That’s how its done, see you next time.

Creating Keyboard Shortcut to TFS Source Control Explorer In Visual Studio

One of the most hated actions that a programmer has to do is having to click on anything in the IDE. One way to avoid this needless waste of movement is to create keyboard shortcuts. These shortcuts allow the programmer to keep her hands on the keyboard where it belongs.

Keyboard shortcuts are created by mapping a Visual Studio IDE Command to a key sequence, this is achieved with the following steps.

  1. Open Visual Studio
  2. On the Tools menu select options
  3. In the Options dialog expand Environment > KeyboardImage
  4. In the Show commands containing edit box type view.tfs and from the resulting list select View.TfsTeamExplorer.
  5. In the Press shortcut keys edit box enter Ctrl + F8 this will assign that key combination the the command.
  6. Hit the Ok button, now you can easily access the Team Explorer from a keyboard shortcut.