Category Archives: Computer Science

LINQPad, an essential tool for .Net programmers

Good evening everyone,

Since I started this blog I wrote a few posts about computer science and programming in particular, but I recently realized that I hardly ever discuss which tools I recommend.

The reason why I thought it was a waste of time is because most of the time, blogger will talk about a given tool because the editor contacted them and gave them a free license against a review. With proper disclosure of the arrangement and an objective point of view of the product, I don’t see any problem with that, but I though there was already enough material available online to discuss well-known tools.

However, I think some of them remain unknown and in some instances quite unfairly so. The tool I want to talk quickly about in this post is a perfect example: it’s call LINQPad.

First, I want to say that I did not have any contact with the editor, and that I bought my license (which enables more options, I’ll come back to this, but the main features are free).  Hence, this “review” is totally objective.

LINQPad is a lightweight software which can be run as a single .exe (whithout any installation required except the .net framework). It has mainly two purposes:

  • It is a C# and F# Scratchpad
  • It helps browse and mine data sources easily

C# and F# Scratchpad

Basically, LINQPad allows you to write a snippet of code in C# or F# and to display some output without having to create and compile a Visual Studio project. This is really incredibly useful.

In a previous post, I discussed the advantages of functional programming. If you wanted to try my example, you would have to open Visual Studio, create a C# console project, write the code in the Main method, compile and see the result in the console. Using LINQPad as a scratchpad, I can simply copy and paste the code, use the Dump() built-in extension method for any object to display it as output and hit F5. I made a little screenshot of the screen’s important parts:

Basic LINQPad example
Basic LINQPad example

This example is fairly basic, but the cool thing is that you can access almost anything in the .Net framework and, if you need to, even reference some of your own DLLs or a even a Nuget package.

Data browsing and mining

Another power feature of LINQPad is its ability to allow you to “import” data source and interact with them easily. As a matter of fact, it comes with several built-in or third party providers which will help you through a simple wizard to configure the access to a given data source which you will then be able to query using LINQ. These data sources can be various: SQL, MySQL, RavenDB, Web Services and so on. Once the wizard is complete, you can access just as you would browse any type of collection which supports LINQ.

Again, you can write some scripts to retrieve some records, process them and display what you need. This is extremely useful if you have a database with a lot of data and you want to try different ways of displaying the information for example. It’s also very useful if you built some software but you didn’t have the time to write the administration part, you will still be able to extract the data and use it before you get the chance to create beautiful grid and everything -or before you actually give up because LINQPad might be enough if you don’t need to be over user-friendly.

That’s it, have a look at it, and buy it if you want to have some nice features like the intellisense and helpers of the sort. It really helps me a lot and avoids me creating a lot of useless visual studio projects.

Cheers,

Jeremie

C# Parallel Processing for Stock Paths simulation

Good evening everyone!

2013 hasn’t started very well for me as I’ve been sick for the past week and completely unable to work on anything even on a bit of CFA. Thankfully, I’m now starting to get better and I can again put a few words – or pieces of code – together.

I decided to write this quick post because my fiancee’s sister Elsa forwarded me a few questions she had about the material of an exam she had this morning in Embedded Systems. As you might guess, some of the material was dedicated to handling concurrency issues which made me realize that I never discussed parallel programming in C#.

Besides, I just bought a brand new laptop as my previous one was really looking like it was going to die any minute and, without a computer to play around with, I would really be like a professional hockey player without a stick  (I hope you like the metaphor). As a matter of fact, this new computer (an Asus G55V) is quite powerful: it has an Intel i7 2.4 Ghz processor with 12 GB of RAM. With this kind of setup, I should be able to run 7 computations at a time (using each of the 7 cores of the Intel i7). Cool huh?

Therefore, I decided to create a little, very simple example of parallel processing in C# applied to quantitative finance. The classic case of parallel processing in this field is usually referring to so-called Monte-Carlo simulations, where you simulate a very large amount of different paths for the price of a stock price according to a given model.

I will split this post in two distinct parts: the model and the simulation. For those of you who are not interested about the financial aspects of this post, feel free to just jump to Part II.

Part I : the model

Definition

I chose the model to be the Geometric Brownian Motion, which is used for the well-known Black-Scholes famework. The model states the dynamics of a price process $S_t$ as follows:

