Category Archives: General

Quantitative Finance and Computer Science: quick comparison between R and MATLAB

Hi everybody,

It’s a few weeks without a post and I apologize for that; I am at the moment looking for a job in Geneva, and it’s a bit time consuming as you would imagine.

Anyway, I kept working a bit on different projects and I had the idea to create this post. What encouraged me was several e-mails I received from some former classmates and some questions I recently had to ask. I noticed that, most of us, “quants”, “analysts” or whatever you call your job, are usually wondering what technology to use for in specific situations.

Most of the time, we end up using programming languages we know best. For example, students used to MATLAB will use it instead of R, programmers used to .Net (C#, Visual Basic) will use it instead of the JVM (Java). If you have next to you a whole desk of hugely experimented people, then you might have good hints to guide you in your technology decisions, but here are a few remarks I gathered during my work.


First of all, R is free and open-source as opposed to MATLAB which has an initial fee, plus additional cost for each additional package.

R being open-source, you can browse the Comprehensive R Archive Network (CRAN) to fit the packages you like and start using them. However, you will sometimes find that the documentation is not very clear and hence you might have to ask several questions on the R mailing lists which are not very user-friendly themselves.

Using MATLAB however, you will get packages about different subject (such as Fixed Income, Derivatives, Data Handling and so on) which are quite complete and have a pretty good documentation. I’d say that in term of support, it’s money well spent.

If you wish to integrate this technology within one of your software, I’ve heard (but not tried) that integration is pretty easy. This might allow you to spare some time you’d have spent on handling data marshaling or library implementation in the native language.

As far as R is concerned you will find some very useful packages such as XTS for time series handling. You will also be able to find handy financial packages to perform computations on bond pricing. However, you might have some problems finding the package that suits your need. There is a trade-off between the number of packages available and their quality. However, once you’ve found what you need, you’ll be pretty happy. In terms of integration, I have to tackle the problem myself, and I’d say that Dirk Eddelbuettel’s blog and libraries will help you a lot. If you wish to use R withing Excel, you can use RExcel from Statcon which is pretty easy to work. The following book might help you handle:

R Through Excel: A Spreadsheet Interface for Statistics, Data Analysis, and Graphics (Use R)

For those of you who wish to dig deep into R foundations, the following book is excellent:

Software for Data Analysis: Programming with R (Statistics and Computing)

In terms of performance, I’d say that MATLAB seems to be quicker to find optimization results. Again, this might be due to the fact that I did not find the right R package, but who cares? After my different researches, and especially for the game-theoretic approach on ice hockey penatlies, I had to use MATLAB to get the program to converge before loosing patience.

That’s all from now, but I’ll soon be back to compare the JVM and the .Net framework for finance computations and MATLAB or R integrations.

Until then,

Have fun!

Martingale Strategy simulation on Roulette

Good afternoon everybody,

I know that most people who have a slight background in probability theory try to apply it to casinos games. I even reckon that most teachers now choose this approach to interest their students to the field.

However, I have the chance to know people working in casinos, and I know too well that “the casino always wins” to be foolish enough to try and test a strategy for real.

It is interesting to distinguish the different kind of games you can find in the casinos. Mainly, I’d say that you have all kinds of electronic games, the blackjack and the roulette.

Ed Thorp famously showed in his book “Beat the dealer” the “counting strategy” which allows the player to have an advantage over the dealer in the long term. We now know that the casinos have very little sympathy for card counters, and I will let you try your luck if you fancy your chances and don’t care too much about your health.

Electronic games have been beaten as well by computer scientists who identified the pseudo-random sequences of the machines and hence were able to make significant profits, allegedly. Indeed, this is just a rumor, but I would be interested to have a concrete article about that if you know a real story.

Several tries have been made to beat the roulette. Ed Thorp, again, tried to have some portable computer trying to guess the expected landing zone of the ball, but didn’t do very well. The thing with roulette, is that it is one of the fairest game you will find in the casino. Indeed, the only thing that give the edge to the house is the presence of the “0” on the board (some even have a “00”). If it wasn’t for that, the house wouldn’t be able to make any money on the long-term, and the game would be fair.

Let’s consider a simple example, the expectation of the returns for a bet of 1 on red without the zero is:

$$E[X]=0.5 \cdot 1 + 0.5 \cdot -1=0$$

Yet with the zero (which pays 1/2 to any color bet) the expectation is:

$$E[X]=\frac{18}{37} (1-1) + \frac{1}{37} (-0.5) = \frac{1}{74} = -0.0135$$

This argument should be enough to discourage anybody from going further with this kind of color betting strategy without having an edge on where the ball will land.

However, you will often hear some people pretending to be able to make profit by playing the so-called “Martingale strategy”. The idea is to bet the amount that was lost so far, plus the minimum bet to make sure that, at the first win, you will end up with a profit of exactly the minimum bet.

In essence, this idea is not too bad. However there are 2 majors problems. The first one is that the casinos limit the maximum bets and the second one is that the player doesn’t have infinite wealth. Hence, with a long enough sequence of losses, the player would not have sufficient funds to play the next bet.

I think people tend to underestimate how quickly they would be out of money to apply such a strategy. Therefor, I decided to implement a little software which allows you to do a simulation with a defined initial wealth.

The strategy I follow is simple: I apply the martingale strategy as long as I lose, and I keep all the extra earning in my pocket. Hence, I stop playing when I am facing a run of losses exceeding my initial wealth. I then leave the casino with the sum of the excess earnings I won.

You can download the software here.

You will be able to do a manual simulation which will give you the exact evolution of the strategy as well as the history of the roulette.

You will also have the possibility to run several instances of the strategy in the background and to get a summary of your performance.

Assuming you use the default initial wealth of 100, and assuming the minimum bet is 5, you will see that the results converge with sufficient simulations.

It turns out that, with such parameters, you will end up leaving the casino having lost on average 25, that is, you lost your 100 but you earned 75 along the way.

It is also interesting to note that, if you increase the initial wealth, you will not perform better. This is due to the fact that you actually lower the damage limitation.

Note also that, in that strategy, losses sequences increase you bids exponentially whereas winning streaks keep a bid of 5.

Try it yourself, and let me know what you think of the app.

See you next time!

Here we go!

Hi everybody, welcome to my blog.

I am trying this experience in order to share thoughts, studies and research I’ve made recently.

Since most of the articles I will be publishing on this blog will be in English, I decided to adopt this language for this website.

Basically, the topics I will discuss in here will be Finance (news, research, books), Game Theory (research …) and Sport (comments, betting, …).

Please feel free to comment any post if you want to express your opinion or contribute to the subject.

I’ll post again soon to add content.

See ya,