This repository has been archived by the owner on Jan 10, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#68953776]
- Loading branch information
Michael Webb
committed
Oct 28, 2014
1 parent
ef0fa3a
commit a4973ab
Showing
17 changed files
with
199 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,107 +1,124 @@ | ||
class Statistics | ||
include Enumerable | ||
def initialize(wagers) | ||
|
||
def initialize(wager_condition = nil) | ||
setup_intervals | ||
wagers = wagers.prefetch_joins if wagers.respond_to?(:prefetch_joins) | ||
wagers.each do |wager| | ||
interval_for(wager.relative_confidence).add(wager) | ||
# Sums up the total count and the accuracy into bands | ||
sql = <<-EOS | ||
SELECT | ||
ROUND(rel_conf - 5, -1) band, | ||
COUNT(*) sample_size, | ||
SUM(correct) / COUNT(*) accuracy | ||
FROM | ||
(SELECT | ||
CASE | ||
WHEN r.confidence >= 50 THEN r.confidence | ||
ELSE 100 - r.confidence | ||
END rel_conf, | ||
CASE | ||
WHEN (r.confidence >= 50 AND j.outcome = 1) OR | ||
(r.confidence < 50 AND j.outcome = 0) THEN 1 | ||
ELSE 0 | ||
END correct | ||
FROM responses r | ||
INNER JOIN predictions p ON r.prediction_id = p.id | ||
INNER JOIN (SELECT prediction_id, MAX(id) id | ||
FROM judgements | ||
GROUP BY prediction_id) mrj ON mrj.prediction_id = p.id | ||
INNER JOIN judgements j ON j.id = mrj.id | ||
WHERE r.confidence IS NOT NULL | ||
#{"AND #{wager_condition}" if wager_condition}) rc | ||
GROUP BY ROUND(rel_conf - 5, -1) | ||
EOS | ||
data = ActiveRecord::Base.connection.execute(sql) | ||
data.each do |datum| | ||
@intervals[datum[0]].update(datum) | ||
end | ||
self | ||
end | ||
|
||
def each | ||
@intervals.keys.sort.each do |key| | ||
yield @intervals[key] | ||
end | ||
self | ||
end | ||
|
||
def headings | ||
collect(&:heading) | ||
end | ||
|
||
def sizes | ||
collect(&:count) | ||
end | ||
|
||
def size | ||
sizes.sum | ||
end | ||
|
||
def accuracies | ||
collect(&:accuracy) | ||
end | ||
|
||
def image_url | ||
|
||
#TODO: refactor (extract and such) and port to http://gchartrb.rubyforge.org/ | ||
|
||
# http://code.google.com/apis/chart/#shape_markers | ||
# circle, red, first set, all points, 20px, on top of everything | | ||
# circle, red, first set, all points, 20px, on top of everything | | ||
blob = 'o,AAAAFF,0,-1,25,1' | ||
# horizontal line, color, ignored, start point, end point | ||
fifty_line = 'r,44FF44,0,0.49,0.51' | ||
fifty_line = 'r,44FF44,0,0.49,0.51' | ||
# line, color, data set, size, priority | ||
joiner_line = 'D,FFCCCC,0,0,0.5,-1' | ||
markers = [blob, fifty_line, joiner_line].join('|') | ||
intervals = collect(&:heading).join(",") | ||
intervals = self.headings.join(",").gsub("%", "") | ||
accuracies = self.accuracies.join(",") | ||
# Images api uses values 0..100 only | ||
sizes = self.sizes.join(',') | ||
image_size = "355x200" | ||
grid_lines = "20,20" | ||
# http://code.google.com/apis/chart/#multiple_axes_labels | ||
axis = 'x,x,y,y' | ||
# second axis, start at 50, go to 100 | ||
# second axis, start at 50, go to 100 | ||
axis_ranges = '0,50,100|1,0,100' | ||
# if we specify they are scaled for us | ||
data_ranges = "50,100,0,100,0,#{self.sizes.max}" | ||
title = 'How sure are you? Confidence vs. Accuracy (%)' | ||
axis_labels = "1:|Confidence (%)|3:|Accuracy" | ||
axis_labels_positions = "1,50|3,50" | ||
|
||
"http://chart.apis.google.com/chart?" + [ | ||
"cht=s", # scatterplot | ||
# "chtt=#{title}", | ||
"chg=#{grid_lines}", | ||
"chds=#{data_ranges}", | ||
"chm=#{markers}", | ||
"chxt=#{axis}", | ||
"chxr=#{axis_ranges}", | ||
"chd=t:#{intervals}|#{accuracies}|#{sizes}", | ||
"chd=t:#{intervals}|#{accuracies}|#{sizes}", | ||
"chs=#{image_size}", | ||
"chxl=#{axis_labels}", | ||
"chxp=#{axis_labels_positions}", | ||
].join('&') | ||
end | ||
|
||
def interval_for(percentage) | ||
@intervals[((percentage)/10)*10] | ||
end | ||
|
||
|
||
def setup_intervals | ||
@intervals = {100 => Interval.new("100",(100..100))} | ||
[50,60,70,80,90].each do |start| | ||
range = (start..start+9) | ||
@intervals[start] = Interval.new("#{start}",range) | ||
@intervals = {} | ||
[50,60,70,80,90,100].each do |band| | ||
@intervals[band] = Interval.new(band) | ||
end | ||
end | ||
|
||
class Interval | ||
attr_reader :heading | ||
def initialize(heading,range) | ||
@heading = heading | ||
@range = range | ||
@wagers = [] | ||
attr_reader :heading, :accuracy, :count | ||
def initialize(band) | ||
@heading = "#{band}%" | ||
@accuracy = 0 | ||
@count = 0 | ||
end | ||
def add(wager) | ||
@wagers.push(wager) | ||
end | ||
def count | ||
@wagers.reject {|w| w.unknown? }.size | ||
end | ||
def accuracy | ||
count > 0 ? (correct.to_f/count*100).round : 0 | ||
end | ||
def correct | ||
@wagers.select {|w| w.correct? }.size | ||
|
||
def update(row) | ||
@accuracy = ((row[2] || 0) * 100).round.to_i | ||
@count = row[1] || 0 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
<span class='title'><%= link_to show_title(prediction.description), prediction %></span> | ||
<%= render :partial => 'summary', :locals => {:prediction => prediction} -%> | ||
<%= render :partial => 'predictions/summary', :locals => {:prediction => prediction} -%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.