What is MATLAB?

MATLAB is a high-level programming language used most commonly in technical computing, such as for numerical computation or algorithm implementation. Developed originally by mathematician Cleve Moler in the 1970s, MATLAB was officially released in 1984 and has since grown into one of the most popular languages for scientists and engineers. It is also frequently used in the finance industry, a popular destination for young engineers looking for a job.

Why is MATLAB useful?

Most importantly, MATLAB is based on LAPACK, a linear algebra software library originally written for the scientific programming language FORTRAN. As a consequence, MATLAB offers powerful out-of-the-box functionality for operations on systems of linear equations (i.e. matrices) that would be way more difficult to implement in a language like C or Java.

I would even say that programming in MATLAB will generally take less time than in most traditional languages, which is why it is often used to prototype new ideas. Good MATLAB code is relatively concise. You won't see as many loops or if statements than in other languages, for instance, and variable declarations can be neglected in the majority of cases (memory pre-allocation is important, however). This not only reduces programming time but makes the code easier to read, too.

Furthermore, you do not have to compile MATLAB code before you can run it. Much like in Java and Python, MATLAB programmes can be run immediately and you can even test code snippets directly in the IDE's command window. That is possible because the compilation and interpretation tasks are done in the background when you run your software. This can be a big time saver for large projects where each recompilation would take several minutes or even hours to complete, but it comes with the obvious disadvantage that interpreted code is usually not as fast as compiled code.

What are MATLAB's disadvantages?

First of all, it is often said that software written MATLAB is slow. That statement is simply not universally true. With its new just-in-time (JIT) compiler, which pre-compiles certain parts of your code before actually running it, MATLAB performance has improved a lot over recent years, resulting in run times that are perfectly acceptable for almost all applications. However, it is true that for certain high-performance tasks where every microsecond (or even picosecond!) counts, as in high-frequency trading, it is recommended to run compiled software. Still, most firms prefer to test their ideas in MATLAB (or R or Python) due to the aforementioned time savings during development before porting the software to C.

Second, MATLAB was designed specifically for numeric tasks. Unlike C, Java or Python, it is no general-purpose programming language. Therefore, MATLAB should only be used when actually doing maths or when working with data. The interfacing with other applications or the operating system, for example, should be left to another language that is better equipped for that particular task.

Finally, compared to the open-source languages R and Python, an obvious disadvantage of MATLAB is that it is relatively expensive. Lower pricing for academic institutions and individual programmers is available, but when you need to purchase many of MATLAB's toolboxes the total can still be significant.

MATLAB in Finance

Taking these positives and negatives into account, it may become clear why MATLAB is widely used in the finance industry today. Financial markets deliver a constant stream of data that can be analysed in MATLAB more quickly than in many other languages. Learning MATLAB is definitely easier than learning C. (It will still take a lot of hands-on programming experience to discover the language's intricacies that ultimately result in elegant and efficient code, of course.) The available finance and trading toolboxes allow for an easy integration of MATLAB into a bank's or hedge fund's workflow without the need of extensive interface development.

Future posts will explain the MATLAB programming language in more detail and also highlight how to apply MATLAB to the world of finance.

Programming for investment managers

Being able to comprehend and ideally write code has become an important skill in almost every industry over the past 30 years. This is especially true since the breakthrough success of personal computing and eventually the internet in the mid-1990s. More recently, cloud computing and Big Data have boosted companies' reliance on programmers and data analysts to a whole new level.

In the finance profession, for example, programming in a scripting language has become a common task for quantitative researchers, risk managers, asset managers and, yes, even traders. For data analysts, financial markets are a dream-come-true because market participants' trading activities produce a constant stream of information, most of which is instantly available in numeric and machine-readable form. The amount of data produced is so vast that analysing the data manually is practically impossible, hence requiring analysts to be able to code so computers can do the work for them.

Geeks (or "quants", as they were warily called by old-school traders in the 90s) have consequently become a hot commodity in the banking and hedge fund world, despite their involvement in recent market meltdowns, such as the subprime mortgage crisis and the Flash Crash of May 2010. These setbacks notwithstanding, it is undeniable that diligent statistical analysis can add significant value to investment management in the long run, as proven by the real money track record of hedge funds and commodity trading advisors (CTAs) such as AQR, Winton Capital, AHL, the lesser known Millburn Ridgefield or the legendary Renaissance Technologies, although the latter is arguably impossible for outsiders to look into and hence judge objectively based on actual performance data.

Considering the importance of statistics and data analysis for today's asset management profession, I decided to add a "programming" category to this blog. I'm going to write tutorials and code examples for certain programming tasks that you might encounter in the investment profession as well as educational posts about language fundamentals, focussing on MATLAB, Python and R, all of which are commonly used by quants throughout the hedge fund world.

Speed of cellfun in MATLAB

When I started programming in Matlab, I quickly realized that in order to reduce the running time of your code you had to make use of Matlab's handy vectorization features whenever possible. Suddenly I became highly suspicious of loops, which I had airily been using in VBA and other languages, and as a result tried to avoid them as much as I could. However, as the data sets I analysed grew in size and I had to use cell arrays, such as when displaying date strings along with numerical values in a GUI table, it dawned on me that using built-in functions instead of loops may not always result in faster code.

Matlab comes with the built-in cellfun function. It basically applies the same operation on each cell in a cell array. To check whether the individual cells in the cell array are empty, for instance, you can have cellfun run the isempty function on each cell instead of writing a loop to do it. However, there are two ways of using cellfun and one of them is really slow while the other is actually quite fast. You can use the @(x) syntax, which turns out to be slow, and a string syntax, which is faster than loops but only works with few simple functions. This fact is not adequately pointed out in Matlab's documentation, in my opinion.

Continue reading "Speed of cellfun in MATLAB"