Provides cohorts (in the form of ActiveRecord scopes) that dynamically widen until they contain a certain number of records.
big_cohort widens by successively removing what it finds to be the most restrictive characteristic until it reaches the minimum number of records
strict_cohort widens by eliminating characteristics in order until it reaches the minimum number of records
This has been at use at impact.brighterplanet.com since April 2010, where it helps sift through climate data to come up with meaningful emissions calculations.
Let's pretend the U.S. Census provided information about birthday and favorite color:
class Citizen < ActiveRecord::Base extend CohortScope self.minimum_cohort_size = 1_000 end
Now I need to run a calculation that ideally uses birthday and favorite color, but most importantly looks at a large cohort:
Citizen.big_cohort :birthdate => (Date.parse('1980-01-01')..Date.parse('1990-01-01')), :favorite_color => 'heliotrope' # => [... a cohort of at least 1,000 records (otherwise it's empty), where everybody's favorite color MAY be heliotrope and everybody's birthday MAY be between 1980 and 1990 (at least one of those characteristics will hold) ...]
What if my calculation privileges favorite color? In other words, if you can't give me a cohort of minimum size within the birthday characteristic, at least give me one where everybody loves heliotrope:
ordered_characteristics =  ordered_characteristics << [:favorite_color, 'heliotrope'] ordered_characteristics << [:birthdate, (Date.parse('1980-01-01')..Date.parse('1990-01-01'))] Citizen.strict_cohort *favorite_color_matters_most # => [... a cohort of at least 1,000 records (otherwise it's empty), where everybody's favorite color IS heliotrope and everybody's birthday MAY be between 1980 and 1990 ...]
Copyright © 2012 Brighter Planet, Inc. See LICENSE for details.