Archive for the ‘Programming’ category

Calculating NCAA March Madness Bracket using Chess ELO predictive scoring

March 13th, 2012

Hokay, So I had a lot of fun with this. Let me start by saying I’m not the first to do this.  However, after a lot of Googling, I found surprisingly few NCAA Bracket predictions using the ELO system. Those that I did weren’t transparent about the data they used.  I wanted to do it so I could see the result with data I knew, and as a good excuse to code some Ruby.

Overview

First, the ELO system. The ELO system is a way of calculating the relative skill between two players (and thus a probability for one to win in a future match). Wikipedia has an excellent write-up including the history and the math behind the scoring.  In a nutshell, it calculates an expected result based on the rankings of the two teams.  It then compares the actual result to the expected one, and adjusts each player’s rank accordingly (increasing it for the winner, subtracting for the looser).  If a favored team wins, the adjustment is small.  If an underdog wins, the adjustment is larger.

Assumptions

Your conclusion is only as good as your assumptions, and we’ll we need to make a few.  Most of the work is done by choosing the ELO system, it’s one of the simpler systems for relative rankings.  For our data, we’re only interested in what two teams played, and which team won.  We ignore the final score, whether traveling or home, players used, fouls, timeouts, point distribution, etc.  Also, for the purposes of this calculation, if the game went into overtime, I count it as a tie.  That’s probably the most debatable assumption, but I feel it’s valid because it essentially means after an hour, the two teams displayed equal skill.

Data

So this turned out to be the hardest part. I wanted to use the 2011-2012 season as my dataset. After a half-hour of Googling, I couldn’t find the data in a well-structured format (read: csv or xls).  So I had to resort to web scraping.

The best website I could find was the official NCAA site.  They have a page with the Men’s Division 1 listing by team, where you can click into each team, to see a game history (amongst other things). Let’s grab it.

wget --mirror "http://stats.ncaa.org/team/
             inst_team_list?sport_code=MBB&division=1"

Well that was fun.  wget was a little overzealous, so I moved all the relevent pages (those starting with 10740) into their own folder.  I then wrote a Ruby script to organize the data, clean it up, and write it to a file.

a

The output from that script is a beautifully structured file, if I do say so myself.  Well, at least from a data perspective.

a

Number Crunching

Okay, so now it’s time to actually calculate the elos.  I basically wrote a straight implementation of the math as presented on WikipedidaThe second ruby script, reads in the scores, calculates the adjustments, and keeps track of the changes.

Here the output while it’s running:

a

lastly, it sorts the results and writes them to a file.

Conclusions

Here it is:

Finall ELO Predicted Bracket

a

We can see that the comparing our generated results to the seeded rankings, there’s a lot of overlap.  The top three teams are predicted exactly as seeded.  However, from there the list diverges quite a bit. For example, Murray St. is expected to take the west, but didn’t get seeded so hot.

So, if this wins, I’ll get some money from our office bracket pool. Which is nice. And if it doesn’t, it will be proof that my computer messed up on the calculation.

—–EDIT——

My initial calculations didn’t account for the order in which the games were played.  Although I didn’t think this would have a big influence, running the script on a computer that lists the data files in a different order actually made some big differences.  Thus, I changed the data scraping script to account for the dates, and calculate all ELO scores in the order that they were played.  This should result in a more accurate, and reproducable result.  Here is the updated script, and the updated final result.  Here’s my Final Bracket.

Re-Ordering select list elements with jQuery

February 21st, 2011

So, I came across an interesting development problem.  I was embedding a ‘select’ list from a 3rd party web service, and I wanted to re-order the items shown.  More specifically, I wanted the most commonly selected elements to be at the top of the list.  Because I couldn’t access the raw HTML, directly changing the order was not an option.  jQuery to the rescue!  Here’s a small snippet I came up with that did just the trick:

$('option[value='myVal']').detach().prepend('select.mySelect');

Viola!  Way easy!

Buddypress Group Invite by Email

March 17th, 2010

Here’s another small plugin I wrote: Group Invite By Email.  Basically, it adds an email field to the bottom of the usual “send-invites” screen.  Then, when you click “send invites” it sends the person a nice little email explaining they’ve been invited to your group, and giving them a link to register on your site.

