Mathematics of hedging bets 
I bet £20 at odds of 3.0.  I have the chance to hedge this bet by laying an amount £$L$ at odds of 3.2, but will have to pay 5% commission on the winnings.  How should I choose $L$ to maximise my minimum payoff?

Original post below:

Could someone please help me out with the equations behind this calulator? I need to work out the equation for the lay stake which gives an answer of £19.05 in the example. I hope this question makes it a bit clearer. Any help is greatly appreciated.
Here is an example
 A: You can have a look at Wikipedia article about betting exchanges for more details on what backing and laying actually means. Notice that to make profit, laying odds have to be lower than backing odds. (Laying odds say how much you have to pay out.) So in the situation described in your post you would be losing money.
In everything I write below I will use decimal odds.
Laying as usual betting. Laying means that you behave like a bookmaker. I.e., you accept bet from another user of the betting exchange. Other than that, it is basically betting on the opposite outcome but with different odds. Namely if the odds are $o$ and if the amount is $b$, laying means that you accepted somebody's bet with odds $o$ and they wagered amount $b$.


*

*That means that you are risking $bo-b$. (This is the amount you have to pay out if you lose. The other player wagered $b$ so he has to get $bo$. We subtract the amount he wagered himself.) 

*Let us denote commission by $c$. If you win, the other player pays $b$. After subtracting the commission, your profit is $b(1-c)$.

*The total winning (i.e., winning+risking, profit+wagered) is $b(1-c)+b(o-1)=b(o-c)$.

*The ratio between total winning and risked amount is $(o-c)/(1-c)$.


So if you prefer to think about laying this way, it is the same as the usual betting, but you have to modify the odds to
$$O=\frac{o-c}{1-c}.$$
You can find information how to calculate profit for complementary events here: How is potential profit from an arbitrage bet (a.k.a. surebet) calculated? (sports.SE).
This calculator. Let us check what you actually calculate in the linked calculator.
With one bookmaker, you have wagered $b'$ for odds $o'$, so there you stand to win $o'b'$ or lose $b'$. The situation with the betting exchange is analysed above.
You want to maximize your profit, which means that possible win (including wagered amount) is in both cases the same. That means
\begin{gather*}
W=b(o-c)=o'b'
\end{gather*}
From this you get 
$$b=\frac{o'}{o-c}\cdot b'.$$
This means you are wagering (risking) $B=b'+b(o-1)$.
The total profit will be $W-B$.

In your case $b'=20$, $o'=3.2$, $o=3.2$, so you get
$$b=\frac{3}{3.2-0.05}\cdot20 \doteq 19.05.$$
Google calculator
In the case of either outcome, total winnings are $o'b'=60$. But after subtracting $B=b'+b(o-1)=20+19.048\cdot2.2 \doteq 61.91$ you see that you are actually 
Google calculator loosing around $1.91$.
To summarize once again in numbers what was described in symbols above:


*

*One possible outcome is that you win $3\cdot 20=60$ in your original bet. Your profit here is $60-20=40$. (You originally wagered $40$.) But in the other one you have to payout $2.2\cdot19.05=41.91$. So you lost $1.91$.

*The other possibility is that you lose $20$. In the betting exchange you won $19.05$. But after applying the commission, it is only $19.05\cdot0.95\doteq18.10$. So you are still losing $1.90$.


In fact, the amount you are losing (or winning) should be the same in both cases, the difference above is due to rounding errors.
A: The situation is that you have bet 20 at odds of 3.0, and have the opportunity to lay an amount $L$ at odds of 3.2, subject to 5% commission on any win.  There are two outcomes then, one in which the lay bet loses so that your payoff is $-2.2L  + 40$ (since you win the original bet and receive 40, but must pay out 2.2 times the amount laid), and the other in which the lay bet wins so that your payoff is $-20 + 0.95L$ (since you've lost the 20 staked on the original bet but receive the amount laid, less 5% commission).
What the calculator does is to find the value of $L$ which maximises the minimum of the two possible payoffs.  In other words, the value of $L$ making the worst case senario as good as possible.
Here's a plot of the two payofs: the red line is the payoff as a function of $L$ if the lay bet loses, and the blue line is the payoff if the lay bet wins.

The minimum of the two payoffs, as you can see, is largest where the lines meet: that is, for the value of $L$ such that $-20+0.95L = 2.2L+40$. Solving that you get $L=60/3.15$ which is the 19.05 the calculator gives you.
