Denys Rtveliashvili
ai

Greek Letters in Quant Code

Executive Summary

Nowadays, it is possible to use non-ASCII letters in code. Most of the code does not benefit from it. However, Greek letters in quant code may be an important exception.

Non-ASCII Characters in Code

Historically, most of the programming languages apart from a notable exception had all the code limited to ASCII characters. In fact, in the olden times, languages did not even make a distinction between uppercase and lowercase letters.

There is merit to ASCII-only code. For a start, it appears that all keyboards contain the required characters, ensuring availability for everyone. It is also trivial to type. Making a single letter requires just one or two key presses. That is totally not the case with some other writing systems.

However, times change and so do programming languages. Support for non-ASCII code is being added. In particular, PEP 3131 had added support for non-ASCII identifiers to Python. This did not change much, however. I am still to see Python code which would make use of this flexibility.

ASCII vs ASCII+Greek

I do believe that the vast majority of the code must not use non-ASCII. There appears to be no value in that. However, it seems that quant code is a significant exception.

In order to demonstrate the merit of using Greek symbols, let’s consider a code which computes the following formula:

$$ \frac{\alpha_1 e^{-\lambda_1 \Delta x} + \alpha_2 e^{-\lambda_2 \Delta x}}{\sqrt{\sigma \Delta x}} $$

Don’t ask me what it means, it is just a deliberately random mathematical expression.

Let’s also assume that we have a vector of values \(\Delta x\), and we want to compute the expression for each item of the vector, so we are going to be using everyone’s favourite library called numpy.

"""
Computes .....

:param delta_x:  .....
:param alpha_1:  .....
:param lambda_1: .....
:param alpha_2:  .....
:param lambda_1: .....
:param sigma:    .....
:return: ....
"""
def expression(delta_x, alpha_1, lambda_1, alpha_2, lambda_2, sigma):
    numerator = alpha_1*np.exp(-lambda_1*delta_x) + alpha_2*np.exp(-lambda_2*delta_x)
    denominator = np.sqrt(sigma*delta_x)
    return numerator/denominator

Is it readable? Yes. Is it a joy to read? Not really.

Now consider this alternative:

"""
Computes .....

:param Δ_x: .....
:param α_1: .....
:param λ_1: .....
:param α_2: .....
:param λ_1: .....
:param σ:   .....
:return: ....
"""
def expression(Δ_x, α_1, λ_1, α_2, λ_2, σ):
    numerator = α_1*np.exp(-λ_1*Δ_x) + α_2*np.exp(-λ_2*Δ_x)
    denominator = np.sqrt(σ*Δ_x)
    return numerator/denominator

And while we are at it, we can also consider a more condensed version:

"""
Computes .....

:param Δx: .....
:param α1: .....
:param λ1: .....
:param α2: .....
:param λ1: .....
:param σ:  .....
:return: ....
"""
def expression(Δx, α1, λ1, α2, λ2, σ):
    return (α1*np.exp(-λ1*Δx) + α2*np.exp(-λ2*Δx)) / np.sqrt(σ*Δx)

Am I the only one who thinks that the versions with Greek letters are cleaner and easier to read? Can you say with a straight face that the ASCII-only version is easier to read and comprehend?

Bonus: Greek Letters in Plots and Tables

Here are a couple of useful facts which are good to know when doing quant work.

First: markdown sections in Jupyter Notebook support TeX notation. While you would still have to type “\alpha”, the end result would show “α”, which is useful.

Second: when displaying tables (via pandas DataFrame) or showing plots, you can also use TeX notation. Just make sure that you put your formulae between dollar signs, like this: $\lambda_i$.

Challenges

The obvious challenge is how to type Greek letters in the code. Unless you live in Greece, you probably do not have a Greek keyboard layout. Also, switching between layouts just to type a single letter is no fun.

However, there are solutions.

For example, there is a system called Compose Key. It has been around for four decades and is available out of the box in any Linux (one more reason to use Linux on desktop). It allows you to type whatever you want by pressing reasonable key sequences, and it is configurable. Typing Greek letters in it (without switching to a different keyboard layout) is trivial. If you are interested, I have published a configuration for Compose on Linux which adds Greek letters and other symbols here.

It appears that there is software for Windows and Mac which does the same thing and in a similar way. It is just not a part of the standard distribution.

Are there any other challenges? I am not aware of any real ones. Inertia? Perhaps people are too accustomed to eyeHurtingGodAwful_Identifiers that they are not willing to improve their life any more. I do not know.

Conclusion

It is technically possible and not really difficult to use Greek letters in quant code. The benefits of that are better readability and terseness. This should, in theory, lead to higher productivity and lower number of bugs. You and your team may benefit from breaking the tradition of ASCII-centrism.