# Linear programming – a gambler example [Python]

Introduction

You want to bet money on a given event and be sure to win, so you got the beautiful idea of betting on all possibilities at the same time? It doesn’t really work in the real life, but let’s look into this problem from the mathematical point of view and afterwards implement it in Python. I’m going to use scipy.optimize.linprog module in order to “minimize a linear objective function subject to linear equality and inequality constraints” [1].

Simple Maths Background…

A sample space describes all possible outcomes for a given experiment. “Oucomes” could be everything. We consider now a sample space made of three mutually exclusive outcomes for 2 given numbers N and M.

Event 1: N > M

Event 2: N < M

Event 3: N = M

Normally the higher is the risk, the more you can win (or lose). If someone (which I will call bookmaker) would offer you 100 times your betting money for the Event 3 (because it’s not lileky to happen) it’s expected from him to offer you much less for the remaining outcomes in such a way to make you lose money if you would bet on all events at the same time and to take advantage of the gamblers behaviour stastistic.

Now suppose you have many bookmakers offering you different rewards for your bet on the same sample space. What does it happen if you can choose to bet on all outcomes but from different rewards sources / bookmakers? I’m going to use the following notation:

x[0], x[1], x[2] : Money to bet on mutually exclusive events
w[0], w[1], w[2] : Winning coefficients (e.g. if I bet on the first outcome and it happens, I win w[0]*x[0])
l[0], l[1], l[2] : Loss coefficients (e.g. if I bet on the first outcome and it does not happens, I lose l[0]*x[0])

Logically, since all outcomes are mutually exclusive, if you win by betting on Event 1 you lose by betting on Event 2 and 3. We want to bet on all outcomes. The resulting money must be greater than the money you bet and exceed it by a margin you wish. For example, if the Event 1 occurs it applies:

w[0]*x[0] + l[1]*x[1] + l[2]*x[2] > x[0] + x[1] + x[2] + margin[0]

or equivalently, for all the events:

(w[0]-1)*x[0] + (l[1]-1)*x[1] + (l[2]-1)*x[2] > margin[0]

(l[0]-1)*x[0] + (w[1]-1)*x[1] + (l[2]-1)*x[2] > margin[1]

(l[0]-1)*x[0] + (l[1]-1)*x[1] + (w[2]-1)*x[2] > margin[2]

The same inequalities can be expressed in matrix form: A*x < b

whereby we define the vectors…

…A as

…x as

…b as

Complete Python code example

Code explanation

In the first part the user must define some sample vectors, namely w, l, b. Furthermore we defined upper or lower bounds for our unknowns. In our case, a lower bound must be zero (since we can’t bet negative money!) and an upper bound can be defined as we wish, e.g. 10000 Euros.

From the first input arrays w and l, it’s possible to generate the system matrix A. An alternative code for A would be the following.

We are interested in minimizing our left-hand side of the inequalities. According to the way linprog accept parameters, we only need to define the array (here called c) which multiplies the unknowns vector x inside the objective function. It corresponds to the elementwise sum of all rows of matrix A.