$$ dS_t = \mu S_t dt + \sigma S_t dW_t $$

Note that the return are intrinsically modeled like this:

$$\frac{dS_t}{S_t} = \mu dt + \sigma dW_t \sim \mathcal{N}(\mu ~ dt, \sigma^2 dt)$$

To simulate this price process using a programming language, I need to discretize the equation:

$$ \Delta S_t = S_{t + \Delta t} – S_t = \mu S_t \Delta t + \sigma S_t W_{\Delta t} $$

For simplicity’s sake, I’ll assume my model to be looking at daily prices and I will simulate prices every day so $\Delta t = 1$:

$$ \Delta S_t = S_{t + 1} – S_t = \mu S_t + \sigma S_t Z_t \quad \text{with} \quad Z_t \sim \mathcal{N}(0,1) $$

The implementation

The implementation is very simple: I created a very simple StockPath class which is supposed to simulate a the path of the prices of an arbitrary stock. I gave it the following private methods:

/// <summary>
/// Computes the change in price (delta S) between two points in the paths.
/// </summary>
/// <param name="currentStockPrice">The price of the stack at the first (known) point.</param>
/// <param name="randomNoise">The random noise used to estimate the change.</param>
/// <returns>The absolute change in price between two points according to the model.</returns>
/// <remarks>The underlying model assumes a geometric brownian motion.</remarks>
private double _computeStockPriceChange(double currentStockPrice, double randomNoise)
{
    return currentStockPrice * _mean * _timeInterval + _standardDeviation * currentStockPrice * randomNoise;
}

/// <summary>
/// Computes the next stock price given a <paramref name="currentStockPrice"/>.
/// </summary>
/// <param name="currentStockPrice">The price of the stack at the first (known) point.</param>
/// <param name="randomNoise">The random noise used to estimate the change.</param>
/// <returns>The next stock price according to the model. This value will never be less than 0.0.</returns>
/// <remarks>
/// The model makes sure that a price cannot actually go below 0.0.
/// The underlying model assumes a geometric brownian motion.
private double _computeNextStockPrice(double currentStockPrice, double randomNoise)
{
    return Math.Max(currentStockPrice + _computeStockPriceChange(currentStockPrice, randomNoise), 0.0);
}

Note that I also made sure that the stock price $S_t$ would never go below 0.0 (this is not the case in the mathematical model I stated above).

The computation of the path is performed using Math.Net library to generate the normal variables, and is done in the constructor:

/// <summary>
/// Initializes a stock path.
/// </summary>
/// <param name="nbrSteps">The number of steps in the path.</param>
/// <param name="mean">The mean of the returns.</param>
/// <param name="std">The standard deviation of the returns.</param>
/// <param name="timeInterval">The time between two evaluation points.</param>
/// <param name="initialPoint">The starting point of a path.</param>
public StockPath(int nbrSteps, double mean, double std, double timeInterval, double initialPoint)
{
	//Assigns internal setup variables
	_timeInterval = timeInterval;
	_mean = mean;
	_standardDeviation = std;

	//Using Math.Net library to compute the required random noise.
	Normal normal = Normal.WithMeanStdDev(0, 1);
	IEnumerable<double> returns = normal.Samples().Take(nbrSteps);

	//Explicit implementation of aggregation mechanism.
	_lastPrice = returns.Aggregate(initialPoint, _computeNextStockPrice);
}

Note that for simplicity and to diminish the computation time, I do not save each step of the price computation: I save only the last price.

Part II: simulation

For those of you who weren’t interested in the model specifics, this is where the main topic of this posts starts. We previously defined how we compute each path. The main problem with path computation is that you cannot parallelize it because you need to know $S_t$ to compute $S_{t+1}$. However, you can compute different paths in parallel, as they are completely independent from each other. To demonstrate the advantage of using parallel computations, I will want to simulate a very large amount of paths (100’000 of them) each of which will have 10 years of daily data (2520 points).

Why is it useful to compute the paths in parallel?

