Quantitative Analysis, Risk Management, Modelling, Algo Trading, and Big Data Analysis

# Quants, NumPy, and LOTTO

Lesson 9>>

Since I’m working over the Volume I of Python for Quants ebook and I am going through NumPy abilities, they leave me speechless despite the rain. Somehow. Every time. There is so much flexibility in expressing your thoughts, ideas, and freedom of coding of logical and mathematical concepts as a quant. With its grace of syntax, with its purity of simplicity.

When I was 16 I used to play LOTTO: the game where for your 3 favourite combinations of 6 numbers out of 49 you pay $\$1$. It was obvious that hitting “six” was an equivalent of the probability of: $$\frac{1}{C^6_{49}} = \left( \frac{49!}{6!(49-6)!} \right)^{-1} = \frac{1}{13983816} \ \ .$$ I had my favourite 6 numbers. The magic was that I believed that I could win the entire prize. Somehow, the game was worth trying. You never know when your lucky day comes! Monte-Carlo Simulation for LOTTO It is out of any doubts that our mind can create certain circumstances in real world (that we do not immediately understand) that allow us to bend a reality to our knees. How? Well, if you attend any mind control seminar you learn on the power of your mind and how to trick mathematical, logical reasoning. That applies to LOTTO game as well. There is nearly 14M combinations and we aim to win that game. A low probability. A high reward if you are lucky. I used to play with my favourite combination: 7, 9, 11, 21, 35, 45 for nearly 3 months. I was visualising a situation when one day exactly those numbers could appear in the LOTTO drawing machine next Wednesday or Saturday. It took me 3 months to hit 5 out of 6 numbers! The financial reward was nice, trust me! But because I was of small faith, I did not hit the bull between its eyes. Now it’s your turn! Python language is superb. It offers you an opportunity to simulate your luck just in few minutes. Say, you are like me, you randomly pick up 6 out of 49 numbers and you play, two times a week, and… wait, for your early retirement. With NumPy library in Python, the estimation of the waiting time is straightforward. Analyse the following code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 # Quants, NumPy, and LOTTO # (c) 2014 QuantAtRisk.com, by Pawel Lachowicz import numpy as np def lotto_numbers(): ok = False while not ok: x = np.random.choice(49,6,replace=False) x.sort() tmp = np.where(x == 0) (m, )= tmp[0].shape if(m == 0): ok = True return x fav = lotto_numbers() # choose your favourite 6 numbers print(fav) match = False; i = 0 while not match: tmp = lotto_numbers() cmp = (tmp == fav) i += 1 if cmp.all(): match = True print("Iterations: %g\nProbability = %.2e" % (i,1./i) ) print(tmp) It’s a nice combination of most useful NumPy functions in action: .random.choice(), .where(), .shape(), .sort(), and .all(). In line #9 we create a 6-element vector of random integers drawn from a set$[0,49]$and in line #10 we force them all to be sorted. Because$0$is not the part of the game, we need to check for it presence and draw again if detected. Line #11 returns a temporary array storing indexes where$x$has a zero-element. Since we use .random.choice function with a parameter replace=False, we can be sure that there is only one zero (if drawn). Therefore, the result of line #12 should be$m$equal 0 or 1. In the main program (lines #17-29) we repeat the loop until we find the match between a (random) selection of our favourite 6 numbers and a new LOTTO draw. In line #23 an array cmp holds 6 boolean elements as the result of comparison. Therefore, making the arrays sorted is essential part of the code. Finally, in line #25 a NumPy function of .all() returns True or False if all elements are True (or not), respectively. Nice and easy. An exemplary outcome you can get is: [ 1 2 4 11 13 18] Iterations: 37763 Probability = 2.65e-05 [ 1 2 4 11 13 18] To provide you with a feeling how long you need to wait to hit your favourite “six” let’s calculate: >>> 37763/(2*52) 363 what returns a number of years. Trust me, if you really really really want to win playing only with your lucky numbers, you can. It is a matter of faith, luck, and… time. As a homework try to modify the code to run it$10^4\$ times and plot the histogram of probabilities. Is it Normal or Uniform?

Now I guess I should say Good luck!

• Digital Cosmology

Isn’t there a closed form solution for the mean time? Could use the Poisson distribution I think.

• Robert DeFilippi

Oh, and for the extra homework I got a logarithmic distribution. Correct or way off?

• http://www.quantatrisk.com Pawel

Our logic may suggest a uniform distribution since we use .random.choice() function. But… Try to repeat the code 10E+5 times. Do you still derive the same log distribution? If so, the question is why?

• Robert DeFilippi

How long did it take your system to run this? I’m currently at 10min, without a single iteration.

• http://www.quantatrisk.com Pawel

Haha :) Haven’t I mentioned it might take some… time? ;) It’s a simple but tricky piece of a code that breaks our logic. If you run it for 10 min without a single result, it is fine. At some point you hit your “six”.

• John Hall

The thing that annoys me most is when I forget the double parentheses.