Once they complete the registration, it automatically adds them to the group.  It does this by checking their email address against a list of outstanding invites.  This removes the extra hassle of needing to invite them to the group after you’ve gotten them to sign up, and them needing to approve your group invitation.

In the future, this functionality will likely be bundled with another plugin already existing: Invite Anyone. That plugin allows you to invite non-friends to your group; this one allows you to invite non-members.

Here it is:
buddypress-group-invite-email (.zip)

Buddypress Default Forum Topic

March 14th, 2010

This plugin I wrote that creates a default topic for each group forum in BuddyPress.  Please note, you have to open up the PHP file, and replace the values for the default title and text with what you would like to use.  Even if you are unfamiliar with PHP, this should be very straightforward.

To Install:
-Extract and upload the files to your wp-content/plugins/ folder.
-Go to the “Plugins” screen on the admin dashboard, and activate the plugin
-Enjoy.

Here it is:
default-forum-topic (.zip)

Buddypress Group Document Store

December 24th, 2009

I just uploaded my largest wordpress plugin yet: Buddypress Group Documents!

Group Documents creates a page within each BuddyPress group to upload and store documents.  Documents can be edited and deleted either by the owner or by the group administrator.  Document activity is logged in the main activity stream, and is also tied to the user and group activity streams.  The site administrator can set filters on file extensions, and soon (in the next update) users can set email notification preferences.  There is also a “Recent Uploads” widget than can be used to show any number of uploaded documents.

Get it while it’s hot!
WordPress.org Plugin Download Page

Installation
Make sure WPMU and BuddyPress are installed and active.
Copy the plugin folder buddypress-group-documents/ into /wp-content/plugins/

Browse to the plugin administration screen and activate the plugin.

There will now be a “Group Documents” menu item under the “BuddyPress” menu.  Here you will find a list of all file extensions allowed for uploaded files.  Please check and make sure the list suits you.

Please don’t hesitate to contact me, especially if you run into trouble.  I will respond promptly.  email hidden; JavaScript is required

Buddypress jQuery is not defined error

December 16th, 2009

While developing a Buddypress plugin, I was running into a problem where none of my Javascript was working. Instead, Firebug was telling me: “jQuery is not defined”.  This was quite frustrating, as I could verify that the jQuery file was indeed, being loaded.  While a few other people received the same error for different reasons (corrupt files, etc.) I did not quickly find the solution for my problem.  Here it is:

Problem: Buddypress calls your plugin’s JS files BEFORE jQuery files.
This applies when you use the function wp_enqueue_script() – as you should.

Solution: Tell wp_enqueue_script your file depends on jQuery.
It turns out, wp_enqueue_script() takes a few optional parameters – the third being an array of other js files yours is dependent on.

Here’s how the call looks after the change:
wp_enqueue_script(‘my-js-file’,’my-file-path.js’,array(‘jquery’));

Viola!  Your js file is now loaded after jQuery and you’re good to go.

Using Custom Profile Field Data in Buddypress Templates

November 18th, 2009

I ran into what I thought would be fairly common need for Buddypress theme development, the ability to pull data from a custom user profile field and display it. After searching a bit, I could only find way to pull either random fields, or all of the fields together. So after digging around in the core files, I found the pieces to put together my own function for the task.

1. Create the file “bp-custom.php” and place it directly in the plugins folder. Note, do not place it in the “buddypress” folder, otherwise it will be overwritten when upgrading. If the file exists, simply append to it rather than creating a new one.

2. Paste the following function in:

function bp_the_site_member_profile_data($field_name = '', $echo = 1) {
  if( !$field_name ) {
    return false;
  }
  global $site_members_template;
  if( function_exists( 'xprofile_get_field_data' ) ) {
    $data = xprofile_get_field_data( $field_name,
                                     $site_members_template->member->id);
    if( $echo ) {
      echo "$data";
      return true;
    } else {
      return $data;
    }
  }
}

3. You can now use the function ‘bp_the_site_member_profile_data(‘MyField’) in your templates (of course, substituting ‘MyField’ with the one you’d like to use). Note, this must be place inside a ‘Members Loop’ so that the $site_members_template variable is populated.  Odds are, you’re inserting this near other profile data already in the loop, so there should be no problems.

