Wednesday 21 December 2016

Retarus Charity Game is Bridges of Koningberg

Julie sent me a link to this game, and said I should play it, as it supports Techtonica, which does great things (copying from their webpage): "Free tech training with living and childcare stipends and job placement for low-income women and non-binary adults. #BridgeTheTechGap".  Great, I'm on board with all that.

The game is essentially a more complicated version of the Bridges of Koningsberg problem.  There are nodes you need to pass through, and you can't retrace steps.  I did the first 17 levels before realizing I should get back to work, so I waited until I got home to finish all the levels (and bring Techtonica above 50000 stars.  I assume everybody gets some money, and that it's not some sort of monstrous three charities enter, one charity leaves thing).  It wasn't until level 29/30 that I realized that there had to be some sort of algorithm to solve these, and not just "magical topology powers."  So I sat down to write out what I was doing with diagrams and words, to make sense of it.


Here's the board.

Nodes with only two edges must be traveled through both, obviously.

So, we can block off any route that the two-edge nodes force us to exclude.
 I ended up largely solving this from the end, because I know a path must go to the terminal node, and that immediately forces the route into the two nodes in the bottom right corner.  Knowing that the intermediate node already had two edges on the path excludes any other path from using the other two edges.
And this provides the next big hint to how the algorithm works:  Once you've blocked edges from being part of the path, you turn some nodes that were previously connected to 3+ nodes into 2-edge nodes, so you can iterate the process again.  I've labeled the first node that is downgraded like this with a star.

Another round of marking and blocking gets the pseudo-path near the start point, but there aren't any clear ways to continue at that end.

Which is good that I noticed that in the upper right, the one edge I crossed off in stage 1a is connected to another edge that is also forbidden.  This makes the one node a 2-edge.  The square path at the top is redundant, so I chose one to exclude.  This connects those two corner nodes to the straight 2-edge in the middle.

I'm not 100% sure that my solution here is unique, but I'm not redrawing my diagrams again.  In any case, my logic here says that the start can't continue straight past that first target node, as it would then either need to go down (which leads to the end), or straight (back through that 2-edge), which isn't value.  I already excluded the bottom route, so it will need to turn up.  Blocking that route forces the empty node to have 2-edges, which connects the "to-the-end" path with the "back through the straight 2-edge" path.  This blocks the down edge out of the top-center node, forcing it to be a turn.

That forces the empty node above it to have one exit blocked, and then things fall into place. 
See, 50008.
And I think that this algorithm solves any similar game, although there are two edge cases.  The first is when the grid is over connected with edges.  This should always have trivial solutions that are obvious (and the early stages have extra connectivity, and that simply means that there are multiple paths that are valid.  Path length was not a constraint).  The second problem case is when the objective is not possible because it's a true Bridges of Koningsberg problem with no solution.

Tuesday 25 October 2016

Urban/Rural Populations


I saw this tweet this morning as part of a thread, and it didn't quite seem right to me.  The urban/rural factor is certainly one component, but it seems like race and income should also be major factors.  Unfortunately I didn't fully dig through the census site to find a data set that contains all those variables, so this is just an analysis of the census county based urbanization statistics and the 2012 presidential election outcomes on a county basis.  To be completely fair, this is only an analysis of the subset of those two data sets that have identical spelling/spacing for the county names, so 3069 counties from the 3222 available in the census sample.  A quick check seems to indicate that Alaska and Puerto Rice aren't matched between the two.

In any case, plotting the vote fraction for Barack Obama as a function of urban population fraction doesn't show a overly strong trend:

There is a lot of scatter in this relation, but there is an uptick for the most urban counties.
Smoothing the data by taking medians/robust sigma values in 0.02 wide bins shows the uptick clearer.

The purple "fit" is simply a constant with a break to linear placed to match the binned data.
This suggests that for the vast majority of counties, there's little change, but once a certain threshold of urbanization is reached, the median county becomes more likely to vote Democratic as it urbanizes.  A fuller description would try to disentangle this effect from racial, economic, educational, and simple population based components.

