Monday, July 02, 2012

Sharing a Burger Between Three

There are several ways to divide a burger evenly between three people.

Probably the best is to cut it radially (like a pizza). It can be tricky working out exactly where to make the cuts, but if you can pull it off, then all the pieces will be roughly identical (topping distribution notwithstanding).

But for the sake of arguing, lets say you want to divide the burger by making two parallel cuts: Where, then, do you make the cuts so that all three people get the same amount of burger?

NB/ This gets quite maths-heavy, so if you're not interested in that sort of thing, feel free to skip right to the end for the solution.



Geometry

For simplicity, we're going to consider the burger as a circle, and make the cuts so that each chunk has the same area. The two cuts are going to be the same distance from, and parallel to, the central axis of the burger, so we only need to consider the position of one of the cuts.

Here's the set-up
We work out the area of the cut-off as the area of the circular segment, minus the area of the triangle.


- Aside: Radians

Radians are basically an alternative way of measuring angles. For maths and physics they're generally more useful than degrees.

They're relatively easy - there are 2pi radians in a full circle, so 2pi radians = 360 degrees

1 radian = 180/pi = 57.3 degrees
1 degree = pi/180 = 0.017 radians, etc.

*    *    *

Back to the circle; with angle x in radians, the area of the circular segment is
The area of a triangle is half base times height..


- Aside: Area of the Triangle

We start by splitting the triangle down the middle, so that we have two identical right angle triangles
The height, l = r*cos(x/2)

The base, b = 2*(r*sin(x/2))

So the area of the triangle is (l*b)/2 = r^2 sin(x/2)cos(x/2)

Finally, use the identity sin(2x) = 2sin(x)cos(x)
to get
*    *    *

So the area of the cut-off is
and it needs to equal a third the area of the circle = 1/3 pi r^2.

So first, we need to find x satisfying
or
Once we have a value for x, we find where to make the cut from


Intermission

The thing about this equation is it doesn't have an exact, analytical solution - to find the solution you have to use numerical methods. Well, I say you have to use numerical methods; these days you can just type the equation into WolframAlpha, and you'll get a solution like *snaps fingers*

Which is nice. I even have the WolframAlpha app on my phone. But when I thought up this question I was on holiday in Sherwood forest, where there was literally no mobile singal.

So that was out of the question. And since I'm not in the habit of carrying a scientific calculator around with me, I was stuck with the basic calculator on my phone. It looks like this:
No trig functions, no square roots, no pi button. It doesn't even do brackets, or have a memory function. Luckily, I am in the habit of carrying around a notepad and pen.

Anyway, there are two ways of working this out with only a basic calculator. The first is 'easier', but only if you know some stuff, and the numbers happen to be nice (in this case, they kind of are). The second is harder, in that it requires more number crunching, but it'll work with any numbers, and can be more precise.

Again, feel free to skip to the solution if you're not interested in the gritty details.



Method One

First of all, here's a graph of the two sides of the equation
hand-drawn with Skitch
We want to find the point at which the two graphs cross. We can see that that happens somewhere between 2pi/3 and pi (120 and 180 degrees). So, lets make a guess that it's exactly halfway between these two values: 5pi/6 (150 degrees).

For the right hand side of the equation: 5pi/6 - 2pi/3 = 0.52

NB/ I'm using pi=3.1416 (rounded to 4 decimal places). If you prefer, you could use the approximation 22/7. The result should be roughly the same.

For the left hand side of the equation, we need to work out sin(5pi/6)

At A-level, we were expected to memorise sin() and cos() of angles 0, 30, 45, 60, 90, and 180 (degrees). We were also expected to know the formulas for sin() and cos() of sums of angles. For sin(), it works like
Why is this important? Well 150 degrees = 180 - 30 (5pi/6 rads = pi - pi/6)

So
And since 0.5 is pretty close to 0.52 - less than 5% error - we can accept the convenience of that answer and say it's close enough.

So our approximate value of x is 5pi/6 = 2.618

[Incidentally, the identity for sin(2x) is just a special case of the above, with a=b=x; i.e. sin(2x) = sin(x+x) = 2sin(x)cos(x)]


Now we just need to work out l/r = cos(x/2) = cos(5pi/12)

