weighted_average

Do weighted averages in ActiveRecord.

Rationale

You have a bunch of flight records with passenger count and distance.

  • Flight EWR <-> MSN; 30,000 passengers last month; 500 miles

  • Flight EWR <-> BOM; 15 passengers last month; 10,000 miles

The average distance is (10_000 + 500) / 2 = 5250.

The average distance weighted by passenger count is (30_000 * 500 + 15 * 10_000) / (10_500) = 1442.

Usage

Using FlightSegment from Brighter Planet’s earth gem:

>> FlightSegment.weighted_average(:distance, :weighted_by => :passengers)
=> 2436.1959

You can also see the SQL that is generated:

>> FlightSegment.weighted_average_relation(:distance, :weighted_by => :passengers).to_sql
=> "SELECT (SUM((`flight_segments`.`distance`) * `flight_segments`.`passengers`) / SUM(`flight_segments`.`passengers`)) AS weighted_average FROM `flight_segments` WHERE (`flight_segments`.`distance` IS NOT NULL)"

Copyright © 2010 Seamus Abshere, Andy Rossmeissl, Ian Hough, and Matt Kling. See LICENSE for details.