Let’s take a simple real-life example: cooking. Assume you invited a group of friends for dinner. You decided to have smoked salmon and toasts for appetizers and a pizza for the main. Now, what you would most probably do is prepare the pizza, put it in the oven, and while it is cooking, you would make the toasts, and while the toasts are in the toaster, you would put the salmon in a plate and so on. Once everything is done, you put everything in the table. In classic programming, you just cannot do that.

Haven’t we already been doing exactly that forever?

No, but everything was made to make you believe so. To keep it simple, a processor (CPU) can only do a single thing at a time: that is, if you were a processor, you would be stuck in front of the oven while the pizza is cooking. Some of you might argue that old computers (who had only a single CPU) managed to do several things at a time: you could still move the mouse or open a text editor while running an installation. That’s not really true! Actually, what the computer was doing was to alternate between different tasks. To come back to the kitchen example, if you were a computer, you could stop the oven, put the first glass on the table, switch back the oven, wait a little, stop the oven again, put the second glass on the table, restart the oven, wait again a little, and so on… This could look like you’re doing several things at the same time, but in fact, you’re splitting your time between several tasks. In short, a CPU performs computations sequentially. The problem with this “fake” parallelization is that although it looks like everything happens at the same time, you actually waste a lot of time because you can’t do many things at the same time.

What about today?

Modern computers are equipped with multiple cores. For example, my computer has an Intel i7 processor: it has 7 different cores that can process computations on their own. This means that you can compute 7 different things in parallel. They work as a team; for the kitchen example the team in the kitchen. This does not mean that I can compute anything in parallel, I need the tasks to be independent (at least to some extent). Now, this doesn’t mean that I can really compute things 7 times quicker. The reason for that is because the computer needs to perform some operations to make synchronize the results together once they are done. In the kitchen example, the team need to make sure that they do not do things recklessly; they need the plates to be ready at a given time; they need to organize themselves. Overall, there is an organisational overhead, but it is offset by the ability of being able to perform several things at the same time.

Is it difficult to perform parallel computations in C#?

There are several ways to perform parallel computing in C#. Today, I would like to discuss the simplest one: PLINQ. In my functional programming post,  I already showed how to use the classical LINQ framework to handle collections functionally. What is incredible about PLINQ is that it automatically handles the parallel aspect of collection operations after the function AsParallel() is called. So, first, I define a creator function which creates a new stock path for a given index (the index is actually useless in our case, but enables us to treat the computations functionally):

Func<int,StockPath> creator = x => new StockPath(nbrPoints, mean, stdDev);

What I want to do is to make sure that this creator is called in parallel several times. Here is now the function I implemented to run a simulation of several paths of a given amounts of points:

/// <summary>
/// Runs a simulation and prints results on the console.
/// </summary>
/// <param name="nbrPoints">The number of points for each path to be generated.</param>
/// <param name="nbrPaths">The number of paths to be generated.</param>
/// <param name="simulationName">The name of the simulation</param>
/// <param name="creator">The function used to create the <seealso cref="StockPath"/> from a given index.</param>
/// <param name="mode">The <see cref="Program.ExecutionMode"/></param>
public static void RunSimulation(int nbrPoints, int nbrPaths, string simulationName, Func<int,StockPath> creator, ExecutionMode mode)
{
	Stopwatch stopWatch = new Stopwatch();
	StockPath[] paths = new StockPath[nbrPaths];
	IEnumerable<int> indices = Enumerable.Range(0, nbrPaths - 1);
	Console.WriteLine("Starting " + simulationName + " simulation.");
	stopWatch.Start();

	switch (mode)
	{
		case ExecutionMode.CLASSIC:
			paths = indices.Select(creator).ToArray();
			break;
		case ExecutionMode.PARALLEL:
			paths = indices.AsParallel().Select(creator).ToArray();
			break;
		default:
			throw new ArgumentException("Unknown execution mode", "mode");
	}

	stopWatch.Stop();
	Console.WriteLine("End of " + simulationName + " simulation.");
	var lastPrices = paths.Select(x => x.LastPrice);
	Console.WriteLine("Min price: " + lastPrices.Min().ToString("N2"));
	Console.WriteLine("Max price: " + lastPrices.Max().ToString("N2"));
	Console.WriteLine("Computation time: " + stopWatch.Elapsed.TotalSeconds.ToString("N2") + " sec");
}

