# Advice on learning C++ and its integration with Python/R/MATLAB for quantitative finance

For some background information, I am a PhD student in economics. Although I did not study in finance previously, I took a course on stochastic calculus and a course on asset pricing in incomplete markets at the PhD level. We're mostly talking about option pricing models and their applications (think, GARCH models, stochastic volatility models, jump diffusion models, etc.). I have been working on option pricing problems lately and I am starting to realize how computationally intensive these things can be. For example, calibrating an option pricing model by defining a gaussian likelihood in the implied volatility space requires me to price a several thousands of option contracts which can be very long to do, even if I enjoy a quasi-closed form pricing formula.

So, I was wondering how much effort it would take to learn how to use C++ to do some of the heavy lifting when I need to increase speed. I already know how to use MATLAB, R and I am slowly learning how to use Python. The professor with whom I am working also suggested I learn object oriented programming in Python as it would often simplify my life. I started doing this and it's going rather well. So, how much effort are we talking about? In my case, we're looking at basically doing some matrix algebra, numerical intergration, etc. or, in some cases, perhaps running relatively simple Monte Carlo simulations and being able to move results back and forth between a higher level language (like Python, R or MATLBA) and C++.

The second question is how useful is it? Would I really get enough use out of this to justify the time I might have to spend learning it? I mean, I can do a lot of things using existing software packages and I have spent a great deal of time being able to hack my way around those problems, but I will be needing custom routines from time to time. In short, I'd like to have a better idea of the trade offs involved here from someone who actually use C++ to solve computationally intensive problems.

The last question is: does anyone have relevant resources on the topic? If possible, I would like tutorials that includes examples dealing with how to run simple regressions on data, how to run a Monte Carlo simulation or how to price European options because those are close enough to what I do for me to relate to what I already know about programming in other languages.

• Most of what I do involves econometrics. I did some work in macroeconomic forecasting using machine learning. It was uber time consumming -- we're talking about half a million forecasting errors in the main backtest across models, variables and horizons. All of my current projects involve econometrics and most of my work will involve a heavy empirical element or simulations... So, econometrics, mostly. Commented Apr 21, 2020 at 20:07
• C++ is the fastest when it comes to Monte Carlo simulations, and you can easily compile and source your functions in R through the excellent Rcpp package. The other way is to use Python and JIT compilation through Numba, which should be just a bit slower than C++ but doesn't require to switch between two different languages. OOP is nice here, but if you use QuantLib-Python you already have very structured objects: you can relax and enjoy some basic functional programming design. Commented Apr 22, 2020 at 8:06
• Using the Numba package (numba.pydata.org) to compile mathematical functions in C would be the way to go. You get the speed of C within Python. Also, FWIW, Python isn't just for scripting. If you really want to build all the objects from scratch, OOP in Python is no different in principle than in C++. Commented Apr 22, 2020 at 11:44
• I though Numpy was already written in C, don't understand the point of Numba?
– user24980
Commented May 19, 2020 at 11:01
• @jbs Eg.: To price options, I might have to use Monte Carlo simulations. It starts to matter when you have tens of thousands of contracts to price inside a maximum likelihood estimation. You can vectorize some of the work to speed it up, but you cannot vectorize everything in Python -- you'll have to work with loops somewhere for this. Alternatively, you can call compiled C++ code to do it and, chances are, that code will run WAY faster. That's why people use Numba in Python or Mex files in MATLAB. Commented May 19, 2020 at 14:01

I come from a background of Matlab and Python primarily having only briefly used C++ in a course way back from uni and I've been picking up C++ and object oriented thinking from the book "C++ design patterns and derivatives pricing" without far too much difficulty and am having quite a lot of fun with it. The book applies the standard object oriented programming approach to derivatives pricing so it might be exactly what you're looking for. The first chapter is building a very simple Monte Carlo option pricer and then he continues on showing how to do it "the right way" with OOP in a way that allows you to continuously add to the program in an organic way in order for it to do more complex things.

I think that touches upon your first and third question, as for your second one I think the answer that the author of the book commonly gives is "If you want a job as a Quant then you need to learn C++ so suck it up", if it's essential for your research I can't tell you. From my understanding the primary benefit of building logic in C++ or OOP in general is that the code will be easily extendable and reusable, which is of big benefit if you're working on projects in teams and will be needing to hand over a code base to other people or have someone else try to maintain your code but might not be one of your greater concerns.

The massive advantage of object-oriented design is conceptual. People have trouble seeing the ‘objects’. The rest of the advantages are just corollaries of a clear mind.

A pricing model comprises market data (curves, surfaces, and cubes, by market, IR, EQ, FX etc.), a dynamics or marginal distribution (Hull-White, SABR, etc.), a product (aka payoff, Asian option, Bermudan note etc.) and a method for computing the expectation (Monte Carlo, PDE etc.). And this is just the begining. Each of these entities have their own structure: one can’t get to stochastic local volatility model without carefully building on top of Brownian motion (geometric or arithmetic), local volatility, then finally SLV.

There is no reason why one would confuse or mix these entities or would implement them any other way but the OOP way.

To echo a previous answer, you can read C++ Design Patterns and Derivatives Pricing by the late Mark Joshi. Plus there are other books listed in this answer.

I'm going to make some different recommendations though.

There are lots of programming languages used in quantitative finance. Certainly C++ is popular, especially when every ounce of performance is required. But it is hardly the only choice, and I would personally recommend against it if performance isn't your main concern. (R and Python are both reasonable and widely used.)

Also, I would recommend against OOP, even in C++. Classes are great, but inheritance and dynamic polymorphism (virtual functions) have fallen out of favor compared to generic programming (template).

• I am interested in C++ specifically for speed. I already know MATLAB, R and I have been using Python recently, but some tasks like maximum likelihood estimation in GARCH option pricing models involve numerically evaluating tens of thousands of integrals several times over. As much could be said of some bootstrap methods, especially in the context of simulations. As for OOP, I used it in MATLAB and found it to be considerably more convenient than functional programming for what I had to do. For the rest, I'm not a computer scientist; I don't know exactly what you mean. Commented May 19, 2020 at 13:45
• For the reccord, I started to follow a tutorial series on youtube to learn C++ to get the gist of it. My goal is mostly to be able to use it in conjunction with Python, R or MATLAB as required (professors aren't comfortable in all languages, so it depends). Commented May 19, 2020 at 13:53