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...]
Sunday, June 24, 2012
Too Many Lotteries
Labels:
everyday maths,
games,
lottery,
maths,
nerd,
over-thinking,
probability,
problem solving,
proofs
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)
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,
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
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.]
“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 valuesWhich 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.]
Saturday, March 24, 2012
Some Twitter Infographics
I did some stuff like this before. And I figured, while I was updating my network graphs, why not update some of the other graphics?
And it helps that I worked out how to easily extract data from Twitter (see previous blog). The code is here. Again, rate limits apply.
Who Do I Follow?
This is one of the ones I did before - collect together the bios of the people I follow, then make a word cloud (using Wordle)
Basically, I follow a bunch of geeks and writers. Who like 'things'. So really, same as a year and a half ago.
I would point out though that 6 of the people I follow don't have bios, and about 7 just have lyrics.
Data here.
Who Tweets the Most?
These rates are worked out as (total tweets posted)/(total days online). Obviously, the actually post rate will vary over different time scales..
Bubble chart (made with ManyEyes) - bubbles sized by tweet rate (the numbers on some of the bubbles).
The graph below gives a better idea of relative rates, and 'rankings' (click to embiggen)
The blue line is actual values.
The orange is a logarithmic trend-line. It's a pretty good fit (R2=0.95); and, loosely speaking, it means ~70% of the tweets in my timeline come from ~30% of the people I follow. [cf: Pareto Principle]
You get similar log-shaped graphs when you split up the genders.
Full data here.
Chattiest Gender?
You can read all the explanation, caveats, etc. in the previous posts (here and here). I'm just going to go straight into the data.
I follow 27 men and 21 women (excluding celebrities, etc.). The stats are as follow:
Basically, the women tweet more on average, and their rates are more spread out than for the men. In fact, roughly three quarters of the men tweet less than half of the women. Also, there's one outlier in the female group.
This is similar to what we found last time; although the women's average and spread aren't quite as high (average: 13.79 vs 19.21), and the men's average has increased slightly (6.21 vs 5.29).
If you take the ratio of the averages, the women tweet 2.15 times as much as the men. But maybe I just follow particularly chatty women..
Here's treemap (ManyEyes), which should give you a better idea of the gender balance (boxes sized by tweet rate)
Specifically, the graphic above is 62.5% purple (female).
Data here.
Where in the World Are My Followers?
The site I used last time doesn't seem to exist anymore. So I'm using MapMyFollowers instead. As the name suggests, these are my followers, rather than just the people I follow. Nonetheless..
Mostly in the UK and the US. As you'd probably expect.
I will point out though, some of the locations are a little suspect. Some people haven't made their location available so aren't included, and others seem to be in countries they couldn't possibly be in. But it's the best we can do.
Here's a zoom in on the UK
What Do I Tweet?
Made with Wordle, with data from TweetStats.
Words are sized by how often I tweet them; and by extension, @usernames are sized by how often I tweet those people.
In fact, here are the people I 'mention' the most (TweetStats)
Couldn't get a good source on who @replies me. That was one of the things Twoolr used to do..
When Do I Tweet?
Twoolr used to be awesome for Twitter statistics. But sadly, when they left beta, they started charging. And their free service went to shit. Luckily, I found TweetStats. Weirdly, it doesn't need you to log-in or anything, but somehow it can pull data on (nearly) all your tweets - beyond the 3,200 limit. Strange.
Here's some more graphs
Basically, I tweet most on a Friday and Saturday, and at around 1-2pm.
And I've never tweeted at 5am. But that's probably because I'm always asleep at 5am
Except that one time I got really drunk. (SleepBot)
How Much Do I Tweet?
This is another one I used to go to Twoolr for. And, to be fair, I still could. But that only goes as far back as April '10, and its graphics aren't as clear. Here's TweetStats again
Like I said before, I didn't tweet much in my first year. In fact, I only posted 36 tweets in all of 2009.
Now, the one problem with TweetStats is that 5 month gap in 2010. Why is this significant? Well, I was definitely tweeting during that time. In fact, by my estimates, over those 5 months I posted 5,724 tweets (~37tweets/day). So those 5 months account for 43% of all my tweets.
See, the thing is, in 2010, I was out of university, single, and unemployed. I posted a total 8,823 tweets - 24tweets/day. Since I've been back at university, that number's dropped to 11tweets/day.
That lull in Summer 2011 was when I was spending all my time on Tumblr and watching classic Doctor Who. Incidentally, I haven't posted on Tumblr since the start of September '11. It's terribly addictive, you see. I wouldn't recommend it; unless you're addicted to Doctor Who and Sherlock, and have lots of time on your hands..
So yeah.
Oatzy.
[Self-indulgent statistics, and pretty illustrations.]
And it helps that I worked out how to easily extract data from Twitter (see previous blog). The code is here. Again, rate limits apply.
Who Do I Follow?
This is one of the ones I did before - collect together the bios of the people I follow, then make a word cloud (using Wordle)
Basically, I follow a bunch of geeks and writers. Who like 'things'. So really, same as a year and a half ago.
I would point out though that 6 of the people I follow don't have bios, and about 7 just have lyrics.
Data here.
Who Tweets the Most?
These rates are worked out as (total tweets posted)/(total days online). Obviously, the actually post rate will vary over different time scales..
Bubble chart (made with ManyEyes) - bubbles sized by tweet rate (the numbers on some of the bubbles).
The graph below gives a better idea of relative rates, and 'rankings' (click to embiggen)
The blue line is actual values.
The orange is a logarithmic trend-line. It's a pretty good fit (R2=0.95); and, loosely speaking, it means ~70% of the tweets in my timeline come from ~30% of the people I follow. [cf: Pareto Principle]
You get similar log-shaped graphs when you split up the genders.
Full data here.
Chattiest Gender?
You can read all the explanation, caveats, etc. in the previous posts (here and here). I'm just going to go straight into the data.
I follow 27 men and 21 women (excluding celebrities, etc.). The stats are as follow:
Men:For clarity, here's a boxplot (made in R)
Average = 6.21 tweets/day
Standard Deviation = 6.47
Women:
Average = 13.79 tweets/day
Standard Deviation = 14.27
Basically, the women tweet more on average, and their rates are more spread out than for the men. In fact, roughly three quarters of the men tweet less than half of the women. Also, there's one outlier in the female group.
This is similar to what we found last time; although the women's average and spread aren't quite as high (average: 13.79 vs 19.21), and the men's average has increased slightly (6.21 vs 5.29).
If you take the ratio of the averages, the women tweet 2.15 times as much as the men. But maybe I just follow particularly chatty women..
Here's treemap (ManyEyes), which should give you a better idea of the gender balance (boxes sized by tweet rate)
Specifically, the graphic above is 62.5% purple (female).
Data here.
Where in the World Are My Followers?
The site I used last time doesn't seem to exist anymore. So I'm using MapMyFollowers instead. As the name suggests, these are my followers, rather than just the people I follow. Nonetheless..
Mostly in the UK and the US. As you'd probably expect.
I will point out though, some of the locations are a little suspect. Some people haven't made their location available so aren't included, and others seem to be in countries they couldn't possibly be in. But it's the best we can do.
Here's a zoom in on the UK
What Do I Tweet?
Made with Wordle, with data from TweetStats.
Words are sized by how often I tweet them; and by extension, @usernames are sized by how often I tweet those people.
In fact, here are the people I 'mention' the most (TweetStats)
Couldn't get a good source on who @replies me. That was one of the things Twoolr used to do..
When Do I Tweet?
Twoolr used to be awesome for Twitter statistics. But sadly, when they left beta, they started charging. And their free service went to shit. Luckily, I found TweetStats. Weirdly, it doesn't need you to log-in or anything, but somehow it can pull data on (nearly) all your tweets - beyond the 3,200 limit. Strange.
Here's some more graphs
Basically, I tweet most on a Friday and Saturday, and at around 1-2pm.
And I've never tweeted at 5am. But that's probably because I'm always asleep at 5am
Except that one time I got really drunk. (SleepBot)
How Much Do I Tweet?
This is another one I used to go to Twoolr for. And, to be fair, I still could. But that only goes as far back as April '10, and its graphics aren't as clear. Here's TweetStats again
Like I said before, I didn't tweet much in my first year. In fact, I only posted 36 tweets in all of 2009.
Now, the one problem with TweetStats is that 5 month gap in 2010. Why is this significant? Well, I was definitely tweeting during that time. In fact, by my estimates, over those 5 months I posted 5,724 tweets (~37tweets/day). So those 5 months account for 43% of all my tweets.
See, the thing is, in 2010, I was out of university, single, and unemployed. I posted a total 8,823 tweets - 24tweets/day. Since I've been back at university, that number's dropped to 11tweets/day.
That lull in Summer 2011 was when I was spending all my time on Tumblr and watching classic Doctor Who. Incidentally, I haven't posted on Tumblr since the start of September '11. It's terribly addictive, you see. I wouldn't recommend it; unless you're addicted to Doctor Who and Sherlock, and have lots of time on your hands..
So yeah.
Oatzy.
[Self-indulgent statistics, and pretty illustrations.]
Labels:
data analysis,
data tracking,
gender,
graph,
infographic,
nerd,
OCD,
programming,
python,
sleep,
sociology,
statistics,
tumblr,
tweets,
twitter
Wednesday, March 14, 2012
Friend Network Evolution
Back in February 2009, I created my Twitter account, upon the insistence of my then-girlfriend. I didn't get it. Back then, Facebook was where it was at, I didn't really get Twitter's appeal. I was pretty much just following the handful of people I knew in real life, and Stephen Fry.
So I didn't use it much. I'd pop up every now and then, post a couple tweets and give up on it again. At one point, I even developed an irrational dislike of it - whenever I saw a site had a "follow us on Twitter" button, it irked me for some reason.
But at some point, towards the end of 2009/early 2010, I gave it yet another try. I don't know why. And even when I started using it, I was resistant; still half-heartedly hating it. But what was different this time, is I started chatting with people, and I was introduced to new people.
People who don't get Twitter think it's just that thing where you can tell people when you're eating a sandwich. It's not. It's the people that make Twitter. (Tweens and arseholes notwithstanding.)
But I'm going off on a tangent.
By August 2010, I was well into Twitter - I was posting around 30 tweets per day, and I had around 30 friends*. And back then, I decided I wanted to see what my friends network looked like. So I broke out Python and the Twitter API, I pulled data, and I made the graph. Here's an updated version of it.
[click to embiggen]
Fairly small, and tidy, and relatively uncomplicated. The bulk on the right is the people I knew in real life (from school, etc.) with a few strands of new acquaintances. Note how tightly packed and interconnected they are. To the left is mostly people I met through Twitter - and in particular, through PkmnTrainerJ.
(In case you hadn't figured it out, the node and label sizes are proportional to number of connections.)
By December 2010, I decided to have a look again.
Again, this is an update of the version I originally posted; and in this case, I've tried to arrange it so that key people stay in approximately the same place.
So you still vaguely have that left-right divide, but now there's much more mixing in the middle. I'd made some new friends, but more interesting is the people who were already in the graph who formed new connections with others in my graph.
I'd also like to draw your attention to shinelikestars_ (formerly shinelikestars6) - take a look at the previous graph, can you spot him? From 2 shared connections to 8 in the space of two months. I don't think there's any sort of point I'm trying to make here. I'm just pointing it out 'cause it's interesting.
And for the next year and a half I didn't do any data collecting. It became too labourious - Twitter changed its API, so that my old code didn't work, and I had to do everything by hand.
So, the latest graph was March 2011 (technical details below). As you can imagine, a lot can happen in a year and a half.
First of all, the new people add, and the old people removed. But more importantly, look how much tighter, and how much more 'segmented' the graph is.
There are now three major groups, loosely centred on the three most connected of my friends.
On the far right are, again, the people I knew in real life. In particular, note how little that group has changed since the first graph.
In the middle, we have 'Shiney's People' - people I was introduced to by shinelikestars_. And on the left are the people I was introduced to by PkmnTrainerJ.
The smaller groups circled in red are cliques - smaller subgroups that, at least from my point of view, form their own little groupings, where (almost) everyone is interconnected. The bottom left 'clique', for example, is my parents and big sister.
And I suspect, if you were to extend the graph beyond my network, you would find that those cliques are just parts of larger interconnected groups.
In case you were wondering, PkmnTrainerJ and SallyBembridge are most connected, both with degree 14. shinelikestars_ is next most, with degree 10.
Notable disappearing nodes - Benjidoom, who deleted his account, then created a new, private one (benjirino); and AimlessAmy, who is a long story.
I should also point out that the people I follow who aren't friends with anyone else in my network do not appear in the pictured graphs. Not that they aren't as cool, they just don't join onto the graph.
* I use friend here to mean people who I follow and who follow me back. Though I would probably consider all the people in my current network (including those not pictured) friends to some degree.
Technical stuff
You can read details on how I collected the data before, in the previous blogs. But, as I say, those methods don't work anymore.
For this run, I read up on the API, and found some bits that don't need authentication to grab and manipulate.
First, you can grab a list of a user's friends with this URL
https://api.twitter.com/1/friends/ids.xml?screen_name=<username>
This will give you a list of the friends ID numbers, so you also need to use this to grab usernames
https://api.twitter.com/1/users/lookup.xml?user_id=<idnumber>
There is also a URL to check if a user follows another user
https://api.twitter.com/1/friendships/exists.xml?user_id_a=<idnumber1>&user_id_b=<idnumber2>
Which works through the browser, but I couldn't get to work in my code. So in place, I used the site DoesFollow.com; partly because it uses the URL scheme
DoesFollow.com/user1/user2.
Which is very convenient. Though I do worry all the requests might be putting strain on that site's server.
So, putting all that together with a bit of Python, you get something like this.
A few important points:
1) It will take a while to run. I have ~50 friends, and it took well over an hour to pull all the data. In terms of computational complexity, it's O(n^2), but each of those operations takes a significant amount of time.
2) Twitter has an API limit of 150 requests per hour. The number of API requests the code will make is ~ the number of friends being looked up. I think. Which means, if you have more than 100 or so friends, this code probably won't work. Sorry. There might be a way around it, but I don't know how.
3) Obviously, this doesn't work on protected accounts. So for those people you will have to grab data by hand. Though it's not too bad for a small enough number of people.
If you do want to use the code, I've made it so you just have to change the username at the top, and run it. You will need to install Python though.
For creating the graphs, I previously use ManyEyes. But I moved to using Gephi, because it allows for more customising. The output from the code is a text file with a list of name pairs, which you can import directly into Gephi. It will build the graph for you, and then you're free to play as you like.
Aaand... Yeah, I think that's about it.
Oatzy.
[shinelikestars6 lost his red circle, on account of he isn't my nemesis anymore.]
So I didn't use it much. I'd pop up every now and then, post a couple tweets and give up on it again. At one point, I even developed an irrational dislike of it - whenever I saw a site had a "follow us on Twitter" button, it irked me for some reason.
But at some point, towards the end of 2009/early 2010, I gave it yet another try. I don't know why. And even when I started using it, I was resistant; still half-heartedly hating it. But what was different this time, is I started chatting with people, and I was introduced to new people.
People who don't get Twitter think it's just that thing where you can tell people when you're eating a sandwich. It's not. It's the people that make Twitter. (Tweens and arseholes notwithstanding.)
But I'm going off on a tangent.
By August 2010, I was well into Twitter - I was posting around 30 tweets per day, and I had around 30 friends*. And back then, I decided I wanted to see what my friends network looked like. So I broke out Python and the Twitter API, I pulled data, and I made the graph. Here's an updated version of it.
[click to embiggen]
Fairly small, and tidy, and relatively uncomplicated. The bulk on the right is the people I knew in real life (from school, etc.) with a few strands of new acquaintances. Note how tightly packed and interconnected they are. To the left is mostly people I met through Twitter - and in particular, through PkmnTrainerJ.
(In case you hadn't figured it out, the node and label sizes are proportional to number of connections.)
By December 2010, I decided to have a look again.
Again, this is an update of the version I originally posted; and in this case, I've tried to arrange it so that key people stay in approximately the same place.
So you still vaguely have that left-right divide, but now there's much more mixing in the middle. I'd made some new friends, but more interesting is the people who were already in the graph who formed new connections with others in my graph.
I'd also like to draw your attention to shinelikestars_ (formerly shinelikestars6) - take a look at the previous graph, can you spot him? From 2 shared connections to 8 in the space of two months. I don't think there's any sort of point I'm trying to make here. I'm just pointing it out 'cause it's interesting.
And for the next year and a half I didn't do any data collecting. It became too labourious - Twitter changed its API, so that my old code didn't work, and I had to do everything by hand.
So, the latest graph was March 2011 (technical details below). As you can imagine, a lot can happen in a year and a half.
First of all, the new people add, and the old people removed. But more importantly, look how much tighter, and how much more 'segmented' the graph is.
There are now three major groups, loosely centred on the three most connected of my friends.
On the far right are, again, the people I knew in real life. In particular, note how little that group has changed since the first graph.
In the middle, we have 'Shiney's People' - people I was introduced to by shinelikestars_. And on the left are the people I was introduced to by PkmnTrainerJ.
The smaller groups circled in red are cliques - smaller subgroups that, at least from my point of view, form their own little groupings, where (almost) everyone is interconnected. The bottom left 'clique', for example, is my parents and big sister.
And I suspect, if you were to extend the graph beyond my network, you would find that those cliques are just parts of larger interconnected groups.
In case you were wondering, PkmnTrainerJ and SallyBembridge are most connected, both with degree 14. shinelikestars_ is next most, with degree 10.
Notable disappearing nodes - Benjidoom, who deleted his account, then created a new, private one (benjirino); and AimlessAmy, who is a long story.
I should also point out that the people I follow who aren't friends with anyone else in my network do not appear in the pictured graphs. Not that they aren't as cool, they just don't join onto the graph.
* I use friend here to mean people who I follow and who follow me back. Though I would probably consider all the people in my current network (including those not pictured) friends to some degree.
Technical stuff
You can read details on how I collected the data before, in the previous blogs. But, as I say, those methods don't work anymore.
For this run, I read up on the API, and found some bits that don't need authentication to grab and manipulate.
First, you can grab a list of a user's friends with this URL
https://api.twitter.com/1/friends/ids.xml?screen_name=<username>
This will give you a list of the friends ID numbers, so you also need to use this to grab usernames
https://api.twitter.com/1/users/lookup.xml?user_id=<idnumber>
There is also a URL to check if a user follows another user
https://api.twitter.com/1/friendships/exists.xml?user_id_a=<idnumber1>&user_id_b=<idnumber2>
Which works through the browser, but I couldn't get to work in my code. So in place, I used the site DoesFollow.com; partly because it uses the URL scheme
DoesFollow.com/user1/user2.
Which is very convenient. Though I do worry all the requests might be putting strain on that site's server.
So, putting all that together with a bit of Python, you get something like this.
A few important points:
1) It will take a while to run. I have ~50 friends, and it took well over an hour to pull all the data. In terms of computational complexity, it's O(n^2), but each of those operations takes a significant amount of time.
2) Twitter has an API limit of 150 requests per hour. The number of API requests the code will make is ~ the number of friends being looked up. I think. Which means, if you have more than 100 or so friends, this code probably won't work. Sorry. There might be a way around it, but I don't know how.
3) Obviously, this doesn't work on protected accounts. So for those people you will have to grab data by hand. Though it's not too bad for a small enough number of people.
If you do want to use the code, I've made it so you just have to change the username at the top, and run it. You will need to install Python though.
For creating the graphs, I previously use ManyEyes. But I moved to using Gephi, because it allows for more customising. The output from the code is a text file with a list of name pairs, which you can import directly into Gephi. It will build the graph for you, and then you're free to play as you like.
Aaand... Yeah, I think that's about it.
Oatzy.
[shinelikestars6 lost his red circle, on account of he isn't my nemesis anymore.]
Labels:
data analysis,
graph,
graph theory,
hand-waving,
infographic,
maths-free,
nerd,
network,
programming,
python,
sociology,
twitter
Monday, March 05, 2012
So What Was the Best Day To Go Shopping?
Alright, let's be done with this.
Just a quick reminder - what I did was collect Foursquare check-in data for various shopping centres around the UK, in the hope that the data might show something interesting.
Previous blog posts on this data collecting - Best Day to Go Shopping, Panic Saturday, Christmas Eve.
Anyway, I've been collecting data for over 3 months now. And that seems like quite enough.
Here's a graph of (normalised) averaged check-ins on each day of the week for 4 periods:
DecAv (blue) is 21st Nov 2011 to 18th Dec 2011
ChrAv (grey) is 19th Dec 2011 to 1st Jan 2012
JanAv (orange) is 5th Jan 2012 to 2nd Feb 2012
FebAv (green) is 6th Feb 2012 to 4th Mar 2012
Aside from the two weeks either side of Christmas (grey) - when people, apparently, did their shopping more midweek - the pattern is basically the same.
For further clarity, here's the average of those three averages (excluding Christmas)
And here is the order of days, from least to most busy ('relative busyness' in brackets):
1) Wednesday (1.00)
2) Monday (1.01)
3) Tuesday (1.03)
4) Thursday (1.11)
5) Sunday (1.15)
6) Friday (1.24)
7) Saturday (1.78)
Note that the differences between Monday, Tuesday, and Wednesday are not statistically significant - they're essentially the same, and are likely to be as busy as each other/not noticeably different.
So, to answer the title question - Monday, Tuesday, and Wednesday are the best days to go shopping. At least, in as much as they're the days shopping centres are likely to be least busy. And, as you'd expect, Saturday is, by far, the worst/most busy.
And the last thing to point out is that these are the averages over 20 shopping centres for a ~3 month period - numbers for specific locations, and at different times (eg holidays) are likely to deviate from the averages.
And, basically, that's that.
If you're interested, you can see the raw check-in data here.
Oatzy.
[That was definitely worth the effort.]
Just a quick reminder - what I did was collect Foursquare check-in data for various shopping centres around the UK, in the hope that the data might show something interesting.
Previous blog posts on this data collecting - Best Day to Go Shopping, Panic Saturday, Christmas Eve.
Anyway, I've been collecting data for over 3 months now. And that seems like quite enough.
Here's a graph of (normalised) averaged check-ins on each day of the week for 4 periods:
DecAv (blue) is 21st Nov 2011 to 18th Dec 2011
ChrAv (grey) is 19th Dec 2011 to 1st Jan 2012
JanAv (orange) is 5th Jan 2012 to 2nd Feb 2012
FebAv (green) is 6th Feb 2012 to 4th Mar 2012
Aside from the two weeks either side of Christmas (grey) - when people, apparently, did their shopping more midweek - the pattern is basically the same.
For further clarity, here's the average of those three averages (excluding Christmas)
And here is the order of days, from least to most busy ('relative busyness' in brackets):
1) Wednesday (1.00)
2) Monday (1.01)
3) Tuesday (1.03)
4) Thursday (1.11)
5) Sunday (1.15)
6) Friday (1.24)
7) Saturday (1.78)
Note that the differences between Monday, Tuesday, and Wednesday are not statistically significant - they're essentially the same, and are likely to be as busy as each other/not noticeably different.
So, to answer the title question - Monday, Tuesday, and Wednesday are the best days to go shopping. At least, in as much as they're the days shopping centres are likely to be least busy. And, as you'd expect, Saturday is, by far, the worst/most busy.
And the last thing to point out is that these are the averages over 20 shopping centres for a ~3 month period - numbers for specific locations, and at different times (eg holidays) are likely to deviate from the averages.
And, basically, that's that.
If you're interested, you can see the raw check-in data here.
Oatzy.
[That was definitely worth the effort.]
Labels:
christmas,
data analysis,
data tracking,
foursquare,
graph,
hypothetical question,
nerd,
sociology,
statistics
Saturday, March 03, 2012
Where are the Carriages?
<rant>
So it's around 9am, and you're waiting for a train. The trains in your area are a bit scummy, but whatever, you can't drive, and you've got to get to work/uni somehow. It's pretty busy, what with it being 9am, and when the train finally pulls up.. it's a single carriage.
Now a single car has around 50 seats, and this train is packed tight, with people standing in the aisles and the doorways, being forced to get intimate. And with everyone on board the conductor can barely get through the door. There are clearly enough people on this train to fill two cars, with people still having to stand.
So what gives? You're pretty pissed about having to stand for half an hour, and you've been inadvertently touched by strangers in ways you're not comfortable with. So you get your complaining hat on, and you turn to the internet to unleash the fury.
The train company's website directs you to their Twitter feed, where a poor public relations person is taking a barrage of vitriol from other angry commuters, while doing their best to remain polite and professional.
Several other people have already made your complaint, but all they're getting in return is "Apologies we try to avoid this where possible", or that the short trains were "due to operational reasons". And that's not really a satisfying response. You're not even sure it means anything.
</rant>
So Where Are The Carriages?
Disclaimer: I have no idea how the train company actually operates. I just like to write blogs about applied maths.
Here's the setup - you're in charge of logistics for the train company. You have a fixed number of trains/cars, and, for each day, a list of services and expected numbers of passengers.
How do you apportion the carriages between the services?
Obviously, every service needs at least one car, and services with more passengers need more cars.
So if you had, for example, three services with 50, 100, and 150 passengers respectively, and 6 cars, then it's easy to divvy them up - one for the first, two for the second, and three for the third.
But in general, it won't be possible to divide up the cars exactly like that; so what do you do about remainders?
This is actually similar to the problem of apportioning parliamentary seats between states in the US.
Currently there are 435 seats in the House of Representatives, which need to be shared out between the 50 states according to each state's population - the idea being that each seat should represent roughly the same number of voters.
There are various methods for doing this, but I'm only going to go over two of them.
The Hamilton method
Hamilton's Method is the easiest and most intuitive.
Say you've got only two services - service A has 85 passengers, service B has 115 passengers (200 passengers total). And you happen to have 4 carriages - a total 200 seats. So seats for everyone! Except service A needs 1.7 cars, and service B needs 2.3 cars. And you can't divide a car into two chunks.
But for starters, you can give one car to service A (50 seats), and two to service B (100 seats). So what about the fourth? Regardless of which service you give it to, some people are going to end up having to stand. So what you want to do is minimise that number.
For service A, 35 people need a seat. For service B, 15 people need a seat. So it makes sense to give the last car to service A. 15 people still have to stand, but - short of building more cars - there's really nothing you can do about that.
This is also known as the largest remainder method.
What if the passenger numbers were 75 and 125? Well in that case, I guess it would have to be a judgement call.
Anyway, that's the basic idea of the Hamilton method - divide up the cars as far as you can, then give the remaining car(s) to the service(s) which 'need them most'.
Things get trickier when you're dealing with larger numbers of passengers and cars, but ultimately it's pretty straight-forward.
The Huntington-Hill Method
Where trains differ from apportioning of parliamentary seats, is that a carriage has a fixed capacity, whereas the number of people a seat can represent is free to change.
And this means that you won't encounter some of the 'quirks' that arise from the Hamilton method - such as the Alabama Paradox (where increasing the total number of seats can mean a state losing seats), or the Population Paradox (where increasing a state's population can result in that state losing seats).
[You won't encounter them, but they're worth mentioning 'cause they're pretty cool.]
Huntington-Hill's Method - the one currently used by the House of Representatives - is, generally, a more powerful method than Hamilton's, and isn't susceptible to the various paradoxes. Another benefit is that it can be set up to guarantee that each state will get at least one seat.
It works by assigning seats, one by one, based on each state's 'priority quotient' - itself based on the state's population, and a 'modified divisor' based on the number of seats already allocated to that state.
But one advantage Hamilton's Method has over HHM, is that it will always give each service its ideal number of cars, either rounded up of down to the nearest whole number. So if a service's ideal number is 3.67, then Hamilton's method will assign either 3 or 4 cars to that service.
HHM, on the other hand, can 'violate quota' - that is, it can result in a given service being assigned more or fewer cars than it's ideal number (e.g. the 3.67 service might end up with only 2 cars). But this problem only occurs when the number of cars is fixed prior to apportioning. And sadly, it usually is.
As an aside, it turns out it's impossible to find a 'perfect' method of apportioning - one with neither paradoxes nor quota violating. Incidentally, the mathematical theory surrounding voting is quite fascinating. You know, if you're into that sort of thing.
Unfortunately..
Either method would work perfectly well. The problem is, while rounding up or down to the nearest car may seem trivial, the 50 seats that that one car represents can be a significant gain/loss for a service.
The lack of precise information can cause problems as well. You can only estimate how many passengers a given service will have in advance, and if you under-estimate, people are gonna be pretty pissed.
And on top of that, the numbers have to be recalculated from time to time as passenger numbers change..
Basically, getting the number of cars right can be tricky.
Of course, this all assumes that Northern Rail (or whatever company) has enough carriages to adequately satisfy its needs in the first place. Some how I doubt that's the case.
So ideally, NR needs to work out how many cars it actually needs, and build them. But I can't see that happening. And even if it did happen, it'd probably mean higher fares. And fares are pretty bad as they are.
Really, though, NR could do with a complete makeover just in general, given how shitty it is. Seriously, you should see the difference between them and the London Midland service. Ridiculous.
Incidentally, I also have strong feelings about the buses; don't even get me started. Trams are alright though - one every 10mins, seldom short of seats, sufficient leg-room.. bliss.
tl;dr - Where are the carriages, then? As it turns out, it's probably just that they don't have enough to go around.
This is what we get for privatising the trains...
Oatzy.
[My complaining hat is a trilby. I like to look bad-ass while I'm complaining.]
So it's around 9am, and you're waiting for a train. The trains in your area are a bit scummy, but whatever, you can't drive, and you've got to get to work/uni somehow. It's pretty busy, what with it being 9am, and when the train finally pulls up.. it's a single carriage.
Now a single car has around 50 seats, and this train is packed tight, with people standing in the aisles and the doorways, being forced to get intimate. And with everyone on board the conductor can barely get through the door. There are clearly enough people on this train to fill two cars, with people still having to stand.
So what gives? You're pretty pissed about having to stand for half an hour, and you've been inadvertently touched by strangers in ways you're not comfortable with. So you get your complaining hat on, and you turn to the internet to unleash the fury.
The train company's website directs you to their Twitter feed, where a poor public relations person is taking a barrage of vitriol from other angry commuters, while doing their best to remain polite and professional.
Several other people have already made your complaint, but all they're getting in return is "Apologies we try to avoid this where possible", or that the short trains were "due to operational reasons". And that's not really a satisfying response. You're not even sure it means anything.
</rant>
So Where Are The Carriages?
Disclaimer: I have no idea how the train company actually operates. I just like to write blogs about applied maths.
Here's the setup - you're in charge of logistics for the train company. You have a fixed number of trains/cars, and, for each day, a list of services and expected numbers of passengers.
How do you apportion the carriages between the services?
Obviously, every service needs at least one car, and services with more passengers need more cars.
So if you had, for example, three services with 50, 100, and 150 passengers respectively, and 6 cars, then it's easy to divvy them up - one for the first, two for the second, and three for the third.
But in general, it won't be possible to divide up the cars exactly like that; so what do you do about remainders?
This is actually similar to the problem of apportioning parliamentary seats between states in the US.
Currently there are 435 seats in the House of Representatives, which need to be shared out between the 50 states according to each state's population - the idea being that each seat should represent roughly the same number of voters.
There are various methods for doing this, but I'm only going to go over two of them.
The Hamilton method
Hamilton's Method is the easiest and most intuitive.
Say you've got only two services - service A has 85 passengers, service B has 115 passengers (200 passengers total). And you happen to have 4 carriages - a total 200 seats. So seats for everyone! Except service A needs 1.7 cars, and service B needs 2.3 cars. And you can't divide a car into two chunks.
But for starters, you can give one car to service A (50 seats), and two to service B (100 seats). So what about the fourth? Regardless of which service you give it to, some people are going to end up having to stand. So what you want to do is minimise that number.
For service A, 35 people need a seat. For service B, 15 people need a seat. So it makes sense to give the last car to service A. 15 people still have to stand, but - short of building more cars - there's really nothing you can do about that.
This is also known as the largest remainder method.
What if the passenger numbers were 75 and 125? Well in that case, I guess it would have to be a judgement call.
Anyway, that's the basic idea of the Hamilton method - divide up the cars as far as you can, then give the remaining car(s) to the service(s) which 'need them most'.
Things get trickier when you're dealing with larger numbers of passengers and cars, but ultimately it's pretty straight-forward.
The Huntington-Hill Method
Where trains differ from apportioning of parliamentary seats, is that a carriage has a fixed capacity, whereas the number of people a seat can represent is free to change.
And this means that you won't encounter some of the 'quirks' that arise from the Hamilton method - such as the Alabama Paradox (where increasing the total number of seats can mean a state losing seats), or the Population Paradox (where increasing a state's population can result in that state losing seats).
[You won't encounter them, but they're worth mentioning 'cause they're pretty cool.]
Huntington-Hill's Method - the one currently used by the House of Representatives - is, generally, a more powerful method than Hamilton's, and isn't susceptible to the various paradoxes. Another benefit is that it can be set up to guarantee that each state will get at least one seat.
It works by assigning seats, one by one, based on each state's 'priority quotient' - itself based on the state's population, and a 'modified divisor' based on the number of seats already allocated to that state.
But one advantage Hamilton's Method has over HHM, is that it will always give each service its ideal number of cars, either rounded up of down to the nearest whole number. So if a service's ideal number is 3.67, then Hamilton's method will assign either 3 or 4 cars to that service.
HHM, on the other hand, can 'violate quota' - that is, it can result in a given service being assigned more or fewer cars than it's ideal number (e.g. the 3.67 service might end up with only 2 cars). But this problem only occurs when the number of cars is fixed prior to apportioning. And sadly, it usually is.
As an aside, it turns out it's impossible to find a 'perfect' method of apportioning - one with neither paradoxes nor quota violating. Incidentally, the mathematical theory surrounding voting is quite fascinating. You know, if you're into that sort of thing.
Unfortunately..
Either method would work perfectly well. The problem is, while rounding up or down to the nearest car may seem trivial, the 50 seats that that one car represents can be a significant gain/loss for a service.
The lack of precise information can cause problems as well. You can only estimate how many passengers a given service will have in advance, and if you under-estimate, people are gonna be pretty pissed.
And on top of that, the numbers have to be recalculated from time to time as passenger numbers change..
Basically, getting the number of cars right can be tricky.
Of course, this all assumes that Northern Rail (or whatever company) has enough carriages to adequately satisfy its needs in the first place. Some how I doubt that's the case.
So ideally, NR needs to work out how many cars it actually needs, and build them. But I can't see that happening. And even if it did happen, it'd probably mean higher fares. And fares are pretty bad as they are.
Really, though, NR could do with a complete makeover just in general, given how shitty it is. Seriously, you should see the difference between them and the London Midland service. Ridiculous.
Incidentally, I also have strong feelings about the buses; don't even get me started. Trams are alright though - one every 10mins, seldom short of seats, sufficient leg-room.. bliss.
tl;dr - Where are the carriages, then? As it turns out, it's probably just that they don't have enough to go around.
This is what we get for privatising the trains...
Oatzy.
[My complaining hat is a trilby. I like to look bad-ass while I'm complaining.]
Labels:
algorithm,
hand-waving,
maths,
optimisation,
public transport,
rant,
rush hour,
trains,
voting
Friday, February 24, 2012
A Problem With Puzzles
Here's a puzzle, tweeted by Aetherling
Have a go at it yourself. Solution below.
You see these sorts of puzzle every now and again. And there are usually common 'tricks' to them.
My first instinct was to add the digits. Nothing. Modulus? Nope. Sum and modulus, multiply digits, square, divide? Nope.
So, what?
For some reason, I decided I was going to try some algebra - ignore the fact that the left-hand sides of the equations are numbers, and treat them as variables with some unknown values. And then, add up whatever those values are to get the numbers on the right of the equations.
i.e. 9313 -> '9' + 2x'3' + '1' = 1; 1111 -> 4x'1' = 0; etc.
So from 1111=2222=3333=5555=7777=0 we get that 1,2,3,5,7 = 0
From 0000=6666=9999=4 we get that 0,6,9 = 1
And from 6855=3, 1+'8'=3 => 8=2
4 is undefined. And that makes the answer to the puzzle 2581=2.
It took me about half an hour to come up with that answer..
"If all you have is a hammer, everything looks like a nail"
That is the right answer. But the puzzle suggests that a preschooler would get the correct answer with ease. And I doubt a preschooler would do so with algebra.
So, what would a preschooler do? And why does it take 'higher educated' people so much longer?
What's the 'correct' answer? Take a look at the below
Basically, it's the number of loops in the digits on the left-hand side. This had to be pointed out to me by Aerliss.
But here's the interesting thing - take a look at the values I assigned to the number.
I got to the same answer, and effectively 'derived' the number of loops in the digits, without realising that that's what I was doing.
Source on the subheading quote - Maslow's Hammer.
It's not a perfect analogy, but the idea is this - the more mathematical training you have, the more techniques you'll be trying to use to solve the problem; possibly causing you to miss the 'obvious' answer. As, indeed, I did.
In this analogy, maths is the hammer.
And, strictly speaking, this puzzle isn't a maths problem - even though it's presented as if it were. And even though it can be solved using maths.
Another Problem
Here's another puzzle, recently posted on Reddit
The problem here is that the correct answer isn't known.
The 'obvious' answer (in as much as the one that most commenters came up with) was to add the four cross numbers, divide by 10, and round to the nearest whole number. In that case, the missing number is (23+20+12+3)/10 = 5.8 ~ 6 (B).
But that doesn't explain the set up - why the crosses? Are the positions of the numbers in the cross significant? And rounding is a little messy.
The most compelling proposed solution, once again, doesn't actually involve any maths:
Finite Rule Paradox
At any rate, what the comment thread shows is, in the absence of a 'true' answer, any answer is potentially correct with some valid justification. Case in point, I've just presented two different possible answers with two valid justifications.
This is an example of Wittgenstein's Finite Rule Paradox.
Stated explicitly - "This was our paradox: no course of action could be determined by a rule, because any course of action can be made out to accord with the rule"
Basically, what I described above..
There's a clearer explanation here. The topic is also a plot point in the novel (and subsequent film adaptation) The Oxford Murders; recommended if you're into maths-fueled murder mysteries.
The paradox is more apparent in 'find the next term in the sequence' puzzles.
For example, if you're given 2, 4, 8, 16 and asked what the next number is, then the obvious answer is 32 - twice the previous number/powers of two.
However, 31 is an equally valid continuation, as Marcus du Sautoy explains:
In fact, you can define a function, f(n), which satisfies the first 4 terms of the above sequence, and will give any number you like for the fifth.
The Easy Path
How is that possible? Lagrange Interpolation.
[Discussed in Professor Stewart's Hoard of Mathematical Treasures]
Lagrange interpolation applies to any (numerical) sequence of any (finite) length; so, there are infinitely many ways to continue any given sequence - and, necessarily, one cannot say with absolute certainty that a given sequence will continue in one particular way, only.
Unless the person who set the puzzle says otherwise. Because if it's their puzzle, they know what answer they're looking for. And if you try to score easy marks on an exam by citing Wittgenstein, you're probably going to fail.
Of course, Lagrange polynomials - although interesting that they exist - are likely to be the least interesting way to continue a sequence. Certainly, the equivalent Lagrange polynomial isn't as meaningful or interesting as the circle division sequence; even though they both give the same first 5 terms.
And a Lagrange polynomial will probably never be the most obvious/aesthetically pleasing solution.
There Must Be Another Way
The important thing is, the existence of Lagrange polynomials 'proves' the Finite Rule Paradox for numerical sequences.
By the same logic, there must exist some functions, f(a,b,c,d), which satisfy the first 20 equations in the first puzzle, but give some values other than 2 for the last. Or, multiple functions which do give 2, but by different methods to the correct one.
Of course, such functions aren't likely to be easy to find. Especially when there are 20 'initial rules' to satisfy. But the fact that there are finitely many rules means it's possible.
Not that counting loops, or manipulating letters count as mathematical functions. But the point still stands.
At any rate, the important thing to remember is not to get too obsessed with the consequences of the Finite Rule Paradox. Or you may end up trying to lobotomise yourself to prove a theory.
Oatzy.
[What comes next - 3, 3, 5, 4, 4, 3, 5, 5, 4, ?]
Have a go at it yourself. Solution below.
You see these sorts of puzzle every now and again. And there are usually common 'tricks' to them.
My first instinct was to add the digits. Nothing. Modulus? Nope. Sum and modulus, multiply digits, square, divide? Nope.
So, what?
For some reason, I decided I was going to try some algebra - ignore the fact that the left-hand sides of the equations are numbers, and treat them as variables with some unknown values. And then, add up whatever those values are to get the numbers on the right of the equations.
i.e. 9313 -> '9' + 2x'3' + '1' = 1; 1111 -> 4x'1' = 0; etc.
So from 1111=2222=3333=5555=7777=0 we get that 1,2,3,5,7 = 0
From 0000=6666=9999=4 we get that 0,6,9 = 1
And from 6855=3, 1+'8'=3 => 8=2
4 is undefined. And that makes the answer to the puzzle 2581=2.
It took me about half an hour to come up with that answer..
"If all you have is a hammer, everything looks like a nail"
That is the right answer. But the puzzle suggests that a preschooler would get the correct answer with ease. And I doubt a preschooler would do so with algebra.
So, what would a preschooler do? And why does it take 'higher educated' people so much longer?
What's the 'correct' answer? Take a look at the below
Basically, it's the number of loops in the digits on the left-hand side. This had to be pointed out to me by Aerliss.
But here's the interesting thing - take a look at the values I assigned to the number.
I got to the same answer, and effectively 'derived' the number of loops in the digits, without realising that that's what I was doing.
Source on the subheading quote - Maslow's Hammer.
It's not a perfect analogy, but the idea is this - the more mathematical training you have, the more techniques you'll be trying to use to solve the problem; possibly causing you to miss the 'obvious' answer. As, indeed, I did.
In this analogy, maths is the hammer.
And, strictly speaking, this puzzle isn't a maths problem - even though it's presented as if it were. And even though it can be solved using maths.
Another Problem
Here's another puzzle, recently posted on Reddit
The problem here is that the correct answer isn't known.
The 'obvious' answer (in as much as the one that most commenters came up with) was to add the four cross numbers, divide by 10, and round to the nearest whole number. In that case, the missing number is (23+20+12+3)/10 = 5.8 ~ 6 (B).
But that doesn't explain the set up - why the crosses? Are the positions of the numbers in the cross significant? And rounding is a little messy.
The most compelling proposed solution, once again, doesn't actually involve any maths:
"As far as I can tell, the answer is C: 7 [...] The formula for the number in the center is the number of unique letters shared by the top and bottom numbers as written in English plus the number of unique letters shared by the right and left numbers.."
Finite Rule Paradox
At any rate, what the comment thread shows is, in the absence of a 'true' answer, any answer is potentially correct with some valid justification. Case in point, I've just presented two different possible answers with two valid justifications.
This is an example of Wittgenstein's Finite Rule Paradox.
Stated explicitly - "This was our paradox: no course of action could be determined by a rule, because any course of action can be made out to accord with the rule"
Basically, what I described above..
There's a clearer explanation here. The topic is also a plot point in the novel (and subsequent film adaptation) The Oxford Murders; recommended if you're into maths-fueled murder mysteries.
The paradox is more apparent in 'find the next term in the sequence' puzzles.
For example, if you're given 2, 4, 8, 16 and asked what the next number is, then the obvious answer is 32 - twice the previous number/powers of two.
However, 31 is an equally valid continuation, as Marcus du Sautoy explains:
"[L]et me explain why 31 can be a perfectly legitimate way to continue the sequence 2, 4, 8, 16 ... Draw three dots on a circle and join the dots with lines. The circle gets divided into four pieces. If you now take four dots on the circle and draw all the lines between the dots then you cut the circle into eight pieces. Five dots leads to 16 pieces. But if you draw all the lines between six dots you will only get 31 pieces rather than the 32 you'd expect."
In fact, you can define a function, f(n), which satisfies the first 4 terms of the above sequence, and will give any number you like for the fifth.
The Easy Path
How is that possible? Lagrange Interpolation.
[Discussed in Professor Stewart's Hoard of Mathematical Treasures]
Lagrange interpolation applies to any (numerical) sequence of any (finite) length; so, there are infinitely many ways to continue any given sequence - and, necessarily, one cannot say with absolute certainty that a given sequence will continue in one particular way, only.
Unless the person who set the puzzle says otherwise. Because if it's their puzzle, they know what answer they're looking for. And if you try to score easy marks on an exam by citing Wittgenstein, you're probably going to fail.
Of course, Lagrange polynomials - although interesting that they exist - are likely to be the least interesting way to continue a sequence. Certainly, the equivalent Lagrange polynomial isn't as meaningful or interesting as the circle division sequence; even though they both give the same first 5 terms.
And a Lagrange polynomial will probably never be the most obvious/aesthetically pleasing solution.
"[H]e conjectured that, though in principle all answers were equally probable, there might be something engraved on the human psyche [...] which guided most people to the same place, to the answer that seemed the simplest, clearest or most satisfying. He was definitely thinking that some kind of aesthetic principle was operating a priori which only let through a few possible answers for the final choice." - The Oxford Murders, Chptr 9Though, as seen in the Reddit puzzle, the simplest answer isn't necessarily the most correct one.
There Must Be Another Way
The important thing is, the existence of Lagrange polynomials 'proves' the Finite Rule Paradox for numerical sequences.
By the same logic, there must exist some functions, f(a,b,c,d), which satisfy the first 20 equations in the first puzzle, but give some values other than 2 for the last. Or, multiple functions which do give 2, but by different methods to the correct one.
Of course, such functions aren't likely to be easy to find. Especially when there are 20 'initial rules' to satisfy. But the fact that there are finitely many rules means it's possible.
Not that counting loops, or manipulating letters count as mathematical functions. But the point still stands.
At any rate, the important thing to remember is not to get too obsessed with the consequences of the Finite Rule Paradox. Or you may end up trying to lobotomise yourself to prove a theory.
Oatzy.
[What comes next - 3, 3, 5, 4, 4, 3, 5, 5, 4, ?]
Labels:
games,
hand-waving,
maths,
nerd,
over-thinking,
philosophy,
problem solving,
puzzles,
random
Saturday, February 18, 2012
Funny Story
So there's four of us at the pub, and we're playing the one word game. And we decide that, since this is the future, we should play a marathon game, via Twitter (complete with hashtag, #OneWordGame).
The One Word Game is where a group of people attempt to collectively tell a story, one word at a time. (Also known as Word at a Time Story).
At this point, we've been playing for about two weeks.
So what's our story? Before that, lets play a game.
Below are two 'stories' - one of them is our word-at-a-time story, the other has been randomly generated (using Markov text generation).
Which is which?
The correct answer is, ours is the first one - probably evident from the vulgar language.
As for the second one; I've discussed Markov generators before. In the simplest case, they work by randomly picking words based on how likely they are to follow the preceding word. This particular example was generated from the first four chapters of Dirk Gently's Holistic Detective Agency (Douglas Adams) using this site.
How does this relate to our game?
Well, the game was spread out over such a period of time that, usually, I'd forgotten the story much beyond the previous few words. So, I was picking my words based on what seemed like a coherent continuation to those few. Not unlike a Markov generator.
And, you have to admit, the two make about as much sense as each other.
At any rate, the game is still on-going..
Oatzy.
[What? I never claim our story was funny..]
The One Word Game is where a group of people attempt to collectively tell a story, one word at a time. (Also known as Word at a Time Story).
At this point, we've been playing for about two weeks.
So what's our story? Before that, lets play a game.
Below are two 'stories' - one of them is our word-at-a-time story, the other has been randomly generated (using Markov text generation).
Which is which?
When the wizard was masturbating upon a picture of your face which is ugly he said that a giant octopus with swashbuckling offspring however walrus munched on his wardrobe handles while reciting to Steve the only one who played the harmonica case under his umpalumpa cheese tasted spectacular when dipped in French wine and peanut butter making apples with a slightly tangy bumhole that had smelly teeth and pickled onions with slices of cherry on toast. Suddenly a massive caterpillar spawned from a seed which smells of turnip. When select acorns they exploded penises all turned into a
There was no further sound other than the soft tinkle of water. But in the middle of the surface area were encrusted with hard-caked earth but the girl who was confined them to perpetual darkness. This time there would be a very narrow neck extending about an inch above the rest. Could be worth a try I think he changes his name from time to time. Since it was meant to support an entire career However I
The correct answer is, ours is the first one - probably evident from the vulgar language.
As for the second one; I've discussed Markov generators before. In the simplest case, they work by randomly picking words based on how likely they are to follow the preceding word. This particular example was generated from the first four chapters of Dirk Gently's Holistic Detective Agency (Douglas Adams) using this site.
How does this relate to our game?
Well, the game was spread out over such a period of time that, usually, I'd forgotten the story much beyond the previous few words. So, I was picking my words based on what seemed like a coherent continuation to those few. Not unlike a Markov generator.
And, you have to admit, the two make about as much sense as each other.
At any rate, the game is still on-going..
Oatzy.
[What? I never claim our story was funny..]
Labels:
algorithm,
games,
markov chain,
maths-free,
random,
twitter,
writing
Subscribe to:
Posts (Atom)





























