For this one, 5pi/12 rads = 75 degrees = 45 + 30, so we can use
So
And we just have to evaluate that. But we don't have a square root button. Now, I just happen to know that sqrt(3) ~ 1.73 and sqrt(2) ~ 1.41.

But I'm just weird like that. Let's say you don't. How do you work it out?


- Aside: Square Roots

There are several ways of working out square roots with just basic operators. For two easy examples:

The first is 'Trial and Improvement' - pick a number, square it, does that give the right answer? If not, pick another number based on whether the last guess was too big or too small.

For example: sqrt(3)
1.5 -> 2.25 -> too small
1.7 -> 2.89 -> too small
1.8 -> 3.24 -> too big
1.75 -> 3.0625 -> too big
1.73 -> 2.9929 -> too small
1.74 -> 3.0276 -> too big
1.735 -> 3.010225 -> too big
1.7325 -> 3.00155625 -> too big
1.732 -> 2.999824 -> too small
etc.

The second method is the "Babylonian Method". It's more systematic, and can converge to the correct answer quicker than guessing. But it can be irritating if your calculator doesn't have a memory function.

It uses the recurrence relation
Basically, you make a guess xn. Divide the number you want to square root (S) by xn. If xn is lower than the actual square root, then S/xn will be greater than it. That means the actual root will be between xn and S/xn, so we make the next guess xn+1 the average of these two values. Repeat until x is sufficiently accurate.

For example: sqrt(2)
x0 = 1.5 -> 2/1.5 = 1.33
x1 = (1.5 + 1.33)/2 = 1.4166.. -> 2/1.4167 = 1.41176..
x2 = (1.4167 + 1.41176..)/2 = 1.41421.. -> 2/1.41421 = 1.41421..
*    *    *

Whatever way you do it, you repeat the process until you get the degree of accuracy you're happy with.

You should get the answer around l/r = 0.259



Method Two

We go back to the equation sin(x) = x - 2pi/3

We still have to find the solution numerically, we still don't have a calculator with a sin() function, and this time the numbers don't work out nicely.

So, the question is, how do we calculate sin(x)?


- Aside: Taylor Expansion

The Taylor Expansion of a function is a way of fitting a polynomial (sums of powers) to a more complicated function. It works like this
Basically, it gives a way of converting a function we can't calculate into an infinite sum of powers of x, which we can calculate.

It's usually expanded around the origin (x0=0), since the equations work out neater. But you can do it around any point, x0=a. This is useful if the value you are trying to calculate is far from x=0. The closer x is to x0=a, the quicker the sum converges.

Even though the expansion is an infinite sum, it's usually sufficient to just take the first few terms, since each additional term makes a smaller and smaller contribution to the sum.

So the trick is working out how many terms you need to include to get some desired level of accuracy.

*    *    *

In this case, I'm going to use the Taylor Expansion of sin(x) around x0=pi, since the approximate value (2.6) is nearer to pi than 0.

Here's what the expansion looks like

So, how many terms do we need to include?

Here's what the graph looks like for different numbers of terms
plotted with WolframAlpha
For a value around 2.6, it can be shown that including the first two terms is correct to ~3 decimal places; the first three terms is correct to ~5 decimal places; the first four terms to ~7 decimal places, etc.

So I would probably go to the third term (for 5dp), but only take the result to 3dp.

NB/ We shouldn't get too hung up on getting an extremely accurate value for x, since we're already getting rounding errors from the factors of pi in the expansion. Also, since we're calculating x to 5dp, we should use pi=3.14159

That means we want to solve
which can't be solved exactly.

So, for finding the correct value (without WolframAlpha), we can use any root-finding method. For what it's worth, I used Trial and Improvement; the other methods are easier with a computer.

But, note that the function is decreasing
So if the guess gives a value greater than zero, you need to increase the value of x (and vice versa).

