Latest Entries »

In tis section, we will focus on a case in the Markov chain Monte Carlo (MCMC) subject. MCMC contains algorithms for sampling from a probability distribution. The sampling happens by a Markov chain, i.e. it is a random process that changes from one state to another. In this article, we will use the Metropolis-Hastings algorithm to generate a random walk with a proposal density. We will then either accept or reject the proposed moves according to a certain condition.

We will perform some experiments to study the behavior of the Metropolis-Hastings on a test suite of target distributions. The test suite consists in this case of a Gaussian distribution in one dimension. The distributions are characterized by ther mean and variance. Therefore the target distribution is the mixture

p\left ( x \right ) = w_{1}\frac{1}{\sigma _{1}\sqrt{2\Pi }}e^{-\frac{1}{2}\frac{\left (x-\mu _{1}\right )^{2}}{\sigma _{2}^{1}}} + w_{2}\frac{1}{\sigma _{2}\sqrt{2\Pi }}e^{-\frac{1}{2}\frac{\left (x-\mu _{2}\right )^{2}}{\sigma _{2}^{2}}}

The weights 0 \leqslant  w_{1}, w_{2} \leq  1 and w_{1} + w_{2} = 1 measure the relative contribution of both Gaussian distributions. Without loss of generality, we can always assume that the first mean \mu _{1}= 0

As for a target distribution, we will us p(x) \propto 0.3e^{-2x^{2}}+ 0.7e^{-0.2(x-10)^{2}}

For additional notes, I refer to the paper An Introduction to MCMC for Machine Learning by C. Andrieu, N. de Freitas, A. Doucet and M. Jordan.

The proposal distributions are the Gaussian
q\left ( x^{*},x \right )=\frac{1}{2\Pi \sqrt{|\mathrm{C}|}}e^{-\frac{1}{2}\left ( x^{*}-x \right )^{\top}\mathrm{C}^{-1}\left ( x^{*}-x \right )}
\mathrm{C}=\begin{pmatrix}  \sigma  & 0\\    0& \sigma   \end{pmatrix},
and the uniform distribution over the interval [x-r,x+r]
q(x^{*},x)=\left\{\begin{matrix}  \frac{1}{2r} & x \in [x-r,x+r] \\    0& otherwise  \end{matrix}\right.

where x \ is the current state and x^{*}\ is the candidate new state.

Now, we can begin with the experiments. We will collect 5,000 samples after the chain has converged to the invariant distribution, i.e. after the burn-in period. We will then construct a histogram based on the collected samples and compared with the target distribution. For these experiments, we will use Java.

Windows Runtime (WinRT) is the principal provider of system services for applications in Windows 8 that will be designed to incorporate the new “Metro” style. It is Microsoft’s new programming model that supports development in C++, C#, VB.NET as well as JavaScript. WinRT components are designed with an eye to interoperability between multiple languages and APIs, including native (C++), managed (C#) and scripting (JavaScript).

WinRT is not a replacement for any part of Windows we have come to know. Desktop applications written in earlier versions of Windows, will run properly in the desktop mode on Windows 8.

The following picture shows an overview of the traditional desktop applications and the new metro style applications. We also see the APIs that supports the programming languages in the new platform.

Through the above APIs, the developer can communicate with other APIs on the basis of numerous objects. There are more than 800 objects available for approaching APIs. Every object implements several interfaces. These interfaces are located in the registry.

These are the API’s of Windows Runtime:

Every WinRT object supports interfaces, and two of these interfaces are very important: IUknown and IInspectable. The first one is a sort of a parent interface where every other interface inherit from it, directly or indirectly. IIspectable is an interface to inspect an object and to know what the members are. If you want to create an object in WinRT, for example, to communicate with the webcam, you call a certain class. This class looks then in the Activation Store, which is located in the registry, to implement the necessary interfaces. Once the interfaces are implemented, this information is stored in the Windows metadata.

Below there is an example of the FileInformation class which has 3 interfaces. As a developer you will not see these interfaces, but they will be implemented as soon as you use the FileInformation class. What exactly happens here is that the FileInformation class will obtain the interfaces from the Activation Store. The object is then activated from the Registry, and stored in the Windows metadata.

So once we have created an object, we can communicate and interact with it. On the left of the image below, we have our object, and at the right of it there are  our applications written in C + +, C # and JavaScript. Now the question is: when will our applications communicate with the object to use its services?

If it’s a C + + application, the compiler goes at compile time to the metadata and takes all the information it needs out of it, and puts it in the executable at runtime so you no longer need to look at the metadata. So it then can communicate directly with the object. In C # and VB, it is a combination of both at compile time and at runtime exploring the metadata and taking the information necessary to communicate with the object. The same happens with JavaScript, only the metadata is generated dynamically at runtime, which is actually intended for dynamic programming languages such as JavaScript.


This was a short introduction about Microsoft’s new platform Windows Runtime and a description of what happens when you create an object in it’s environment.

The images used in this article come from Microsoft’s Build event.

Delegates in C#

A delegate is a type that references a method. Once a delegate is assigned a method, it behaves exactly like that method. The delegate method can be used like any other method, with parameters and a return value.

Any method that matches the delegate’s signature, which consists of the return type and parameters, can be assigned to the delegate. This makes it possible to programmatically change method calls, and also plug new code into existing classes. As long as you know the delegate’s signature, you can assign your own delegated method.

The first step is defining the delegate:

public delegate int Calculate (int value1, int value2);

Second, we create a method which will be assigned to the delegate object:

public int add(int value1, int value2){
 return value1 + value2;

Now, we create the delegate object and assigning the method to it:

MyClass mc = new MyClass();

Calculate add = new Calculate(mc.add);

Finally, we call the method via the delegate object:

Console.WriteLine("Adding two values: " + add(10, 6));

Get every new post delivered to your Inbox.