Friday, 27 July 2012

A Guide to C# Interfaces for the Dotnet Architect

When drawing up an interface, one may wonder: "What are the valid constructs I can include in a C# interface?" An ARCHITECT of .Net software must be clear on this point, to be able to design interfaces that are a) SYNTACTICALLY CORRECT b) FIT FOR PURPOSE.

The answer is not as obvious as it may seem.

What CAN be put into a C# Interface? (MPE IND - Mumbai-Pune Expressway, India)
Methods, certainly.  Properties, yes. Events and indexers too - but not operators - why, because operators are declared static, and C# has no mechanism to require a type to implement static methods. The concept of interfaces in C# is thus much richer than the same construct in Java (which supports declaration of methods and constants).

What CANNOT be put into a C# Interface?
The reason why C# uses static operator overloading is based on cost/benefit analysis (read this piece from the C# compiler team. The question naturally arises since in  C++ operator overloading is done at instance level rather than static level (doesn't use the static keyword)). The upshot (or rather downshot) of this is that operator overloading cannot be prescribed at the interface level.

An Interface Specifies a Contract
A non-abstract implementation of an interface MUST implement ALL its members.

Accessibility Modifiers and Interfaces (Restaurant Example)
By definition, all interface members are public, since an interface is the public visage of a class or struct, it is what the class or struct wants to show to the outside world. (Like a restaurant that allows customers to orderFood, payBill, addTip and so forth). It may also have properties such as Address, StyleOfCuisine and so on. It may not expose the method CookFood unless clientele are able to cook their own food, for example using a grill built into the table. That may be part of the implementation but not part of the interface. An indexer may not be appropriate, unless be are creating an interface for RestuarantChain and want to access particular outlets of the chain. Events could be things like: CallForTakeawayOrder.

Boost Math Basics

Understanding Boost Math is about a) understanding the underlying mathematical concepts, but additionally you have to understand b) what the typenames mean.

Quick revision guide on typenames in C++: They are used to disambiguate type declarations, but have a dual role in that they can be used in place of class in template parameter declarations.

Math typenames from Boost:
1. RealType - denotes a real number, usually represented/approximated by, a double.
2. Policy - transmits behaviour, e.g. what to do when certain error conditions are met. An example in STL of a policy class would be std::allocator which communicates memory management policy to container classes.

You also need to understand error functions, mathematically denoted by erf(z) and erfc(z)=1-erf(z). erfz is e to the minus t squared integrated between 0 to z and "normalized" by two divided by the square root of pi.

The error function (also known as the "Gauss error function") is one of the "special functions" in mathematics. It has a sigmoid (or "S") shape.

You will see also that Boost header files have a .hpp extension rather than .h (which signifies pure C++). .h files are also used in C.

Wednesday, 18 July 2012

Inverting the STANDARD Cumulative Normal Distribution in Excel and C++

For various financial applications, you will need to invert the cumulative standard normal distribution. The correct Excel worksheet function for this is:

NORMSINV

You need to give it a number between 0 and 1. The function NORMSINV is confusing, because the name does not allude to the fact that we are inverting the CUMULATIVE normal distribution. (NORMINV is the cousin of NORMSINV that inverts the Normal Distribution more generally. Consequently, mean and standard deviation parameters must be specified explicitly. NORMSINV can be regarded as a special case of NORMINV that just accepts a probability as parameter).

Here is an algorithm for computing the inverse cumulative normal distribution. Note that the function is a continuous, non-linear (curve rather than a straight line) function and maps the (0,1) open interval on the x-axis into the entirety of the real line (although most visibly between -3 and 3 on the y-axis). It's very important to understand visually what this function is, to avoid confusing it with any other related function.

If you want to do this in Visual C++, then the Boost Math library (written by John Maddock and Paul Bristow) will do the needful.

Boost implements a generalized version of this, using the notion of Quantiles. A quantile can be thought of as the inverse of a Cumulative Distribution Function, returning a value x such that cdf(dist, x) == p. The quantile is also known as the percent point function, or percentile, or the lower critical value of the distribution.

Summary: Quantile = Inverse CDF

To understand the Boost API, we should also understand what is the complement of the cumulative distribution function. This is the probability that a variable take a value greater than x.

Thursday, 12 July 2012

Operator Overloading in C#

A feature from C++. Operands can be custom classes or structs. syntactically just a static method with keyword operator immediately preceding the operator to overload, then the operand params. Exs. +, -, ++, % (modulo), == etc.

Example from MSDN:
public static Complex operator+ (Complex c1, Complex c2)

If you are implementing your own collection classes, you may want to add support for subscripting into the collection. In this case, you cannot "overload" the subscript operator, but instead must create an INDEXER.

An indexer works as follows.

class MyCoolCollectionWithIndexerSupport<T>
{
   public T this[int i]
  {
    get { return _arr[i]; }
    set { _arr[i] = value; }
  }
}

(this is an interesting use of the this keyword. commonly this is used to refer to the current instance of a class, and as a qualifier for variables within a class. static member functions do not have a this pointer).