If you run through all that (I won't go into detail), it gives a value around x=2.605


Alternatively, you could expand around x0=5pi/6 (if you know/can work-out sin and cos of 150 deg without a calculator).

In this case you'd only need up to the term in x^2 (correct to ~4dp). Using this expansion would mean solving a quadratic equation, which is easy. But using this expansion can introduce more rounding errors from the factors of sqrt(3). It's a matter of preference, I guess. The answer should be about the same.


Finally, we need to calculate cos(x/2)

Again, we use the Taylor Expansion to calculate cos(). In this case, we're doing the expansion around x0=0; the expansion is
In this case, you just keep adding terms until the result remains approximately constant to some desired degree of accuracy (3pd).

This gives a value around l/r = 0.265



So What is the Real Answer?

Once I got to somewhere where I could get at WolframAlpha, I checked the real numbers; here are the results:
The approximation of x from Method One (2.618) is an over estimate by ~0.5%, which is relatively acceptable. The approximation from Method Two (2.605) is correct to 3 decimal places, which is definitely acceptable.

And for the value of l/r
From Method One (0.259), the approximation is an under estimate by 2%, and correct to 2 decimal places, so is probably acceptable. The approximation from Method Two (0.265) is, again, correct to 3 decimal places. So that is also acceptable.

So, if the numbers happen to be convenient and you know some trigonometry, you're probably as well using Method One. If not, or if you just want more accuracy, then go for Method Two.



Applying the Results

The results are actually quite nice, in terms of practical application (dividing up a burger). The ratio of the radius (0.265) being close to one quarter, you find the cuts like this
That is, find the central axis, then find the (imaginary) line halfway between the centre and the edge - make the cut halfway between the centre and this imaginary line (maybe cut an extra hair's breadth towards the edge). Repeat on the other side.

Easy.


So, now you know. Obviously, all this applies to dividing any circular thing evenly between three people. You could probably even adapt the methods for sharing between even more people.

And in theory, You could do all this with just pen and paper (no calculator). Though you probably wouldn't want to. I know I wouldn't..


Oatzy.


[Wow, I really managed to stretch that one out.]

Sunday, June 24, 2012

Too Many Lotteries

So recently I'd gotten really into this TV show - it's called 'Life', and it's a 'murder of the week' crime drama starring Damian Lewis. Lewis plays Detective Crews, who was previously in prison for a muder he didn't commit, and he's into zen and such. It's a bit silly. But in an endearing sort of way.

Unfortunately, the only way I could watch it (short of buying the DVDs) is to stay up til 4am. Which isn't really a problem; I don't have anything to get up for. Though it is tricky getting to sleep as the sun's coming up.

Anyway, the point is, by 4am, there are only, like, four adverts on rotation (on FX, at least). These are: upcoming shows on FX, whatever product JML is flogging, that tacky accident helpline ad with Esther Rantzen, and this 'Win Trillions' thing.

Win Trillions is a lottery sindicate thing - from what I understand, it works like this: a bunch of people from around the world get together, each buying tickets for their respective countries' lotteries. If one of the sindicate members wins, they split the winnings with their fellow sindicate members (and presumedly, Win Trillions takes a cut).

Really, though, the details aren't that important. What is important is their slogan - "play more lotteries, get more chances".

Which isn't a false claim. It's just kind of misleading; playinng more lotteries does mean more chances of winning, but only in a similar way as buying more tickets for a single lottery means more chances of winning - your odds of winning are so small to begin with (~1 in 13,983,816) that you'd have to play thousands of lotteries (or buy thousands of tickets) to significantly improve your chances. And there are only so many lotteries in the world.


Strategies

You'll notice I used the word 'similar' back there; the fact is, buying more tickets, and playing more lotteries don't 'improve' odds in the same way.

So the question is, which is the better strategy - buying many tickets for a single lottery, or buying one ticket for each of several lotteries?

First, imagine I have a dice; I'm going to roll the dice, and you have to bet on the outcome. Your choice is this - would you rather place three bets on one dice roll, or one bet on each of three dice rolls?

For the first case, say you place your bets on it being 1, or 2, or 3. Your odds of guessing the correct number (and winning) is 3 in 6 (=0.5).

For the second case, say you bet that each of the rolls will be 6. Then your odds of winning 'something' is odds of winning just one of the dice rolls, plus the odds of winning just two of the dice rolls, plus the odds of winning all three.

Which is trickier to work out. For example, the odds of winning the first roll only, is odds of getting the first roll right (1 in 6), and not getting the second roll right (5 in 6) and not getting the third roll right (5 in 6) - which makes the probability of getting just the first roll right = 1/6 * 5/6 * 5/6 = 25 in 216

And you have to work out the probabilities for all possible winning outcomes (in this case, there are seven winning outcomes, one losing.).

Alternatively, we can use the trick that the odds of winning something is one  minus the odds of winning nothing. The odds of winning nothing is the odds of getting all the bets wrong: 5/6 * 5/6 * 5/6 = 125 in 216 (0.58)

So the odds of winning something is 91 in 216 (=0.42)

So for this dice game, it's better to place your three bets on a single dice roll.

But what about in general?

We say each game has a probability p of winning, and that we're going to place n bets (with n>1).

For the single game bet, the probability of winning is n*p

For the multi-game bet, the probability of winning is

Which is bigger?

Well for n = 1, the games are idential. For n = 2, the single game is 2p, the multi-game is 2p - p^2; the single game has higher odds. For n >= 1/p, the single game gives a probability greater than or equal to 1 - i.e. guaranteed win - where the multi-game always gives a values less than one; again, the single game gives better odds.

So it's looking like the single game is always better. But how do we prove it?

For this, we look at the binomial expansion of (1-p)^n

A Binomial Expansion works like this
where k! is the factorial of k. For example: 2! = 2*1; 3! = 3*2*1; 4! = 4*3*2*1; etc.

In this case, x = 1 and y = -p, so the expansion becomes
Which makes the probability of winning the multi-game
So whether or not the mutli-game has higher odds than the single game depends on the sum of the terms after np.


First, we compare the k-th and (k+1)-th (adjacent) terms in the expansion
Which simplifies to
The righthand side has it's maximum value when n is maximum. We previously defined the maximum value of n as 1/p, so
since p and k are always greater than 0.

Which means
In other words, each term in the expansion is smaller than the ones before it.

This means that, if we pair up terms,
i.e. the sum of each pair will be negative. Therefore, the sum of all the pairs (all the terms in the expansion after np) is negative. So necessarily
Which means the odds of winning the single game is ALWAYS better than the odds of winning the multi-game.


Pay-Outs

You might have noticed that the single game has a single possible payout, where playing the multi-game means you could win double, triple, or even more of the prize. So the odds of winning the multi-game are lower, but you stand to win more.

Does that mean it's worth the extra risk?

For this, we look at the expected winnings for each game. The expected winnings is calculated as the probability of winning multiplied by the prize amount.

So if you were guessing the outcome of a dice roll, and the prize was £1, then the expected winnings from playing that game would be ~17p.

Or think of it like this - if you had to guess the outcome of a dice roll six times, you would expect to be right once out of six. So your expected prize for 6 rolls is £1, or 17p per roll.

For this single game, the payout is easy: w*n*p (where w is the prize for winning one game)

For the multi-game it's more complicate. We can't use the same trick as last time; instead, we have to work out odds of winning one game times prize from one game + odds of winning two games times prize from two game + etc.


The general form is the series sum
Which can be manipulated and simplified to give
Now, if we expand it out again
Then there's a factor of np in each term, so we can take that outside the bracket
And since n is some arbitrary interger, we can substitute m = n - 1 and put it back in summation form
Now, you might notice the summation part is actually a binomial expansion; in this case we have x = (1 - p) and y = p, so
For all values of p and m = n - 1.

Therefore, the expected payout of the multi-game is always w*n*p


You'll notice that this is the exact same expected payout as for the single game. As in, regardless of which strategy you use, your expected payout is the same.

So the question is, would you rather a low risk low prize game, or a high risk (potentially) high prize game?


Complexities

You'll notice for this I assumed all lotteries have the same odds of winning and the same prize amount. This is not necessarily true.

Even so, from this, the best strategy would be to find the lottery with the best single game single bet expected payout (prize * probability of winning), and buy several tickets for that game, rather than spread your money around.

The other thing in lotteries is you can usually win smaller prizes for matching fewer numbers. But I don't imagine that changes which strategy is best.

So now you know. For what it's worth. The odds of winning playing, say, 70 tickets on one lottery is only about two 10,000ths of a percent better than playing one ticket on 70 lotteries.


Incidentally, those derivations up there count as mathematical proofs. So these conclusions I've drawn are irrefutable*. And the results apply to any game where you're placing bets on a single probability random outcome - not just lotteries.


Oatzy.


[*- Assuming I haven't cocked it up...]

Tuesday, May 29, 2012

Sexuality and Population Dynamics

So I was reading this response to a Daily Mail opinion piece about those 'gay cure' bus adverts, and I see this quote (from the Mail article)
Since only about one percent of us are [gay], homosexuality is obviously a departure from the norm... Reversing that proportion would spell the end of the human race, which is clearly undesirable.
There are several things wrong with this statement - some of which the response, linked above, points out.

First of all, that 1% values. The actual number is a tricky figure to pin down. The UK Office of National Statistics puts the figure at 1.5% gay or bisexual; the linked response lists 3 studies which put the value around 7% (for the UK), and various other studies suggest number in the modern West is somewhere between 2-13%.

Secondly, just because something is (statistically) "a departure from the norm", doesn't mean it's bad - for example, ~10% of people are left-handed, ~2% of people have red hair, etc. (Historical beliefs notwithstanding.)


But all that's tangential. What I'm interested in is the second part - would a majority gay population spell the end of humanity?

[NB/ for the remainder of the blog, I shall be using 'gay' colloquially, as shorthand for all of LGBTQUA, etc.]


The Simple Model

It's an interesting population dynamics question, and it's easy to model if we make some assumptions and simplifications:

1) We assume that homosexuals don't reproduce at all (either directly or indirectly). This isn't true - but it seems to be a common belief.

2) We assume that the rest of the population reproduces with some fixed rate (b=0.02059), which doesn't change from year to year. Similarly, we assumes a fixed death rate (d=0.00812), which affects all members of the population equally.

3) We assume that sexuality is binary, definite and static, with a fixed incidence of homosexuality, g = 0.07 - that is, of all children born 7% will be gay.


