Thursday, August 8, 2013

Credit Card Math

Credit cards are an important part of our society [citation needed], and come with an almost unlimited variety of options and perks between them. Some cards have low interest rates, others give you fancy rewards; some cards require you to have a high income, but others are free. The choices really are nearly limitless.

One of my favorite perks offered by credit cards is the ability to get cash back as a fraction of the amount you spend using the card (most likely this is actually a clever ploy to get people to put lots of money on their cards in order to run up large amounts of interest, but we'll ignore that for now). Getting straight cash can be easier to deal with than travel rewards unless you happen to use the same Miles system, and provides you with a lovely sum of money to play with usually once a year.

But not all cards are created equal. If you take a quick look at the terms and conditions of some of the major cash-back cards that are offered, you might get a graph that looks something like this (please note: Scotiabank's cards offered different rates for different purchases, such as groceries, gas, etc. As a result, they weren't included as it made direct unambiguous comparisons impossible.):



Whoa. My mistake. I set the axes equal to each other, and it almost made it look like the cash you'd get back was very nearly insignificant in the scheme of things. Silly me, that can't possibly be true...



Ok, whew, that's much better. A few things to note right off the bat:


  • The graph includes annual fees, which explains why some cards start off negative
  • The analysis assumes that the only important differences between cards are the cash back rewards and annual fees, and ignores differences in interest rates, maximum credit limits, minimum income requirements, etc.
  • At some point in the future, CIBC's thesaurus is likely to run out of cool sounding words to add on to credit card names
The clear winner here is the Capital One card, which provides a free 1.0% cash back system which can be withdrawn from at any time, plus an additional 50% of the accrued cash back once a year (for an equivalent 1.5% cash back system). If we ignore it, though, there is actually a very neat transition between the different cards based on how much you are likely to spend each year:

If you spend:

$0-7,500: Get the TD Cash Back MasterCard. No annual fee and 0.75% cash back is a pretty decent deal, all in all. Take it and run! This is way better than its cousin, the TD Gold Elite Visa, which offers 1% but has a $79 annual fee. This range works out to credit card bills of less $625/month, and if you feel you pay more than that on average, why not try...

$7,500-24,000: The CIBC Dividend Visa. This one also has no annual fee, but its pay structure is tiered (0.25% up to the first $1,500, 0.50% for the next $1,500, and 1% after that), so it takes a while to catch up to the TD card from before. This card is the most profitable for quite a range, though! In the interest of fairness, it is worth mentioning that the RBC Visa Cash Back card is very close and works out to only $0.25 less cash back a year over this range, as the card is a constant 1% reward but cost a $19 annual fee. Its rewards, however, are capped after $25,000 spending per year, so if you're unsure of your spending you're still better to stick with the CIBC card. And if you're going outside of that range anyway, you may as well consider...

$24,000-35,300: The BMO CashBack World MasterCard. (Note how the card names get longer at higher price ranges?) This card offers an aggressive 1.25% cash back, but was offset by a $79 annual fee. If you feel like even that isn't enough to put on a card, go for either...

$35,300-50,000: The CIBC Dividend Infinite or Dividend Unlimited World Elite Visa. (Seriously how ridiculous are these names?). These cards are both tied over this range, and both cards have identical tiered reward schemes and $79 annual fees. Unfortunately, the Dividend Infinite isn't all that infinite, and its rewards are capped after $50,000 of spending, at which point...

$50,000-94,000: The Unlimited World Elite takes over solo in all its shining glory, until...

$94,000 and beyond: The BMO CashBack catches right back up. It's able to do this because its rewards run at a higher rate than the highest tier of the Unlimited World Elite. After this, there's really no stopping it (apart from, of course, the aforementioned Capital One card, which is laughing at all these other cards from the finish line). 

Really, though, if you're making enough money to be able to put $94,000 on a single credit card every year, you probably don't particularly care about which card offers you a handful of dollars more than another one.

Tuesday, July 30, 2013

In Defense of Fluoride

Recently, the #yegvote twitter feed has been occupied with a lot of discussion about water fluoridation, particularly from and against mayoral candidate Curtis Penner (including him tweeting a bibliography of 70 consecutive journal articles early Thursday morning). As has been pointed out, a lot of the twitter debate has devolved into personal attacks on both sides, but the question of water fluoridation is important and worth discussing on its merits alone.

First of all, some fun fluoridation facts!
  • low concentrations of fluoride in your mouth reduces the rate at which your enamel breaks down,
  • fluoride is often naturally present in water all over the world in different concentrations,
  • about 5% of the world's population has fluoride added to the water supply at low concentrations (including Edmonton), and
  • different studies have shown that the presence of fluoride in water can reduce cavities by between 27 and 40% relative to regular brushing

The ideal concentration for fluoride in water appears to be somewhere between 0.5-1.0 milligrams per liter. This is lower than the natural levels found in lots of communities, and in many parts of the world fluoride concentrations are reduced or even eliminated before being pumped into municipal water supplies.
 
Mr. Penner's platform for mayor includes a lengthy paragraph against water fluoridation, with the first argument referencing the Material Safety Data Sheet for the chemical that's being used in Edmonton, hydrofluosilicic acid. His claim appears to be that since this chemical is listed as corrosive and dangerously reactive, we shouldn't have anything to do with it. This is at best a red herring, and not an argument at all - at the high concentrations that the chemical is stored, very nearly anything is poisonous. We add chlorine to our water supply explicitly to kill living organisms, and its MSDS warnings are even more severe than fluoride. This isn't to say that the concentrated version of the chemical is ok to drink, but the final product in our taps is millions of times less concentrated than the solution the data sheet refers to.
 
 Dropping evil-sounding chemical names and referring to alarming MSDS data sheets can't form an argument in and ofitself. People regularly consume citric acid and acetic acid, whose MSDS data sheets name them as flammable and corrosive and include pages of toxicity warnings, but we enjoy them as orange juice and vinegar. The fact of the matter is that data sheets have to cover all possibilities and naturally make anything sound evil - even the data sheet for plain old boring water has lethal dosage information.
 
Mr. Penner then goes on to reference this Harvard meta-analysis on the effects of fluoride on children. His claim is that the study indicates that "children who do not drink fluoride have a 20% better chance of having high intelligence, whereas those who do drink fluoride have a 9% better chance of developing mental retardation." Oddly enough, the words "mental retardation" and the term "20%" don't show up in the journal article at all. In fact, they write instead that their "results support the possibility of adverse effects of fluoride exposures on children's neurodevelopment." Their major finding, actually, is that children who had "high exposure" to fluoride had an IQ that was0.45 points lower than reference children.
 
What constituted "high exposure"? Turns out it ranged from about 3-12 mg/L for most studies. Their reference points - the points the study considered not exposed to fluoride - were between 0.34-2.35 mg/L. In fact, some of the reference healthy populations were drinking water that was two to three timesmore fluoridated than what would ever be allowed in Edmonton's water. An overwhelming majority of the over 70 studies tweeted by Mr. Penner that supposedly support his position deal with high concentrations of naturally-occurring fluoride in India or China, not low concentrations carefully monitored in Canada. So while it most likely is true that high concentrations of fluoride can cause adverse effects, it is far more likely that increasing your intake of any substance by a factor of 5 to 10 over what scientists recommend would be similarly poisonous.
 
It has also been pointed out that Calgary stopped water fluoridation in 2011. This is true - and already has dentists raising alarms about increases in cavities (and don't forget, dentists get less work if people have fewer cavities. They must be really concerned...).
 
With studies showing the positive effects of low concentrations of fluoride, and other studies showing adverse effects only at levels significantly higher than Edmonton's, the scientific argument for those opposing the current fluoride program doesn't seem that strong. The remaining argument is one of policy - is it morally acceptable to add a substance to the water with the goal of treating an entire population?
 
While this is mostly a matter of personal opinion, it is interesting to consider the city’s role and responsibilities in providing water to the public. There is a very defined cost vs. benefit analysis to be made when treating water. No treatment and you'll kill people, add some filters and you'll kill a bunch less - but after a certain point there are massively diminishing returns with regards to how expensive it will be to make water just a little bit safer to drink, with the extreme end being an economically devastating plan to provide everyone with pure distilled water (which, actually, maybe isn't all that ideal after all).
 
If the factors behind water treatment were only economical in nature, then the benefits of saving families hundreds of dollars a year for dental work far outweigh the cost of fluoride at less thana dollar per person. Fluoridation provides a blanket benefit to everyone who drinks tap water, providing unique aid to those who cannot afford regular dental work or perhaps even other sources of fluoride like toothpaste.
 
Water fluoridation has been included with vaccinations and family planning as among the ten greatest public health achievements by the Centers for Disease Control and Prevention, and is supported by major health and dental organizations. As mentioned before, the up to 40% decrease in cavities from fluoridated water is in addition to any benefit already achieved from brushing your teeth. Any policy that so easily and effectively provides this much assistance against such a preventable condition as cavities is surely worth keeping around.
 
As long as the fluoride program that Edmonton currently uses continues to be beneficial, and the low concentrations that are used continue to not be dangerous, the program should not be stopped.

Wednesday, June 26, 2013

NHL 2013 Wrap-up

So the Stanley Cup has been awarded. Congrats to the Chicago Blackhawks!

As I previously mentioned, I've been running a model to try to predict the NHL finals for a few years, and I've been running it again this year. The model outputs looked like this over the last few weeks:

Again, as the time through the playoffs progresses (x-axis), the probability of a given team winning is displayed as the height of the team's bar (so that at any time all teams' probabilities add up to 100%). Originally I had predicted the Senators had the highest chance of winning, with the Bruins in second and the Blackhawks in third. Fortunately enough, two out of those top three actually made it somewhere...

What's important though is to check if I'm anywhere near accurate. It's convenient, I suppose, to say that my top teams all did fairly well, but how well did I actually do? Take a look at this (format stolen straight-up from Wikipedia):

Conference Quarterfinals Conference Semifinals Conference Finals Stanley Cup Finals
1 Pittsburgh Penguins (64.2%) 4 1 Pittsburgh Penguins (22.8%) 4
8 New York Islanders (35.8%) 2 7 Ottawa Senators (77.2%) 1
2 Montreal Canadiens (15.9%) 1 Eastern Conference
7 Ottawa Senators (84.1%) 4
1 Pittsburgh Penguins (31.7%) 0
4 Boston Bruins (68.3%) 4
3 Washington Capitals (37.9%) 3
6 New York Rangers (62.1%) 4
4 Boston Bruins (76.7%) 4 4 Boston Bruins (59.8%) 4
5 Toronto Maple Leafs (23.3%) 3 6 New York Rangers (40.2%) 1
E4 Boston Bruins (52.4%) 2
(Pairings are re-seeded after the first round.)
W1 Chicago Blackhawks (47.6%) 4
1 Chicago Blackhawks (77.0%) 4 1 Chicago Blackhawks (58.2%) 4
8 Minnesota Wild (23.0%) 1 7 Detroit Red Wings (41.8%) 3
2 Anaheim Ducks (40.9%) 3
7 Detroit Red Wings (59.1%) 4
1 Chicago Blackhawks (70.3%) 4
5 Los Angeles Kings (29.7%) 1
3 Vancouver Canucks (37.8%) 0
6 San Jose Sharks (62.2%) 4 Western Conference
St. Louis Blues (43.1%) 2 5 Los Angeles Kings (34.3%) 4
5 Los Angeles Kings (56.9%) 4 6 San Jose Sharks (65.7%) 3
In each bracket, the percentages are the probabilities assigned by my model. A first glance analysis of this bracket shows that in 12/15 of the series, the teams I assigned the highest odds of winning to ended up winning (exceptions are Kings v. Sharks, Penguins v. Senators, and awkwardly Bruins v. Blackhawks).

If someone was flipping a coin, they would expect to predict 12/15 or better only about 1.76% of the time. This percentage is known as a p-value, and a standard convention in, say, medical experiments, is to use a value of 5% for determining if the results are significant or just arose as a matter of chance. Regardless of if this is a reasonable standard or not (this could allow 5% of the results of studies to be due to chance, and is a debate worth having), a "conventional" medical study with a p-value of 1.76% would likely be accepted, leading me to humbly suggest that my model this year was statistically significantly above chance when it comes to predicting the outcomes of series (though not necessarily the playoffs as a whole).

Another way of measuring the accuracy is by using a Brier score (similar to what I had in my previous post) to actually measure the accuracy of the probabilities assigned. It's all well and good to say the teams with the best odds often won, but how good were the odds that I assigned?

The average Brier score for all 15 series was 0.8182 out of 1, where a 50/50 guess for any given series would give a value of 0.75. My results are basically the equivalent of assigning a value of 57% at the outset to every team that actually ends up winning (as opposed to a random value of 50%) - in other words perhaps not definitively accurate and clairvoyant, but still likely significant.

One last thing to try is to plot the overall Brier score over the entirety of the playoffs, and compare that to random chance. It would look like this:


That's actually not so good. Compared to last year, there's virtually no difference between the two. At least in the parts where the two are different (mid-May, for instance), the model is higher than chance, but everywhere else is about even. The similarity between the two suggests that there weren't a lot of cases where series ended in remarkable against-the-odds comebacks, though when one did (Blackhawks v. Red Wings, for instance) it was able to be better predicted by my model than chance.

What's encouraging about all of this is that between all of the different ways of analyzing the accuracy, they converge on suggesting that the parameters that my model uses are reasonable and significant. Also, if I had bet on the first round, I would have made a killing. Too bad...

Monday, June 24, 2013

Spring Weather


I've now been doing this weather analysis of mine for 365 days, and what a year it's been. That's 365 days of exciting weather, culminating with tornado watches a couple of weeks ago and the horrible flooding around Southern Alberta going on right now.

The end of the year also marks the end of my experiment with tracking weather stations, which I previously reported for the summer, fall, and winter. The scores for spring (out of 100) were:

  • Weather Network: 72.08
  • Weather Channel: 69.56
  • Global Weather: 68.52
  • TimeandDate.com: 68.47
  • Environment Canada: 65.96
  • CTV Weather: 64.31
Just in case you didn't have the previous values memorized, this is how they've changed over the last season:


A couple of things about this are really quite noteworthy. First of all, the forecasters are all a lot closer together than they were in the previous two seasons. Good for them? Also, TimeandDate.com and CTV had massive improvements to their scores, with TimeandDate.com jumping two ranks higher. For the fourth season in a row, the Weather Network and the Weather Channel both hold the number 1 and number 2 spot.
As I mentioned before, one of the issues with this analysis is that CTV Weather only presents numerical values for POP up to four days in the future. If we only take these four days for all weather stations, the results change fairly drastically:

  • Weather Network: 74.47
  • CTV Weather: 71.09
  • Global Weather: 70.63
  • Weather Channel: 70.46
  • TimeandDate.com: 69.84
  • Environment Canada: 67.74
The overall score graphs for spring look like this:


As always, graphs like this highlight the fact that weather forecasting gets increasingly more difficult the farther into the future you attempt to predict. Any given day into the future only has a difference in score of about 10 points between any station, and all six weather forecasters have a nearly identical trend decreasing with time.

Since it's now been a year, I can also share the overall yearly scores for each forecaster. They are:
  • Weather Network: 76.39
  • Weather Channel: 73.92
  • Global Weather: 72.00
  • Environment Canada: 71.20
  • TimeandDate.com: 70.02
  • CTV Weather: 60.99*
*CTV Weather isn't a fair comparison as the analysis was started only in the fall, and the best season for all the other forecasters was summer. This probably messes up their average scores a wee bit...

Aside from the difference in numbers of days accounted for and length of analysis, one of the other major differences between stations is the presentation of POP values. For instance, Environment Canada has listed policies that state that if their calculated chance of precipitation is between 30 and 70% it will be listed (at increments of 10%), but 50% isn't allowed to be listed. This forces their values to be listed as either 0, 30, 40, 60, 70, or 100. CTV has a similar policy, but the other stations present values all the way between 0 and 100 at increments of 10.

The problem with this is that all forecasters could predict a 10% chance of rain, and if it does end up raining they'd receive a pretty bad score for that prediction. However, Environment Canada and CTV would receive a much worse score, because they would have published a value of 0%.

If we account for this, the new net yearly scores become:
  • Weather Network: 74.61 (-1.78)
  • Weather Channel: 71.94 (-1.98)
  • Environment Canada: 71.20 (No change)
  • Global Weather: 69.68 (-2.32)
  • TimeandDate.com: 68.14 (-1.87)
  • CTV: 60.99 (No change)
So yeah, changes in how values are presented definitely have an effect on the scores. This isn't quite enough to counteract the full difference between the Weather Network and everyone else, but it does tighten things up a little bit.

One last thing. When a weather station gives a POP value, what is the actual chance of any rain falling?


Ideally, the graph would be a straight line from bottom left to top right, but almost exclusively the bars on the graph are higher than what would be expected.

Part of the reason for this is that the definition of precipitation that I used was similar to how Environment Canada defines POP - the "chance that measurable precipitation (0.2 mm of rain or 0.2 cm of snow) will fall on “any random point of the forecast region” during the forecast period." 0.2 mm isn't a ton of rain, really, so what if I bump the values up to 1 mm?


Well that's quite the improvement. In fact, it substantially helps the scores for all weather stations by about 4-7 points each. What's cool is how close the bars are for low values of POP, and how they tend to diverge at higher values.

Anyway, this has been a pretty cool project for me for the last year. Hope you had fun too!

Wednesday, June 19, 2013

Edmonton Transit Hotspots

Last week's post about optimized traveling around Edmonton was pretty fun, right? It actually got me thinking about some different ways of looking at Edmonton's transportation systems.

Last time was taxis between bars, so I thought this time a cool idea would be the ETS, with its fun combination of buses and LRTs. Of course, now that my blog is the number 1 Google result for "Mathematically Optimized Pub Crawl" I figured I'd expand my horizons a little bit. Without any further delay, why not take a look at this:

This is a heat map of our fair city, based on transit time required to get to our biggest tourist attraction (West Edmonton Mall). The colours range from green (less than 10 minutes) to red (more than two hours). Almost every single neighborhood is within a one hour bus/train/walk (or combination thereof) from WEM apart from the very north-east of Edmonton, but there's a little island around Clareview station of reduced travel time. Cool, eh?

Next up is Century Park station:

Century Park's map is pretty cool. The south side of the city can get there really quickly (particularly close to 23rd Ave in Mill Woods), and along the LRT line in the north is pretty well defined too. Personally I really like the Moat o' Excessive Transit Time that surrounds downtown St. Albert, and the long wait times west of the river on the south side (apparently the buses there don't like the Henday...).

For something completely different, how about Rexall Place?

Perhaps not surprisingly at all, Rexall Place is very easily accessible by public transit. I'd post maps for Commonwealth Stadium, but it would be virtually identical. The LRT line extending as far south as it does really helps, as there are enough relatively direct buses both from St. Albert and West Edmonton that drop people off close to an LRT line. When it comes to great coverage for the city, though, you really can't beat...

...the University of Alberta. Man it's almost like they really wanted it to be easy for people to get to class, eh? Pretty much the only way you'd have a tough time getting into campus (tough being more than about an hour) is if you live in the no-man's-land between Edmonton and St. Albert. Also, we can see the effectiveness of Clareview LRT again, in the top right hand corner.

Speaking of St. Albert...

What's cool about the St. Albert map is that the direct bus lines to downtown, WEM, and the U of A are clearly visible, but going any further than that is pretty much a bad idea.

I like movies. I also like being treated like a very important person. You know where Edmonton's only VIP movie theater is? Windermere.

...but maybe it's better just to drive there.

Monday, June 10, 2013

Traveling Drunkenman Problem

The Traveling Salesman Problem is a classic in computing. Given a list of cities and distances between each city, what is the shortest path that lets a salesperson visit each city exactly once and return to the starting point?

It's a bit of a tough problem at high numbers of cities, due to the exponentially increasing number of paths one can take each time a new city has been added. Because of that, the problem is useful in testing optimization processes that don't rely on simply checking all possible combinations by brute force.

What's more fun is to adapt it in such a way as to optimize, say, your drinking habits. For instance, what's the shortest path to visit the 10 best pubs in Edmonton and return to where you started?

In order to be somewhat objective, I used the Yelp list of the 10 highest rated pubs. Of course, when you're on a mathematically optimized pubcrawl, it's important to not drink and drive - so distances are converted into dollar figures following the City of Edmonton bylaws for taxis.

2 pubs: $19.40 round trip

If we start with the #1 and #2 pubs on the list, it would cost just under $20 to visit them both by taxi. No computer necessary - there's really only one way to get there and back (unless the taxi driver takes you on an expensive sight-seeing tour).

3 pubs: $30.40-33.40 round trip

There are 6 different ways to do this trip once you'd added #3 on the list, but it's not at all difficult to check out all the combinations for yourself and plan it without using a computer. Normally there wouldn't be any variation in the price (as you'd just be traveling along the same three sides of a triangle), but in the case of downtown Edmonton with all its one-way streets, some ways to drive between places end up being slightly more expensive than others.

4: $35.40-39.40

Things get a wee bit more fun here - 24 combinations to deal with, with 12 different paths between pubs to take into account.

5: $39.60-43.60

120 combinations, 20 paths between pubs. Still not too much variation between the paths yet.

6: $42.60-58.80

720 combinations, 30 paths between them. At this point there are relatively significant savings to be had by trying to optimize, but 720 combinations would be an awful lot to check by hand. The major reason why there is suddenly so much variation between different paths is that the 6th pub on the Yelp list is very close to the first. Paths that take advantage of this do well, and paths that ignore it  end up costing a lot more than necessary.

7: $46.00-63.80

5,040 combinations. Things are getting really exciting now.

8: $49.40-72.60

40,320 combinations formed from 56 different possible paths between the 8 pubs.

9: $53.00-82.40

362,880 combinations. Let's just say that's an awful lot of things to check by hand (also messes with Excel a wee bit...).

10: $57.00-98.40

3,628,800 combinations. Again, quite a bit of a burden on poor Excel. But what's cool is that by optimizing your path between 10 of Edmonton's most popular pubs, you can save more than $40 in taxi fees.

This optimized pubcrawl through these pubs would look something like this:


What became readily obvious was the sheer amount of computing power necessary to check all of these. I have Excel files that are over half a gigabyte large in order to calculate the 4 million different combinations. Solutions to larger sized problems typically require other cool algorithms that I didn't feel like actually studying to learn.

Enjoy! Hope to see you out on the nerdiest tour of Edmonton's finest pubs next weekend!

Special thanks to Finbarr Timbers and Elizabeth Croteau for the idea, and Daniel Johns for helping me out of a tricky spot with VBA.

Sunday, June 2, 2013

Beer Math

Over the last week I had the privilege of going both to MKT and Underground Tap and Grill in Edmonton. I had a very pleasant experience at both. They are both known for having large beer menus, so I took the opportunity to try out a couple of new beers.

Of course, one of the problems with such a large selection on tap is that it's easy to get overwhelmed by the sheer number of options. Underground, for instance, has 72 beers on tap - how are you supposed to pick a new one worth trying? A knowledgeable beer connoisseur could probably read the descriptions and ask pointed questions, but an extreme enginerd could just use math.

Yay!

Take a look at this menu. Each beer is offered at a different combination of volume, alcohol content, and cost.


Based on this, it's actually pretty straightforward to find which beer has the most alcohol per volume at the lowest cost. In fact, it would look something like this:

First of all, that's quite the variation! You could get almost three times as drunk off of the Salmon Fly Honey Rye as the Pilsner for the same cost. My goodness - I would never advocate for such a thing.

Of course, there are many factors that influence the cost of alcohol. One of them is the mandatory  minimum alcohol prices legislated by the Government of Alberta. Their rules work out to something like this:

  • Spirits/Liqueurs: $2.75/oz.
  • Wine (by the glass): $0.35/oz.
  • Draft Beer: $0.16/oz.
  • Beer/Cider/Cooler (bottle or can): $2.75 each
Assuming an average alcohol content of 5%, a can of beer selling at a minimum of $2.75 works out to $4.58 per ounce of alcohol - more expensive, in fact, than two of the beers sold at Underground! Before you think you can use this to break the law and stick it to the man ($0.12 per ounce of alcohol at a time!), it's important to point out that as these are draft beers, they are still selling for more than $0.16 per ounce of beer, and as such are still completely legal. In fact, the minimum alcohol cost for draft beer works out to $3.20 per ounce of of alcohol. Basically, if you're paying anything more than that, you're getting ripped off...

And as a quick aside before we finish, according to the Alberta rules, wine is the cheapest way to get drunk at $2.92 per ounce of alcohol. At this rate, it would only take $28.26 worth of wine to kill me by alcohol poisoning. How cost effective!