UPDATE:
As of BuddyPress 1.2, this functionality has been included. While in the members loop, you can use the included function: ‘bp_member_profile_data( ‘field=the field name’ )’ to much the same effect.

Dynamic Display of the Alphabet with PHP

December 23rd, 2008

Here’s a neat trick I recently used: Say you want to the display the alphabet on your web page.  The most likely scenario being for paging links to organize a directory of people or businesses. PHP has a chr() function, which displays the ASCII character for any given integer.

Rather than looping through an array with 26 values, or worse yet, typing out 26 lines of code, just loop through the display code 26 times.

<?php
for ($i=65; $i<=90; $i++) {
 echo chr($i);
}
?>

For those not familiar with ASCII mappings, values 65-90 represent the uppercase letters A-Z. Alternately, you could use the values 97-122 for lowercase a-z.  If you wanted to mix the two (say to display uppercase, but use lowercase in the link) just use the strtoupper() or strtolower() functions inside the loop.  Here’s a more applicable sample:

<?php
for ($i=97; $i<=122; $i++) {
 $x = chr($i);
 echo '<a href="memberlist.php?alpha=' . $x . '>' . strtoupper($x) . '</a>';
}
?>

You can see an example of both applied here.

Coding Music

December 10th, 2008

Music is a big part of programming.  Nothing gets me zen faster than immersion in a good back beat.  As such, I’d like to pass on a few genres and artists that I think stand out as being conducive to cranking out code.

Trance:

Eric Jordan

Eric Jordan

By far, I spend the most time listening to Eric Jordan.  His trance mixes are stunning.  They are always creative, drawing from his vast knowlege of obscure, but impressive tracks.  He is a master at creating a mood, evoking emotion, and pulling the listener in.  Every month he posts a new mix on his website: neverrain.com, available for free to download.  His mixes tend to have more subtle melodies, and less vocals, so I find them extra conducive to zoning out and programming complex algorithms.

asd

Vocal Trance:

DJ GT

DJ GT

DJ GT takes a similar approach, creating hour long mixes and posting them for free on his website, generationtrance.com. He is equally talented, although his track lists tend to be slightly more mainstream.  All of the songs he uses contain lyrics, which makes the mixes a little more structured and digestable for those not as used to electronic music.  I’ll throw one of his many tracks in the queue when I want something more upbeat to tap my feet to.

asd

Psycadelic Trance:

Shpongle

Shpongle

Shpongle is group out of the UK that defies description.  Generally, I’d label it Psy Trance, but you’ll find heavy influences of world beat, classical, opera, jazz, ska, punk, dub, and half a dozen other genres.  It all adds up to 100% awesome.  Lyrics in their music are not arranged into verses to tell a story, but rather sampled to become part of the ambience and reinforce the mood.  My only wish is that they had more than three albums.

asd

Dub:

O.T.T.

OTT

If I had to describe Dub to someone who hadn’t heard it, I’d label it the offspring of Reggae and Trance.  It’s characterized by a slower tempo than most electronica, with a heavier bassline and more emphasis on the ambience of the music.  Ott’s first album Blumenkraft stands out as my favorite mix of any artist mentioned here.  It’s simply the most powerful weapon against the drone of office background noise I’ve found.  I save it for when I need to write that recursive function I’ve been putting off all week.

asd

Industrial:

Front Line Assembly

Front Line Assembly

Two of my favorite artists are Bill Leeb and Rhys Fulber.  They’ve been making electronic music for two decades, and they’ve made some of the best.  They’ve gone under several different names, the most popular being Delerium and Front Line Assembly.  Delerium, like Shpongle, defies description and has gone through several distinct transitions through the years.  I own and love the entire collection, but the music I find best to program to however, are their oldest albums, found here and here.  Each of those are a two CD set that compiles all the their earliest releases which are (sadly) no longer available.  The music is very experimental, and it laid the groundwork for the industrial genre.

Well, that concludes the tour of my favorite programming music.  I hope you enjoyed the trip; be careful opening the overhead bins, as items may have shifted in flight.  I’m always open for recommendataion, so if you hear a good tune that makes you stop and close your eyes to listen, let me know.