So the model we're using is preposterously simplified - effectively,

new population = old population + births - deaths
And we can easily program this model and run simulations.

Now, there are two ways to interpret the article's statement in the context of our model -


1) "if 99% of the (initial) population were gay" assuming the 7% incidence

The outcome looks like this
In this case, the population drops at first, but then starts to grow again, exponentially - certainly not the end of the human race. Also notable is that the proportion of the population that is gay stabilises at 7% (as expected).

2) "if 99% of people were born gay"

Now, the outcome looks like this
Exponential decay - so, in this case, humanity would die out. But if 99% of the population isn't reproducing, what do you expect?

Here are the phase portraits of the above two cases, for a slightly different view of what's happening (gay population along x, straight population along y)
Again, for g=0.07 the population tends to infinity; for g=0.99 the population tends to zero. Note that this behaviour is (ultimately) the same for all initial population ratios.


What I Learned in MAS271

We don't actually need numerical simulations to study the behaviour of this (or similar) systems - the model can be solved analytically.

First of all, we generalise the system. We start with a pair of coupled, first-order differential equations describing the behaviour of the two populations
Now, these two populations are mutually exclusive subgroups of the total population - that is, total population z = x + y. So with a little mathematical trickery, we can combine these two into a single equation, describing the behaviour of the total population
This is a second-order, linear differential equation, so has the general solution of the form
Where lambda values are given by
And A-coefficients are constants, which are determined by the system's initial conditions. If the initial total population is z0, and the inital size of the x population is x0, then we get
In other words, we now have an (analytical) equation which completely describes how the population will behave for any given set of variable.