The two most important lines are the number #20 and #23. The rest of the function basically computes the executions time and prints the results on the console. Running the simulation in both modes (sequential and parallel) gives me the following results:

Execution mode Computation time
Sequential 32.33 sec
Parallel 06.29 sec

As you can see, running the paths in parallel improved greatly the computation time of the simulation; it’s about 5 times quicker. As expected, we did not perform 7 times better (the number of available cores) because we had to do the extra background work of synchronizing the results at the end of the computation.

Summary

Given the extreme simplicity (only adding the AsParallel() call) of the code to get into parallel mode, using PLINQ seems to be a very good and very accessible solution for simple cases such as this one.

Of course the performance will vary with the processor being used but in general, I would recommend using this PLINQ solution. There are a few important things to think about when using parallelism: think about whether it is really useful:

  • Are the computations really independent?
  • Are there enough computations to compensate for the extra work required to synchronize the results?

The whole project is available on my GitHub account here: https://github.com/SRKX/ParallelStockPaths.

I hope you enjoyed the demo,

See you next time,

Jeremie

Free top quality online classes

Good afternoon everyone!

I decided today to take the opportunity to write this post about a new resource I discovered a few month ago and which I have been using ever since then, Coursera.

The idea behind Coursera is to provide free online video classes to be accessed by students all over the world at any time, for free. The first question that storms to mind is What kind of class does it provide?. I’d say that the scope would be anything that you can learn in an Undergraduate program and beyond (Graduate, Phd, …), and that’s the beauty of it. The second question is Who’s teaching the class? Well fasten you seat belts, because here is the answer:

United States

  • Stanford University
  • Princeton University
  • University of Michigan
  • University of Pennsylvania
  • University of Virginia
  • University of Washington
  • University of Illinois at Urbana-Champaign
  • University of California, San Francisco
  • California Institute of Technology
  • Duke University
  • Georgia Institute of Technology
  • John Hopkins University
  • Rice University

Canada

  • University of Toronto

Scotland:

  • University of Edinburgh

Switzerland:

  • Ecole Polytechnique Federale de Lausanne (EPFL)

Yes, it is astonishing. Don’t even try to think that the universities sent second-zone professors to use the tool, first class academics are teaching you the material through the video as if it was a 1-to-1 session. To name a few, and mostly because they are the one I knew or heard of before discovering the website:

  • Andrew NG (Stanford, Co-founder of Coursera), teaching Machine Learning
  • Dan Boneh (Stanford) teaching Cryptography
  • Martin Odersky (EPFL, whom I have the chance to know) teaching Functional programming

As I mentioned, the scope of the classes is very broad. The first courses that got my attention were focused on computer science and statistics (I’ll come back to the one I took or am planning to take), but I saw medical schools classes, history, mathematics, social engineering, and I believe there is and will be more.

The format of each class is similar and particularly brilliant. The curriculum is split week by week  each of which have particular topic. Your are provided with a series of short videos (called segments) 5 to 25 minutes long from what I’ve seen, which makes it particularly convenient for user who have a day job or students which are already following a heavy program. Each segment will typically start with an introduction where you will see the professor and then switches to displaying slides which are dynamically annotated by the professor while he speaks. Even better, the annotated slides are available from download before the class starts, so you can literally feel as if you were in the classroom although you are … at home.

Another great aspects on Coursera is that it is interactive. During a video, the professor can pause the cast and ask a question to which the viewer can answer either by choosing between several options or by filling in a specific value and then validate. This is not blocking (you can skip the question) but is pretty handy to check whether you understood what was said thus far in the lecture. Most importantly each week is accompanied with an exercise set which is graded (I’ll come back to the grading mechanism) and which also works either on plain value filling or multiple choice picking. Extra assignments for extra points can require either submitting answers to challenging problem or even code (I’ve never done it though).

As I mentioned, the exercises sets are graded and each course ends up with a final exam. If you successfully complete the assignments and the final, you will the “pass” the class and receive a certificate of completion (I think this depends on the class, but all the ones I’ve taken so far provided that option). As certification is purely optional you can enroll to a course and basically just watch the videos, or the specific part you’re interested in.

