Class: Perfer::Statistics
- Inherits:
-
Object
- Object
- Perfer::Statistics
- Includes:
- Math
- Defined in:
- lib/perfer/statistics.rb
Constant Summary collapse
- CONFIDENCE_LEVEL =
0.95
- ALPHA =
significance level
1.0 - CONFIDENCE_LEVEL
- T_QUANTILES =
Student’s t quantiles is used as n is small (= number of measurements) Indexed by: probability, degrees of freedom
{ 0.975 => [ nil, 12.71, 4.303, 3.182, 2.776, 2.571, 2.447, 2.365, 2.306, 2.262, 2.228, # 1-10 2.201, 2.179, 2.160, 2.145, 2.131, 2.120, 2.110, 2.101, 2.093, 2.086, # 11-20 2.080, 2.074, 2.069, 2.064, 2.060, 2.056, 2.052, 2.048, 2.045, 2.042, # 21-30 2.040, 2.037, 2.035, 2.032, 2.030, 2.028, 2.026, 2.024, 2.023, 2.021 # 31-40 ] }
Class Method Summary collapse
Instance Method Summary collapse
- #coefficient_of_variation ⇒ Object
-
#initialize(sample) ⇒ Statistics
constructor
A new instance of Statistics.
-
#margin_of_error ⇒ Object
Assumes a standard normal distribution This is half the width of the confidence interval for the mean.
- #maximum_absolute_deviation ⇒ Object
- #mean ⇒ Object
- #mean_absolute_deviation ⇒ Object
- #median ⇒ Object
- #median_absolute_deviation ⇒ Object
- #size ⇒ Object
- #standard_deviation ⇒ Object
- #standard_error ⇒ Object
- #variance ⇒ Object
Constructor Details
#initialize(sample) ⇒ Statistics
Returns a new instance of Statistics.
33 34 35 |
# File 'lib/perfer/statistics.rb', line 33 def initialize(sample) @sample = sample end |
Class Method Details
.t_quantile(p, degrees_of_freedom) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/perfer/statistics.rb', line 23 def self.t_quantile(p, degrees_of_freedom) if degrees_of_freedom <= 40 T_QUANTILES[p][degrees_of_freedom] elsif degrees_of_freedom <= 100 T_QUANTILES[p][degrees_of_freedom.round(-1)] else 1.960 end end |
Instance Method Details
#coefficient_of_variation ⇒ Object
68 69 70 |
# File 'lib/perfer/statistics.rb', line 68 def coefficient_of_variation standard_deviation / mean end |
#margin_of_error ⇒ Object
Assumes a standard normal distribution This is half the width of the confidence interval for the mean
86 87 88 |
# File 'lib/perfer/statistics.rb', line 86 def margin_of_error Statistics.t_quantile(1.0 - ALPHA/2, size-1) * standard_error end |
#maximum_absolute_deviation ⇒ Object
90 91 92 |
# File 'lib/perfer/statistics.rb', line 90 def maximum_absolute_deviation @sample.map { |v| (v - mean).abs }.max end |
#mean ⇒ Object
41 42 43 |
# File 'lib/perfer/statistics.rb', line 41 def mean @mean ||= @sample.inject(0.0) { |sum, i| sum + i } / size end |
#mean_absolute_deviation ⇒ Object
76 77 78 |
# File 'lib/perfer/statistics.rb', line 76 def mean_absolute_deviation @sample.inject(0.0) { |dev, i| dev + (i - mean).abs } / size end |
#median ⇒ Object
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/perfer/statistics.rb', line 45 def median @median ||= begin sorted = @sample.sort if size.odd? sorted[size/2] else (sorted[size/2-1] + sorted[size/2]) / 2.0 end end end |
#median_absolute_deviation ⇒ Object
80 81 82 |
# File 'lib/perfer/statistics.rb', line 80 def median_absolute_deviation Statistics.new(@sample.map { |i| (i - median).abs }).median end |
#size ⇒ Object
37 38 39 |
# File 'lib/perfer/statistics.rb', line 37 def size @sample.size end |
#standard_deviation ⇒ Object
64 65 66 |
# File 'lib/perfer/statistics.rb', line 64 def standard_deviation sqrt(variance) end |
#standard_error ⇒ Object
72 73 74 |
# File 'lib/perfer/statistics.rb', line 72 def standard_error standard_deviation / sqrt(size) end |
#variance ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/perfer/statistics.rb', line 56 def variance mean = mean() @sample.inject(0.0) { |var, i| d = i - mean var + d*d } / (size - 1) # unbiased sample variance end |