Incidentally, the equations for x and y have the same form as z. The only difference is the A-coefficients.


Boom or Bust

What we want to do is be able to determine the long term behaviour of the population based on the given set of variables - that is, will the population grow, or die out?

For the coupled system above, there is only one stationary point, at (x,y) = (0,0) - i.e. when the total population is zero. What this means is, if the population is zero, it will stay zero. If it has any other value, then the population size will change over time.

In terms of the stability of the system, if (0,0) is stable, the population will tend towards zero - i.e. will die out. If (0,0) is unstable, the population will tend to infinity - i.e. grow indefinitely.

As it turns out, the lambda values above are the eigenvalues of the coupled system, meaning their values determine the stability of the system:

i) if both lambdas are negative, the system is stable and the population will die out;

ii) if one or both of the lambdas are positive, the system is unstable and the population will grow to infinity.

Now, if we take the limit of z(t) for t tends to infinity, we can simplify the population equation
What this tells us is, if lambda1 is less than one the population will die out. If lambda1 is greater than one, the population will grow.

And we can use this to determine the conditions for instability - the conditions under which the population will grow. In this case the condition is lambda1 > 0. But this can be simplified down to
There's one last trick we can do - determine what proportion of the total population will be in the sub-population x (e.g. what what fraction of the population will be gay) as t tends to infinity.

