Friday, 24 January 2014

Accessing a server running in VirtualBox

I ran Ubuntu in VirtualBox on a Windows 7 host. When I started a Unicorn HTTP server in the guest Ubuntu OS, it reported as listening on address 0.0.0.0. To access this web server in the Windows host browser, you can check the IP address of the virtual machine using ifconfig and use that IP address in the host browser.

Events and delegates in C#

Delegates

A delegate is a reference type, but instead of referring to an object, a delegate refers to a method. In other words, a delegate is a type that represents references to methods with a particular parameter list and return type.  This is called encapsulating the method. When you create the delegate, you specify a method signature and return type; you can encapsulate any matching method with that delegate. It is like a function pointer in C++ but are type safe (the method passed needs to conform to the method signature).
You create a delegate with the delegate keyword, followed by a return type and the signature of the methods that can be delegated to it, as in the following:
public delegate int FindResult(object obj1, object obj2);
class MediaStorage {
  public delegate int PlayMedia();
  public void ReportResult(PlayMedia playerDelegate)
  {
    if (playerDelegate( ) == 0)
    {
        Console.WriteLine("Media played successfully.");
    }
    else
    {
        Console.WriteLine("Media did not play successfully.");
    }
  }
}
public class AudioPlayer
{
    private int audioPlayerStatus;

    public int PlayAudioFile( )
    {
        Console.WriteLine("Simulating playing an audio file here.");
        audioPlayerStatus = 0;
        return audioPlayerStatus;
    }
}

MediaStorage myMediaStorage = new MediaStorage( );
// instantiate the delegates
// PlayAudioFile conforms to the PlayMedia delegate signature
MediaStorage.PlayMedia audioPlayerDelegate = new
                   MediaStorage.PlayMedia(myAudioPlayer.PlayAudioFile);
myMediaStorage.ReportResult(audioPlayerDelegate);
Events and Delegates
class Clock {
// Define the SecondChangedHandler delegate type. This delegate will encapsulate any method that
// returns void and that takes two parameters. 
public delegate void SecondChangeHandler(
    object clock, TimeInfoEventArgs timeInformation);
// Create an instance of the delegate
public SecondChangeHandler SecondChanged;
}
class DisplayClock {
public void Subscribe(Clock theClock)
{
  theClock.SecondChanged +=
    new Clock.SecondChangeHandler(WriteLogEntry);
}
}
The problem with the above code snippet is that other methods can call SecondChanged directly and this is not normally the original class intends..  For example:
Console.WriteLine("Calling the method directly!");
System.DateTime dt = System.DateTime.Now.AddHours(2);
TimeInfoEventArgs timeInformation =
    new TimeInfoEventArgs(  dt.Hour,dt.Minute,dt.Second);
theClock.SecondChanged(theClock, timeInformation);
To prevent external calls to the delegated method directly, one can make the delegate private, but then this will prevent clients from registering with the delegate at all. What's needed is a way to say, "This delegate is designed for event handling: you may subscribe and unsubscribe, but you may not invoke it directly."


To fix your program, change your definition of SecondChanged from:
public SecondChangeHandler SecondChanged;
to the following:
public event SecondChangeHandler SecondChanged;
Adding the event keyword fixes both problems. Classes can no longer attempt to subscribe to the event using the assignment operator (=), as they could previously, nor can they invoke the event directly, as was done in the preceding example. Either of these attempts will now generate a compile error.

There are two ways of looking at SecondChanged now that you've modified it. In one sense, it is simply a delegate instance to which you've restricted access using the keyword event. In another, more important sense,SecondChanged is an event, implemented by a delegate of type SecondChangeHandler. These two statements mean the same thing, but the latter is a more object-oriented way of looking at it, and better reflects the intent of this keyword: to create an event that your object can raise, and to which other objects can respond.
You can also assign this delegate by writing the shortened version:
theClock.SecondChanged += TimeHasChanged;
Reference:
http://msdn.microsoft.com/en-us/library/orm-9780596521066-01-17.aspx
http://msdn.microsoft.com/en-us/library/ms173171.aspx

Tuesday, 14 January 2014

Linux

X Window System (X11) is the windowing system for bitmap display, common on UNIX-like OSes. X provides the basic framework for a GUI environment. It does not mandate the user interface - this is handled by individual programs, such as window managers. A window manager controls the placement and appearance of application windows. Examples of window managers include GNOME 2, KDE and Awesome.

Friday, 10 January 2014

Installing OpenAFS on MacOS X 10.9

The latest OpenAFS for MacOS is only for 10.8. If you try to install this version on 10.9, the installer will say the required version is 10.8 and you can't continue. However you can still install it through the command line and it works.

I followed the instructions on this website. No restart was needed. I wasn't able to configure the default cell through the OpenAFS preference setting. It may be due to the permission issue. So I manually edited ThisCell file in /var/db/openafs/etc with sudo privilege.

Thursday, 9 January 2014

TeXlipse pdf viewer syncing

On Windows, SumatraPDF works well with TeXlipse pdf viewer syncing. On a 64-bit Windows 7, we need 64-bit SumatraPDF for the inverse search to work. The following website gives details on how to configure both TeXlipse and SumatraPDF:
http://joerglenhard.wordpress.com/2011/12/29/texlipse-and-sumatrapdf/

For the pdflatex.exe setting, the following works:
-synctex=1 -interaction=nonstopmode --src-specials %input
-synctex=1 means zipping the synctex file. The synctex file enables synchronization between the source file and the pdf output file.

Monday, 23 December 2013

Receiver operating characteristic (ROC) curve

ROC curve is a plot of true positive rate (true positive / total actual positive) vs. false positive rate (false positive / total actual negative, FP / (TN + FP) ). It shows the performance of a classifier as a trade off between selectivity (specificity, true negative rate TN / (TN + FP)) and sensitivity (recall rate, true positive rate).

false positive rate = 1 - true negative rate


Choosing the operating point

Let 
alpha = cost of false positive 
beta = cost of missing a positive (false negative)
p = proportion of positive cases

The average expected cost of classification at point x, y in the ROC space is 
C = (1-p) alpha x + p beta (1-y)

The error rate can be obtained by setting the misclassification costs equal to each other and unity. So
error rate = (1 - p) x + p (1 - y)

This equation means that points on the ROC space with equal error rate are straight lines. 

EER - euqal error rate is at the point where false positive rate = false negative rate.


References:

Sunday, 15 December 2013

Sydney trip

Money matters

  • Withdrawal from a Westpac ATM (BoA affiliated) has a foreign transaction fee of 3%. There is no additional ATM withdrawal fee.
  • Use credit cards with no foreign transaction fees as often as possible.