One interesting thing to note is that only about a third of the country lives in a county below that break point.  This plot is just the CDF of the population data plotted as a function of the urbanization metric.

Saturday 27 August 2016

I'm positive those elevators are making different sounds.

My apartment building has two elevators, descriptively labeled "elevator 1" and "elevator 2".  Recently, something happened, and elevator 2 was out of service for nearly a month.  When it started working again, the floor display didn't work, and the sounds sounded vaguely off.

Over the past two days, I've been trying to ride on both, so I could take videos that I could then extract the audio and do a power spectrum analysis on the beeps.

Those peaks are definitely different.
One thing to note is that the elevator 1 audio is much noisier than elevator 2.  I suspect this is partially my fault (holding the phone in different ways), but it points out another difference since elevator 2's problem: elevator 2 no longer has a functional fan.  There's also some noticeable impacts of my phone's microphone, with the dip at 5500 Hz and 8000 Hz.

The spikes are clearly the sound of the beep, and taking the peaks of the ~1000 Hz spike shows that elevator 1 has the peak at 1007.8125 Hz, with elevator 2 at 937.5 Hz (with these values being very dependent on the fairly low sampling frequency I'm using.  The samples are separated by 23.4375 Hz).

It's easy to scale elevator 2 by the ratio of the peaks.

This aligns the peaks at ~3000 Hz and ~4000 Hz.  The lower noise elevator 2 suggests a peak at ~2000 Hz as well.

So yes, those elevators are definitely making different sounds.



Sunday 21 August 2016

It seemed like there were more women's sports in this Olympics. Is that because they did better?

First up, it's entirely possible that this feeling is just because I never really watch any sports, and the news tends to focus on men's sports (with the possible exception of soccer).  Therefore, against that background, seeing any women's sports might just feel like an improvement.

However, given that NBC tape delayed everything, they had a large amount of leeway to tune what they programmed based on the results they already knew.  In this case, making events where the US won a medal more prominent might help increase ratings.

Conveniently, wikipedia lists all the results, and has a nice set of tables about how the US did.  So the question is: did the women's events produce more medals per participant than the men's events did?

To get a reasonable answer, I simply counted the number of medals won (split by type) per sport category, and divided by the number of participants in that category.  There are some complications with this method.  First, team events produce a higher fraction, so doing well in team events helps.  I've included each team member as a separate medal, and after some minor research, include the team members who didn't participate in the final.  I had them excluded on the first pass, but looking around it seems like those team members do get a medal as well.  This doesn't change the final numbers by much (mostly it just bumps swimming up even more).  Second, this ignores the Biles/Ledecky/Phelps effect, where one person dominates a sport heavily.  Still, normalizing by total participants ensures that it's not just a case of flooding a sport with lots of people, and winning that way.

So the results are:
Full sample average at 44.6%.

Full sample average at 51%.
Obviously there's lots of scatter.  Also obvious is that there is no good angle to rotate the labels to prevent overlaps.  In any case, all those nights of swimming, gymnastics and volleyball make sense, as those are sports that the women do well in.  Same for men's diving, although men's gymnastics might have been slightly over represented.

I also don't remember seeing any basketball, but that might have been sent to one of the other channels, and not the main NBC.  It's also possible based on the score differentials, that NBC just decided that those would be very boring games to watch, and skipped them for that reason.

Tuesday 19 July 2016

How does bedtime change with age?

I saw something earlier today that made me wonder how people's bedtime changes as they get older.  A quick google search pointed me to this study, which is probably the best/easiest to find data set that I'm likely to find just sitting online waiting.  There are a few obvious flaws:


  1. It relies on users of the Jawbone UP for the data sample.  I have never heard of this device, and I'm guessing a lot of people are in the same boat.
  2. Of those that do know of the Jawbone UP, they're probably younger than the average person, just because young people tend to use new tech at a higher rate.
  3. There's likely class/income/etc. biases, as not everyone has the money to spend on a $50 fitness doodle.
  4. There isn't actually any age data in the data set.
That last one isn't really that big of a show stopper.  

First step, scan through the source code to find the file that actually contains the data being used for the interactive map.  It's called counties.prod_.js, and nicely lists the county and state, bedtime in 12-hour format (minus 7, likely to ensure that there isn't a problem at the midnight boundary), as well as some other data I parsed out and saved in case I want to revisit something later.