Finally, I want to share the different courses I enrolled in:

  • Machine Learning
  • Gamification
  • Model Thinking
  • Neural Networks for Machine Learning
  • Probabilistic Graphical Models
  • An introduction to interactive programming using Python
  • Cryptography
  • Basic Behavioral Neurology
  • Computational Investing Part I

To sum up, Coursera is free, interactive, and can provide certification upon completion of assignments and a final exam. Try it out!

See you soon!

Why should we use functional programming: a simple example

Good evening,

I’m just taking the time to add more content on functional programming. When I talk about this paradigm with programmers who haven’t had the time to work with it, they always tell me that the code is difficult to understand and that it would be difficult to maintain. I would like to show a simple example which shows that this statement is wrong.

Because I would agree that if you work in an environment where nobody works in F# it is not very likely that you will switch programming technology, I will use C# for my example. Indeed, C# supports some functional programming features as discussed in this book:

Functional Programming in C#: Classic Programming Techniques for Modern Projects (Wrox Programmer to Programmer)

I will take a simple example to demonstrate why functional programming can actually be much simpler to read and help developers avoid making mistakes. Let’s assume you have a list of integers and that you want to get the sum of the squares of the elements of the list.

In imperative programming (the paradigm used normally in C#) you would proceed as follows:

int[] myList = new int[] {1,2,3};

double res=0;
for (int i=0; i<3; i++)
{
   res+=Math.Pow(myList[i],2);
}

return res;

As you can see, it takes about 6 lines (with clear formatting) because you have to setup a result variable, and the you have to make sure that this variable is implemented by the square of each number. You also need to see that you don’t make any mistake in the definition of the loop as it depends on the length of the list.

Now, let’s see how you can do exactly the same thing using functional programming:

int[] myList = new int[] {1,2,3};

return myList.Select(x=>Math.Pow(x,2)).Sum();

As a matter of fact, you can see that the “algorithm” is much shorter there is no problem considering the length of the list or setting up a return variable. I also believe that the code is easier to read, because you do not have to tell the program everything it has to do, you just explain the logic and the actual implementation happens in the background.

That’s it for today,

See you next time!

Handling interaction between the Business Layer and the UI with the MVVM design pattern

Hi everyone,

I’m taking the time to write this little post because I came across a great design pattern to handle interaction between the Business Layer (BL) and the user interface (UI).

This problem usually arises when creating multi-layers application which can be used by different kinds of users. As this blog is mainly dedicated to finance, I’ll take as an example an application which provides access to the financial time series of different assets.

When you implement this kind of programs, you might want to consume the business objects from, say, a backtesting platform to see how a strategy performs over some interval. However, you might also simply want to create a UI in WPF to see how the different assets performed using your favorite charting library.

The thing is, when you tackle user interaction, your objects need to be “interactive”. That is, you might want to rename you asset, or you might want to be willing to assign an asset to a different asset class, and you want the UI to update automatically.

Let’s take a simple example. You have a list of assets displaying the assets names. When you click on an asset in the list, the details show up in a form somewhere else on the window. If you were to modify the name of the asset, you would like the change to happen also in the list. To do so, you need to notify the list (the UI) that the (business) object has changed, otherwise, nothing will happen.

To notify changes of a property, you can implement the INotifiedPropertyChanged interface from the .Net framework.

However, there is a problem with implementing such interface within your Business Layer. Indeed, this means that your objects will handle the notifications, even when no UI is looking for it (in the backtesting platform for example). This could result in useless computations and reduce the efficiency of your computations.

This is why you would like to use the MVVM design pattern (for Model-View-ViewModel). The idea is to create enhanced versions of the objects which implement all the features required for user interaction. This object would be implemented in a different project which include the Business Layer. In our example, we would create a class called, for example, AssetViewModel which has a private property containing the business class Asset and providing access to all necessary properties with notifications upon changes.

This pattern can then be extended to represent the data available behind a complex window, as described in this article by Josh Smith. You then just set the View Model of the window as its DataContext, which allows you to bind the UI elements to it, and simply remove all the code behind.

Finally, another great advantage of MVVM is that you can test the UI workflow using simple Unit Test on your ViewModels.

For more information, you can also read the following book:

Building Enterprise Applications with Windows Presentation Foundation and the Model View ViewModel Pattern

See you next time!