For this, we determine the equation for population x (in a similar fashion to z), and take advantage of the limits to get the equation
Note that this equation is time-independant, i.e. the proportion will tend to some constant value.


Back to Basics

Just as a sort of check, we'll go back to the basic model. For this, we have
Which gives lambda and A values
Which gives the population equation
Which behaves like the graphs at the top. And this tends to
And gives
That is, the proportion of the population that will be gay stabilises at the pre-defined 'birth' ratio - which is what we saw from the numerical model. Note also that this value is independent of initial conditions and doesn't vary over time.


Simple Instability

Finally, for the simple model, the condition for instability (growth) is given by
For the numerical values chosen above, this gives the condition g < 0.606 - that is, if less than 61% of the population is 'born' gay, then the population won't die out. And 61% is a pretty high upper limit - a majority, in fact.

Similarly, we can use this inequality to find under what conditions g=0.99 is viable. In this case, it requires a birth rate 100 times the death rate.

So, if the death rate is 0.008 (i.e. 8 death for every 1,000 people), then it requires a birth rate of 0.8 (800 birth per 1,000 people). Now, for each 1,000 people, 99% is gay, meaning 10 straight people per 1,000. Of those, about half is female (5). In other words, each woman would have to give birth to 160 babies per year!

Clearly this isn't feasible. But for comparisons sake, consider bees or ants - these species are eusocial, meaning populations made of one fertile queen, and hundreds of (mostly) sterile drones. In this case, the high birth-to-death ratio is more reasonable, and the predominantly non-reproducing population is still viable.


A More Complex Model

In the simple model we assumed that only the straight population reproduces, and that both populations die at the same rate. So we make the following modifications:

1) First of all, lets tweak the model so that the gay population has children as well, with some separate birth rate, b1.

2) Of those born to gay couples, some proportion g1 will be gay (NB/ studies show that the majority of children raised by same-sex parents grow up to be heterosexual).

3) And for completeness, lets say that the gay population has a different death rate as well, d1.

Here's what the model looks like
And here are the coupled equations
Now, all the previously derived general equations and such from earlier still apply. But in this case, the variables can't really be simplified, and writing them out in full would just be a mess.

So if we take, for example, b and d as before, and b1 = 0.00103, d1 = 0.01218, and g1 = 0.03, here are the new phase diagrams for g = 0.07 and 0.99
For this model, the phase portraits are basically the same as for the simple model - i.e. the effect of using the modified model over the simple model is negligible.


Complex Conditions

So, for this model, the condition for growth looks like this
The important thing to note here is, if the condition for the simple model (g<1-d/b) is satified, then the condition for the complex model is necessarilty satisfied.

In fact, if we set g to its maximum possible value 1 (i.e. all babies born to straight couple are gay), then the human race will still persist so long as g1 > d1/b1.

And what this means is, so long as the birth rate (b1) is greater than the death rate (d1), then every baby born could be gay, the human race still wouldn't die out.

However, given the low birth rate for gay couples (compared to death rate), this might not actually be satisfied IRL. In which case, an entirely gay population is still not sustainable.

For the b and d values already chosen, we can use the condition to plot how the upper limit of g varies with g1
So for these values, the upper limit of g can increase by no more than 3.4%. So, again, the extra complexity doesn't give a significantly different result compared to the simple model.

In fact, the additional variables only become significant when (b1/d1)g1 > 0.15 (for the values above); or in general, when
So, if we keep b and d as before, g=0.07, and re-set b1=b, d1=d, and g1=0.06, then (b1/d1)g1=0.152, and the phase portrait looks like this
Here, the upper limit on g has risen to 0.67. (cf: 0.61 for the simple model). And, if we set the initial condition x0=0.07*z0, then the limit of x/z = 0.0697; just less than g.



tl;dr - So, would homosexuality ever mean the end of the human race? - Under some circumstances, yes. But in most (real world) cases, no. And even if a majority of the population were LGBT, the human race still wouldn't necessarily die out.

BUT, as a final note - remember that these are very simplified models of population dynamics. So take the results with a grain of salt. The science of sexuality is much more complex and much less abstract than this.


Oatzy.


[Putting my degree to good use.]