Second step, trawl through the Census data for a county-by-county population breakdown, with age information.  That's here (although the full country data is actually 112MB, not the 11MB the page claims).  Then it's just a matter of pulling out the population data for 2015 (the closest match to the sleep data), setting the age for entire age groups at the midpoint, and calculating the weighted average age for each county (using the population in the age group as the weight).

So what's the result?
Other than me not truncating the best fit line.

The answer seems to be "Yes.  Bedtime is slightly earlier for older counties."  There's a bit of a plateau in the 16-20 range, but there's a reasonable decrease, even with the scatter.

Of course, using the full population isn't really the best, since the users of the Jawbone UP are likely adults, and not kids.  Redoing this analysis with just people older than 20 (due to the way the census data is binned into 5-year groups):


Basically the same, just shifted to an older age.


Wednesday 22 June 2016

Working to get a consistent set of census data.

It's annoying, because the census doesn't have a single format that they use for all historical data.
Particularly in the effectively arbitrary old age cuts.  Why were they fine during the 80s, then slightly worse in the 90s, and then really bad in the 2000s?  No clue, but that's the data I have from the census, so that's what I'm using.  
The gap is from the lack of data between 2010 when the previous decade estimates stop and 2014 when the future projections begin.
This is kind of interesting too.  I initially started just plotting the population with age=0, with the intent to visualize generations.  The baby boom is really obvious in the purple curve.  I then added samples at different ages, lagged to use a consistent time base.  This thought this would give a probe of immigration, but that doesn't seem to be the case, as there aren't any major gaps in the first three samples in the 1850-1900 range.  I think the sag in the age=60 is just life expectancy issues.  That's also clearly apparent in the beyond 2000 area as well.  The projection yielding the 2014-2060 isn't predicting that to improve too much it seems.

Friday 29 April 2016

Dumbing of Age characters

I read Dumbing of Age, but I don't really pay super close attention to things.  There are 1773 comics as of today, and it's hard to keep track of stories that long over a long period of time.  There are also a lot of characters that I have trouble keeping track of.  "Wait, how do you know how many comics it has?"

Today's story is all "Amber pushes Danny away because she's angry and making bad decisions."  My thought was, "Who else is Amber's friend?  Joyce, right?"

amber danny 91 0.408072 0.325
amazi-girl danny 40 0.3125 0.142857
amber ethan 60 0.269058 0.285714
amber dina 53 0.237668 0.24424
amazi-girl dorothy 24 0.1875 0.0526316
amazi-girl joyce 19 0.148438 0.0255376
amazi-girl walky 18 0.140625 0.0392157
amber joyce 30 0.134529 0.0403226
amazi-girl sal 16 0.125 0.0695652
amazi-girl amber 15 0.117188 0.0672646

Ethan, Dina, and then Dorothy maybe.  Yeah, I was wondering this enough that I wrote a bot to scrape all the comics to pull out the tags that are applied to each comic, since conveniently list all the characters appearing in that comic.  Then, I looked at the pairwise matches in that set and dumped them out in "character A", "character B", "number of appearances together", and number of appearances for each of A and B, converted here into fractions of all appearances that are together.

So that was waste of time.  I also have dates, and you can extract chapters from the urls (which I saved), so more analysis could be done (my thought was to try to do some sort of connection map), but I still haven't eaten dinner.  Also, I discovered that this comic is the only one to have no characters appearing, so had to handle that case (SOLO_APPEAR is in that with SOLO_APPEAR).